From 32fbb23f3b3bfdd62bbfc5e1ef034c1ea36f0a00 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Sun, 19 Nov 2023 17:57:24 +0200 Subject: [PATCH] kubeadm: remove usage of the TryRunCommand() function The function TryRunCommand() uses an exponential backoff, which is good, but it's inconsistent and only used in a couple of places. Remove its usage in the token.go#UpdateOrCreateTokens() and switch to using the standard function used in other places - PollUntilContextTimeout(). Remove wait.go#TryRunCommand(), as there are no other usages. --- .../app/phases/bootstraptoken/node/token.go | 24 ++++++++++++------- cmd/kubeadm/app/util/apiclient/wait.go | 18 -------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/cmd/kubeadm/app/phases/bootstraptoken/node/token.go b/cmd/kubeadm/app/phases/bootstraptoken/node/token.go index 539d7ef623c..7b13bbdd9ea 100644 --- a/cmd/kubeadm/app/phases/bootstraptoken/node/token.go +++ b/cmd/kubeadm/app/phases/bootstraptoken/node/token.go @@ -22,10 +22,12 @@ import ( "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" clientset "k8s.io/client-go/kubernetes" bootstraputil "k8s.io/cluster-bootstrap/token/util" bootstraptokenv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1" + kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants" "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient" ) @@ -46,15 +48,21 @@ func UpdateOrCreateTokens(client clientset.Interface, failIfExists bool, tokens } updatedOrNewSecret := bootstraptokenv1.BootstrapTokenToSecret(&token) - // Try to create or update the token with an exponential backoff - err = apiclient.TryRunCommand(func() error { - if err := apiclient.CreateOrUpdateSecret(client, updatedOrNewSecret); err != nil { - return errors.Wrapf(err, "failed to create or update bootstrap token with name %s", secretName) - } - return nil - }, 5) + + var lastError error + err = wait.PollUntilContextTimeout( + context.Background(), + kubeadmconstants.APICallRetryInterval, + kubeadmconstants.APICallWithWriteTimeout, + true, func(_ context.Context) (bool, error) { + if err := apiclient.CreateOrUpdateSecret(client, updatedOrNewSecret); err != nil { + lastError = errors.Wrapf(err, "failed to create or update bootstrap token with name %s", secretName) + return false, nil + } + return true, nil + }) if err != nil { - return err + return lastError } } return nil diff --git a/cmd/kubeadm/app/util/apiclient/wait.go b/cmd/kubeadm/app/util/apiclient/wait.go index 7808afdb214..f7939dddc3a 100644 --- a/cmd/kubeadm/app/util/apiclient/wait.go +++ b/cmd/kubeadm/app/util/apiclient/wait.go @@ -280,21 +280,3 @@ func getStaticPodSingleHash(client clientset.Interface, nodeName string, compone staticPodHash := staticPod.Annotations["kubernetes.io/config.hash"] return staticPodHash, nil } - -// TryRunCommand runs a function a maximum of failureThreshold times, and retries on error. If failureThreshold is hit; the last error is returned -func TryRunCommand(f func() error, failureThreshold int) error { - backoff := wait.Backoff{ - Duration: 5 * time.Second, - Factor: 2, // double the timeout for every failure - Steps: failureThreshold, - } - return wait.ExponentialBackoff(backoff, func() (bool, error) { - err := f() - if err != nil { - // Retry until the timeout - return false, nil - } - // The last f() call was a success, return cleanly - return true, nil - }) -}