Skip to content

Commit

Permalink
adding logic to add owner reference to ImageStream, so that ImageStre…
Browse files Browse the repository at this point in the history
…am gets deleted on OperatorPipeline deletion

Signed-off-by: Adam D. Cornett <[email protected]>
  • Loading branch information
acornett21 committed Aug 12, 2024
1 parent 969254d commit 81fab96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/reconcilers/certified_image_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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...
}

Expand Down
13 changes: 13 additions & 0 deletions internal/reconcilers/marketplace_image_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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...
}

Expand Down

0 comments on commit 81fab96

Please sign in to comment.