Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions images/hooks/pkg/hooks/install-vmclass-generic/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,30 +283,33 @@ 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{}{
"op": "remove",
"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
}
Expand Down
6 changes: 3 additions & 3 deletions images/hooks/pkg/hooks/install-vmclass-generic/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
Expand Down
Loading