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.
This commit is contained in:
Lubomir I. Ivanov 2023-11-19 17:57:24 +02:00
parent 557118897d
commit 32fbb23f3b
2 changed files with 16 additions and 26 deletions

View File

@ -22,10 +22,12 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes" clientset "k8s.io/client-go/kubernetes"
bootstraputil "k8s.io/cluster-bootstrap/token/util" bootstraputil "k8s.io/cluster-bootstrap/token/util"
bootstraptokenv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1" 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" "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
) )
@ -46,15 +48,21 @@ func UpdateOrCreateTokens(client clientset.Interface, failIfExists bool, tokens
} }
updatedOrNewSecret := bootstraptokenv1.BootstrapTokenToSecret(&token) updatedOrNewSecret := bootstraptokenv1.BootstrapTokenToSecret(&token)
// Try to create or update the token with an exponential backoff
err = apiclient.TryRunCommand(func() error { var lastError error
if err := apiclient.CreateOrUpdateSecret(client, updatedOrNewSecret); err != nil { err = wait.PollUntilContextTimeout(
return errors.Wrapf(err, "failed to create or update bootstrap token with name %s", secretName) context.Background(),
} kubeadmconstants.APICallRetryInterval,
return nil kubeadmconstants.APICallWithWriteTimeout,
}, 5) 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 { if err != nil {
return err return lastError
} }
} }
return nil return nil

View File

@ -280,21 +280,3 @@ func getStaticPodSingleHash(client clientset.Interface, nodeName string, compone
staticPodHash := staticPod.Annotations["kubernetes.io/config.hash"] staticPodHash := staticPod.Annotations["kubernetes.io/config.hash"]
return staticPodHash, nil 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
})
}