replicasets.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "fmt"
  16. "time"
  17. apps "k8s.io/api/apps/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. "k8s.io/kubernetes/test/e2e/framework/replicaset"
  22. "k8s.io/kubernetes/test/e2e/upgrades"
  23. "github.com/onsi/ginkgo"
  24. imageutils "k8s.io/kubernetes/test/utils/image"
  25. )
  26. const (
  27. interval = 10 * time.Second
  28. timeout = 5 * time.Minute
  29. rsName = "rs"
  30. scaleNum = 2
  31. )
  32. // TODO: Test that the replicaset stays available during master (and maybe
  33. // node and cluster upgrades).
  34. // ReplicaSetUpgradeTest tests that a replicaset survives upgrade.
  35. type ReplicaSetUpgradeTest struct {
  36. UID types.UID
  37. }
  38. // Name returns the tracking name of the test.
  39. func (ReplicaSetUpgradeTest) Name() string { return "[sig-apps] replicaset-upgrade" }
  40. // Setup creates a ReplicaSet and makes sure it's replicas ready.
  41. func (r *ReplicaSetUpgradeTest) Setup(f *framework.Framework) {
  42. c := f.ClientSet
  43. ns := f.Namespace.Name
  44. nginxImage := imageutils.GetE2EImage(imageutils.Nginx)
  45. ginkgo.By(fmt.Sprintf("Creating replicaset %s in namespace %s", rsName, ns))
  46. replicaSet := replicaset.NewReplicaSet(rsName, ns, 1, map[string]string{"test": "upgrade"}, "nginx", nginxImage)
  47. rs, err := c.AppsV1().ReplicaSets(ns).Create(replicaSet)
  48. framework.ExpectNoError(err)
  49. ginkgo.By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready", rsName))
  50. framework.ExpectNoError(replicaset.WaitForReadyReplicaSet(c, ns, rsName))
  51. r.UID = rs.UID
  52. }
  53. // Test checks whether the replicasets are the same after an upgrade.
  54. func (r *ReplicaSetUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) {
  55. c := f.ClientSet
  56. ns := f.Namespace.Name
  57. rsClient := c.AppsV1().ReplicaSets(ns)
  58. // Block until upgrade is done
  59. ginkgo.By(fmt.Sprintf("Waiting for upgrade to finish before checking replicaset %s", rsName))
  60. <-done
  61. // Verify the RS is the same (survives) after the upgrade
  62. ginkgo.By(fmt.Sprintf("Checking UID to verify replicaset %s survives upgrade", rsName))
  63. upgradedRS, err := rsClient.Get(rsName, metav1.GetOptions{})
  64. framework.ExpectNoError(err)
  65. if upgradedRS.UID != r.UID {
  66. framework.ExpectNoError(fmt.Errorf("expected same replicaset UID: %v got: %v", r.UID, upgradedRS.UID))
  67. }
  68. ginkgo.By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready after upgrade", rsName))
  69. framework.ExpectNoError(replicaset.WaitForReadyReplicaSet(c, ns, rsName))
  70. // Verify the upgraded RS is active by scaling up the RS to scaleNum and ensuring all pods are Ready
  71. ginkgo.By(fmt.Sprintf("Scaling up replicaset %s to %d", rsName, scaleNum))
  72. _, err = replicaset.UpdateReplicaSetWithRetries(c, ns, rsName, func(rs *apps.ReplicaSet) {
  73. *rs.Spec.Replicas = scaleNum
  74. })
  75. framework.ExpectNoError(err)
  76. ginkgo.By(fmt.Sprintf("Waiting for replicaset %s to have all of its replicas ready after scaling", rsName))
  77. framework.ExpectNoError(replicaset.WaitForReadyReplicaSet(c, ns, rsName))
  78. }
  79. // Teardown cleans up any remaining resources.
  80. func (r *ReplicaSetUpgradeTest) Teardown(f *framework.Framework) {
  81. // rely on the namespace deletion to clean up everything
  82. }