From 21f47a3147a734b0c7f01c751588dbc735fc049c Mon Sep 17 00:00:00 2001 From: Mozes Date: Fri, 28 Nov 2025 16:01:51 +0100 Subject: [PATCH] Add new option to define api-versions from file To implement compatibility with different K8S versions the Capabilities.APIVersions is used in the helm chart's template yaml or tpl files. A helm version hardcodes a fixed set of APIVersions from a specific K8S release. A new option (--default-api-versions-file) is added to load the full list of APIVersions from files that "mocks" the API list of a given K8S version or distribution, so the default list can be overriden. This way the compatibility of a helm chart can be tested offline using helm template with different set of K8S API Versions as input. The output of helm template operation can be input to kubeval or kubeconform to test the schema compatibility. Example: helm template mychart-1.0.0.tgz --api-versions-file k8s.v1.27.txt where the k8s.v127.txt can contain the list of API versions. Example ```txt v1 v1/Binding v1/ComponentStatus v1/ConfigMap v1/Endpoints v1/Event v1/LimitRange v1/Namespace v1/Node v1/PersistentVolume v1/PersistentVolumeClaim v1/Pod v1/PodTemplate v1/ReplicationController v1/ResourceQuota v1/Secret v1/Service v1/ServiceAccount apps/v1 apps/v1/ControllerRevision apps/v1/DaemonSet apps/v1/Deployment apps/v1/ReplicaSet apps/v1/StatefulSet etc. ``` Signed-off-by: Mozes --- pkg/cmd/template.go | 18 ++++++++++++++++++ pkg/cmd/template_test.go | 11 +++++++++++ pkg/cmd/testdata/default-api-versions.txt | 3 +++ .../template-default-api-versions-file1.txt | 8 ++++++++ .../template-default-api-versions-file2.txt | 8 ++++++++ .../chart-listing-apiversions/Chart.yaml | 8 ++++++++ .../chart-listing-apiversions/README.md | 4 ++++ .../templates/configmap.yaml | 6 ++++++ .../chart-listing-apiversions/values.yaml | 0 9 files changed, 66 insertions(+) create mode 100644 pkg/cmd/testdata/default-api-versions.txt create mode 100644 pkg/cmd/testdata/output/template-default-api-versions-file1.txt create mode 100644 pkg/cmd/testdata/output/template-default-api-versions-file2.txt create mode 100644 pkg/cmd/testdata/testcharts/chart-listing-apiversions/Chart.yaml create mode 100644 pkg/cmd/testdata/testcharts/chart-listing-apiversions/README.md create mode 100644 pkg/cmd/testdata/testcharts/chart-listing-apiversions/templates/configmap.yaml create mode 100644 pkg/cmd/testdata/testcharts/chart-listing-apiversions/values.yaml diff --git a/pkg/cmd/template.go b/pkg/cmd/template.go index cf68c6c46..df5ee5fa1 100644 --- a/pkg/cmd/template.go +++ b/pkg/cmd/template.go @@ -55,6 +55,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { client := action.NewInstall(cfg) valueOpts := &values.Options{} var kubeVersion string + var apisFiles []string var extraAPIs []string var showFiles []string @@ -94,6 +95,21 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { client.ReleaseName = "release-name" client.Replace = true // Skip the name check client.APIVersions = common.VersionSet(extraAPIs) + if len(apisFiles) > 0 { + x := []string{} + for _, apisFile := range apisFiles { + data, err := os.ReadFile(apisFile) + if err != nil { + return err + } + for _, line := range strings.Split(string(data), "\n") { + if strings.TrimSpace(line) != "" { + x = append(x, strings.TrimSpace(line)) + } + } + } + common.DefaultCapabilities.APIVersions = common.VersionSet(x) + } client.IncludeCRDs = includeCrds rel, err := runInstall(args, client, valueOpts, out) @@ -203,6 +219,8 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { f.BoolVar(&client.IsUpgrade, "is-upgrade", false, "set .Release.IsUpgrade instead of .Release.IsInstall") f.StringVar(&kubeVersion, "kube-version", "", "Kubernetes version used for Capabilities.KubeVersion") f.StringSliceVarP(&extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions (multiple can be specified)") + f.StringSliceVarP(&apisFiles, "default-api-versions-file", "", []string{}, + "Override default Capabilities.APIVersions from a local text file, one apiVerion per line. e.g.: \napps/v1/StatefulSet\napps/v1/ReplicaSet") f.BoolVar(&client.UseReleaseName, "release-name", false, "use release name in the output-dir path.") f.String( "dry-run", diff --git a/pkg/cmd/template_test.go b/pkg/cmd/template_test.go index 5bcccf5d0..8c08d05db 100644 --- a/pkg/cmd/template_test.go +++ b/pkg/cmd/template_test.go @@ -166,6 +166,17 @@ func TestTemplateCmd(t *testing.T) { cmd: fmt.Sprintf("template '%s' -f %s/extra_values.yaml", chartPath, chartPath), golden: "output/template-subchart-cm-set-file.txt", }, + { + name: "template with default-api-versions-file", + cmd: fmt.Sprintf("template '%s' --default-api-versions-file testdata/default-api-versions.txt", + "testdata/testcharts/chart-listing-apiversions"), + golden: "output/template-default-api-versions-file1.txt", + }, + { + name: "template with default-api-versions-file and api-versions", + cmd: fmt.Sprintf("template '%s' --default-api-versions-file testdata/default-api-versions.txt --api-versions helm.sh/extra1 --api-versions helm.sh/extra2", "testdata/testcharts/chart-listing-apiversions"), + golden: "output/template-default-api-versions-file2.txt", + }, } runTestCmd(t, tests) } diff --git a/pkg/cmd/testdata/default-api-versions.txt b/pkg/cmd/testdata/default-api-versions.txt new file mode 100644 index 000000000..f2fb34dd9 --- /dev/null +++ b/pkg/cmd/testdata/default-api-versions.txt @@ -0,0 +1,3 @@ +apps/v1/Deployment +apps/v1/StatefulSet +networking.k8s.io/v1/NetworkPolicy diff --git a/pkg/cmd/testdata/output/template-default-api-versions-file1.txt b/pkg/cmd/testdata/output/template-default-api-versions-file1.txt new file mode 100644 index 000000000..c60008a8e --- /dev/null +++ b/pkg/cmd/testdata/output/template-default-api-versions-file1.txt @@ -0,0 +1,8 @@ +--- +# Source: chart-listing-apiversions/templates/configmap.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: "release-name-chart-listing-apiversions" +data: + apiVersions: [apps/v1/Deployment apps/v1/StatefulSet networking.k8s.io/v1/NetworkPolicy] diff --git a/pkg/cmd/testdata/output/template-default-api-versions-file2.txt b/pkg/cmd/testdata/output/template-default-api-versions-file2.txt new file mode 100644 index 000000000..67caabcba --- /dev/null +++ b/pkg/cmd/testdata/output/template-default-api-versions-file2.txt @@ -0,0 +1,8 @@ +--- +# Source: chart-listing-apiversions/templates/configmap.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: "release-name-chart-listing-apiversions" +data: + apiVersions: [apps/v1/Deployment apps/v1/StatefulSet networking.k8s.io/v1/NetworkPolicy helm.sh/extra1 helm.sh/extra2] diff --git a/pkg/cmd/testdata/testcharts/chart-listing-apiversions/Chart.yaml b/pkg/cmd/testdata/testcharts/chart-listing-apiversions/Chart.yaml new file mode 100644 index 000000000..50168dd3f --- /dev/null +++ b/pkg/cmd/testdata/testcharts/chart-listing-apiversions/Chart.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +description: Testing .Capabilities.APIVersions +home: https://helm.sh/helm +name: chart-listing-apiversions +sources: + - https://github.com/helm/helm +version: 0.1.0 +type: application diff --git a/pkg/cmd/testdata/testcharts/chart-listing-apiversions/README.md b/pkg/cmd/testdata/testcharts/chart-listing-apiversions/README.md new file mode 100644 index 000000000..3a2ddf21d --- /dev/null +++ b/pkg/cmd/testdata/testcharts/chart-listing-apiversions/README.md @@ -0,0 +1,4 @@ +# Helm Chart to print .Capabilities.APIVersions + +Used for testing `helm template . --default-api-versions-file ` +option diff --git a/pkg/cmd/testdata/testcharts/chart-listing-apiversions/templates/configmap.yaml b/pkg/cmd/testdata/testcharts/chart-listing-apiversions/templates/configmap.yaml new file mode 100644 index 000000000..c8d987760 --- /dev/null +++ b/pkg/cmd/testdata/testcharts/chart-listing-apiversions/templates/configmap.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: "{{.Release.Name}}-{{.Chart.Name}}" +data: + apiVersions: {{ .Capabilities.APIVersions }} diff --git a/pkg/cmd/testdata/testcharts/chart-listing-apiversions/values.yaml b/pkg/cmd/testdata/testcharts/chart-listing-apiversions/values.yaml new file mode 100644 index 000000000..e69de29bb