open-cluster-management.io/governance-policy-propagator@v0.13.0/controllers/propagator/replicatepolicy_pb_eventHandler.go (about) 1 package propagator 2 3 import ( 4 "context" 5 6 "k8s.io/client-go/util/workqueue" 7 "sigs.k8s.io/controller-runtime/pkg/client" 8 "sigs.k8s.io/controller-runtime/pkg/event" 9 "sigs.k8s.io/controller-runtime/pkg/handler" 10 "sigs.k8s.io/controller-runtime/pkg/reconcile" 11 12 policiesv1 "open-cluster-management.io/governance-policy-propagator/api/v1" 13 "open-cluster-management.io/governance-policy-propagator/controllers/common" 14 ) 15 16 // HandlerForBinding maps a PlacementBinding to the targeted RepPolicies that are either directly in its 17 // subjects list, or are in a PolicySet which is a subject of this PlacementBinding. 18 func HandlerForBinding(c client.Client) handler.EventHandler { 19 return &handlerForBinding{ 20 c, 21 } 22 } 23 24 type handlerForBinding struct { 25 c client.Client 26 } 27 28 // Create implements EventHandler. 29 func (e *handlerForBinding) Create(ctx context.Context, 30 evt event.CreateEvent, q workqueue.RateLimitingInterface, 31 ) { 32 e.mapAndEnqueue(ctx, q, evt.Object) 33 } 34 35 // Update implements EventHandler. Update only targeted(modified) objects 36 func (e *handlerForBinding) Update(ctx context.Context, 37 evt event.UpdateEvent, q workqueue.RateLimitingInterface, 38 ) { 39 log.V(1).Info("Detect placementBinding and update targeted replicated-policies") 40 //nolint:forcetypeassert 41 newObj := evt.ObjectNew.(*policiesv1.PlacementBinding) 42 //nolint:forcetypeassert 43 oldObj := evt.ObjectOld.(*policiesv1.PlacementBinding) 44 45 if !(common.IsForPolicyOrPolicySet(newObj) || common.IsForPolicyOrPolicySet(oldObj)) { 46 return 47 } 48 49 oldRepPolcies := common.GetRepPoliciesInPlacementBinding(ctx, e.c, oldObj) 50 newRepPolcies := common.GetRepPoliciesInPlacementBinding(ctx, e.c, newObj) 51 52 // If the BindingOverrides or subFilter has changed, all new and old policies need to be checked 53 if newObj.BindingOverrides.RemediationAction != oldObj.BindingOverrides.RemediationAction || 54 newObj.SubFilter != oldObj.SubFilter { 55 for _, obj := range newRepPolcies { 56 q.Add(obj) 57 } 58 59 // The workqueue will handle any de-duplication. 60 for _, obj := range oldRepPolcies { 61 q.Add(obj) 62 } 63 64 return 65 } 66 67 // Otherwise send only affected replicated policies 68 affectedRepPolicies := common.GetAffectedObjs(oldRepPolcies, newRepPolcies) 69 for _, obj := range affectedRepPolicies { 70 q.Add(obj) 71 } 72 } 73 74 // Delete implements EventHandler. 75 func (e *handlerForBinding) Delete(ctx context.Context, 76 evt event.DeleteEvent, q workqueue.RateLimitingInterface, 77 ) { 78 e.mapAndEnqueue(ctx, q, evt.Object) 79 } 80 81 // Generic implements EventHandler. 82 func (e *handlerForBinding) Generic(ctx context.Context, 83 evt event.GenericEvent, q workqueue.RateLimitingInterface, 84 ) { 85 e.mapAndEnqueue(ctx, q, evt.Object) 86 } 87 88 func (e *handlerForBinding) mapAndEnqueue(ctx context.Context, 89 q workqueue.RateLimitingInterface, obj client.Object, 90 ) { 91 pBinding := obj.(*policiesv1.PlacementBinding) 92 reqs := e.getMappedReplicatedPolicy(ctx, pBinding) 93 94 for _, req := range reqs { 95 q.Add(req) 96 } 97 } 98 99 func (e *handlerForBinding) getMappedReplicatedPolicy(ctx context.Context, 100 pBinding *policiesv1.PlacementBinding, 101 ) []reconcile.Request { 102 if !(common.IsForPolicyOrPolicySet(pBinding)) { 103 return nil 104 } 105 106 return common.GetRepPoliciesInPlacementBinding(ctx, e.c, pBinding) 107 }