Skip to content

Commit

Permalink
Merge pull request #21 from castai/add-retries-for-version
Browse files Browse the repository at this point in the history
Add retry for the k8s version call
  • Loading branch information
apasyniuk authored Mar 6, 2024
2 parents d514924 + d81a74a commit ed02d2d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 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)
}

3 changes: 2 additions & 1 deletion version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http/httptest"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/kubernetes"
Expand All @@ -32,7 +33,7 @@ func Test(t *testing.T) {
defer s.Close()
client := kubernetes.NewForConfigOrDie(&rest.Config{Host: s.URL})

got, err := Get(client)
got, err := Get(logrus.NewEntry(logrus.New()), client)
if err != nil {
return
}
Expand Down

0 comments on commit ed02d2d

Please sign in to comment.