From f6a0b350962e435b7b683309df2685e70c0e62bb Mon Sep 17 00:00:00 2001 From: Adam Pasyniuk Date: Wed, 6 Mar 2024 13:54:13 +0100 Subject: [PATCH] Add retires for the k8s version call --- main.go | 2 +- version/version.go | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 3b01bb9..fa49a3b 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ func main() { log.Fatalf("err creating clientset: %v", err) } - k8sVersion, err := version.Get(clientset) + k8sVersion, err := version.Get(log, clientset) if err != nil { log.Fatalf("failed getting kubernetes version: %v", err) } diff --git a/version/version.go b/version/version.go index 9ba92cf..91ac503 100644 --- a/version/version.go +++ b/version/version.go @@ -6,6 +6,8 @@ import ( "regexp" "strconv" + "github.com/cenkalti/backoff/v4" + "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/version" "k8s.io/client-go/kubernetes" ) @@ -15,13 +17,22 @@ type Interface interface { MinorInt() int } -func Get(clientset kubernetes.Interface) (Interface, error) { +func Get(log *logrus.Entry, clientset kubernetes.Interface) (Interface, error) { cs, ok := clientset.(*kubernetes.Clientset) if !ok { return nil, fmt.Errorf("expected clientset to be of type *kubernetes.Clientset but was %T", clientset) } - sv, err := cs.ServerVersion() + var sv *version.Info + err := backoff.Retry(func() error { + var err error + sv, err = cs.ServerVersion() + if err != nil { + log.Warnf("failed getting server version, retrying: %v", err) + return err + } + return nil + }, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 5)) if err != nil { return nil, fmt.Errorf("getting server version: %w", err) } @@ -54,4 +65,3 @@ type HandlerVersion struct { func (a *HandlerVersion) String() string { return fmt.Sprintf("GitCommit=%q GitRef=%q Version=%q", a.GitCommit, a.GitRef, a.Version) } -