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 <nyitrai.mozes@gmail.com>
pull/31589/head
Mozes 10 months ago
parent dbd07fa16c
commit 21f47a3147
No known key found for this signature in database
GPG Key ID: 5A48CEAE5E02437D

@ -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",

@ -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)
}

@ -0,0 +1,3 @@
apps/v1/Deployment
apps/v1/StatefulSet
networking.k8s.io/v1/NetworkPolicy

@ -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]

@ -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]

@ -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

@ -0,0 +1,4 @@
# Helm Chart to print .Capabilities.APIVersions
Used for testing `helm template . --default-api-versions-file <filepath>`
option

@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: "{{.Release.Name}}-{{.Chart.Name}}"
data:
apiVersions: {{ .Capabilities.APIVersions }}
Loading…
Cancel
Save