@@ -18,6 +18,7 @@ package controller
1818
1919import (
2020 "context"
21+ "errors"
2122
2223 corev1 "k8s.io/api/core/v1"
2324 apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -28,6 +29,7 @@ import (
2829 ctrl "sigs.k8s.io/controller-runtime"
2930 "sigs.k8s.io/controller-runtime/pkg/client"
3031 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
32+ "sigs.k8s.io/controller-runtime/pkg/event"
3133 "sigs.k8s.io/controller-runtime/pkg/log"
3234 "sigs.k8s.io/controller-runtime/pkg/metrics"
3335 "sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -198,10 +200,9 @@ func (r *NodeCIDRAllocationReconciler) Reconcile(ctx context.Context, req ctrl.R
198200 return ctrl.Result {}, err
199201 }
200202
201- rl .Info (
202- "reconciling matching Nodes with NodeCIDRAllocation ..." ,
203- "nodeCIDRAllocation" , nodeCIDRAllocation .GetName (),
204- )
203+ //
204+ // Begin allocation process
205+ //
205206
206207 freeSubnets := make (map [uint8 ][]string )
207208 for _ , node := range matchingNodes .Items {
@@ -323,6 +324,9 @@ func (r *NodeCIDRAllocationReconciler) finalizeReconcile(ctx context.Context, no
323324 r .updateNodeCIDRAllocationStatus (ctx , nodeCIDRAllocation , nodes , err )
324325 r .updatePrometheusMetrics (ctx )
325326
327+ // remove or add Node taints according to allocation status of each matching node
328+ err = errors .Join (err , r .updateNodeTaints (ctx , nodes ))
329+
326330 // passthrough for err (if non-nil) to the Reconcile Result
327331 return err
328332}
@@ -384,6 +388,24 @@ func (r *NodeCIDRAllocationReconciler) updateNodeCIDRAllocationStatus(ctx contex
384388 }
385389}
386390
391+ func (r * NodeCIDRAllocationReconciler ) updateNodeTaints (ctx context.Context , nodes * corev1.NodeList ) error {
392+ ntc := & NodeTaintClient {}
393+ for _ , n := range nodes .Items {
394+ ntc .Handle (& n )
395+ if err := r .Client .Update (ctx , & n ); err != nil {
396+ if apierrors .IsNotFound (err ) {
397+ // node was removed after reconcile request was created - skip the Node
398+ continue
399+ }
400+
401+ // error trying to add the Node taint to the Node object
402+ return err
403+ }
404+ }
405+
406+ return nil
407+ }
408+
387409// triggerNodeCIDRAllocationReconcileFromNodeChange is a mapping function which takes a Node object
388410// and returns a list of reconciliation requests for all NodeCIDRAllocation resources that have a matching NodeSelector
389411func (r * NodeCIDRAllocationReconciler ) triggerNodeCIDRAllocationReconcileFromNodeChange (ctx context.Context , o client.Object ) []reconcile.Request {
@@ -429,9 +451,12 @@ func (r *NodeCIDRAllocationReconciler) SetupWithManager(mgr ctrl.Manager) error
429451 Watches (
430452 & corev1.Node {},
431453 handler .EnqueueRequestsFromMapFunc (r .triggerNodeCIDRAllocationReconcileFromNodeChange ),
432- builder .WithPredicates (predicate .Or (
433- predicate.LabelChangedPredicate {},
434- )),
454+ builder .WithPredicates (predicate.Funcs {
455+ CreateFunc : func (_ event.CreateEvent ) bool { return true },
456+ UpdateFunc : func (_ event.UpdateEvent ) bool { return false },
457+ DeleteFunc : func (_ event.DeleteEvent ) bool { return false },
458+ GenericFunc : func (_ event.GenericEvent ) bool { return false },
459+ }),
435460 ).
436461 Complete (r )
437462}
0 commit comments