Skip to content

Commit

Permalink
config refactoring: use avast retry-go
Browse files Browse the repository at this point in the history
Signed-off-by: Karen Almog <[email protected]>
  • Loading branch information
Karen Almog committed Jan 13, 2022
1 parent 211db00 commit 4930e80
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions pkg/config/api_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"time"

"github.com/avast/retry-go"
"github.com/imdario/mergo"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -36,25 +37,28 @@ var (

// run a config-request from the API and wait until the API is up
func (rules *ClientConfigLoadingRules) getConfigFromAPI(client k0sv1beta1.K0sV1beta1Interface) (*v1beta1.ClusterConfig, error) {
timeout := time.After(120 * time.Second)
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
// Keep trying until we're timed out or got a result or got an error
for {
select {
// Got a timeout! fail with a timeout error
case <-timeout:
return nil, fmt.Errorf("timed out waiting for API to return cluster-config")
// Got a tick, we should check on doSomething()
case <-ticker.C:
logrus.Debug("fetching cluster-config from API...")
cfg, err := rules.configRequest(client)
if err != nil {
continue
}
return cfg, nil

var cfg *v1beta1.ClusterConfig
var err error
ctx, cancelFunction := context.WithTimeout(context.Background(), 120*time.Second)
// clear up context after timeout
defer cancelFunction()

err = retry.Do(func() error {
// five minutes here are coming from maximum theoretical duration of kubelet bootstrap process
// we use retry.Do with 10 attempts, back-off delay and delay duration 500 ms which gives us
// 225 seconds here
logrus.Debug("fetching cluster-config from API...")
cfg, err = rules.configRequest(client)
if err != nil {
return err
}
return nil
}, retry.Context(ctx))
if err != nil {
return nil, fmt.Errorf("timed out waiting for API to return cluster-config")
}
return cfg, nil
}

// when API config is enabled, but only node config is needed (for bootstrapping commands)
Expand Down

0 comments on commit 4930e80

Please sign in to comment.