Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/controller/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,18 @@ func updateHelmParameter(goal api.PatternParameter, actual []argoapi.HelmParamet
}
return false
}

func syncApplicationWithPrune(client argoclient.Interface, app *argoapi.Application, namespace string) error {
app.Operation = &argoapi.Operation{
Sync: &argoapi.SyncOperation{
Prune: true,
},
}

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

return nil
}
15 changes: 13 additions & 2 deletions internal/controller/pattern_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ func (r *PatternReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// -- GitOps Subscription
targetSub, _ := newSubscriptionFromConfigMap(r.fullClient)
_ = controllerutil.SetOwnerReference(qualifiedInstance, targetSub, r.Scheme)
if operatorConfigMap, err := GetOperatorConfigmap(); err == nil {
if err := controllerutil.SetOwnerReference(operatorConfigMap, targetSub, r.Scheme); err != nil {
return r.actionPerformed(qualifiedInstance, "error setting owner of gitops subscription", err)
}
} else {
return r.actionPerformed(qualifiedInstance, "error getting operator configmap", err)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment as to why we transfer ownership for our future selves?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a comment a couple lines below where the update happens that I've now expanded a bit. Reproducing for convenience on this comment:

Historically the subscription was owned by the pattern, not the operator. If this is the case,
we update the owner reference to the operator itself. When the subscription is owned by the pattern,
deleting the pattern removes the subscription and some, but not all, argo resources. This causes
subsequent pattern installations to try to start argo in namespaced mode and any charts requiring
cluster-wide access, like Vault, will fail to install. Having the subscription owned by the operator
allows subsequent pattern installations to reuse the openshift gitops operator already on the cluster.

Is this good enough? I'm happy to improve it if it's not clear

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

sub, _ := getSubscription(r.olmClient, targetSub.Name)
if sub == nil {
Expand Down Expand Up @@ -528,6 +534,10 @@ func (r *PatternReconciler) finalizeObject(instance *api.Pattern) error {
return fmt.Errorf("updated application %q for removal", app.Name)
}

if err := syncApplicationWithPrune(r.argoClient, app, ns); err != nil {
return err
}

if haveACMHub(r) {
return fmt.Errorf("waiting for removal of that acm hub")
}
Expand Down Expand Up @@ -604,7 +614,8 @@ func (r *PatternReconciler) onReconcileErrorWithRequeue(p *api.Pattern, reason s
}
if duration != nil {
log.Printf("Requeueing\n")
return reconcile.Result{RequeueAfter: *duration}, err
// Return nil error when we have a duration to avoid exponential backoff
return reconcile.Result{RequeueAfter: *duration}, nil
}
return reconcile.Result{}, err
}
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"

configv1 "github.com/openshift/api/config/v1"
)
Expand Down Expand Up @@ -415,3 +416,17 @@ func IntOrZero(secret map[string][]byte, key string) (int64, error) {

return strconv.ParseInt(string(val), 10, 64)
}

// Gets the configmap for the Patterns Operator. (Used as an owner reference for the operator itself.)
func GetOperatorConfigmap() (*corev1.ConfigMap, error) {
config, err := ctrl.GetConfig()
if err != nil {
return nil, fmt.Errorf("failed to get config: %s", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("failed to call NewForConfig: %s", err)
}

return clientset.CoreV1().ConfigMaps(OperatorNamespace).Get(context.Background(), OperatorConfigMap, metav1.GetOptions{})
}