Skip to content

Commit 8ff052c

Browse files
committed
update old gitops subscriptions to be owned by operator and add logic to determine if sync is in progress during finalization of pattern
1 parent b903231 commit 8ff052c

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

internal/controller/argo.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"log"
2323
"os"
24+
"slices"
2425
"strconv"
2526
"strings"
2627

@@ -962,17 +963,24 @@ func updateHelmParameter(goal api.PatternParameter, actual []argoapi.HelmParamet
962963
return false
963964
}
964965

965-
func syncApplicationWithPrune(client argoclient.Interface, app *argoapi.Application, namespace string) error {
966+
// syncApplicationWithPrune syncs the application with prune and force options if such a sync is not already in progress.
967+
// Returns true if a sync with prune and force is already in progress, false otherwise
968+
func syncApplicationWithPrune(client argoclient.Interface, app *argoapi.Application, namespace string) (bool, error) {
969+
if app.Operation != nil && app.Operation.Sync != nil && app.Operation.Sync.Prune && slices.Contains(app.Operation.Sync.SyncOptions, "Force=true") {
970+
return true, nil
971+
}
972+
966973
app.Operation = &argoapi.Operation{
967974
Sync: &argoapi.SyncOperation{
968-
Prune: true,
975+
Prune: true,
976+
SyncOptions: []string{"Force=true"},
969977
},
970978
}
971979

972980
_, err := client.ArgoprojV1alpha1().Applications(namespace).Update(context.Background(), app, metav1.UpdateOptions{})
973981
if err != nil {
974-
return fmt.Errorf("failed to sync application %q with prune: %w", app.Name, err)
982+
return false, fmt.Errorf("failed to sync application %q with prune: %w", app.Name, err)
975983
}
976984

977-
return nil
985+
return true, nil
978986
}

internal/controller/pattern_controller.go

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

163163
// -- GitOps Subscription
164164
targetSub, _ := newSubscriptionFromConfigMap(r.fullClient)
165-
if operatorConfigMap, err := GetOperatorConfigmap(); err == nil {
165+
operatorConfigMap, err := GetOperatorConfigmap()
166+
if err == nil {
166167
if err := controllerutil.SetOwnerReference(operatorConfigMap, targetSub, r.Scheme); err != nil {
167168
return r.actionPerformed(qualifiedInstance, "error setting owner of gitops subscription", err)
168169
}
@@ -182,7 +183,20 @@ func (r *PatternReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
182183
return r.actionPerformed(qualifiedInstance, "update gitops subscription", errSub)
183184
}
184185
} else {
185-
logOnce("The gitops subscription is not owned by us, leaving untouched")
186+
// Historically the subscription was owned by the pattern, not the operator. If this is the case,
187+
// we update the owner reference to the operator itself.
188+
if err := controllerutil.RemoveOwnerReference(qualifiedInstance, sub, r.Scheme); err == nil {
189+
if err := controllerutil.SetOwnerReference(operatorConfigMap, sub, r.Scheme); err != nil {
190+
return r.actionPerformed(qualifiedInstance, "error setting patterns operator owner reference of gitops subscription", err)
191+
}
192+
// Persist the updated ownerReferences on the Subscription
193+
if _, err := r.olmClient.OperatorsV1alpha1().Subscriptions(SubscriptionNamespace).Update(context.Background(), sub, metav1.UpdateOptions{}); err != nil {
194+
return r.actionPerformed(qualifiedInstance, "error updating gitops subscription owner references", err)
195+
}
196+
return r.actionPerformed(qualifiedInstance, "updated patterns operator owner reference of gitops subscription", nil)
197+
} else {
198+
logOnce("The gitops subscription is not owned by us, leaving untouched")
199+
}
186200
}
187201
logOnce("subscription found")
188202

@@ -534,8 +548,14 @@ func (r *PatternReconciler) finalizeObject(instance *api.Pattern) error {
534548
return fmt.Errorf("updated application %q for removal", app.Name)
535549
}
536550

537-
if err := syncApplicationWithPrune(r.argoClient, app, ns); err != nil {
538-
return err
551+
if app.Status.Sync.Status == argoapi.SyncStatusCodeOutOfSync {
552+
inProgress, err := syncApplicationWithPrune(r.argoClient, app, ns)
553+
if err != nil {
554+
return err
555+
}
556+
if inProgress {
557+
return fmt.Errorf("sync with prune and force is already in progress for application %q", app.Name)
558+
}
539559
}
540560

541561
if haveACMHub(r) {

0 commit comments

Comments
 (0)