Skip to content

Commit f4788fd

Browse files
committed
Split off gitops sub reconciliation into its own function
To reduce the complexity of the reconcile loop a bit
1 parent e30ff8e commit f4788fd

1 file changed

Lines changed: 83 additions & 66 deletions

File tree

internal/controller/pattern_controller.go

Lines changed: 83 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import (
5555
configclient "github.com/openshift/client-go/config/clientset/versioned"
5656
routeclient "github.com/openshift/client-go/route/clientset/versioned"
5757
v1 "github.com/operator-framework/api/pkg/operators/v1"
58-
operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
5958

6059
olmclient "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
6160
"k8s.io/client-go/kubernetes"
@@ -188,71 +187,8 @@ func (r *PatternReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
188187
}
189188

190189
// -- GitOps Subscription
191-
// Only disable the default ArgoCD instance for non-legacy deployments.
192-
// For legacy deployments, the gitops-operator's default instance is still in use.
193-
disableDefault := !isLegacyArgoNamespace()
194-
targetSub, err := newSubscriptionFromConfigMap(r.fullClient, disableDefault)
195-
196-
if err != nil {
197-
return r.actionPerformed(qualifiedInstance, "error creating new subscription from configmap", err)
198-
}
199-
subscriptionName, subscriptionNamespace := DetectGitOpsSubscription()
200-
// If the pattern operator is installed to the new vp namespace we need to create a ns, operatorgroup for the new sub
201-
if DetectOperatorNamespace() != LegacyOperatorNamespace {
202-
// Create namespace for gitops subscription
203-
if err := createNamespace(r.fullClient, subscriptionNamespace); err != nil {
204-
return r.actionPerformed(qualifiedInstance, "error creating namespace for gitops subscription", err)
205-
}
206-
207-
// Create operatorgroup for gitops subscription
208-
var og *v1.OperatorGroup
209-
if og, err = getOperatorGroup(r.olmClient, subscriptionNamespace); err != nil {
210-
return r.actionPerformed(qualifiedInstance, "error getting operatorgroup for gitops subscription", err)
211-
}
212-
if og == nil {
213-
if err := createOperatorGroup(r.olmClient, subscriptionNamespace); err != nil {
214-
return r.actionPerformed(qualifiedInstance, "error creating operatorgroup for gitops subscription", err)
215-
}
216-
}
217-
}
218-
219-
var currentSub *operatorv1alpha1.Subscription
220-
221-
if currentSub, err = getSubscription(r.olmClient, subscriptionName, subscriptionNamespace); err != nil {
222-
return r.actionPerformed(qualifiedInstance, "error getting gitops subscription", err)
223-
}
224-
225-
if currentSub == nil {
226-
if err = createSubscription(r.olmClient, targetSub); err != nil {
227-
return r.actionPerformed(qualifiedInstance, "error creating gitops subscription", err)
228-
}
229-
} else {
230-
// Remove any stale owner references from the subscription (historically set by
231-
// the pattern or the operator configmap). Cross-namespace owner references are
232-
// not allowed, so we clean them up and rely on the subscription persisting
233-
// independently.
234-
changed := false
235-
if err := controllerutil.RemoveOwnerReference(qualifiedInstance, currentSub, r.Scheme); err == nil {
236-
changed = true
237-
}
238-
operatorConfigMap, cmErr := GetOperatorConfigmap()
239-
if cmErr == nil {
240-
if err := controllerutil.RemoveOwnerReference(operatorConfigMap, currentSub, r.Scheme); err == nil {
241-
changed = true
242-
}
243-
}
244-
if changed {
245-
if _, err := r.olmClient.OperatorsV1alpha1().Subscriptions(currentSub.Namespace).Update(context.Background(), currentSub, metav1.UpdateOptions{}); err != nil {
246-
return r.actionPerformed(qualifiedInstance, "error removing stale owner references from gitops subscription", err)
247-
}
248-
return r.actionPerformed(qualifiedInstance, "removed stale owner references from gitops subscription", nil)
249-
}
250-
251-
// Check version/channel etc
252-
updatedSub, errSub := updateSubscription(r.olmClient, targetSub, currentSub)
253-
if updatedSub {
254-
return r.actionPerformed(qualifiedInstance, "update gitops subscription", errSub)
255-
}
190+
if done, result, subErr := r.reconcileGitOpsSubscription(qualifiedInstance); done {
191+
return result, subErr
256192
}
257193
logOnce("subscription found")
258194

@@ -383,6 +319,87 @@ func (r *PatternReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
383319
return result, nil
384320
}
385321

322+
// reconcileGitOpsSubscription ensures the GitOps operator subscription exists and is up-to-date.
323+
// It returns (done, result, err) — when done is true the caller should return result/err immediately.
324+
func (r *PatternReconciler) reconcileGitOpsSubscription(qualifiedInstance *api.Pattern) (done bool, result ctrl.Result, err error) {
325+
// Only disable the default ArgoCD instance for non-legacy deployments.
326+
// For legacy deployments, the gitops-operator's default instance is still in use.
327+
disableDefault := !isLegacyArgoNamespace()
328+
targetSub, err := newSubscriptionFromConfigMap(r.fullClient, disableDefault)
329+
330+
if err != nil {
331+
res, e := r.actionPerformed(qualifiedInstance, "error creating new subscription from configmap", err)
332+
return true, res, e
333+
}
334+
subscriptionName, subscriptionNamespace := DetectGitOpsSubscription()
335+
// If the pattern operator is installed to the new vp namespace we need to create a ns, operatorgroup for the new sub
336+
if DetectOperatorNamespace() != LegacyOperatorNamespace {
337+
// Create namespace for gitops subscription
338+
if err := createNamespace(r.fullClient, subscriptionNamespace); err != nil {
339+
res, e := r.actionPerformed(qualifiedInstance, "error creating namespace for gitops subscription", err)
340+
return true, res, e
341+
}
342+
343+
// Create operatorgroup for gitops subscription
344+
var og *v1.OperatorGroup
345+
if og, err = getOperatorGroup(r.olmClient, subscriptionNamespace); err != nil {
346+
res, e := r.actionPerformed(qualifiedInstance, "error getting operatorgroup for gitops subscription", err)
347+
return true, res, e
348+
}
349+
if og == nil {
350+
if err := createOperatorGroup(r.olmClient, subscriptionNamespace); err != nil {
351+
res, e := r.actionPerformed(qualifiedInstance, "error creating operatorgroup for gitops subscription", err)
352+
return true, res, e
353+
}
354+
}
355+
}
356+
357+
currentSub, err := getSubscription(r.olmClient, subscriptionName, subscriptionNamespace)
358+
if err != nil {
359+
res, e := r.actionPerformed(qualifiedInstance, "error getting gitops subscription", err)
360+
return true, res, e
361+
}
362+
363+
if currentSub == nil {
364+
if err = createSubscription(r.olmClient, targetSub); err != nil {
365+
res, e := r.actionPerformed(qualifiedInstance, "error creating gitops subscription", err)
366+
return true, res, e
367+
}
368+
} else {
369+
// Remove any stale owner references from the subscription (historically set by
370+
// the pattern or the operator configmap). Cross-namespace owner references are
371+
// not allowed, so we clean them up and rely on the subscription persisting
372+
// independently.
373+
changed := false
374+
if err := controllerutil.RemoveOwnerReference(qualifiedInstance, currentSub, r.Scheme); err == nil {
375+
changed = true
376+
}
377+
operatorConfigMap, cmErr := GetOperatorConfigmap()
378+
if cmErr == nil {
379+
if err := controllerutil.RemoveOwnerReference(operatorConfigMap, currentSub, r.Scheme); err == nil {
380+
changed = true
381+
}
382+
}
383+
if changed {
384+
if _, err := r.olmClient.OperatorsV1alpha1().Subscriptions(currentSub.Namespace).Update(context.Background(), currentSub, metav1.UpdateOptions{}); err != nil {
385+
res, e := r.actionPerformed(qualifiedInstance, "error removing stale owner references from gitops subscription", err)
386+
return true, res, e
387+
}
388+
res, e := r.actionPerformed(qualifiedInstance, "removed stale owner references from gitops subscription", nil)
389+
return true, res, e
390+
}
391+
392+
// Check version/channel etc
393+
updatedSub, errSub := updateSubscription(r.olmClient, targetSub, currentSub)
394+
if updatedSub {
395+
res, e := r.actionPerformed(qualifiedInstance, "update gitops subscription", errSub)
396+
return true, res, e
397+
}
398+
}
399+
400+
return false, ctrl.Result{}, nil
401+
}
402+
386403
func (r *PatternReconciler) createGiteaInstance(input *api.Pattern) error {
387404
gitConfig := input.Spec.GitConfig
388405
clusterWideNS := getClusterWideArgoNamespace()

0 commit comments

Comments
 (0)