Skip to content

Commit

Permalink
Add retires for the k8s version call
Browse files Browse the repository at this point in the history
  • Loading branch information
apasyniuk committed Mar 6, 2024
1 parent d514924 commit f6a0b35
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 13 additions & 3 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}

0 comments on commit f6a0b35

Please sign in to comment.