...

Source file src/k8s.io/kubernetes/pkg/controller/statefulset/stateful_set_status_updater.go

Documentation: k8s.io/kubernetes/pkg/controller/statefulset

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package statefulset
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	apps "k8s.io/api/apps/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    26  	clientset "k8s.io/client-go/kubernetes"
    27  	appslisters "k8s.io/client-go/listers/apps/v1"
    28  	"k8s.io/client-go/util/retry"
    29  )
    30  
    31  // StatefulSetStatusUpdaterInterface is an interface used to update the StatefulSetStatus associated with a StatefulSet.
    32  // For any use other than testing, clients should create an instance using NewRealStatefulSetStatusUpdater.
    33  type StatefulSetStatusUpdaterInterface interface {
    34  	// UpdateStatefulSetStatus sets the set's Status to status. Implementations are required to retry on conflicts,
    35  	// but fail on other errors. If the returned error is nil set's Status has been successfully set to status.
    36  	UpdateStatefulSetStatus(ctx context.Context, set *apps.StatefulSet, status *apps.StatefulSetStatus) error
    37  }
    38  
    39  // NewRealStatefulSetStatusUpdater returns a StatefulSetStatusUpdaterInterface that updates the Status of a StatefulSet,
    40  // using the supplied client and setLister.
    41  func NewRealStatefulSetStatusUpdater(
    42  	client clientset.Interface,
    43  	setLister appslisters.StatefulSetLister) StatefulSetStatusUpdaterInterface {
    44  	return &realStatefulSetStatusUpdater{client, setLister}
    45  }
    46  
    47  type realStatefulSetStatusUpdater struct {
    48  	client    clientset.Interface
    49  	setLister appslisters.StatefulSetLister
    50  }
    51  
    52  func (ssu *realStatefulSetStatusUpdater) UpdateStatefulSetStatus(
    53  	ctx context.Context,
    54  	set *apps.StatefulSet,
    55  	status *apps.StatefulSetStatus) error {
    56  	// don't wait due to limited number of clients, but backoff after the default number of steps
    57  	return retry.RetryOnConflict(retry.DefaultRetry, func() error {
    58  		set.Status = *status
    59  		// TODO: This context.TODO should use a real context once we have RetryOnConflictWithContext
    60  		_, updateErr := ssu.client.AppsV1().StatefulSets(set.Namespace).UpdateStatus(context.TODO(), set, metav1.UpdateOptions{})
    61  		if updateErr == nil {
    62  			return nil
    63  		}
    64  		if updated, err := ssu.setLister.StatefulSets(set.Namespace).Get(set.Name); err == nil {
    65  			// make a copy so we don't mutate the shared cache
    66  			set = updated.DeepCopy()
    67  		} else {
    68  			utilruntime.HandleError(fmt.Errorf("error getting updated StatefulSet %s/%s from lister: %v", set.Namespace, set.Name, err))
    69  		}
    70  
    71  		return updateErr
    72  	})
    73  }
    74  
    75  var _ StatefulSetStatusUpdaterInterface = &realStatefulSetStatusUpdater{}
    76  

View as plain text