@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"reflect"
23
+ "sort"
23
24
24
25
corev1 "k8s.io/api/core/v1"
25
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -65,20 +66,22 @@ func (c *ClusterTaintPolicyController) Reconcile(ctx context.Context, req contro
65
66
return controllerruntime.Result {}, client .IgnoreNotFound (err )
66
67
}
67
68
68
- if ! clusterObj .DeletionTimestamp .IsZero () {
69
- klog .V (4 ).Infof ("Cluster(%s) is deleting." , req .Name )
70
- return controllerruntime.Result {}, nil
71
- }
72
-
73
69
clusterTaintPolicyList := & policyv1alpha1.ClusterTaintPolicyList {}
74
70
listOption := & client.ListOptions {UnsafeDisableDeepCopy : ptr .To (true )}
75
71
if err := c .Client .List (ctx , clusterTaintPolicyList , listOption ); err != nil {
76
72
klog .Errorf ("Failed to list ClusterTaintPolicy, error: %v" , err )
77
73
return controllerruntime.Result {}, err
78
74
}
79
75
76
+ // Sorting ensures a deterministic processing order and ensures consistent taint generation for Clusters.
77
+ // This prevents repeated taint addition/removal cycles when conflicting policies exist.
78
+ policies := clusterTaintPolicyList .Items
79
+ sort .Slice (policies , func (i , j int ) bool {
80
+ return policies [i ].CreationTimestamp .Before (& policies [j ].CreationTimestamp )
81
+ })
82
+
80
83
clusterCopyObj := clusterObj .DeepCopy ()
81
- for _ , policy := range clusterTaintPolicyList . Items {
84
+ for _ , policy := range policies {
82
85
if policy .Spec .TargetClusters != nil && ! util .ClusterMatches (clusterCopyObj , * policy .Spec .TargetClusters ) {
83
86
continue
84
87
}
@@ -217,6 +220,16 @@ func (c *ClusterTaintPolicyController) SetupWithManager(mgr controllerruntime.Ma
217
220
DeleteFunc : func (_ event.DeleteEvent ) bool { return false },
218
221
}
219
222
223
+ clusterTaintPolicyPredicateFn := predicate.Funcs {
224
+ CreateFunc : func (_ event.CreateEvent ) bool { return true },
225
+ UpdateFunc : func (event event.UpdateEvent ) bool {
226
+ oldPolicy := event .ObjectOld .(* policyv1alpha1.ClusterTaintPolicy )
227
+ newPolicy := event .ObjectNew .(* policyv1alpha1.ClusterTaintPolicy )
228
+ return ! reflect .DeepEqual (oldPolicy .Spec , newPolicy .Spec )
229
+ },
230
+ DeleteFunc : func (_ event.DeleteEvent ) bool { return false },
231
+ }
232
+
220
233
clusterTaintPolicyMapFunc := handler .MapFunc (
221
234
func (ctx context.Context , policyObj client.Object ) []reconcile.Request {
222
235
clusterList := & clusterv1alpha1.ClusterList {}
@@ -240,7 +253,8 @@ func (c *ClusterTaintPolicyController) SetupWithManager(mgr controllerruntime.Ma
240
253
return controllerruntime .NewControllerManagedBy (mgr ).
241
254
Named (ControllerName ).
242
255
For (& clusterv1alpha1.Cluster {}, builder .WithPredicates (clusterStatusConditionPredicateFn )).
243
- Watches (& policyv1alpha1.ClusterTaintPolicy {}, handler .EnqueueRequestsFromMapFunc (clusterTaintPolicyMapFunc )).
256
+ Watches (& policyv1alpha1.ClusterTaintPolicy {}, handler .EnqueueRequestsFromMapFunc (clusterTaintPolicyMapFunc ),
257
+ builder .WithPredicates (clusterTaintPolicyPredicateFn )).
244
258
WithOptions (controller.Options {RateLimiter : ratelimiterflag.DefaultControllerRateLimiter [controllerruntime.Request ](c .RateLimiterOptions )}).
245
259
Complete (c )
246
260
}
0 commit comments