Skip to content

Commit e34dbbc

Browse files
committed
Cancel health checks on new reconciliation request
Signed-off-by: Matheus Pimenta <[email protected]>
1 parent d1925bf commit e34dbbc

File tree

5 files changed

+25
-45
lines changed

5 files changed

+25
-45
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
github.com/fluxcd/pkg/cache v0.12.0
2929
github.com/fluxcd/pkg/http/fetch v0.20.0
3030
github.com/fluxcd/pkg/kustomize v1.23.0
31-
github.com/fluxcd/pkg/runtime v0.88.0
31+
github.com/fluxcd/pkg/runtime v0.89.1-0.20251010110127-c6bc6a65ae30
3232
github.com/fluxcd/pkg/ssa v0.60.0
3333
github.com/fluxcd/pkg/tar v0.15.0
3434
github.com/fluxcd/pkg/testserver v0.13.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ github.com/fluxcd/pkg/http/fetch v0.20.0 h1:/Lvcu1JzABBLuQYuLKYh1K02a+RqbP4b5wIZ
206206
github.com/fluxcd/pkg/http/fetch v0.20.0/go.mod h1:9inwDiGOpuo14Rp06TpcgsYSkvp4YM+uWCjgDmpXMNk=
207207
github.com/fluxcd/pkg/kustomize v1.23.0 h1:4tNh30OsIj96YRfVP7qP0Fv3QTwdBo/udfZIcccL6NI=
208208
github.com/fluxcd/pkg/kustomize v1.23.0/go.mod h1:ZojUvmI4RiHk3BH3L3mBQ4ZDbNkiWfX9LvOMBjKq5Tc=
209-
github.com/fluxcd/pkg/runtime v0.88.0 h1:EFPJ0jnRino6yUEwiNtQTpUNyCf96N2MJb+S7LVG648=
210-
github.com/fluxcd/pkg/runtime v0.88.0/go.mod h1:qkmPX009tgiWufQ2Vj0QhyNgEU+0Cnz7Xy/naihLM10=
209+
github.com/fluxcd/pkg/runtime v0.89.1-0.20251010110127-c6bc6a65ae30 h1:iy8cdKPpD9Iaxj30jUc5A/zeO774weMnPvA725yEnq0=
210+
github.com/fluxcd/pkg/runtime v0.89.1-0.20251010110127-c6bc6a65ae30/go.mod h1:qkmPX009tgiWufQ2Vj0QhyNgEU+0Cnz7Xy/naihLM10=
211211
github.com/fluxcd/pkg/sourceignore v0.15.0 h1:tB30fuk4jlB3UGlR7ppJguZ3zaJh1iwuTCEufs91jSM=
212212
github.com/fluxcd/pkg/sourceignore v0.15.0/go.mod h1:mZ9X6gNtNkq9ZsD35LebEYjePc7DRvB2JdowMNoj6IU=
213213
github.com/fluxcd/pkg/ssa v0.60.0 h1:ikA78TWSLDmIc8I/goGAU/buYF6jto/gswE5hnOfWGk=

