Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry for the k8s version call #21

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,4 +1,4 @@
//go:generate mockgen -destination ./mock/version.go . Interface

Check failure on line 1 in version/version.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/castai/spot-handler/version [github.com/castai/spot-handler/version.test]
package version

import (
Expand All @@ -6,6 +6,8 @@
"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 @@
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 @@
func (a *HandlerVersion) String() string {
return fmt.Sprintf("GitCommit=%q GitRef=%q Version=%q", a.GitCommit, a.GitRef, a.Version)
}

Loading