Skip to content

Add support for resource-policy: keep (#246) #582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2024
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
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