node_lease.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package common
  14. import (
  15. "fmt"
  16. "time"
  17. coordv1beta1 "k8s.io/api/coordination/v1beta1"
  18. corev1 "k8s.io/api/core/v1"
  19. apiequality "k8s.io/apimachinery/pkg/api/equality"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/diff"
  22. "k8s.io/apimachinery/pkg/util/wait"
  23. clientset "k8s.io/client-go/kubernetes"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  26. testutils "k8s.io/kubernetes/test/utils"
  27. . "github.com/onsi/ginkgo"
  28. . "github.com/onsi/gomega"
  29. )
  30. var _ = framework.KubeDescribe("NodeLease", func() {
  31. var nodeName string
  32. f := framework.NewDefaultFramework("node-lease-test")
  33. BeforeEach(func() {
  34. nodes := framework.GetReadySchedulableNodesOrDie(f.ClientSet)
  35. Expect(len(nodes.Items)).NotTo(BeZero())
  36. nodeName = nodes.Items[0].ObjectMeta.Name
  37. })
  38. Context("when the NodeLease feature is enabled", func() {
  39. It("the kubelet should create and update a lease in the kube-node-lease namespace", func() {
  40. leaseClient := f.ClientSet.CoordinationV1beta1().Leases(corev1.NamespaceNodeLease)
  41. var (
  42. err error
  43. lease *coordv1beta1.Lease
  44. )
  45. By("check that lease for this Kubelet exists in the kube-node-lease namespace")
  46. Eventually(func() error {
  47. lease, err = leaseClient.Get(nodeName, metav1.GetOptions{})
  48. if err != nil {
  49. return err
  50. }
  51. return nil
  52. }, 5*time.Minute, 5*time.Second).Should(BeNil())
  53. // check basic expectations for the lease
  54. Expect(expectLease(lease, nodeName)).To(BeNil())
  55. By("check that node lease is updated at least once within the lease duration")
  56. Eventually(func() error {
  57. newLease, err := leaseClient.Get(nodeName, metav1.GetOptions{})
  58. if err != nil {
  59. return err
  60. }
  61. // check basic expectations for the latest lease
  62. if err := expectLease(newLease, nodeName); err != nil {
  63. return err
  64. }
  65. // check that RenewTime has been updated on the latest lease
  66. newTime := (*newLease.Spec.RenewTime).Time
  67. oldTime := (*lease.Spec.RenewTime).Time
  68. if !newTime.After(oldTime) {
  69. return fmt.Errorf("new lease has time %v, which is not after old lease time %v", newTime, oldTime)
  70. }
  71. return nil
  72. }, time.Duration(*lease.Spec.LeaseDurationSeconds)*time.Second,
  73. time.Duration(*lease.Spec.LeaseDurationSeconds/4)*time.Second)
  74. })
  75. It("the kubelet should report node status infrequently", func() {
  76. By("wait until node is ready")
  77. framework.WaitForNodeToBeReady(f.ClientSet, nodeName, 5*time.Minute)
  78. By("wait until there is node lease")
  79. var err error
  80. var lease *coordv1beta1.Lease
  81. Eventually(func() error {
  82. lease, err = f.ClientSet.CoordinationV1beta1().Leases(corev1.NamespaceNodeLease).Get(nodeName, metav1.GetOptions{})
  83. if err != nil {
  84. return err
  85. }
  86. return nil
  87. }, 5*time.Minute, 5*time.Second).Should(BeNil())
  88. // check basic expectations for the lease
  89. Expect(expectLease(lease, nodeName)).To(BeNil())
  90. leaseDuration := time.Duration(*lease.Spec.LeaseDurationSeconds) * time.Second
  91. By("verify NodeStatus report period is longer than lease duration")
  92. // NodeStatus is reported from node to master when there is some change or
  93. // enough time has passed. So for here, keep checking the time diff
  94. // between 2 NodeStatus report, until it is longer than lease duration
  95. // (the same as nodeMonitorGracePeriod), or it doesn't change for at least leaseDuration
  96. lastHeartbeatTime, lastStatus := getHeartbeatTimeAndStatus(f.ClientSet, nodeName)
  97. lastObserved := time.Now()
  98. err = wait.Poll(time.Second, 5*time.Minute, func() (bool, error) {
  99. currentHeartbeatTime, currentStatus := getHeartbeatTimeAndStatus(f.ClientSet, nodeName)
  100. currentObserved := time.Now()
  101. if currentHeartbeatTime == lastHeartbeatTime {
  102. if currentObserved.Sub(lastObserved) > 2*leaseDuration {
  103. // heartbeat hasn't changed while watching for at least 2*leaseDuration, success!
  104. e2elog.Logf("node status heartbeat is unchanged for %s, was waiting for at least %s, success!", currentObserved.Sub(lastObserved), 2*leaseDuration)
  105. return true, nil
  106. }
  107. e2elog.Logf("node status heartbeat is unchanged for %s, waiting for %s", currentObserved.Sub(lastObserved), 2*leaseDuration)
  108. return false, nil
  109. }
  110. if currentHeartbeatTime.Sub(lastHeartbeatTime) >= leaseDuration {
  111. // heartbeat time changed, but the diff was greater than leaseDuration, success!
  112. e2elog.Logf("node status heartbeat changed in %s, was waiting for at least %s, success!", currentHeartbeatTime.Sub(lastHeartbeatTime), leaseDuration)
  113. return true, nil
  114. }
  115. if !apiequality.Semantic.DeepEqual(lastStatus, currentStatus) {
  116. // heartbeat time changed, but there were relevant changes in the status, keep waiting
  117. e2elog.Logf("node status heartbeat changed in %s (with other status changes), waiting for %s", currentHeartbeatTime.Sub(lastHeartbeatTime), leaseDuration)
  118. e2elog.Logf("%s", diff.ObjectReflectDiff(lastStatus, currentStatus))
  119. lastHeartbeatTime = currentHeartbeatTime
  120. lastObserved = currentObserved
  121. lastStatus = currentStatus
  122. return false, nil
  123. }
  124. // heartbeat time changed, with no other status changes, in less time than we expected, so fail.
  125. return false, fmt.Errorf("node status heartbeat changed in %s (with no other status changes), was waiting for %s", currentHeartbeatTime.Sub(lastHeartbeatTime), leaseDuration)
  126. })
  127. // a timeout is acceptable, since it means we waited 5 minutes and didn't see any unwarranted node status updates
  128. if err != nil && err != wait.ErrWaitTimeout {
  129. framework.ExpectNoError(err, "error waiting for infrequent nodestatus update")
  130. }
  131. By("verify node is still in ready status even though node status report is infrequent")
  132. // This check on node status is only meaningful when this e2e test is
  133. // running as cluster e2e test, because node e2e test does not create and
  134. // run controller manager, i.e., no node lifecycle controller.
  135. node, err := f.ClientSet.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
  136. Expect(err).To(BeNil())
  137. _, readyCondition := testutils.GetNodeCondition(&node.Status, corev1.NodeReady)
  138. Expect(readyCondition.Status).To(Equal(corev1.ConditionTrue))
  139. })
  140. })
  141. })
  142. func getHeartbeatTimeAndStatus(clientSet clientset.Interface, nodeName string) (time.Time, corev1.NodeStatus) {
  143. node, err := clientSet.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
  144. Expect(err).To(BeNil())
  145. _, readyCondition := testutils.GetNodeCondition(&node.Status, corev1.NodeReady)
  146. Expect(readyCondition.Status).To(Equal(corev1.ConditionTrue))
  147. heartbeatTime := readyCondition.LastHeartbeatTime.Time
  148. readyCondition.LastHeartbeatTime = metav1.Time{}
  149. return heartbeatTime, node.Status
  150. }
  151. func expectLease(lease *coordv1beta1.Lease, nodeName string) error {
  152. // expect values for HolderIdentity, LeaseDurationSeconds, and RenewTime
  153. if lease.Spec.HolderIdentity == nil {
  154. return fmt.Errorf("Spec.HolderIdentity should not be nil")
  155. }
  156. if lease.Spec.LeaseDurationSeconds == nil {
  157. return fmt.Errorf("Spec.LeaseDurationSeconds should not be nil")
  158. }
  159. if lease.Spec.RenewTime == nil {
  160. return fmt.Errorf("Spec.RenewTime should not be nil")
  161. }
  162. // ensure that the HolderIdentity matches the node name
  163. if *lease.Spec.HolderIdentity != nodeName {
  164. return fmt.Errorf("Spec.HolderIdentity (%v) should match the node name (%v)", *lease.Spec.HolderIdentity, nodeName)
  165. }
  166. return nil
  167. }