k8s.io/kubernetes@v1.29.3/test/utils/replicaset.go (about)

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