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

Retry setting Out Of Service flags #235

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -141,7 +141,7 @@ func main() {
os.Exit(1)
}

if err := utils.InitOutOfServiceTaintFlags(mgr.GetConfig()); err != nil {
if err := utils.InitOutOfServiceTaintFlagsWithRetry(context.Background(), mgr.GetConfig()); err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement

setupLog.Error(err, "unable to verify out-of-service taint support. out-of-service taint isn't supported")
}

Expand Down
23 changes: 22 additions & 1 deletion pkg/utils/taints.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package utils

import (
"context"
"fmt"
"regexp"
"strconv"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -54,7 +57,25 @@ func DeleteTaint(taints []v1.Taint, taintToDelete *v1.Taint) ([]v1.Taint, bool)
return newTaints, deleted
}

func InitOutOfServiceTaintFlags(config *rest.Config) error {
// InitOutOfServiceTaintFlagsWithRetry tries to initialize the OutOfService flags based on k8s version, in case it fails (potentially due to network issues) it will retry for a limited number of times
func InitOutOfServiceTaintFlagsWithRetry(ctx context.Context, config *rest.Config) error {
slintes marked this conversation as resolved.
Show resolved Hide resolved

var err error
interval := 2 * time.Second // retry every 2 seconds
timeout := 10 * time.Second // for a period of 10 seconds

// Since the last internal error returned by InitOutOfServiceTaintFlags also indicates whether polling succeed or not, there is no need to also keep the context error returned by PollUntilContextTimeout.
// Using wait.PollUntilContextTimeout to retry initOutOfServiceTaintFlags in case there is a temporary network issue.
_ = wait.PollUntilContextTimeout(ctx, interval, timeout, true, func(ctx context.Context) (bool, error) {
if err = initOutOfServiceTaintFlags(config); err != nil {
return false, nil // Keep retrying
}
return true, nil // Success
})
return err
}

func initOutOfServiceTaintFlags(config *rest.Config) error {
if cs, err := kubernetes.NewForConfig(config); err != nil || cs == nil {
if cs == nil {
err = fmt.Errorf("k8s client set is nil")
Expand Down
Loading