Skip to content

Commit

Permalink
feat: add support for resource-policy: keep (#246) (#582)
Browse files Browse the repository at this point in the history
The annotation `helm.sh/resource-policy: keep` instructs Helm to skip
deleting this resource when a Helm operation would result in its
deletion. As the resource is not actually deleted, exclude it from the
generated diff.
  • Loading branch information
phooijenga authored May 25, 2024
1 parent dd8b4e6 commit 0652b98
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
4 changes: 3 additions & 1 deletion diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O

for _, key := range removed {
oldContent := oldIndex[key]
doDiff(&report, key, oldContent, nil, options)
if oldContent.ResourcePolicy != "keep" {
doDiff(&report, key, oldContent, nil, options)
}
}

for _, key := range added {
Expand Down
45 changes: 45 additions & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,22 @@ spec:
`,
}}

specReleaseKeep := map[string]*manifest.MappingResult{
"default, nginx, Deployment (apps)": {

Name: "default, nginx, Deployment (apps)",
Kind: "Deployment",
Content: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
annotations:
helm.sh/resource-policy: keep
`,
ResourcePolicy: "keep",
}}

t.Run("OnChange", func(t *testing.T) {
var buf1 bytes.Buffer
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{}}
Expand Down Expand Up @@ -438,6 +454,35 @@ spec:
require.Equal(t, ``, buf2.String())
})

t.Run("OnChangeRemoved", func(t *testing.T) {
var buf1 bytes.Buffer
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}}

if changesSeen := Manifests(specRelease, nil, &diffOptions, &buf1); !changesSeen {
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
}

require.Equal(t, `default, nginx, Deployment (apps) has been removed:
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: nginx
- `+`
`, buf1.String())
})

t.Run("OnChangeRemovedWithResourcePolicyKeep", func(t *testing.T) {
var buf2 bytes.Buffer
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{}}

if changesSeen := Manifests(specReleaseKeep, nil, &diffOptions, &buf2); changesSeen {
t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`")
}

require.Equal(t, ``, buf2.String())
})

t.Run("OnChangeSimple", func(t *testing.T) {
var buf1 bytes.Buffer
diffOptions := Options{"simple", 10, false, true, []string{}, 0.0, []string{}}
Expand Down
17 changes: 10 additions & 7 deletions manifest/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
)

const (
hookAnnotation = "helm.sh/hook"
hookAnnotation = "helm.sh/hook"
resourcePolicyAnnotation = "helm.sh/resource-policy"
)

var yamlSeparator = []byte("\n---\n")

// MappingResult to store result of diff
type MappingResult struct {
Name string
Kind string
Content string
Name string
Kind string
Content string
ResourcePolicy string
}

type metadata struct {
Expand Down Expand Up @@ -169,9 +171,10 @@ func parseContent(content string, defaultNamespace string, normalizeManifests bo
name := parsedMetadata.String()
return []*MappingResult{
{
Name: name,
Kind: parsedMetadata.Kind,
Content: content,
Name: name,
Kind: parsedMetadata.Kind,
Content: content,
ResourcePolicy: parsedMetadata.Metadata.Annotations[resourcePolicyAnnotation],
},
}, nil
}
Expand Down

0 comments on commit 0652b98

Please sign in to comment.