Skip to content

Commit 0652b98

Browse files
authored
feat: add support for resource-policy: keep (#246) (#582)
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.
1 parent dd8b4e6 commit 0652b98

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

diff/diff.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O
5858

5959
for _, key := range removed {
6060
oldContent := oldIndex[key]
61-
doDiff(&report, key, oldContent, nil, options)
61+
if oldContent.ResourcePolicy != "keep" {
62+
doDiff(&report, key, oldContent, nil, options)
63+
}
6264
}
6365

6466
for _, key := range added {

diff/diff_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ spec:
259259
`,
260260
}}
261261

262+
specReleaseKeep := map[string]*manifest.MappingResult{
263+
"default, nginx, Deployment (apps)": {
264+
265+
Name: "default, nginx, Deployment (apps)",
266+
Kind: "Deployment",
267+
Content: `
268+
apiVersion: apps/v1
269+
kind: Deployment
270+
metadata:
271+
name: nginx
272+
annotations:
273+
helm.sh/resource-policy: keep
274+
`,
275+
ResourcePolicy: "keep",
276+
}}
277+
262278
t.Run("OnChange", func(t *testing.T) {
263279
var buf1 bytes.Buffer
264280
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{}}
@@ -438,6 +454,35 @@ spec:
438454
require.Equal(t, ``, buf2.String())
439455
})
440456

457+
t.Run("OnChangeRemoved", func(t *testing.T) {
458+
var buf1 bytes.Buffer
459+
diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}}
460+
461+
if changesSeen := Manifests(specRelease, nil, &diffOptions, &buf1); !changesSeen {
462+
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`")
463+
}
464+
465+
require.Equal(t, `default, nginx, Deployment (apps) has been removed:
466+
467+
- apiVersion: apps/v1
468+
- kind: Deployment
469+
- metadata:
470+
- name: nginx
471+
- `+`
472+
`, buf1.String())
473+
})
474+
475+
t.Run("OnChangeRemovedWithResourcePolicyKeep", func(t *testing.T) {
476+
var buf2 bytes.Buffer
477+
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{}}
478+
479+
if changesSeen := Manifests(specReleaseKeep, nil, &diffOptions, &buf2); changesSeen {
480+
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`")
481+
}
482+
483+
require.Equal(t, ``, buf2.String())
484+
})
485+
441486
t.Run("OnChangeSimple", func(t *testing.T) {
442487
var buf1 bytes.Buffer
443488
diffOptions := Options{"simple", 10, false, true, []string{}, 0.0, []string{}}

manifest/parse.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ import (
1111
)
1212

1313
const (
14-
hookAnnotation = "helm.sh/hook"
14+
hookAnnotation = "helm.sh/hook"
15+
resourcePolicyAnnotation = "helm.sh/resource-policy"
1516
)
1617

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

1920
// MappingResult to store result of diff
2021
type MappingResult struct {
21-
Name string
22-
Kind string
23-
Content string
22+
Name string
23+
Kind string
24+
Content string
25+
ResourcePolicy string
2426
}
2527

2628
type metadata struct {
@@ -169,9 +171,10 @@ func parseContent(content string, defaultNamespace string, normalizeManifests bo
169171
name := parsedMetadata.String()
170172
return []*MappingResult{
171173
{
172-
Name: name,
173-
Kind: parsedMetadata.Kind,
174-
Content: content,
174+
Name: name,
175+
Kind: parsedMetadata.Kind,
176+
Content: content,
177+
ResourcePolicy: parsedMetadata.Metadata.Annotations[resourcePolicyAnnotation],
175178
},
176179
}, nil
177180
}

0 commit comments

Comments
 (0)