Skip to content

Commit

Permalink
now compares min reconcile.Result not min duration
Browse files Browse the repository at this point in the history
  • Loading branch information
njtran committed Sep 8, 2021
1 parent 5c8e002 commit 16a51dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
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.MinDuration(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 MinDuration(durations ...time.Duration) time.Duration {
var min time.Duration
for _, duration := range durations {
if (duration < min && duration != 0) || (duration != 0 && min == 0) {
min = duration
}
}
return min
}
20 changes: 20 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,21 @@ func RetryIfError(ctx context.Context, err error) (reconcile.Result, error) {
}
return reconcile.Result{Requeue: err != nil}, nil
}

// MinResult returns the smallest reconcile.Result.
// Results that want to requeue immediately take priority
func MinResult(results ...reconcile.Result) (result reconcile.Result) {
min := time.Duration(math.MaxInt64)
for _, r := range results {
if r.Requeue && r.RequeueAfter == 0 {
result.Requeue = true
return
}
if r.RequeueAfter != 0 && r.RequeueAfter < min {
min = r.RequeueAfter
}
}
result.RequeueAfter = min
return

}

0 comments on commit 16a51dc

Please sign in to comment.