Skip to content

Commit

Permalink
Merge pull request #399 from hongweiliu17/STONEINTG-643
Browse files Browse the repository at this point in the history
fix(STONEINTG-643): don't process snapshot with nonexisting app
  • Loading branch information
hongweiliu17 authored Nov 1, 2023
2 parents 945e4ba + f14ab1c commit 7eee0a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
24 changes: 21 additions & 3 deletions controllers/snapshot/snapshot_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package snapshot

import (
"context"
"fmt"

"github.com/redhat-appstudio/integration-service/cache"

Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions controllers/snapshot/snapshot_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package snapshot

import (
"reflect"
"time"

"k8s.io/apimachinery/pkg/api/errors"

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 7eee0a6

Please sign in to comment.