Fix false negatives in chartNeedsAPIVersions

Signed-off-by: Eric Latham <ericoliverlatham@gmail.com>
pull/13146/head
Eric Latham 1 year ago
parent 50f64dc628
commit aff79adb39

@ -97,11 +97,12 @@ type Configuration struct {
Log func(string, ...interface{}) Log func(string, ...interface{})
} }
// chartNeedsAPIVersions returns true if any template in the given Chart references .Capabilities.APIVersions. // chartNeedsAPIVersions returns true if any of the Chart's templates reference "APIVersions".
// If it returns false, it is safe to omit Capabilities.APIVersions from rendering values.
func chartNeedsAPIVersions(chart *chart.Chart) bool { func chartNeedsAPIVersions(chart *chart.Chart) bool {
if chart != nil { if chart != nil {
for _, template := range chart.Templates { for _, template := range chart.Templates {
if bytes.Contains(template.Data, []byte(".Capabilities.APIVersions")) { if bytes.Contains(template.Data, []byte("APIVersions")) {
return true return true
} }
} }

@ -286,6 +286,12 @@ func Test_chartNeedsAPIVersions(t *testing.T) {
is.Equal(chartNeedsAPIVersions(ch), false) is.Equal(chartNeedsAPIVersions(ch), false)
ch.Templates = append(ch.Templates, &chart.File{Name: "templates/api-versions", Data: []byte("{{ .Capabilities.APIVersions }}")}) ch.Templates = append(ch.Templates, &chart.File{Name: "templates/api-versions", Data: []byte("{{ .Capabilities.APIVersions }}")})
is.Equal(chartNeedsAPIVersions(ch), true) is.Equal(chartNeedsAPIVersions(ch), true)
ch.Templates = append(ch.Templates, &chart.File{Name: "templates/api-versions", Data: []byte("# {{ .Capabilities.APIVersions }}")})
is.Equal(chartNeedsAPIVersions(ch), true)
ch.Templates = append(ch.Templates, &chart.File{Name: "templates/api-versions", Data: []byte("{{ $cap.APIVersions }}")})
is.Equal(chartNeedsAPIVersions(ch), true)
ch.Templates = append(ch.Templates, &chart.File{Name: "templates/api-versions", Data: []byte("{{ get (.Capabilities | toJson | fromJson) \"APIVersions\" }}")})
is.Equal(chartNeedsAPIVersions(ch), true)
} }
func TestConfiguration_Init(t *testing.T) { func TestConfiguration_Init(t *testing.T) {

Loading…
Cancel
Save