Skip to content

Commit 9aab6c0

Browse files
committed
update pattern deletion logic
this commit includes two key changes: 1. A sync with prune is triggered to remove all applications part of the pattern BEFORE the subscriptions and other resources are deleted to prevent applications from becoming undeleteable if CRs, webhooks, etc disappear. 2. Subscription for openshift-gitops is no longer owned by pattern instances. Previously, deleting a pattern would delete this subscription and a new subscription would not be able to be created on the reinstall of a pattern since the CSV still existed. The subscription is necessary since it sets the env var ARGOCD_CLUSTER_CONFIG_NAMESPACES to '*'. Without the subscription the ArgoCD instance created by the new pattern would be created in namespaced mode and many charts like vault will not install since they try to create clusterwide resources like clusterrolebindings
1 parent 71fc5e1 commit 9aab6c0

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

internal/controller/argo.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,3 +961,18 @@ func updateHelmParameter(goal api.PatternParameter, actual []argoapi.HelmParamet
961961
}
962962
return false
963963
}
964+
965+
func syncApplicationWithPrune(client argoclient.Interface, app *argoapi.Application, namespace string) error {
966+
app.Operation = &argoapi.Operation{
967+
Sync: &argoapi.SyncOperation{
968+
Prune: true,
969+
},
970+
}
971+
972+
_, err := client.ArgoprojV1alpha1().Applications(namespace).Update(context.Background(), app, metav1.UpdateOptions{})
973+
if err != nil {
974+
return fmt.Errorf("failed to sync application %q with prune: %w", app.Name, err)
975+
}
976+
977+
return nil
978+
}

internal/controller/pattern_controller.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,11 @@ func (r *PatternReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
162162

163163
// -- GitOps Subscription
164164
targetSub, _ := newSubscriptionFromConfigMap(r.fullClient)
165-
_ = controllerutil.SetOwnerReference(qualifiedInstance, targetSub, r.Scheme)
166-
167165
sub, _ := getSubscription(r.olmClient, targetSub.Name)
168166
if sub == nil {
169167
err = createSubscription(r.olmClient, targetSub)
170168
return r.actionPerformed(qualifiedInstance, "create gitops subscription", err)
171-
} else if ownedBySame(targetSub, sub) {
169+
} else if owner, ok := sub.Labels["app.kubernetes.io/managed-by"]; ok && owner == "patterns-operator" {
172170
// Check version/channel etc
173171
// Dangerous if multiple patterns do not agree, or automatic upgrades are in place...
174172
changed, errSub := updateSubscription(r.olmClient, targetSub, sub)
@@ -528,6 +526,10 @@ func (r *PatternReconciler) finalizeObject(instance *api.Pattern) error {
528526
return fmt.Errorf("updated application %q for removal", app.Name)
529527
}
530528

529+
if err := syncApplicationWithPrune(r.argoClient, app, ns); err != nil {
530+
return err
531+
}
532+
531533
if haveACMHub(r) {
532534
return fmt.Errorf("waiting for removal of that acm hub")
533535
}
@@ -604,7 +606,8 @@ func (r *PatternReconciler) onReconcileErrorWithRequeue(p *api.Pattern, reason s
604606
}
605607
if duration != nil {
606608
log.Printf("Requeueing\n")
607-
return reconcile.Result{RequeueAfter: *duration}, err
609+
// Return nil error when we have a duration to avoid exponential backoff
610+
return reconcile.Result{RequeueAfter: *duration}, nil
608611
}
609612
return reconcile.Result{}, err
610613
}

internal/controller/subscription.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func newSubscriptionFromConfigMap(r kubernetes.Interface) (*operatorv1alpha1.Sub
7373
ObjectMeta: metav1.ObjectMeta{
7474
Name: GitOpsDefaultPackageName,
7575
Namespace: SubscriptionNamespace,
76+
Labels: map[string]string{"app.kubernetes.io/managed-by": "patterns-operator"},
7677
},
7778
Spec: spec,
7879
}

0 commit comments

Comments
 (0)