Skip to content

Commit

Permalink
Add a label and annotation for subscriptions managed by OperatorPolicy
Browse files Browse the repository at this point in the history
The operatorpolicy.policy.open-cluster-management.io/managed label is
set to an empty value on the Subscription. The
operatorpolicy.policy.open-cluster-management.io/managed annotation
is set to <policy name>.<policy namespace>.

The label doesn't set a value because the value could be too long.

This will allow querying for subscriptions managed by operator policies.

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
  • Loading branch information
mprahl committed Jun 6, 2024
1 parent 7215454 commit df43073
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions controllers/operatorpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import (
const (
OperatorControllerName string = "operator-policy-controller"
CatalogSourceReady string = "READY"
ManagedByLabel string = "operatorpolicy.policy.open-cluster-management.io/managed"
ManagedByAnnotation string = ManagedByLabel
)

var (
Expand Down Expand Up @@ -1109,6 +1111,36 @@ func (r *OperatorPolicyReconciler) musthaveSubscription(
return nil, nil, false, fmt.Errorf("error converting the retrieved Subscription to the go type: %w", err)
}

if policy.Spec.RemediationAction.IsEnforce() {
labels := mergedSub.GetLabels()
if _, ok := labels[ManagedByLabel]; !ok {
if labels == nil {
labels = map[string]string{}
}

labels[ManagedByLabel] = ""

mergedSub.SetLabels(labels)

updateNeeded = true
}

expectedValue := policy.Namespace + "." + policy.Name
annotations := mergedSub.GetAnnotations()

if annotations[ManagedByAnnotation] != expectedValue {
if annotations == nil {
annotations = map[string]string{}
}

annotations[ManagedByAnnotation] = expectedValue

mergedSub.SetAnnotations(annotations)

updateNeeded = true
}
}

if !updateNeeded {
subResFailed := mergedSub.Status.GetCondition(operatorv1alpha1.SubscriptionResolutionFailed)

Expand Down Expand Up @@ -1163,14 +1195,14 @@ func (r *OperatorPolicyReconciler) musthaveSubscription(
earlyConds = append(earlyConds, calculateComplianceCondition(policy))
}

err = r.TargetClient.Update(ctx, foundSub)
err = r.TargetClient.Update(ctx, mergedSub)
if err != nil {
return mergedSub, nil, changed, fmt.Errorf("error updating the Subscription: %w", err)
}

foundSub.SetGroupVersionKind(subscriptionGVK) // Update stripped this information
mergedSub.SetGroupVersionKind(subscriptionGVK) // Update stripped this information

updateStatus(policy, updatedCond("Subscription"), updatedObj(foundSub))
updateStatus(policy, updatedCond("Subscription"), updatedObj(mergedSub))

return mergedSub, earlyConds, true, nil
}
Expand Down

0 comments on commit df43073

Please sign in to comment.