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

     1  // Copyright Contributors to the Open Cluster Management project
     2  
     3  package automation
     4  
     5  import (
     6  	"context"
     7  
     8  	"k8s.io/apimachinery/pkg/types"
     9  	"sigs.k8s.io/controller-runtime/pkg/client"
    10  	"sigs.k8s.io/controller-runtime/pkg/handler"
    11  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    12  
    13  	policiesv1 "open-cluster-management.io/governance-policy-propagator/api/v1"
    14  	policyv1beta1 "open-cluster-management.io/governance-policy-propagator/api/v1beta1"
    15  )
    16  
    17  func policyMapper(c client.Client) handler.MapFunc {
    18  	return func(ctx context.Context, obj client.Object) []reconcile.Request {
    19  		//nolint:forcetypeassert
    20  		policy := obj.(*policiesv1.Policy)
    21  
    22  		var result []reconcile.Request
    23  
    24  		policyAutomationList := &policyv1beta1.PolicyAutomationList{}
    25  
    26  		err := c.List(ctx, policyAutomationList, &client.ListOptions{Namespace: policy.GetNamespace()})
    27  		if err != nil {
    28  			return nil
    29  		}
    30  
    31  		found := false
    32  		policyAutomation := policyv1beta1.PolicyAutomation{}
    33  
    34  		for _, policyAutomationTemp := range policyAutomationList.Items {
    35  			if policyAutomationTemp.Spec.PolicyRef == policy.GetName() {
    36  				found = true
    37  				policyAutomation = policyAutomationTemp
    38  
    39  				break
    40  			}
    41  		}
    42  
    43  		if found {
    44  			modeType := policyAutomation.Spec.Mode
    45  			// Do not queue during scan mode
    46  			if modeType != "scan" && modeType == policyv1beta1.Once || modeType == policyv1beta1.EveryEvent {
    47  				// The same policyAutomation mapping logic for once and everyEvent mode
    48  				request := reconcile.Request{NamespacedName: types.NamespacedName{
    49  					Name:      policyAutomation.GetName(),
    50  					Namespace: policyAutomation.GetNamespace(),
    51  				}}
    52  				result = append(result, request)
    53  			}
    54  		}
    55  
    56  		return result
    57  	}
    58  }