replicasets.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "fmt"
  17. "time"
  18. appsv1 "k8s.io/api/apps/v1"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. "k8s.io/kubernetes/test/e2e/framework/replicaset"
  24. "k8s.io/kubernetes/test/e2e/upgrades"
  25. "github.com/onsi/ginkgo"
  26. imageutils "k8s.io/kubernetes/test/utils/image"
  27. )
  28. const (
  29. interval = 10 * time.Second
  30. timeout = 5 * time.Minute
  31. rsName = "rs"
  32. scaleNum = 2
  33. )
  34. // TODO: Test that the replicaset stays available during master (and maybe
  35. // node and cluster upgrades).
  36. // ReplicaSetUpgradeTest tests that a replicaset survives upgrade.
  37. type ReplicaSetUpgradeTest struct {
  38. UID types.UID
  39. }
  40. // Name returns the tracking name of the test.
  41. func (ReplicaSetUpgradeTest) Name() string { return "[sig-apps] replicaset-upgrade" }
  42. // Setup creates a ReplicaSet and makes sure it's replicas ready.
  43. func (r *ReplicaSetUpgradeTest) Setup(f *framework.Framework) {
  44. c := f.ClientSet
  45. ns := f.Namespace.Name
  46. nginxImage := imageutils.GetE2EImage(imageutils.Nginx)
  47. ginkgo.By(fmt.Sprintf("Creating replicaset %s in namespace %s", rsName, ns))
  48. replicaSet := newReplicaSet(rsName, ns, 1, map[string]string{"test": "upgrade"}, "nginx", nginxImage)
  49. rs, err := c.AppsV1().ReplicaSets(ns).Create(context.TODO(), replicaSet, metav1.CreateOptions{})
  50. framework.ExpectNoError(err)
  51. ginkgo.By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready", rsName))
  52. framework.ExpectNoError(replicaset.WaitForReadyReplicaSet(c, ns, rsName))
  53. r.UID = rs.UID
  54. }
  55. // Test checks whether the replicasets are the same after an upgrade.
  56. func (r *ReplicaSetUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) {
  57. c := f.ClientSet
  58. ns := f.Namespace.Name
  59. rsClient := c.AppsV1().ReplicaSets(ns)
  60. // Block until upgrade is done
  61. ginkgo.By(fmt.Sprintf("Waiting for upgrade to finish before checking replicaset %s", rsName))
  62. <-done
  63. // Verify the RS is the same (survives) after the upgrade
  64. ginkgo.By(fmt.Sprintf("Checking UID to verify replicaset %s survives upgrade", rsName))
  65. upgradedRS, err := rsClient.Get(context.TODO(), rsName, metav1.GetOptions{})
  66. framework.ExpectNoError(err)
  67. if upgradedRS.UID != r.UID {
  68. framework.ExpectNoError(fmt.Errorf("expected same replicaset UID: %v got: %v", r.UID, upgradedRS.UID))
  69. }
  70. ginkgo.By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready after upgrade", rsName))
  71. framework.ExpectNoError(replicaset.WaitForReadyReplicaSet(c, ns, rsName))
  72. // Verify the upgraded RS is active by scaling up the RS to scaleNum and ensuring all pods are Ready
  73. ginkgo.By(fmt.Sprintf("Scaling up replicaset %s to %d", rsName, scaleNum))
  74. _, err = replicaset.UpdateReplicaSetWithRetries(c, ns, rsName, func(rs *appsv1.ReplicaSet) {
  75. *rs.Spec.Replicas = scaleNum
  76. })
  77. framework.ExpectNoError(err)
  78. ginkgo.By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready after scaling", rsName))
  79. framework.ExpectNoError(replicaset.WaitForReadyReplicaSet(c, ns, rsName))
  80. }
  81. // Teardown cleans up any remaining resources.
  82. func (r *ReplicaSetUpgradeTest) Teardown(f *framework.Framework) {
  83. // rely on the namespace deletion to clean up everything
  84. }
  85. // newReplicaSet returns a new ReplicaSet.
  86. func newReplicaSet(name, namespace string, replicas int32, podLabels map[string]string, imageName, image string) *appsv1.ReplicaSet {
  87. return &appsv1.ReplicaSet{
  88. TypeMeta: metav1.TypeMeta{
  89. Kind: "ReplicaSet",
  90. APIVersion: "apps/v1",
  91. },
  92. ObjectMeta: metav1.ObjectMeta{
  93. Namespace: namespace,
  94. Name: name,
  95. },
  96. Spec: appsv1.ReplicaSetSpec{
  97. Selector: &metav1.LabelSelector{
  98. MatchLabels: podLabels,
  99. },
  100. Replicas: &replicas,
  101. Template: v1.PodTemplateSpec{
  102. ObjectMeta: metav1.ObjectMeta{
  103. Labels: podLabels,
  104. },
  105. Spec: v1.PodSpec{
  106. Containers: []v1.Container{
  107. {
  108. Name: imageName,
  109. Image: image,
  110. SecurityContext: &v1.SecurityContext{},
  111. },
  112. },
  113. },
  114. },
  115. },
  116. }
  117. }