internal/controller/kustomization_controller.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -985,42 +985,20 @@ func (r *KustomizationReconciler) checkHealth(ctx context.Context,
985985

986986
// Check the health with a default timeout of 30sec shorter than the reconciliation interval.
987987
healthCtx := ctx
988-
if r.CancelHealthCheckOnNewRevision {
989-
// Create a cancellable context for health checks that monitors for new revisions
990-
var cancel context.CancelFunc
991-
healthCtx, cancel = context.WithCancel(ctx)
992-
defer cancel()
993-
994-
// Start monitoring for new revisions to allow early cancellation
995-
go func() {
996-
ticker := time.NewTicker(5 * time.Second)
997-
defer ticker.Stop()
998-
999-
for {
1000-
select {
1001-
case <-healthCtx.Done():
1002-
return
1003-
case <-ticker.C:
1004-
// Get the latest source artifact
1005-
latestSrc, err := r.getSource(ctx, obj)
1006-
if err == nil && latestSrc.GetArtifact() != nil {
1007-
if newRevision := latestSrc.GetArtifact().Revision; newRevision != revision {
1008-
const msg = "New revision detected during health check, cancelling"
1009-
r.event(obj, revision, originRevision, eventv1.EventSeverityInfo, msg, nil)
1010-
ctrl.LoggerFrom(ctx).Info(msg, "current", revision, "new", newRevision)
1011-
cancel()
1012-
return
1013-
}
1014-
}
1015-
}
1016-
}
1017-
}()
988+
cancelHealthCheckOnEnqueue := r.CancelHealthCheckOnNewRevision // we could OR this with a boolean in the object spec
989+
if cancelHealthCheckOnEnqueue {
990+
healthCtx = runtimeCtrl.GetObjectContext(ctx)
1018991
}
1019992
if err := manager.WaitForSetWithContext(healthCtx, toCheck, ssa.WaitOptions{
1020993
Interval: 5 * time.Second,
1021994
Timeout: obj.GetTimeout(),
1022995
FailFast: r.FailFast,
1023996
}); err != nil {
997+
if cancelHealthCheckOnEnqueue && runtimeCtrl.IsObjectEnqueued(ctx) {
998+
const msg = "New reconciliation request detected during health check, cancelling"
999+
r.event(obj, revision, originRevision, eventv1.EventSeverityInfo, msg, nil)
1000+
ctrl.LoggerFrom(ctx).Info(msg)
1001+
}
10241002
conditions.MarkFalse(obj, meta.ReadyCondition, meta.HealthCheckFailedReason, "%s", err)
10251003
conditions.MarkFalse(obj, meta.HealthyCondition, meta.HealthCheckFailedReason, "%s", err)
10261004
return fmt.Errorf("health check failed after %s: %w", time.Since(checkStart).String(), err)

internal/controller/kustomization_manager.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/builder"
2727
"sigs.k8s.io/controller-runtime/pkg/client"
2828
"sigs.k8s.io/controller-runtime/pkg/controller"
29-
"sigs.k8s.io/controller-runtime/pkg/handler"
3029
"sigs.k8s.io/controller-runtime/pkg/predicate"
3130
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3231

32+
runtimeCtrl "github.com/fluxcd/pkg/runtime/controller"
3333
"github.com/fluxcd/pkg/runtime/predicates"
3434
sourcev1 "github.com/fluxcd/source-controller/api/v1"
3535

@@ -129,43 +129,45 @@ func (r *KustomizationReconciler) SetupWithManager(ctx context.Context, mgr ctrl
129129
return fmt.Errorf("failed creating index %s: %w", indexSecret, err)
130130
}
131131

132-
ctrlBuilder := ctrl.NewControllerManagedBy(mgr).
133-
For(&kustomizev1.Kustomization{}, builder.WithPredicates(
132+
wr := runtimeCtrl.WrapReconciler(r)
133+
134+
ctrlBuilder := runtimeCtrl.NewControllerManagedBy(mgr, wr).
135+
For(&kustomizev1.Kustomization{},
134136
predicate.Or(predicate.GenerationChangedPredicate{}, predicates.ReconcileRequestedPredicate{}),
135-
)).
137+
).
136138
Watches(
137139
&sourcev1.OCIRepository{},
138-
handler.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(indexOCIRepository)),
140+
wr.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(indexOCIRepository)),
139141
builder.WithPredicates(SourceRevisionChangePredicate{}),
140142
).
141143
Watches(
142144
&sourcev1.GitRepository{},
143-
handler.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(indexGitRepository)),
145+
wr.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(indexGitRepository)),
144146
builder.WithPredicates(SourceRevisionChangePredicate{}),
145147
).
146148
Watches(
147149
&sourcev1.Bucket{},
148-
handler.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(indexBucket)),
150+
wr.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(indexBucket)),
149151
builder.WithPredicates(SourceRevisionChangePredicate{}),
150152
).
151153
WatchesMetadata(
152154
&corev1.ConfigMap{},
153-
handler.EnqueueRequestsFromMapFunc(r.requestsForConfigDependency(indexConfigMap)),
155+
wr.EnqueueRequestsFromMapFunc(r.requestsForConfigDependency(indexConfigMap)),
154156
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}, opts.WatchConfigsPredicate),
155157
).
156158
WatchesMetadata(
157159
&corev1.Secret{},
158-
handler.EnqueueRequestsFromMapFunc(r.requestsForConfigDependency(indexSecret)),
160+
wr.EnqueueRequestsFromMapFunc(r.requestsForConfigDependency(indexSecret)),
159161
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}, opts.WatchConfigsPredicate),
160162
)
161163

162164
if opts.WatchExternalArtifacts {
163165
ctrlBuilder = ctrlBuilder.Watches(
164166
&sourcev1.ExternalArtifact{},
165-
handler.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(indexExternalArtifact)),
167+
wr.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(indexExternalArtifact)),
166168
builder.WithPredicates(SourceRevisionChangePredicate{}),
167169
)
168170
}
169171

170-
return ctrlBuilder.WithOptions(controller.Options{RateLimiter: opts.RateLimiter}).Complete(r)
172+
return ctrlBuilder.WithOptions(controller.Options{RateLimiter: opts.RateLimiter}).Complete(wr)
171173
}

internal/controller/kustomization_wait_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ spec:
621621
events := getEvents(resultK.GetName(), nil)
622622
var found bool
623623
for _, e := range events {
624-
if e.Message == "New revision detected during health check, cancelling" {
624+
if e.Message == "New reconciliation request detected during health check, cancelling" {
625625
found = true
626626
break
627627
}

0 commit comments

Comments
 (0)