diff --git a/Makefile b/Makefile index c8ced67a8..eaebf4227 100644 --- a/Makefile +++ b/Makefile @@ -105,6 +105,15 @@ test-unit: @echo @echo "==> Running unit tests <==" GO111MODULE=on go test $(GOFLAGS) -run $(TESTS) $(PKG) $(TESTFLAGS) + @echo + @echo "==> Running unit test(s) with ldflags <==" +# Test to check the deprecation warnings on Kubernetes templates created by `helm create` against the current Kubernetes +# version. Note: The version details are set in var LDFLAGS. To avoid the ldflags impact on other unit tests that are +# based on older versions, this is run separately. When run without the ldflags in the unit test (above) or coverage +# test, it still passes with a false-positive result as the resources shouldn’t be deprecated in the older Kubernetes +# version if it only starts failing with the latest. + GO111MODULE=on go test $(GOFLAGS) -run ^TestHelmCreateChart_CheckDeprecatedWarnings$$ ./pkg/lint/ $(TESTFLAGS) -ldflags '$(LDFLAGS)' + .PHONY: test-coverage test-coverage: diff --git a/pkg/chartutil/create.go b/pkg/chartutil/create.go index 50212f9d5..7d77646da 100644 --- a/pkg/chartutil/create.go +++ b/pkg/chartutil/create.go @@ -622,6 +622,10 @@ func Create(name, dir string) (string, error) { return cdir, errors.Errorf("file %s already exists and is not a directory", cdir) } + // Note: If adding a new template below (i.e., to `helm create`) which is disabled by default (similar to hpa and + // ingress below); or making an existing template disabled by default, add the enabling condition in + // `TestHelmCreateChart_CheckDeprecatedWarnings` in `pkg/lint/lint_test.go` to make it run through deprecation checks + // with latest Kubernetes version. files := []struct { path string content []byte diff --git a/pkg/lint/lint_test.go b/pkg/lint/lint_test.go index 5516ec668..8f03ed1b1 100644 --- a/pkg/lint/lint_test.go +++ b/pkg/lint/lint_test.go @@ -137,6 +137,53 @@ func TestHelmCreateChart(t *testing.T) { } } +// TestHelmCreateChart_CheckDeprecatedWarnings checks if any default template created by `helm create` throws +// deprecated warnings in the linter check against the current Kubernetes version (provided using ldflags). +// +// See https://github.com/helm/helm/issues/11495 +// +// Resources like hpa and ingress, which are disabled by default in values.yaml are enabled here using the equivalent +// of the `--set` flag. +// +// Note: This test requires the following ldflags to be set per the current Kubernetes version to avoid false-positive +// results. +// 1. -X helm.sh/helm/v3/pkg/lint/rules.k8sVersionMajor= +// 2. -X helm.sh/helm/v3/pkg/lint/rules.k8sVersionMinor= +// or directly use '$(LDFLAGS)' in Makefile. +// +// When run without ldflags, the test passes giving a false-positive result. This is because the variables +// `k8sVersionMajor` and `k8sVersionMinor` by default are set to an older version of Kubernetes, with which, there +// might not be the deprecation warning. +func TestHelmCreateChart_CheckDeprecatedWarnings(t *testing.T) { + createdChart, err := chartutil.Create("checkdeprecatedwarnings", t.TempDir()) + if err != nil { + t.Error(err) + return + } + + // Add values to enable hpa, and ingress which are disabled by default. + // This is the equivalent of: + // helm lint checkdeprecatedwarnings --set 'autoscaling.enabled=true,ingress.enabled=true' + updatedValues := map[string]interface{}{ + "autoscaling": map[string]interface{}{ + "enabled": true, + }, + "ingress": map[string]interface{}{ + "enabled": true, + }, + } + + linterRunDetails := All(createdChart, updatedValues, namespace, true) + for _, msg := range linterRunDetails.Messages { + if strings.HasPrefix(msg.Error(), "[WARNING]") && + strings.Contains(msg.Error(), "deprecated") { + // When there is a deprecation warning for an object created + // by `helm create` for the current Kubernetes version, fail. + t.Errorf("Unexpected deprecation warning for %q: %s", msg.Path, msg.Error()) + } + } +} + // lint ignores import-values // See https://github.com/helm/helm/issues/9658 func TestSubChartValuesChart(t *testing.T) {