Repository: helm/helm (go)

Issue:
Helm create uses old HPA Version
[Jump to issue](#issue)

Output of `helm version`: 

```shell
➜ helm version    
version.BuildInfo{Version:"v3.9.4", GitCommit:"dbc6d8e20fe1d58d50e6ed30f09a04a77e4c68db", GitTreeState:"clean", GoVersion:"go1.17.13"}
```

Output of `kubectl version`:

```shell
➜ kubectl version                                    
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.3", GitCommit:"434bfd82814af038ad94d62ebe59b133fcb50506", GitTreeState:"clean", BuildDate:"2022-10-12T10:57:26Z", GoVersion:"go1.19.2", Compiler:"gc", Platform:"darwin/amd64"}
Kustomize Version: v4.5.7
Server Version: version.Info{Major:"1", Minor:"24", GitVersion:"v1.24.3-gke.2100", GitCommit:"25d7334511e90d0b636707059c955baebce769cd", GitTreeState:"clean", BuildDate:"2022-08-16T09:24:54Z", GoVersion:"go1.18.3b7", Compiler:"gc", Platform:"linux/amd64"}
```

Cloud Provider/Platform (AKS, GKE, Minikube etc.):  GKE, v1.24.3-gke.2100


# Issue

When creating a new helm chart using `helm create <chart>` the HPA is using an old outdated and soon to be deprecated version

<details>
<summary>Warning</summary>

_note: Task is just a better version of makefile, the command it runs is `helm upgrade <reelase> <chart>

```shell
➜ task upgrade
task: [upgrade] helm upgrade $releaseName $chart
W1102 13:34:17.245653   42748 warnings.go:70] autoscaling/v2beta1 HorizontalPodAutoscaler is deprecated in v1.22+, unavailable in v1.25+; use autoscaling/v2 HorizontalPodAutoscaler
W1102 13:34:17.269708   42748 warnings.go:70] autoscaling/v2beta1 HorizontalPodAutoscaler is deprecated in v1.22+, unavailable in v1.25+; use autoscaling/v2 HorizontalPodAutoscaler
W1102 13:34:20.310690   42748 warnings.go:70] autoscaling/v2beta1 HorizontalPodAutoscaler is deprecated in v1.22+, unavailable in v1.25+; use autoscaling/v2 HorizontalPodAutoscaler
W1102 13:34:20.438095   42748 warnings.go:70] autoscaling/v2beta1 HorizontalPodAutoscaler is deprecated in v1.22+, unavailable in v1.25+; use autoscaling/v2 HorizontalPodAutoscaler
W1102 13:34:20.463040   42748 warnings.go:70] autoscaling/v2beta1 HorizontalPodAutoscaler is deprecated in v1.22+, unavailable in v1.25+; use autoscaling/v2 HorizontalPodAutoscaler
W1102 13:34:20.488023   42748 warnings.go:70] autoscaling/v2beta1 HorizontalPodAutoscaler is deprecated in v1.22+, unavailable in v1.25+; use autoscaling/v2 HorizontalPodAutoscaler

```
</details>


The Deprecation notice is [Here](https://kubernetes.io/docs/reference/using-api/deprecation-guide/#horizontalpodautoscaler-v126)

What would be nice to see is the New version:

## Issues related


https://github.com/helm/helm/issues/10710


Pull request description:
test(create): Add test to check deprecated `apiVersion`s in resource templates created by `helm create`
**What this PR does / why we need it**:
1. Change `HorizontalPodAutoscaler`'s version from `autoscaling/v2beta1` -> `autoscaling/v2` in `helm create`.
2. Add a test `TestHelmCreateChart_CheckDeprecatedWarnings` to check the deprecated versions from being created by `helm create`.
3. A comment is added in `pkg/chartutil/create.go` for future manifest files added to `helm create` to follow the same pattern and update the test.

About `TestHelmCreateChart_CheckDeprecatedWarnings`:
- Checks the manifests created by `helm create` to not have deprecated `apiVersion`s.
- This requires the following `ldflags` to be set per the current Kubernetes version for avoiding false positive results.
    1. `-X helm.sh/helm/v3/pkg/lint/rules.k8sVersionMajor=`\<k8s-major-version>
    2. `-X helm.sh/helm/v3/pkg/lint/rules.k8sVersionMinor=`\<k8s-minor-version>
- In order to avoid the `ldflags` passed for this test from conflicting with other tests, a seperate `go test` is added in `make test-unit`.
- Without the `ldflags`, this test passes with a false positive result. This behavior will help in the former run of `go test` and in `make test-coverage`. Note: This test doesn't contribute to coverage anyway.

Closes #11495 

**Special notes for your reviewer**:
Not sure how backward compatibility is checked when the changes are related to the `apiVersion` of the object. But else, I think this change qualifies as backward compatible. Please check.

**If applicable**:
- [ ] this PR contains documentation
- [x] this PR contains unit tests
- [x] this PR has been tested for backwards compatibility

@userbradley @joejulian @hzliangbin @wujunwei Please validate whether these changes achieve the expected result. Thanks!


Patch:
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


Test patch:
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=<k8s-major-version>
+// 2. -X helm.sh/helm/v3/pkg/lint/rules.k8sVersionMinor=<k8s-minor-version>
+// 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) {