open-cluster-management.io/governance-policy-propagator@v0.13.0/controllers/rootpolicystatus/filters.go (about)

     1  // Copyright Contributors to the Open Cluster Management project
     2  
     3  package policystatus
     4  
     5  import (
     6  	"context"
     7  
     8  	clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
     9  	appsv1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/placementrule/v1"
    10  	"sigs.k8s.io/controller-runtime/pkg/client"
    11  	"sigs.k8s.io/controller-runtime/pkg/event"
    12  	"sigs.k8s.io/controller-runtime/pkg/handler"
    13  	"sigs.k8s.io/controller-runtime/pkg/predicate"
    14  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    15  
    16  	policiesv1 "open-cluster-management.io/governance-policy-propagator/api/v1"
    17  	"open-cluster-management.io/governance-policy-propagator/controllers/common"
    18  )
    19  
    20  // policyStatusPredicate will filter out all policy events except for updates where the generation is the same, which
    21  // implies the status has been updated. If the generation changes, the main policy controller will handle it.
    22  func policyStatusPredicate() predicate.Funcs {
    23  	return predicate.Funcs{
    24  		// Creations are handled by the main policy controller.
    25  		CreateFunc: func(e event.CreateEvent) bool { return false },
    26  		UpdateFunc: func(e event.UpdateEvent) bool {
    27  			//nolint:forcetypeassert
    28  			oldPolicy := e.ObjectOld.(*policiesv1.Policy)
    29  			//nolint:forcetypeassert
    30  			updatedPolicy := e.ObjectNew.(*policiesv1.Policy)
    31  
    32  			// If there was an update and the generation is the same, the status must have changed.
    33  			return oldPolicy.Generation == updatedPolicy.Generation
    34  		},
    35  		// Deletions are handled by the main policy controller.
    36  		DeleteFunc: func(e event.DeleteEvent) bool { return false },
    37  	}
    38  }
    39  
    40  // mapBindingToPolicies maps a placementBinding to all the Policies in its policies list.
    41  func mapBindingToPolicies(c client.Client) handler.MapFunc {
    42  	return func(ctx context.Context, object client.Object) []reconcile.Request {
    43  		log := log.WithValues("placementBinding", object.GetName(), "namespace", object.GetNamespace())
    44  		log.V(2).Info("Reconcile Request for placementBinding")
    45  
    46  		//nolint:forcetypeassert
    47  		pb := object.(*policiesv1.PlacementBinding)
    48  
    49  		return common.GetPoliciesInPlacementBinding(ctx, c, pb)
    50  	}
    51  }
    52  
    53  // mapRuleToPolicies maps a PlacementRule to all the Policies in its policies list.
    54  func mapRuleToPolicies(c client.Client) handler.MapFunc {
    55  	return func(ctx context.Context, object client.Object) []reconcile.Request {
    56  		log := log.WithValues("PlacementRule", object.GetName(), "namespace", object.GetNamespace())
    57  		log.V(2).Info("Reconcile Request for PlacementRule")
    58  
    59  		//nolint:forcetypeassert
    60  		pr := object.(*appsv1.PlacementRule)
    61  
    62  		result, err := common.GetRootPolicyRequests(ctx, c, pr.GetNamespace(), pr.GetName(), common.PlacementRule)
    63  		if err != nil {
    64  			log.Error(err, "Getting root policy results  has error in mapRuleToPolicies")
    65  
    66  			return nil
    67  		}
    68  
    69  		return result
    70  	}
    71  }
    72  
    73  // mapDecisionToPolicies maps a PlacementDecision to all the Policies in its policies list.
    74  func mapDecisionToPolicies(c client.Client) handler.MapFunc {
    75  	return func(ctx context.Context, object client.Object) []reconcile.Request {
    76  		log := log.WithValues("placementDecision", object.GetName(), "namespace", object.GetNamespace())
    77  		log.V(2).Info("Reconcile Request for placementDecision")
    78  
    79  		pd := object.(*clusterv1beta1.PlacementDecision)
    80  		placementName := pd.GetLabels()["cluster.open-cluster-management.io/placement"]
    81  
    82  		result, err := common.GetRootPolicyRequests(ctx, c, pd.GetNamespace(), placementName, common.Placement)
    83  		if err != nil {
    84  			log.Error(err, "Getting root policy results  has error in mapDecisionToPolicies")
    85  
    86  			return nil
    87  		}
    88  
    89  		return result
    90  	}
    91  }