replicaset.go 3.2 KB

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