diff --git a/controllers/snapshot/snapshot_controller.go b/controllers/snapshot/snapshot_controller.go index cb86e8106..4dd43ad5d 100644 --- a/controllers/snapshot/snapshot_controller.go +++ b/controllers/snapshot/snapshot_controller.go @@ -18,6 +18,7 @@ package snapshot import ( "context" + "fmt" "github.com/redhat-appstudio/integration-service/cache" @@ -90,11 +91,28 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return ctrl.Result{}, err } - application, err := loader.GetApplicationFromSnapshot(r.Client, ctx, snapshot) + var application *applicationapiv1alpha1.Application + err = retry.OnError(retry.DefaultRetry, func(_ error) bool { return true }, func() error { + application, err = loader.GetApplicationFromSnapshot(r.Client, ctx, snapshot) + return err + }) if err != nil { - logger.Error(err, "Failed to get Application from the Snapshot") - return ctrl.Result{}, err + if errors.IsNotFound(err) { + if !gitops.IsSnapshotMarkedAsInvalid(snapshot) { + _, err := gitops.MarkSnapshotAsInvalid(r.Client, ctx, snapshot, + fmt.Sprintf("The application %s owning this snapshot doesn't exist, try again after creating application", snapshot.Spec.Application)) + if err != nil { + logger.Error(err, "Failed to update the status to Invalid for the snapshot", + "snapshot.Namespace", snapshot.Namespace, "snapshot.Name", snapshot.Name) + return ctrl.Result{}, err + } + logger.Info("Snapshot integration status condition marked as invalid, the application owning this snapshot cannot be found", + "snapshot.Namespace", snapshot.Namespace, "snapshot.Name", snapshot.Name) + } + } + return helpers.HandleLoaderError(logger, err, "Application", "Snapshot") } + logger = logger.WithApp(*application) var component *applicationapiv1alpha1.Component diff --git a/controllers/snapshot/snapshot_controller_test.go b/controllers/snapshot/snapshot_controller_test.go index 6becb8642..93051b0ff 100644 --- a/controllers/snapshot/snapshot_controller_test.go +++ b/controllers/snapshot/snapshot_controller_test.go @@ -18,6 +18,7 @@ package snapshot import ( "reflect" + "time" "k8s.io/apimachinery/pkg/api/errors" @@ -184,6 +185,29 @@ var _ = Describe("SnapshotController", func() { Expect(err).To(BeNil()) }) + It("Does not return an error if the application cannot be found", func() { + err := k8sClient.Delete(ctx, hasApp) + Eventually(func() bool { + err := k8sClient.Get(ctx, types.NamespacedName{ + Namespace: hasApp.ObjectMeta.Namespace, + Name: hasApp.ObjectMeta.Name, + }, hasApp) + return err != nil + }).Should(BeTrue()) + Expect(err == nil || errors.IsNotFound(err)).To(BeTrue()) + + result, err := snapshotReconciler.Reconcile(ctx, req) + Expect(result).To(Equal(ctrl.Result{})) + Expect(err).To(BeNil()) + Eventually(func() bool { + err := k8sClient.Get(ctx, types.NamespacedName{ + Namespace: hasSnapshot.Namespace, + Name: hasSnapshot.Name, + }, hasSnapshot) + return err == nil && gitops.IsSnapshotMarkedAsInvalid(hasSnapshot) + }, time.Second*20).Should(BeTrue()) + }) + It("can setup the cache by adding a new index field to search for ReleasePlanAdmissions", func() { err := setupCache(manager) Expect(err).ToNot(HaveOccurred())