wait.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2019 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 deployment
  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/util/wait"
  20. clientset "k8s.io/client-go/kubernetes"
  21. deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
  22. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  23. testutils "k8s.io/kubernetes/test/utils"
  24. )
  25. const (
  26. // poll is how often to poll pods, nodes and claims.
  27. poll = 2 * time.Second
  28. pollShortTimeout = 1 * time.Minute
  29. pollLongTimeout = 5 * time.Minute
  30. )
  31. // WaitForObservedDeployment waits for the specified deployment generation.
  32. func WaitForObservedDeployment(c clientset.Interface, ns, deploymentName string, desiredGeneration int64) error {
  33. return testutils.WaitForObservedDeployment(c, ns, deploymentName, desiredGeneration)
  34. }
  35. // WaitForDeploymentWithCondition waits for the specified deployment condition.
  36. func WaitForDeploymentWithCondition(c clientset.Interface, ns, deploymentName, reason string, condType apps.DeploymentConditionType) error {
  37. return testutils.WaitForDeploymentWithCondition(c, ns, deploymentName, reason, condType, e2elog.Logf, poll, pollLongTimeout)
  38. }
  39. // WaitForDeploymentRevisionAndImage waits for the deployment's and its new RS's revision and container image to match the given revision and image.
  40. // Note that deployment revision and its new RS revision should be updated shortly most of the time, but an overwhelmed RS controller
  41. // may result in taking longer to relabel a RS.
  42. func WaitForDeploymentRevisionAndImage(c clientset.Interface, ns, deploymentName string, revision, image string) error {
  43. return testutils.WaitForDeploymentRevisionAndImage(c, ns, deploymentName, revision, image, e2elog.Logf, poll, pollLongTimeout)
  44. }
  45. // WaitForDeploymentComplete waits for the deployment to complete, and don't check if rolling update strategy is broken.
  46. // Rolling update strategy is used only during a rolling update, and can be violated in other situations,
  47. // such as shortly after a scaling event or the deployment is just created.
  48. func WaitForDeploymentComplete(c clientset.Interface, d *apps.Deployment) error {
  49. return testutils.WaitForDeploymentComplete(c, d, e2elog.Logf, poll, pollLongTimeout)
  50. }
  51. // WaitForDeploymentCompleteAndCheckRolling waits for the deployment to complete, and check rolling update strategy isn't broken at any times.
  52. // Rolling update strategy should not be broken during a rolling update.
  53. func WaitForDeploymentCompleteAndCheckRolling(c clientset.Interface, d *apps.Deployment) error {
  54. return testutils.WaitForDeploymentCompleteAndCheckRolling(c, d, e2elog.Logf, poll, pollLongTimeout)
  55. }
  56. // WaitForDeploymentUpdatedReplicasGTE waits for given deployment to be observed by the controller and has at least a number of updatedReplicas
  57. func WaitForDeploymentUpdatedReplicasGTE(c clientset.Interface, ns, deploymentName string, minUpdatedReplicas int32, desiredGeneration int64) error {
  58. return testutils.WaitForDeploymentUpdatedReplicasGTE(c, ns, deploymentName, minUpdatedReplicas, desiredGeneration, poll, pollLongTimeout)
  59. }
  60. // WaitForDeploymentRollbackCleared waits for given deployment either started rolling back or doesn't need to rollback.
  61. // Note that rollback should be cleared shortly, so we only wait for 1 minute here to fail early.
  62. func WaitForDeploymentRollbackCleared(c clientset.Interface, ns, deploymentName string) error {
  63. return testutils.WaitForDeploymentRollbackCleared(c, ns, deploymentName, poll, pollShortTimeout)
  64. }
  65. // WaitForDeploymentOldRSsNum waits for the deployment to clean up old rcs.
  66. func WaitForDeploymentOldRSsNum(c clientset.Interface, ns, deploymentName string, desiredRSNum int) error {
  67. var oldRSs []*apps.ReplicaSet
  68. var d *apps.Deployment
  69. pollErr := wait.PollImmediate(poll, 5*time.Minute, func() (bool, error) {
  70. deployment, err := c.AppsV1().Deployments(ns).Get(deploymentName, metav1.GetOptions{})
  71. if err != nil {
  72. return false, err
  73. }
  74. d = deployment
  75. _, oldRSs, err = deploymentutil.GetOldReplicaSets(deployment, c.AppsV1())
  76. if err != nil {
  77. return false, err
  78. }
  79. return len(oldRSs) == desiredRSNum, nil
  80. })
  81. if pollErr == wait.ErrWaitTimeout {
  82. pollErr = fmt.Errorf("%d old replica sets were not cleaned up for deployment %q", len(oldRSs)-desiredRSNum, deploymentName)
  83. logReplicaSetsOfDeployment(d, oldRSs, nil)
  84. }
  85. return pollErr
  86. }
  87. // WaitForDeploymentRevision waits for becoming the target revision of a delopyment.
  88. func WaitForDeploymentRevision(c clientset.Interface, d *apps.Deployment, targetRevision string) error {
  89. err := wait.PollImmediate(poll, pollLongTimeout, func() (bool, error) {
  90. deployment, err := c.AppsV1().Deployments(d.Namespace).Get(d.Name, metav1.GetOptions{})
  91. if err != nil {
  92. return false, err
  93. }
  94. revision := deployment.Annotations[deploymentutil.RevisionAnnotation]
  95. return revision == targetRevision, nil
  96. })
  97. if err != nil {
  98. return fmt.Errorf("error waiting for revision to become %q for deployment %q: %v", targetRevision, d.Name, err)
  99. }
  100. return nil
  101. }