diff --git a/internal/controller/snapshot/snapshot_adapter.go b/internal/controller/snapshot/snapshot_adapter.go index 28541af58..6db1e9d50 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_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)