k8s.io/kubernetes@v1.29.3/test/utils/update_resources.go (about) 1 /* 2 Copyright 2018 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 "fmt" 21 "time" 22 23 "k8s.io/apimachinery/pkg/runtime/schema" 24 "k8s.io/apimachinery/pkg/util/wait" 25 scaleclient "k8s.io/client-go/scale" 26 "k8s.io/kubectl/pkg/scale" 27 ) 28 29 const ( 30 // Parameters for retrying updates/waits with linear backoff. 31 // TODO: Try to move this to exponential backoff by modifying scale.Scale(). 32 updateRetryInterval = 5 * time.Second 33 updateRetryTimeout = 1 * time.Minute 34 waitRetryInterval = 5 * time.Second 35 waitRetryTimeout = 5 * time.Minute 36 ) 37 38 func RetryErrorCondition(condition wait.ConditionFunc) wait.ConditionFunc { 39 return func() (bool, error) { 40 done, err := condition() 41 return done, err 42 } 43 } 44 45 func ScaleResourceWithRetries(scalesGetter scaleclient.ScalesGetter, namespace, name string, size uint, gvr schema.GroupVersionResource) error { 46 scaler := scale.NewScaler(scalesGetter) 47 preconditions := &scale.ScalePrecondition{ 48 Size: -1, 49 ResourceVersion: "", 50 } 51 waitForReplicas := scale.NewRetryParams(waitRetryInterval, waitRetryTimeout) 52 cond := RetryErrorCondition(scale.ScaleCondition(scaler, preconditions, namespace, name, size, nil, gvr, false)) 53 err := wait.PollImmediate(updateRetryInterval, updateRetryTimeout, cond) 54 if err == nil { 55 err = scale.WaitForScaleHasDesiredReplicas(scalesGetter, gvr.GroupResource(), name, namespace, size, waitForReplicas) 56 } 57 if err != nil { 58 return fmt.Errorf("error while scaling %s to %d replicas: %v", name, size, err) 59 } 60 return nil 61 }