From 11c2faa019dbe17e881b8dadd006115e4b886af3 Mon Sep 17 00:00:00 2001 From: waciumawanjohi Date: Fri, 10 Mar 2023 10:16:26 -0500 Subject: [PATCH] Deleted stamped immutable objects do not cause error (#1187) --- pkg/controllers/common.go | 4 +++ pkg/controllers/workload_reconciler_test.go | 38 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pkg/controllers/common.go b/pkg/controllers/common.go index 3472b4513..d3467c5ad 100644 --- a/pkg/controllers/common.go +++ b/pkg/controllers/common.go @@ -53,6 +53,10 @@ func immutableEquivalenceTest(realizedResource v1alpha1.ResourceStatus, prevReso return false, fmt.Errorf("get unstructured: %w", err) } + if prevObj == nil { + return false, nil + } + prevLabels := prevObj.GetLabels() if lifecycleLabel, ok := prevLabels["carto.run/template-lifecycle"]; !ok || lifecycleLabel == "mutable" { diff --git a/pkg/controllers/workload_reconciler_test.go b/pkg/controllers/workload_reconciler_test.go index 2f36d20f1..9feb9ac1f 100644 --- a/pkg/controllers/workload_reconciler_test.go +++ b/pkg/controllers/workload_reconciler_test.go @@ -1641,6 +1641,44 @@ var _ = Describe("WorkloadReconciler", func() { }) }) }) + + Context("when the previously stamped object no longer exists", func() { + BeforeEach(func() { + referenceToNonExistentObject := v1alpha1.RealizedResource{ + Name: "some-resource", + StampedRef: &v1alpha1.StampedRef{ + ObjectReference: &corev1.ObjectReference{ + APIVersion: "some-api-version", + Kind: "some-kind", + Name: "some-obj-name", + }, + Resource: "some-kind", + }, + TemplateRef: &corev1.ObjectReference{ + Name: "some-template-name", + Kind: "some-template-kind", + }, + } + + wl.Status.Resources = []v1alpha1.ResourceStatus{ + { + RealizedResource: referenceToNonExistentObject, + }, + } + repo.GetWorkloadReturns(wl, nil) + + repo.GetUnstructuredReturnsOnCall(0, nil, nil) + repo.DeleteReturns(fmt.Errorf("some error")) + }) + + It("does not return an error", func() { + _, err := reconciler.Reconcile(ctx, req) + Expect(err).NotTo(HaveOccurred()) + + Expect(repo.DeleteCallCount()).To(Equal(1)) + Expect(out).To(Say(`"msg":"failed to cleanup orphaned objects","workload":"my-namespace/my-workload-name"`)) + }) + }) }) Context("when previous resource was stamped from a template that is no longer on cluster", func() {