Skip to content
Open
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
18 changes: 17 additions & 1 deletion internal/controller/nodereadinessrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"

readinessv1alpha1 "sigs.k8s.io/node-readiness-controller/api/v1alpha1"
Expand Down Expand Up @@ -78,12 +79,27 @@ func NewRuleReadinessController(mgr ctrl.Manager, clientset kubernetes.Interface
}
}

// rulePredicate behaves like GenerationChangedPredicate, but also allows the
// update that first sets DeletionTimestamp so finalizer cleanup is reconciled.
var rulePredicate = predicate.Funcs{
Comment on lines +82 to +84
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Self-review: this could also be expressed as predicate.Or(predicate.GenerationChangedPredicate{}, deletionTimestampPredicate). I kept it as a single predicate because it's only ~10 lines and avoids a one-shot helper, but happy to switch to composition if maintainers prefer using the controller-runtime built-in

UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}
if e.ObjectNew.GetGeneration() != e.ObjectOld.GetGeneration() {
return true
}
return e.ObjectOld.GetDeletionTimestamp().IsZero() &&
!e.ObjectNew.GetDeletionTimestamp().IsZero()
},
}

// SetupWithManager sets up the controller with the Manager.
func (r *RuleReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Named("nodereadiness-controller").
WithOptions(controller.Options{MaxConcurrentReconciles: 1}).
For(&readinessv1alpha1.NodeReadinessRule{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&readinessv1alpha1.NodeReadinessRule{}, builder.WithPredicates(rulePredicate)).
Complete(r)
}

Expand Down
43 changes: 43 additions & 0 deletions internal/controller/nodereadinessrule_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

nodereadinessiov1alpha1 "sigs.k8s.io/node-readiness-controller/api/v1alpha1"
Expand Down Expand Up @@ -1380,4 +1381,46 @@ var _ = Describe("NodeReadinessRule Controller", func() {
}, time.Second*5).Should(BeTrue(), "All AppliedNodes should have corresponding NodeEvaluations")
})
})

Context("rulePredicate", func() {
newRule := func(generation int64, deletionTimestamp *metav1.Time) *nodereadinessiov1alpha1.NodeReadinessRule {
return &nodereadinessiov1alpha1.NodeReadinessRule{
ObjectMeta: metav1.ObjectMeta{
Name: "predicate-rule",
Generation: generation,
DeletionTimestamp: deletionTimestamp,
},
}
}

It("passes updates that bump generation", func() {
Expect(rulePredicate.Update(event.UpdateEvent{
ObjectOld: newRule(1, nil),
ObjectNew: newRule(2, nil),
})).To(BeTrue())
})

It("passes updates that set the deletion timestamp without bumping generation", func() {
now := metav1.Now()
Expect(rulePredicate.Update(event.UpdateEvent{
ObjectOld: newRule(1, nil),
ObjectNew: newRule(1, &now),
})).To(BeTrue(), "deletion-marker updates must trigger reconcile so the finalizer can be removed")
})

It("ignores status-only updates", func() {
Expect(rulePredicate.Update(event.UpdateEvent{
ObjectOld: newRule(1, nil),
ObjectNew: newRule(1, nil),
})).To(BeFalse())
})

It("ignores updates while the rule is already terminating", func() {
now := metav1.Now()
Expect(rulePredicate.Update(event.UpdateEvent{
ObjectOld: newRule(1, &now),
ObjectNew: newRule(1, &now),
})).To(BeFalse())
})
})
})