From 81fab96822b2dd1f6dab3f7646fdc3a924d798d6 Mon Sep 17 00:00:00 2001 From: "Adam D. Cornett" Date: Wed, 7 Aug 2024 14:40:10 -0700 Subject: [PATCH] adding logic to add owner reference to ImageStream, so that ImageStream gets deleted on OperatorPipeline deletion Signed-off-by: Adam D. Cornett --- internal/reconcilers/certified_image_stream.go | 13 +++++++++++++ internal/reconcilers/marketplace_image_stream.go | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/internal/reconcilers/certified_image_stream.go b/internal/reconcilers/certified_image_stream.go index 7bc3fa4..d7fdf72 100644 --- a/internal/reconcilers/certified_image_stream.go +++ b/internal/reconcilers/certified_image_stream.go @@ -13,6 +13,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) const ( @@ -44,6 +45,18 @@ func (r *CertifiedImageStreamReconciler) Reconcile(ctx context.Context, pipeline stream := newImageStream(key) if objects.IsObjectFound(ctx, r.Client, key, stream) { log.Info("existing certified image stream found") + + // setting owner reference on ImageStream CR, so CR gets garbage collected on OperatorPipeline deletion. + // ignoring error, since we do not need/want to requeue on this failure, + // and this should self correct on subsequent reconciles. + err := controllerutil.SetControllerReference(pipeline, stream, r.Scheme) + if err != nil { + log.Info("unable to set owner on certified image stream, "+ + "this resource will need to be cleaned up manually on uninstall", "error", err.Error()) + return false, nil + } + _ = r.Update(ctx, stream) + return false, nil // Existing ImageStream found, do nothing... } diff --git a/internal/reconcilers/marketplace_image_stream.go b/internal/reconcilers/marketplace_image_stream.go index 7ad7582..c155682 100644 --- a/internal/reconcilers/marketplace_image_stream.go +++ b/internal/reconcilers/marketplace_image_stream.go @@ -12,6 +12,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) const ( @@ -43,6 +44,18 @@ func (r *MarketplaceImageStreamReconciler) Reconcile(ctx context.Context, pipeli stream := newImageStream(key) if objects.IsObjectFound(ctx, r.Client, key, stream) { log.Info("existing marketplace image stream found") + + // setting owner reference on ImageStream CR, so CR gets garbage collected on OperatorPipeline deletion. + // ignoring error, since we do not need/want to requeue on this failure, + // and this should self correct on subsequent reconciles. + err := controllerutil.SetControllerReference(pipeline, stream, r.Scheme) + if err != nil { + log.Info("unable to set owner on marketplace image stream, "+ + "this resource will need to be cleaned up manually on uninstall", "error", err.Error()) + return false, nil + } + _ = r.Update(ctx, stream) + return false, nil // Existing ImageStream found, do nothing... }