diff --git a/internal/controller/snapshot/snapshot_adapter.go b/internal/controller/snapshot/snapshot_adapter.go index 449fe9ca4..4b45f1512 100644 --- a/internal/controller/snapshot/snapshot_adapter.go +++ b/internal/controller/snapshot/snapshot_adapter.go @@ -30,7 +30,6 @@ import ( clienterrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" applicationapiv1alpha1 "github.com/konflux-ci/application-api/api/v1alpha1" "github.com/konflux-ci/integration-service/api/v1beta2" @@ -522,18 +521,8 @@ func (a *Adapter) EnsureOverrideSnapshotValid() (controller.OperationResult, err return controller.ContinueProcessing() } - var err error - if !controllerutil.HasControllerReference(a.snapshot) { - a.snapshot, err = gitops.SetOwnerReference(a.context, a.client, a.snapshot, a.application) - if err != nil { - a.logger.Error(err, fmt.Sprintf("Failed to set owner reference for snapshot %s/%s", a.snapshot.Namespace, a.snapshot.Name)) - return controller.RequeueWithError(err) - } - a.logger.Info("Owner reference has been set to snapshot") - } - // validate all snapshotComponents' containerImages/source in snapshot, make all errors joined - var errsForSnapshot error + var err, errsForSnapshot error for _, snapshotComponent := range a.snapshot.Spec.Components { snapshotComponent := snapshotComponent //G601 _, err := a.loader.GetComponent(a.context, a.client, snapshotComponent.Name, a.snapshot.Namespace) diff --git a/internal/controller/snapshot/snapshot_adapter_test.go b/internal/controller/snapshot/snapshot_adapter_test.go index d28aa5919..6c44afcef 100644 --- a/internal/controller/snapshot/snapshot_adapter_test.go +++ b/internal/controller/snapshot/snapshot_adapter_test.go @@ -1925,7 +1925,6 @@ var _ = Describe("Snapshot Adapter", Ordered, func() { result, err := adapter.EnsureOverrideSnapshotValid() Expect(result.CancelRequest).To(BeFalse()) Expect(result.RequeueRequest).To(BeFalse()) - Expect(controllerutil.HasControllerReference(hasInvalidOverrideSnapshot)).To(BeTrue()) Expect(buf.String()).Should(ContainSubstring("Snapshot has been marked as invalid")) Expect(err).ToNot(HaveOccurred()) diff --git a/internal/controller/snapshot/snapshot_controller.go b/internal/controller/snapshot/snapshot_controller.go index f907cea21..61fa1c34a 100644 --- a/internal/controller/snapshot/snapshot_controller.go +++ b/internal/controller/snapshot/snapshot_controller.go @@ -34,6 +34,7 @@ import ( "k8s.io/client-go/util/retry" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/predicate" ) @@ -112,6 +113,16 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return helpers.HandleLoaderError(logger, err, "Application", "Snapshot") } + if !controllerutil.HasControllerReference(snapshot) { + snapshot, err = gitops.SetOwnerReference(ctx, r.Client, snapshot, application) + if err != nil { + logger.Error(err, fmt.Sprintf("Failed to set owner reference for snapshot %s/%s", snapshot.Namespace, snapshot.Name)) + return ctrl.Result{}, err + } + logger.LogAuditEvent(fmt.Sprintf("Application %s has been set as a Controller OwnerReference on Snapshot %s", application.Name, snapshot.Name), + snapshot, helpers.LogActionUpdate) + } + logger = logger.WithApp(*application) adapter := NewAdapter(ctx, snapshot, application, logger, loader, r.Client) diff --git a/internal/controller/snapshot/snapshot_controller_test.go b/internal/controller/snapshot/snapshot_controller_test.go index 7e0f257e8..de5e5a716 100644 --- a/internal/controller/snapshot/snapshot_controller_test.go +++ b/internal/controller/snapshot/snapshot_controller_test.go @@ -178,6 +178,30 @@ var _ = Describe("SnapshotController", func() { Expect(err).To(BeNil()) }) + It("can add the Application as a Controller OwnerReference on Snapshot", func() { + Expect(hasSnapshot.ObjectMeta.OwnerReferences).To(BeNil()) + + req := ctrl.Request{ + NamespacedName: types.NamespacedName{ + Name: hasSnapshot.Name, + Namespace: "default", + }, + } + result, err := snapshotReconciler.Reconcile(ctx, req) + Expect(reflect.TypeOf(result)).To(Equal(reflect.TypeOf(reconcile.Result{}))) + Expect(err).ToNot(HaveOccurred()) + + Eventually(func() bool { + err := k8sClient.Get(ctx, types.NamespacedName{ + Namespace: hasSnapshot.Namespace, + Name: hasSnapshot.Name, + }, hasSnapshot) + return err == nil + }).Should(BeTrue()) + Expect(hasSnapshot.ObjectMeta.OwnerReferences).ToNot(BeNil()) + Expect(hasSnapshot.ObjectMeta.GetOwnerReferences()[0].Name).To(Equal(hasApp.Name)) + }) + It("Does not return an error if the component cannot be found", func() { err := k8sClient.Delete(ctx, hasComp) Eventually(func() bool {