daemonsets.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Copyright 2017 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 upgrades
  14. import (
  15. "context"
  16. "github.com/onsi/ginkgo"
  17. appsv1 "k8s.io/api/apps/v1"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/labels"
  21. "k8s.io/apimachinery/pkg/util/wait"
  22. "k8s.io/kubernetes/pkg/controller"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. "k8s.io/kubernetes/test/e2e/upgrades"
  25. )
  26. // DaemonSetUpgradeTest tests that a DaemonSet is running before and after
  27. // a cluster upgrade.
  28. type DaemonSetUpgradeTest struct {
  29. daemonSet *appsv1.DaemonSet
  30. }
  31. // Name returns the tracking name of the test.
  32. func (DaemonSetUpgradeTest) Name() string { return "[sig-apps] daemonset-upgrade" }
  33. // Setup creates a DaemonSet and verifies that it's running
  34. func (t *DaemonSetUpgradeTest) Setup(f *framework.Framework) {
  35. daemonSetName := "ds1"
  36. labelSet := map[string]string{"ds-name": daemonSetName}
  37. image := framework.ServeHostnameImage
  38. ns := f.Namespace
  39. t.daemonSet = &appsv1.DaemonSet{
  40. ObjectMeta: metav1.ObjectMeta{
  41. Namespace: ns.Name,
  42. Name: daemonSetName,
  43. },
  44. Spec: appsv1.DaemonSetSpec{
  45. Selector: &metav1.LabelSelector{
  46. MatchLabels: labelSet,
  47. },
  48. Template: v1.PodTemplateSpec{
  49. ObjectMeta: metav1.ObjectMeta{
  50. Labels: labelSet,
  51. },
  52. Spec: v1.PodSpec{
  53. Tolerations: []v1.Toleration{
  54. {Operator: v1.TolerationOpExists},
  55. },
  56. Containers: []v1.Container{
  57. {
  58. Name: daemonSetName,
  59. Image: image,
  60. Args: []string{"serve-hostname"},
  61. Ports: []v1.ContainerPort{{ContainerPort: 9376}},
  62. SecurityContext: &v1.SecurityContext{},
  63. },
  64. },
  65. },
  66. },
  67. },
  68. }
  69. ginkgo.By("Creating a DaemonSet")
  70. var err error
  71. if t.daemonSet, err = f.ClientSet.AppsV1().DaemonSets(ns.Name).Create(context.TODO(), t.daemonSet, metav1.CreateOptions{}); err != nil {
  72. framework.Failf("unable to create test DaemonSet %s: %v", t.daemonSet.Name, err)
  73. }
  74. ginkgo.By("Waiting for DaemonSet pods to become ready")
  75. err = wait.Poll(framework.Poll, framework.PodStartTimeout, func() (bool, error) {
  76. return checkRunningOnAllNodes(f, t.daemonSet.Namespace, t.daemonSet.Labels)
  77. })
  78. framework.ExpectNoError(err)
  79. ginkgo.By("Validating the DaemonSet after creation")
  80. t.validateRunningDaemonSet(f)
  81. }
  82. // Test waits until the upgrade has completed and then verifies that the DaemonSet
  83. // is still running
  84. func (t *DaemonSetUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) {
  85. ginkgo.By("Waiting for upgradet to complete before re-validating DaemonSet")
  86. <-done
  87. ginkgo.By("validating the DaemonSet is still running after upgrade")
  88. t.validateRunningDaemonSet(f)
  89. }
  90. // Teardown cleans up any remaining resources.
  91. func (t *DaemonSetUpgradeTest) Teardown(f *framework.Framework) {
  92. // rely on the namespace deletion to clean up everything
  93. }
  94. func (t *DaemonSetUpgradeTest) validateRunningDaemonSet(f *framework.Framework) {
  95. ginkgo.By("confirming the DaemonSet pods are running on all expected nodes")
  96. res, err := checkRunningOnAllNodes(f, t.daemonSet.Namespace, t.daemonSet.Labels)
  97. framework.ExpectNoError(err)
  98. if !res {
  99. framework.Failf("expected DaemonSet pod to be running on all nodes, it was not")
  100. }
  101. // DaemonSet resource itself should be good
  102. ginkgo.By("confirming the DaemonSet resource is in a good state")
  103. res, err = checkDaemonStatus(f, t.daemonSet.Namespace, t.daemonSet.Name)
  104. framework.ExpectNoError(err)
  105. if !res {
  106. framework.Failf("expected DaemonSet to be in a good state, it was not")
  107. }
  108. }
  109. func checkRunningOnAllNodes(f *framework.Framework, namespace string, selector map[string]string) (bool, error) {
  110. nodeList, err := f.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
  111. if err != nil {
  112. return false, err
  113. }
  114. nodeNames := make([]string, 0)
  115. for _, node := range nodeList.Items {
  116. if len(node.Spec.Taints) != 0 {
  117. framework.Logf("Ignore taints %v on Node %v for DaemonSet Pod.", node.Spec.Taints, node.Name)
  118. }
  119. // DaemonSet Pods are expected to run on all the nodes in e2e.
  120. nodeNames = append(nodeNames, node.Name)
  121. }
  122. return checkDaemonPodOnNodes(f, namespace, selector, nodeNames)
  123. }
  124. func checkDaemonPodOnNodes(f *framework.Framework, namespace string, labelSet map[string]string, nodeNames []string) (bool, error) {
  125. selector := labels.Set(labelSet).AsSelector()
  126. options := metav1.ListOptions{LabelSelector: selector.String()}
  127. podList, err := f.ClientSet.CoreV1().Pods(namespace).List(context.TODO(), options)
  128. if err != nil {
  129. return false, err
  130. }
  131. pods := podList.Items
  132. nodesToPodCount := make(map[string]int)
  133. for _, pod := range pods {
  134. if controller.IsPodActive(&pod) {
  135. framework.Logf("Pod name: %v\t Node Name: %v", pod.Name, pod.Spec.NodeName)
  136. nodesToPodCount[pod.Spec.NodeName]++
  137. }
  138. }
  139. framework.Logf("nodesToPodCount: %v", nodesToPodCount)
  140. // Ensure that exactly 1 pod is running on all nodes in nodeNames.
  141. for _, nodeName := range nodeNames {
  142. if nodesToPodCount[nodeName] != 1 {
  143. return false, nil
  144. }
  145. }
  146. // Ensure that sizes of the lists are the same. We've verified that every element of nodeNames is in
  147. // nodesToPodCount, so verifying the lengths are equal ensures that there aren't pods running on any
  148. // other nodes.
  149. return len(nodesToPodCount) == len(nodeNames), nil
  150. }
  151. func checkDaemonStatus(f *framework.Framework, namespace string, dsName string) (bool, error) {
  152. ds, err := f.ClientSet.AppsV1().DaemonSets(namespace).Get(context.TODO(), dsName, metav1.GetOptions{})
  153. if err != nil {
  154. return false, err
  155. }
  156. desired, scheduled, ready := ds.Status.DesiredNumberScheduled, ds.Status.CurrentNumberScheduled, ds.Status.NumberReady
  157. if desired != scheduled && desired != ready {
  158. return false, nil
  159. }
  160. return true, nil
  161. }