replicaset.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 utils
  14. import (
  15. "context"
  16. "fmt"
  17. "testing"
  18. "time"
  19. apps "k8s.io/api/apps/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/wait"
  22. clientset "k8s.io/client-go/kubernetes"
  23. )
  24. type UpdateReplicaSetFunc func(d *apps.ReplicaSet)
  25. func UpdateReplicaSetWithRetries(c clientset.Interface, namespace, name string, applyUpdate UpdateReplicaSetFunc, logf LogfFn, pollInterval, pollTimeout time.Duration) (*apps.ReplicaSet, error) {
  26. var rs *apps.ReplicaSet
  27. var updateErr error
  28. pollErr := wait.PollImmediate(pollInterval, pollTimeout, func() (bool, error) {
  29. var err error
  30. if rs, err = c.AppsV1().ReplicaSets(namespace).Get(context.TODO(), name, metav1.GetOptions{}); err != nil {
  31. return false, err
  32. }
  33. // Apply the update, then attempt to push it to the apiserver.
  34. applyUpdate(rs)
  35. if rs, err = c.AppsV1().ReplicaSets(namespace).Update(context.TODO(), rs, metav1.UpdateOptions{}); err == nil {
  36. logf("Updating replica set %q", name)
  37. return true, nil
  38. }
  39. updateErr = err
  40. return false, nil
  41. })
  42. if pollErr == wait.ErrWaitTimeout {
  43. pollErr = fmt.Errorf("couldn't apply the provided updated to replicaset %q: %v", name, updateErr)
  44. }
  45. return rs, pollErr
  46. }
  47. // Verify .Status.Replicas is equal to .Spec.Replicas
  48. func WaitRSStable(t *testing.T, clientSet clientset.Interface, rs *apps.ReplicaSet, pollInterval, pollTimeout time.Duration) error {
  49. desiredGeneration := rs.Generation
  50. if err := wait.PollImmediate(pollInterval, pollTimeout, func() (bool, error) {
  51. newRS, err := clientSet.AppsV1().ReplicaSets(rs.Namespace).Get(context.TODO(), rs.Name, metav1.GetOptions{})
  52. if err != nil {
  53. return false, err
  54. }
  55. return newRS.Status.ObservedGeneration >= desiredGeneration && newRS.Status.Replicas == *rs.Spec.Replicas, nil
  56. }); err != nil {
  57. return fmt.Errorf("failed to verify .Status.Replicas is equal to .Spec.Replicas for replicaset %q: %v", rs.Name, err)
  58. }
  59. return nil
  60. }
  61. func UpdateReplicaSetStatusWithRetries(c clientset.Interface, namespace, name string, applyUpdate UpdateReplicaSetFunc, logf LogfFn, pollInterval, pollTimeout time.Duration) (*apps.ReplicaSet, error) {
  62. var rs *apps.ReplicaSet
  63. var updateErr error
  64. pollErr := wait.PollImmediate(pollInterval, pollTimeout, func() (bool, error) {
  65. var err error
  66. if rs, err = c.AppsV1().ReplicaSets(namespace).Get(context.TODO(), name, metav1.GetOptions{}); err != nil {
  67. return false, err
  68. }
  69. // Apply the update, then attempt to push it to the apiserver.
  70. applyUpdate(rs)
  71. if rs, err = c.AppsV1().ReplicaSets(namespace).UpdateStatus(context.TODO(), rs, metav1.UpdateOptions{}); err == nil {
  72. logf("Updating replica set %q", name)
  73. return true, nil
  74. }
  75. updateErr = err
  76. return false, nil
  77. })
  78. if pollErr == wait.ErrWaitTimeout {
  79. pollErr = fmt.Errorf("couldn't apply the provided update to replicaset %q: %v", name, updateErr)
  80. }
  81. return rs, pollErr
  82. }