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

Emptiness reconcile now properly backs off #664

Merged
merged 8 commits into from
Sep 8, 2021
7 changes: 3 additions & 4 deletions pkg/controllers/node/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"time"

"github.com/awslabs/karpenter/pkg/apis/provisioning/v1alpha3"
"github.com/awslabs/karpenter/pkg/utils/functional"
"github.com/awslabs/karpenter/pkg/utils/result"

"go.uber.org/multierr"
Expand Down Expand Up @@ -88,7 +87,7 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reco

// 3. Execute reconcilers
node := stored.DeepCopy()
var backoffs []time.Duration
var results []reconcile.Result
var errs error
for _, reconciler := range []interface {
Reconcile(context.Context, *v1alpha3.Provisioner, *v1.Node) (reconcile.Result, error)
Expand All @@ -101,7 +100,7 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reco
} {
res, err := reconciler.Reconcile(ctx, provisioner, node)
errs = multierr.Append(errs, err)
backoffs = append(backoffs, res.RequeueAfter)
results = append(results, res)
}

// 4. Patch any changes, regardless of errors
Expand All @@ -114,7 +113,7 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reco
if errs != nil {
return result.RetryIfError(ctx, errs)
}
return reconcile.Result{RequeueAfter: functional.MaxDuration(backoffs...)}, nil
return result.MinResult(results...), nil
}

func (c *Controller) Register(ctx context.Context, m manager.Manager) error {
Expand Down
12 changes: 0 additions & 12 deletions pkg/utils/functional/functional.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package functional

import (
"strings"
"time"

"go.uber.org/multierr"
)
Expand Down Expand Up @@ -110,14 +109,3 @@ func InvertStringMap(stringMap map[string]string) map[string]string {
}
return inverted
}

// MaxDuration returns the largest duration
func MaxDuration(durations ...time.Duration) time.Duration {
var max time.Duration
for _, duration := range durations {
if duration > max {
max = duration
}
}
return max
}
18 changes: 18 additions & 0 deletions pkg/utils/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package result

import (
"context"
"math"
"time"

"go.uber.org/multierr"
"knative.dev/pkg/logging"
Expand All @@ -29,3 +31,19 @@ func RetryIfError(ctx context.Context, err error) (reconcile.Result, error) {
}
return reconcile.Result{Requeue: err != nil}, nil
}

// MinResult returns the result that wants to requeue the soonest
func MinResult(results ...reconcile.Result) (result reconcile.Result) {
min := time.Duration(math.MaxInt64)
for _, r := range results {
if r.IsZero() {
continue
}
if r.RequeueAfter < min {
min = r.RequeueAfter
result.RequeueAfter = min
result.Requeue = true
}
}
return
}