wait.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 replicaset
  14. import (
  15. "fmt"
  16. "github.com/onsi/ginkgo"
  17. apps "k8s.io/api/apps/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/wait"
  20. clientset "k8s.io/client-go/kubernetes"
  21. appsclient "k8s.io/client-go/kubernetes/typed/apps/v1"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. testutils "k8s.io/kubernetes/test/utils"
  24. )
  25. // RunReplicaSet launches (and verifies correctness) of a replicaset.
  26. func RunReplicaSet(config testutils.ReplicaSetConfig) error {
  27. ginkgo.By(fmt.Sprintf("creating replicaset %s in namespace %s", config.Name, config.Namespace))
  28. config.NodeDumpFunc = framework.DumpNodeDebugInfo
  29. config.ContainerDumpFunc = framework.LogFailedContainers
  30. return testutils.RunReplicaSet(config)
  31. }
  32. // WaitForReadyReplicaSet waits until the replicaset has all of its replicas ready.
  33. func WaitForReadyReplicaSet(c clientset.Interface, ns, name string) error {
  34. err := wait.Poll(framework.Poll, framework.PollShortTimeout, func() (bool, error) {
  35. rs, err := c.AppsV1().ReplicaSets(ns).Get(name, metav1.GetOptions{})
  36. if err != nil {
  37. return false, err
  38. }
  39. return *(rs.Spec.Replicas) == rs.Status.Replicas && *(rs.Spec.Replicas) == rs.Status.ReadyReplicas, nil
  40. })
  41. if err == wait.ErrWaitTimeout {
  42. err = fmt.Errorf("replicaset %q never became ready", name)
  43. }
  44. return err
  45. }
  46. // WaitForReplicaSetDesiredReplicas waits until the replicaset has desired number of replicas.
  47. func WaitForReplicaSetDesiredReplicas(rsClient appsclient.ReplicaSetsGetter, replicaSet *apps.ReplicaSet) error {
  48. desiredGeneration := replicaSet.Generation
  49. err := wait.PollImmediate(framework.Poll, framework.PollShortTimeout, func() (bool, error) {
  50. rs, err := rsClient.ReplicaSets(replicaSet.Namespace).Get(replicaSet.Name, metav1.GetOptions{})
  51. if err != nil {
  52. return false, err
  53. }
  54. return rs.Status.ObservedGeneration >= desiredGeneration && rs.Status.Replicas == *(replicaSet.Spec.Replicas) && rs.Status.Replicas == *(rs.Spec.Replicas), nil
  55. })
  56. if err == wait.ErrWaitTimeout {
  57. err = fmt.Errorf("replicaset %q never had desired number of replicas", replicaSet.Name)
  58. }
  59. return err
  60. }
  61. // WaitForReplicaSetTargetSpecReplicas waits for .spec.replicas of a RS to equal targetReplicaNum
  62. func WaitForReplicaSetTargetSpecReplicas(c clientset.Interface, replicaSet *apps.ReplicaSet, targetReplicaNum int32) error {
  63. desiredGeneration := replicaSet.Generation
  64. err := wait.PollImmediate(framework.Poll, framework.PollShortTimeout, func() (bool, error) {
  65. rs, err := c.AppsV1().ReplicaSets(replicaSet.Namespace).Get(replicaSet.Name, metav1.GetOptions{})
  66. if err != nil {
  67. return false, err
  68. }
  69. return rs.Status.ObservedGeneration >= desiredGeneration && *rs.Spec.Replicas == targetReplicaNum, nil
  70. })
  71. if err == wait.ErrWaitTimeout {
  72. err = fmt.Errorf("replicaset %q never had desired number of .spec.replicas", replicaSet.Name)
  73. }
  74. return err
  75. }
  76. // WaitForReplicaSetTargetAvailableReplicas waits for .status.availableReplicas of a RS to equal targetReplicaNum
  77. func WaitForReplicaSetTargetAvailableReplicas(c clientset.Interface, replicaSet *apps.ReplicaSet, targetReplicaNum int32) error {
  78. desiredGeneration := replicaSet.Generation
  79. err := wait.PollImmediate(framework.Poll, framework.PollShortTimeout, func() (bool, error) {
  80. rs, err := c.AppsV1().ReplicaSets(replicaSet.Namespace).Get(replicaSet.Name, metav1.GetOptions{})
  81. if err != nil {
  82. return false, err
  83. }
  84. return rs.Status.ObservedGeneration >= desiredGeneration && rs.Status.AvailableReplicas == targetReplicaNum, nil
  85. })
  86. if err == wait.ErrWaitTimeout {
  87. err = fmt.Errorf("replicaset %q never had desired number of .status.availableReplicas", replicaSet.Name)
  88. }
  89. return err
  90. }