diff --git a/images/hooks/pkg/hooks/install-vmclass-generic/hook.go b/images/hooks/pkg/hooks/install-vmclass-generic/hook.go index 0991817f3e..17c957e07a 100644 --- a/images/hooks/pkg/hooks/install-vmclass-generic/hook.go +++ b/images/hooks/pkg/hooks/install-vmclass-generic/hook.go @@ -283,20 +283,11 @@ func addPatchesToCleanupMetadata(input *pkg.HookInput, vmClass *v1alpha2.Virtual } } - // Ensure "keep resource" annotation on vmclass/generic, so Helm will keep it - // in the cluster even that we've deleted its manifest from templates. - if _, exists := vmClass.Annotations[helmKeepResourceAnno]; !exists { - patches = append(patches, map[string]interface{}{ - "op": "add", - "path": fmt.Sprintf("/metadata/annotations/%s", jsonPatchEscape(helmKeepResourceAnno)), - "value": nil, - }) - } - annoNames := []string{ helmReleaseNameAnno, helmReleaseNamespaceAnno, } + hasHelmAnnotations := false for _, annoName := range annoNames { if _, exists := vmClass.Annotations[annoName]; exists { patches = append(patches, map[string]interface{}{ @@ -304,9 +295,21 @@ func addPatchesToCleanupMetadata(input *pkg.HookInput, vmClass *v1alpha2.Virtual "path": fmt.Sprintf("/metadata/annotations/%s", jsonPatchEscape(annoName)), "value": nil, }) + hasHelmAnnotations = true } } + // Ensure "keep resource" annotation on vmclass/generic, so Helm will keep resource + // in the cluster even that we've deleted its manifest from templates. + _, hasKeepResourceAnno := vmClass.Annotations[helmKeepResourceAnno] + if hasHelmAnnotations && !hasKeepResourceAnno { + patches = append(patches, map[string]interface{}{ + "op": "add", + "path": fmt.Sprintf("/metadata/annotations/%s", jsonPatchEscape(helmKeepResourceAnno)), + "value": nil, + }) + } + if len(patches) == 0 { return } diff --git a/images/hooks/pkg/hooks/install-vmclass-generic/hook_test.go b/images/hooks/pkg/hooks/install-vmclass-generic/hook_test.go index ad702941b2..9acf829bc4 100644 --- a/images/hooks/pkg/hooks/install-vmclass-generic/hook_test.go +++ b/images/hooks/pkg/hooks/install-vmclass-generic/hook_test.go @@ -318,17 +318,17 @@ var _ = Describe("Install VMClass Generic hook", func() { }) }) - When("vmclass/generic without keep-resource annotation is present", func() { + When("vmclass/generic without keep-resource annotation", func() { It("should not change vmclass/generic and set values", func() { prepareVMClassSnapshotGenericWithoutKeepResource() values.SetMock.Return() patchCollector.CreateMock.Optional() - patchCollector.PatchWithJSONMock.Return() + patchCollector.PatchWithJSONMock.Optional() Expect(Reconcile(context.Background(), newInput())).To(Succeed()) Expect(patchCollector.CreateMock.Calls()).To(HaveLen(0)) - Expect(patchCollector.PatchWithJSONMock.Calls()).To(HaveLen(1)) + Expect(patchCollector.PatchWithJSONMock.Calls()).To(HaveLen(0)) Expect(values.SetMock.Calls()).To(HaveLen(1)) }) })