stateful_set_status_updater.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 statefulset
  14. import (
  15. "context"
  16. "fmt"
  17. apps "k8s.io/api/apps/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  20. clientset "k8s.io/client-go/kubernetes"
  21. appslisters "k8s.io/client-go/listers/apps/v1"
  22. "k8s.io/client-go/util/retry"
  23. )
  24. // StatefulSetStatusUpdaterInterface is an interface used to update the StatefulSetStatus associated with a StatefulSet.
  25. // For any use other than testing, clients should create an instance using NewRealStatefulSetStatusUpdater.
  26. type StatefulSetStatusUpdaterInterface interface {
  27. // UpdateStatefulSetStatus sets the set's Status to status. Implementations are required to retry on conflicts,
  28. // but fail on other errors. If the returned error is nil set's Status has been successfully set to status.
  29. UpdateStatefulSetStatus(set *apps.StatefulSet, status *apps.StatefulSetStatus) error
  30. }
  31. // NewRealStatefulSetStatusUpdater returns a StatefulSetStatusUpdaterInterface that updates the Status of a StatefulSet,
  32. // using the supplied client and setLister.
  33. func NewRealStatefulSetStatusUpdater(
  34. client clientset.Interface,
  35. setLister appslisters.StatefulSetLister) StatefulSetStatusUpdaterInterface {
  36. return &realStatefulSetStatusUpdater{client, setLister}
  37. }
  38. type realStatefulSetStatusUpdater struct {
  39. client clientset.Interface
  40. setLister appslisters.StatefulSetLister
  41. }
  42. func (ssu *realStatefulSetStatusUpdater) UpdateStatefulSetStatus(
  43. set *apps.StatefulSet,
  44. status *apps.StatefulSetStatus) error {
  45. // don't wait due to limited number of clients, but backoff after the default number of steps
  46. return retry.RetryOnConflict(retry.DefaultRetry, func() error {
  47. set.Status = *status
  48. _, updateErr := ssu.client.AppsV1().StatefulSets(set.Namespace).UpdateStatus(context.TODO(), set, metav1.UpdateOptions{})
  49. if updateErr == nil {
  50. return nil
  51. }
  52. if updated, err := ssu.setLister.StatefulSets(set.Namespace).Get(set.Name); err == nil {
  53. // make a copy so we don't mutate the shared cache
  54. set = updated.DeepCopy()
  55. } else {
  56. utilruntime.HandleError(fmt.Errorf("error getting updated StatefulSet %s/%s from lister: %v", set.Namespace, set.Name, err))
  57. }
  58. return updateErr
  59. })
  60. }
  61. var _ StatefulSetStatusUpdaterInterface = &realStatefulSetStatusUpdater{}