Skip to content

Commit e8ddbd7

Browse files
authored
Merge pull request #6399 from XiShanYongYe-Chang/sort-policies-in-clustertaintpolicy-controller
Sort policies in cluster-taint-policy-controller to ensure idempotency
2 parents ca07209 + 8ce34ba commit e8ddbd7

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

pkg/controllers/taint/clustertaintpolicy_controller.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"reflect"
23+
"sort"
2324

2425
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -65,20 +66,22 @@ func (c *ClusterTaintPolicyController) Reconcile(ctx context.Context, req contro
6566
return controllerruntime.Result{}, client.IgnoreNotFound(err)
6667
}
6768

68-
if !clusterObj.DeletionTimestamp.IsZero() {
69-
klog.V(4).Infof("Cluster(%s) is deleting.", req.Name)
70-
return controllerruntime.Result{}, nil
71-
}
72-
7369
clusterTaintPolicyList := &policyv1alpha1.ClusterTaintPolicyList{}
7470
listOption := &client.ListOptions{UnsafeDisableDeepCopy: ptr.To(true)}
7571
if err := c.Client.List(ctx, clusterTaintPolicyList, listOption); err != nil {
7672
klog.Errorf("Failed to list ClusterTaintPolicy, error: %v", err)
7773
return controllerruntime.Result{}, err
7874
}
7975

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+
8083
clusterCopyObj := clusterObj.DeepCopy()
81-
for _, policy := range clusterTaintPolicyList.Items {
84+
for _, policy := range policies {
8285
if policy.Spec.TargetClusters != nil && !util.ClusterMatches(clusterCopyObj, *policy.Spec.TargetClusters) {
8386
continue
8487
}
@@ -217,6 +220,16 @@ func (c *ClusterTaintPolicyController) SetupWithManager(mgr controllerruntime.Ma
217220
DeleteFunc: func(_ event.DeleteEvent) bool { return false },
218221
}
219222

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+
220233
clusterTaintPolicyMapFunc := handler.MapFunc(
221234
func(ctx context.Context, policyObj client.Object) []reconcile.Request {
222235
clusterList := &clusterv1alpha1.ClusterList{}
@@ -240,7 +253,8 @@ func (c *ClusterTaintPolicyController) SetupWithManager(mgr controllerruntime.Ma
240253
return controllerruntime.NewControllerManagedBy(mgr).
241254
Named(ControllerName).
242255
For(&clusterv1alpha1.Cluster{}, builder.WithPredicates(clusterStatusConditionPredicateFn)).
243-
Watches(&policyv1alpha1.ClusterTaintPolicy{}, handler.EnqueueRequestsFromMapFunc(clusterTaintPolicyMapFunc)).
256+
Watches(&policyv1alpha1.ClusterTaintPolicy{}, handler.EnqueueRequestsFromMapFunc(clusterTaintPolicyMapFunc),
257+
builder.WithPredicates(clusterTaintPolicyPredicateFn)).
244258
WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter[controllerruntime.Request](c.RateLimiterOptions)}).
245259
Complete(c)
246260
}

0 commit comments

Comments
 (0)