stateful_set_status_updater.go 2.7 KB

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