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 4b0fd44 commit 2f0a380
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 @@ -48,6 +48,8 @@ const (
OperatorControllerName string = "operator-policy-controller"
CatalogSourceReady string = "READY"
olmGracePeriod = 30 * time.Second
ManagedByLabel string = "operatorpolicy.policy.open-cluster-management.io/managed"
ManagedByAnnotation string = ManagedByLabel
)

var (
Expand Down Expand Up @@ -1148,6 +1150,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 @@ -1187,14 +1219,14 @@ func (r *OperatorPolicyReconciler) musthaveSubscription(
opLog.Info("Updating Subscription to match the desired state", "subName", foundSub.GetName(),
"subNamespace", foundSub.GetNamespace())

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 2f0a380

Please sign in to comment.