func (r *PolicySetReconciler) processPolicySet(ctx context.Context, plcSet *policyv1beta1.PolicySet) bool {
	log.V(1).Info("Processing policy sets")

	needsUpdate := false

	// compile results and compliance state from policy statuses
	compliancesFound := []string{}
	aggregatedCompliance := "Compliant"
	placementsByBinding := map[string]policyv1beta1.PolicySetStatusPlacement{}

	// if there are no policies in the policyset, status should be empty
	if len(plcSet.Spec.Policies) == 0 {
		builtStatus := policyv1beta1.PolicySetStatus{}

		if !equality.Semantic.DeepEqual(plcSet.Status, builtStatus) {
			plcSet.Status = *builtStatus.DeepCopy()

			return true
		}

		return false
	}

	for i := range plcSet.Spec.Policies {
		childPlcName := plcSet.Spec.Policies[i]
		childNamespacedName := types.NamespacedName{
			Name:      string(childPlcName),
			Namespace: plcSet.Namespace,
		}

		childPlc := &policyv1.Policy{}

		err := r.Client.Get(ctx, childNamespacedName, childPlc)
		if err != nil {
			// policy does not exist, log error message and generate event
			var errMessage string
			if errors.IsNotFound(err) {
				errMessage = string(childPlcName) + " not found"
			} else {
				errMessage = strings.Split(err.Error(), "Policy.policy.open-cluster-management.io ")[1]
			}

			log.V(2).Info(errMessage)

			r.Recorder.Event(plcSet, "Warning", "PolicyNotFound",
				fmt.Sprintf(
					"Policy %s is in PolicySet %s but could not be found in namespace %s",
					childPlcName,
					plcSet.GetName(),
					plcSet.GetNamespace(),
				),
			)

			compliancesFound = append(compliancesFound, "notfound")
		} else {
			// aggregate placements
			for _, placement := range childPlc.Status.Placement {
				if placement.PolicySet == plcSet.GetName() {
					placementsByBinding[placement.PlacementBinding] = plcPlacementToSetPlacement(*placement)
				}
			}

			if childPlc.Spec.Disabled {
				// policy is disabled, do not process compliance
				compliancesFound = append(compliancesFound, "disabled")

				continue
			}

			// create list of all relevant clusters
			clusters := []string{}
			for pbName := range placementsByBinding {
				pbNamespacedName := types.NamespacedName{
					Name:      pbName,
					Namespace: plcSet.Namespace,
				}

				pb := &policyv1.PlacementBinding{}

				err := r.Client.Get(ctx, pbNamespacedName, pb)
				if err != nil {
					log.V(1).Info("Error getting placement binding " + pbName)
				}

				var decisions []appsv1.PlacementDecision
				decisions, err = getDecisions(r.Client, *pb, childPlc)
				if err != nil {
					log.Error(err, "Error getting placement decisions for binding "+pbName)
				}

				for _, decision := range decisions {
					clusters = append(clusters, decision.ClusterName)
				}
			}

			// aggregate compliance state
			plcComplianceState := complianceInRelevantClusters(childPlc.Status.Status, clusters)
			if plcComplianceState == "" {
				compliancesFound = append(compliancesFound, "notfound")
			} else {
				compliancesFound = append(compliancesFound, "found")
				if plcComplianceState == "NonCompliant" {
					aggregatedCompliance = "NonCompliant"
				}
			}
		}
	}

	generatedPlacements := []policyv1beta1.PolicySetStatusPlacement{}
	for _, pcmt := range placementsByBinding {
		generatedPlacements = append(generatedPlacements, pcmt)
	}

	builtStatus := policyv1beta1.PolicySetStatus{
		Placement: generatedPlacements,
	}
	if showCompliance(compliancesFound) {
		builtStatus.Compliant = aggregatedCompliance
	}

	if !equality.Semantic.DeepEqual(plcSet.Status, builtStatus) {
		plcSet.Status = *builtStatus.DeepCopy()
		needsUpdate = true
	}

	return needsUpdate
}
// processPolicySet compares the status of a policyset to its desired state and determines whether an update is needed
processPolicySet compares the status of a policyset to its desired state and determines whether an update is needed
// processPolicySet compares the status of a policyset to its desired state and determines whether an update is needed
func (r *PolicySetReconciler) processPolicySet(ctx context.Context, plcSet *policyv1beta1.PolicySet) bool {
	log.V(1).Info("Processing policy sets")
	needsUpdate := false
	compliancesFound := []string{}
	aggregatedCompliance := "Compliant"
	placementsByBinding := map[string]policyv1beta1.PolicySetStatusPlacement{}
	if len(plcSet.Spec.Policies) == 0 {
		builtStatus := policyv1beta1.PolicySetStatus{}
		if !equality.Semantic.DeepEqual(plcSet.Status, builtStatus) {
			plcSet.Status = *builtStatus.DeepCopy()
			return true
		}
		return false
	}
	for i := range plcSet.Spec.Policies {
		childPlcName := plcSet.Spec.Policies[i]
		childNamespacedName := types.NamespacedName{
			Name:      string(childPlcName),
			Namespace: plcSet.Namespace,
		}
		childPlc := &policyv1.Policy{}
		err := r.Client.Get(ctx, childNamespacedName, childPlc)
		if err != nil {
			var errMessage string
			if errors.IsNotFound(err) {
				errMessage = string(childPlcName) + " not found"
			} else {
				errMessage = strings.Split(err.Error(), "Policy.policy.open-cluster-management.io ")[1]
			}
			log.V(2).Info(errMessage)
			r.Recorder.Event(plcSet, "Warning", "PolicyNotFound",
				fmt.Sprintf(
					"Policy %s is in PolicySet %s but could not be found in namespace %s",
					childPlcName,
					plcSet.GetName(),
					plcSet.GetNamespace(),
				),
			)
			compliancesFound = append(compliancesFound, "notfound")
		} else {
			for _, placement := range childPlc.Status.Placement {
				if placement.PolicySet == plcSet.GetName() {
					placementsByBinding[placement.PlacementBinding] = plcPlacementToSetPlacement(*placement)
				}
			}
			if childPlc.Spec.Disabled {
				compliancesFound = append(compliancesFound, "disabled")
				continue
			}
			clusters := []string{}
			for pbName := range placementsByBinding {
				pbNamespacedName := types.NamespacedName{
					Name:      pbName,
					Namespace: plcSet.Namespace,
				}
				pb := &policyv1.PlacementBinding{}
				err := r.Client.Get(ctx, pbNamespacedName, pb)
				if err != nil {
					log.V(1).Info("Error getting placement binding " + pbName)
				}
				var decisions []appsv1.PlacementDecision
				decisions, err = getDecisions(r.Client, *pb, childPlc)
				if err != nil {
					log.Error(err, "Error getting placement decisions for binding "+pbName)
				}
				for _, decision := range decisions {
					clusters = append(clusters, decision.ClusterName)
				}
			}
			plcComplianceState := complianceInRelevantClusters(childPlc.Status.Status, clusters)
			if plcComplianceState == "" {
				compliancesFound = append(compliancesFound, "notfound")
			} else {
				compliancesFound = append(compliancesFound, "found")
				if plcComplianceState == "NonCompliant" {
					aggregatedCompliance = "NonCompliant"
				}
			}
		}
	}
	generatedPlacements := []policyv1beta1.PolicySetStatusPlacement{}
	for _, pcmt := range placementsByBinding {
		generatedPlacements = append(generatedPlacements, pcmt)
	}
	builtStatus := policyv1beta1.PolicySetStatus{
		Placement: generatedPlacements,
	}
	if showCompliance(compliancesFound) {
		builtStatus.Compliant = aggregatedCompliance
	}
	if !equality.Semantic.DeepEqual(plcSet.Status, builtStatus) {
		plcSet.Status = *builtStatus.DeepCopy()
		needsUpdate = true
	}
	return needsUpdate
}
func ( r * PolicySetReconciler ) processPolicySet ( ctx context . Context , plcSet * policyv1beta1 . PolicySet ) bool { log . V ( 1 ) . Info ( " " ) needsUpdate := false compliancesFound := [ ] string { } aggregatedCompliance := " " placementsByBinding := map [ string ] policyv1beta1 . PolicySetStatusPlacement { } if len ( plcSet . Spec . Policies ) == 0 { builtStatus := policyv1beta1 . PolicySetStatus { } if ! equality . Semantic . DeepEqual ( plcSet . Status , builtStatus ) { plcSet . Status = * builtStatus . DeepCopy ( ) return true } return false } for i := range plcSet . Spec . Policies { childPlcName := plcSet . Spec . Policies [ i ] childNamespacedName := types . NamespacedName { Name : string ( childPlcName ) , Namespace : plcSet . Namespace , } childPlc := & policyv1 . Policy { } err := r . Client . Get ( ctx , childNamespacedName , childPlc ) if err != nil { var errMessage string if errors . IsNotFound ( err ) { errMessage = string ( childPlcName ) + " " } else { errMessage = strings . Split ( err . Error ( ) , " " ) [ 1 ] } log . V ( 2 ) . Info ( errMessage ) r . Recorder . Event ( plcSet , " " , " " , fmt . Sprintf ( " " , childPlcName , plcSet . GetName ( ) , plcSet . GetNamespace ( ) , ) , ) compliancesFound = append ( compliancesFound , " " ) } else { for _ , placement := range childPlc . Status . Placement { if placement . PolicySet == plcSet . GetName ( ) { placementsByBinding [ placement . PlacementBinding ] = plcPlacementToSetPlacement ( * placement ) } } if childPlc . Spec . Disabled { compliancesFound = append ( compliancesFound , " " ) continue } clusters := [ ] string { } for pbName := range placementsByBinding { pbNamespacedName := types . NamespacedName { Name : pbName , Namespace : plcSet . Namespace , } pb := & policyv1 . PlacementBinding { } err := r . Client . Get ( ctx , pbNamespacedName , pb ) if err != nil { log . V ( 1 ) . Info ( " " + pbName ) } var decisions [ ] appsv1 . PlacementDecision decisions , err = getDecisions ( r . Client , * pb , childPlc ) if err != nil { log . Error ( err , " " + pbName ) } for _ , decision := range decisions { clusters = append ( clusters , decision . ClusterName ) } } plcComplianceState := complianceInRelevantClusters ( childPlc . Status . Status , clusters ) if plcComplianceState == " " { compliancesFound = append ( compliancesFound , " " ) } else { compliancesFound = append ( compliancesFound , " " ) if plcComplianceState == " " { aggregatedCompliance = " " } } } } generatedPlacements := [ ] policyv1beta1 . PolicySetStatusPlacement { } for _ , pcmt := range placementsByBinding { generatedPlacements = append ( generatedPlacements , pcmt ) } builtStatus := policyv1beta1 . PolicySetStatus { Placement : generatedPlacements , } if showCompliance ( compliancesFound ) { builtStatus . Compliant = aggregatedCompliance } if ! equality . Semantic . DeepEqual ( plcSet . Status , builtStatus ) { plcSet . Status = * builtStatus . DeepCopy ( ) needsUpdate = true } return needsUpdate }
processPolicySet compares the status of a policyset to its desired state and determines whether an update is needed
processPolicySet compares the status of a policyset to its desired state and determines whether an update is needed
// compile results and compliance state from policy statuses // if there are no policies in the policyset, status should be empty // policy does not exist, log error message and generate event // aggregate placements // policy is disabled, do not process compliance // create list of all relevant clusters // aggregate compliance state