From 160da867d05ed3a0585e8bc4bf55f9fdeefe0d9f Mon Sep 17 00:00:00 2001 From: "mengjiao.liu" Date: Thu, 25 Mar 2021 12:09:28 +0800 Subject: [PATCH] validate release name during install Signed-off-by: mengjiao.liu --- cmd/helm/install_test.go | 2 +- cmd/helm/template.go | 2 +- cmd/helm/template_test.go | 2 +- .../testdata/output/install-name-template.txt | 2 +- ...mplate-chart-with-template-lib-archive-dep.txt | 12 ++++++------ .../template-chart-with-template-lib-dep.txt | 12 ++++++------ .../testdata/output/template-name-template.txt | 8 ++++---- cmd/helm/testdata/output/template-set.txt | 8 ++++---- .../output/template-show-only-multiple.txt | 2 +- .../testdata/output/template-show-only-one.txt | 2 +- cmd/helm/testdata/output/template-skip-tests.txt | 2 +- .../testdata/output/template-values-files.txt | 8 ++++---- .../testdata/output/template-with-api-version.txt | 8 ++++---- cmd/helm/testdata/output/template-with-crds.txt | 8 ++++---- .../output/template-with-invalid-yaml-debug.txt | 2 +- .../output/template-with-kube-version.txt | 8 ++++---- cmd/helm/testdata/output/template.txt | 8 ++++---- .../charts/common/README.md | 8 ++++---- cmd/helm/testdata/testcharts/lib-chart/README.md | 8 ++++---- pkg/action/install.go | 15 ++------------- pkg/action/install_test.go | 2 +- pkg/chartutil/validate_name.go | 7 ++++++- 22 files changed, 65 insertions(+), 71 deletions(-) diff --git a/cmd/helm/install_test.go b/cmd/helm/install_test.go index ff025b809..fe98524f6 100644 --- a/cmd/helm/install_test.go +++ b/cmd/helm/install_test.go @@ -123,7 +123,7 @@ func TestInstall(t *testing.T) { // Install, using the name-template { name: "install with name-template", - cmd: "install testdata/testcharts/empty --name-template '{{upper \"foobar\"}}'", + cmd: "install testdata/testcharts/empty --name-template '{{ \"foobar\"}}'", golden: "output/install-name-template.txt", }, // Install, perform chart verification along the way. diff --git a/cmd/helm/template.go b/cmd/helm/template.go index e3c1d421f..f9c51542a 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -74,7 +74,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { } client.DryRun = true - client.ReleaseName = "RELEASE-NAME" + client.ReleaseName = "release-name" client.Replace = true // Skip the name check client.ClientOnly = !validate client.APIVersions = chartutil.VersionSet(extraAPIs) diff --git a/cmd/helm/template_test.go b/cmd/helm/template_test.go index 8fb8292e2..d1f17fe98 100644 --- a/cmd/helm/template_test.go +++ b/cmd/helm/template_test.go @@ -43,7 +43,7 @@ func TestTemplateCmd(t *testing.T) { }, { name: "check name template", - cmd: fmt.Sprintf(`template '%s' --name-template='foobar-{{ b64enc "abc" }}-baz'`, chartPath), + cmd: fmt.Sprintf(`template '%s' --name-template='foobar-{{ b64enc "abc" | lower }}-baz'`, chartPath), golden: "output/template-name-template.txt", }, { diff --git a/cmd/helm/testdata/output/install-name-template.txt b/cmd/helm/testdata/output/install-name-template.txt index 67e06d92b..19952e3c2 100644 --- a/cmd/helm/testdata/output/install-name-template.txt +++ b/cmd/helm/testdata/output/install-name-template.txt @@ -1,4 +1,4 @@ -NAME: FOOBAR +NAME: foobar LAST DEPLOYED: Fri Sep 2 22:04:05 1977 NAMESPACE: default STATUS: deployed diff --git a/cmd/helm/testdata/output/template-chart-with-template-lib-archive-dep.txt b/cmd/helm/testdata/output/template-chart-with-template-lib-archive-dep.txt index dc1aa2907..c954b8e14 100644 --- a/cmd/helm/testdata/output/template-chart-with-template-lib-archive-dep.txt +++ b/cmd/helm/testdata/output/template-chart-with-template-lib-archive-dep.txt @@ -7,7 +7,7 @@ metadata: app: chart-with-template-lib-archive-dep chart: chart-with-template-lib-archive-dep-0.1.0 heritage: Helm - release: RELEASE-NAME + release: release-name name: release-name-chart-with-template-lib-archive-dep spec: ports: @@ -16,30 +16,30 @@ spec: targetPort: http selector: app: chart-with-template-lib-archive-dep - release: RELEASE-NAME + release: release-name type: ClusterIP --- # Source: chart-with-template-lib-archive-dep/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: - name: RELEASE-NAME-chart-with-template-lib-archive-dep + name: release-name-chart-with-template-lib-archive-dep labels: app: chart-with-template-lib-archive-dep chart: chart-with-template-lib-archive-dep-0.1.0 - release: RELEASE-NAME + release: release-name heritage: Helm spec: replicas: 1 selector: matchLabels: app: chart-with-template-lib-archive-dep - release: RELEASE-NAME + release: release-name template: metadata: labels: app: chart-with-template-lib-archive-dep - release: RELEASE-NAME + release: release-name spec: containers: - name: chart-with-template-lib-archive-dep diff --git a/cmd/helm/testdata/output/template-chart-with-template-lib-dep.txt b/cmd/helm/testdata/output/template-chart-with-template-lib-dep.txt index 12adeb28b..74a2a2df8 100644 --- a/cmd/helm/testdata/output/template-chart-with-template-lib-dep.txt +++ b/cmd/helm/testdata/output/template-chart-with-template-lib-dep.txt @@ -7,7 +7,7 @@ metadata: app: chart-with-template-lib-dep chart: chart-with-template-lib-dep-0.1.0 heritage: Helm - release: RELEASE-NAME + release: release-name name: release-name-chart-with-template-lib-dep spec: ports: @@ -16,30 +16,30 @@ spec: targetPort: http selector: app: chart-with-template-lib-dep - release: RELEASE-NAME + release: release-name type: ClusterIP --- # Source: chart-with-template-lib-dep/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: - name: RELEASE-NAME-chart-with-template-lib-dep + name: release-name-chart-with-template-lib-dep labels: app: chart-with-template-lib-dep chart: chart-with-template-lib-dep-0.1.0 - release: RELEASE-NAME + release: release-name heritage: Helm spec: replicas: 1 selector: matchLabels: app: chart-with-template-lib-dep - release: RELEASE-NAME + release: release-name template: metadata: labels: app: chart-with-template-lib-dep - release: RELEASE-NAME + release: release-name spec: containers: - name: chart-with-template-lib-dep diff --git a/cmd/helm/testdata/output/template-name-template.txt b/cmd/helm/testdata/output/template-name-template.txt index b9e7cbbe4..9406048dd 100644 --- a/cmd/helm/testdata/output/template-name-template.txt +++ b/cmd/helm/testdata/output/template-name-template.txt @@ -70,7 +70,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "foobar-YWJj-baz" + app.kubernetes.io/instance: "foobar-ywjj-baz" kube-version/major: "1" kube-version/minor: "20" kube-version/version: "v1.20.0" @@ -88,7 +88,7 @@ spec: apiVersion: v1 kind: ConfigMap metadata: - name: "foobar-YWJj-baz-testconfig" + name: "foobar-ywjj-baz-testconfig" annotations: "helm.sh/hook": test data: @@ -98,7 +98,7 @@ data: apiVersion: v1 kind: Pod metadata: - name: "foobar-YWJj-baz-test" + name: "foobar-ywjj-baz-test" annotations: "helm.sh/hook": test spec: @@ -107,7 +107,7 @@ spec: image: "alpine:latest" envFrom: - configMapRef: - name: "foobar-YWJj-baz-testconfig" + name: "foobar-ywjj-baz-testconfig" command: - echo - "$message" diff --git a/cmd/helm/testdata/output/template-set.txt b/cmd/helm/testdata/output/template-set.txt index 177d8e58c..4040991cf 100644 --- a/cmd/helm/testdata/output/template-set.txt +++ b/cmd/helm/testdata/output/template-set.txt @@ -70,7 +70,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" kube-version/major: "1" kube-version/minor: "20" kube-version/version: "v1.20.0" @@ -88,7 +88,7 @@ spec: apiVersion: v1 kind: ConfigMap metadata: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" annotations: "helm.sh/hook": test data: @@ -98,7 +98,7 @@ data: apiVersion: v1 kind: Pod metadata: - name: "RELEASE-NAME-test" + name: "release-name-test" annotations: "helm.sh/hook": test spec: @@ -107,7 +107,7 @@ spec: image: "alpine:latest" envFrom: - configMapRef: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" command: - echo - "$message" diff --git a/cmd/helm/testdata/output/template-show-only-multiple.txt b/cmd/helm/testdata/output/template-show-only-multiple.txt index 81a1d8e3c..1aac3081a 100644 --- a/cmd/helm/testdata/output/template-show-only-multiple.txt +++ b/cmd/helm/testdata/output/template-show-only-multiple.txt @@ -6,7 +6,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" kube-version/major: "1" kube-version/minor: "20" kube-version/version: "v1.20.0" diff --git a/cmd/helm/testdata/output/template-show-only-one.txt b/cmd/helm/testdata/output/template-show-only-one.txt index a354bc0cc..9cc34f515 100644 --- a/cmd/helm/testdata/output/template-show-only-one.txt +++ b/cmd/helm/testdata/output/template-show-only-one.txt @@ -6,7 +6,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" kube-version/major: "1" kube-version/minor: "20" kube-version/version: "v1.20.0" diff --git a/cmd/helm/testdata/output/template-skip-tests.txt b/cmd/helm/testdata/output/template-skip-tests.txt index 8e8181474..5c907b563 100644 --- a/cmd/helm/testdata/output/template-skip-tests.txt +++ b/cmd/helm/testdata/output/template-skip-tests.txt @@ -70,7 +70,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" kube-version/major: "1" kube-version/minor: "20" kube-version/version: "v1.20.0" diff --git a/cmd/helm/testdata/output/template-values-files.txt b/cmd/helm/testdata/output/template-values-files.txt index 177d8e58c..4040991cf 100644 --- a/cmd/helm/testdata/output/template-values-files.txt +++ b/cmd/helm/testdata/output/template-values-files.txt @@ -70,7 +70,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" kube-version/major: "1" kube-version/minor: "20" kube-version/version: "v1.20.0" @@ -88,7 +88,7 @@ spec: apiVersion: v1 kind: ConfigMap metadata: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" annotations: "helm.sh/hook": test data: @@ -98,7 +98,7 @@ data: apiVersion: v1 kind: Pod metadata: - name: "RELEASE-NAME-test" + name: "release-name-test" annotations: "helm.sh/hook": test spec: @@ -107,7 +107,7 @@ spec: image: "alpine:latest" envFrom: - configMapRef: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" command: - echo - "$message" diff --git a/cmd/helm/testdata/output/template-with-api-version.txt b/cmd/helm/testdata/output/template-with-api-version.txt index 4b2d4ee84..7e1c35001 100644 --- a/cmd/helm/testdata/output/template-with-api-version.txt +++ b/cmd/helm/testdata/output/template-with-api-version.txt @@ -70,7 +70,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" kube-version/major: "1" kube-version/minor: "20" kube-version/version: "v1.20.0" @@ -89,7 +89,7 @@ spec: apiVersion: v1 kind: ConfigMap metadata: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" annotations: "helm.sh/hook": test data: @@ -99,7 +99,7 @@ data: apiVersion: v1 kind: Pod metadata: - name: "RELEASE-NAME-test" + name: "release-name-test" annotations: "helm.sh/hook": test spec: @@ -108,7 +108,7 @@ spec: image: "alpine:latest" envFrom: - configMapRef: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" command: - echo - "$message" diff --git a/cmd/helm/testdata/output/template-with-crds.txt b/cmd/helm/testdata/output/template-with-crds.txt index 758e2e39f..dd58480c9 100644 --- a/cmd/helm/testdata/output/template-with-crds.txt +++ b/cmd/helm/testdata/output/template-with-crds.txt @@ -87,7 +87,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" kube-version/major: "1" kube-version/minor: "20" kube-version/version: "v1.20.0" @@ -105,7 +105,7 @@ spec: apiVersion: v1 kind: ConfigMap metadata: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" annotations: "helm.sh/hook": test data: @@ -115,7 +115,7 @@ data: apiVersion: v1 kind: Pod metadata: - name: "RELEASE-NAME-test" + name: "release-name-test" annotations: "helm.sh/hook": test spec: @@ -124,7 +124,7 @@ spec: image: "alpine:latest" envFrom: - configMapRef: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" command: - echo - "$message" diff --git a/cmd/helm/testdata/output/template-with-invalid-yaml-debug.txt b/cmd/helm/testdata/output/template-with-invalid-yaml-debug.txt index c1f51185c..909c543d3 100644 --- a/cmd/helm/testdata/output/template-with-invalid-yaml-debug.txt +++ b/cmd/helm/testdata/output/template-with-invalid-yaml-debug.txt @@ -3,7 +3,7 @@ apiVersion: v1 kind: Pod metadata: - name: "RELEASE-NAME-my-alpine" + name: "release-name-my-alpine" spec: containers: - name: waiter diff --git a/cmd/helm/testdata/output/template-with-kube-version.txt b/cmd/helm/testdata/output/template-with-kube-version.txt index 836905268..9d326f328 100644 --- a/cmd/helm/testdata/output/template-with-kube-version.txt +++ b/cmd/helm/testdata/output/template-with-kube-version.txt @@ -70,7 +70,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" kube-version/major: "1" kube-version/minor: "16" kube-version/version: "v1.16.0" @@ -88,7 +88,7 @@ spec: apiVersion: v1 kind: ConfigMap metadata: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" annotations: "helm.sh/hook": test data: @@ -98,7 +98,7 @@ data: apiVersion: v1 kind: Pod metadata: - name: "RELEASE-NAME-test" + name: "release-name-test" annotations: "helm.sh/hook": test spec: @@ -107,7 +107,7 @@ spec: image: "alpine:latest" envFrom: - configMapRef: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" command: - echo - "$message" diff --git a/cmd/helm/testdata/output/template.txt b/cmd/helm/testdata/output/template.txt index 4146a0749..58c480b47 100644 --- a/cmd/helm/testdata/output/template.txt +++ b/cmd/helm/testdata/output/template.txt @@ -70,7 +70,7 @@ metadata: name: subchart labels: helm.sh/chart: "subchart-0.1.0" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" kube-version/major: "1" kube-version/minor: "20" kube-version/version: "v1.20.0" @@ -88,7 +88,7 @@ spec: apiVersion: v1 kind: ConfigMap metadata: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" annotations: "helm.sh/hook": test data: @@ -98,7 +98,7 @@ data: apiVersion: v1 kind: Pod metadata: - name: "RELEASE-NAME-test" + name: "release-name-test" annotations: "helm.sh/hook": test spec: @@ -107,7 +107,7 @@ spec: image: "alpine:latest" envFrom: - configMapRef: - name: "RELEASE-NAME-testconfig" + name: "release-name-testconfig" command: - echo - "$message" diff --git a/cmd/helm/testdata/testcharts/chart-with-template-lib-dep/charts/common/README.md b/cmd/helm/testdata/testcharts/chart-with-template-lib-dep/charts/common/README.md index ca0459474..0e06414d6 100755 --- a/cmd/helm/testdata/testcharts/chart-with-template-lib-dep/charts/common/README.md +++ b/cmd/helm/testdata/testcharts/chart-with-template-lib-dep/charts/common/README.md @@ -13,7 +13,7 @@ A few tips for working with Common: - Be careful when using functions that generate random data (like `common.fullname.unique`). They may trigger unwanted upgrades or have other side effects. -In this document, we use `RELEASE-NAME` as the name of the release. +In this document, we use `release-name` as the name of the release. ## Resource Kinds @@ -733,7 +733,7 @@ metadata: labels: app: metadata heritage: "Tiller" - release: "RELEASE-NAME" + release: "release-name" chart: metadata-0.1.0 first: "matt" last: "butcher" @@ -748,7 +748,7 @@ metadata: labels: app: metadata heritage: "Tiller" - release: "RELEASE-NAME" + release: "release-name" chart: metadata-0.1.0 annotations: ``` @@ -791,7 +791,7 @@ Example output: ```yaml app: labelizer heritage: "Tiller" -release: "RELEASE-NAME" +release: "release-name" chart: labelizer-0.1.0 ``` diff --git a/cmd/helm/testdata/testcharts/lib-chart/README.md b/cmd/helm/testdata/testcharts/lib-chart/README.md index aca257924..87b753f25 100644 --- a/cmd/helm/testdata/testcharts/lib-chart/README.md +++ b/cmd/helm/testdata/testcharts/lib-chart/README.md @@ -13,7 +13,7 @@ A few tips for working with Common: - Be careful when using functions that generate random data (like `common.fullname.unique`). They may trigger unwanted upgrades or have other side effects. -In this document, we use `RELEASE-NAME` as the name of the release. +In this document, we use `release-name` as the name of the release. ## Resource Kinds @@ -733,7 +733,7 @@ metadata: labels: app.kubernetes.io/name: metadata app.kubernetes.io/managed-by: "Helm" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" helm.sh/chart: metadata-0.1.0 first: "matt" last: "butcher" @@ -748,7 +748,7 @@ metadata: labels: app.kubernetes.io/name: metadata app.kubernetes.io/managed-by: "Helm" - app.kubernetes.io/instance: "RELEASE-NAME" + app.kubernetes.io/instance: "release-name" helm.sh/chart: metadata-0.1.0 annotations: ``` @@ -791,7 +791,7 @@ Example output: ```yaml app.kubernetes.io/name: labelizer app.kubernetes.io/managed-by: "Tiller" -app.kubernetes.io/instance: "RELEASE-NAME" +app.kubernetes.io/instance: "release-name" helm.sh/chart: labelizer-0.1.0 ``` diff --git a/pkg/action/install.go b/pkg/action/install.go index e91154515..922f728b0 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -54,13 +54,6 @@ import ( "helm.sh/helm/v3/pkg/storage/driver" ) -// releaseNameMaxLen is the maximum length of a release name. -// -// As of Kubernetes 1.4, the max limit on a name is 63 chars. We reserve 10 for -// charts to add data. Effectively, that gives us 53 chars. -// See https://github.com/helm/helm/issues/1528 -const releaseNameMaxLen = 53 - // NOTESFILE_SUFFIX that we want to treat special. It goes through the templating engine // but it's not a yaml file (resource) hence can't have hooks, etc. And the user actually // wants to see this file after rendering in the status command. However, it must be a suffix @@ -458,14 +451,10 @@ func (i *Install) failRelease(rel *release.Release, err error) (*release.Release // - used by a deleted release, and i.Replace is false func (i *Install) availableName() error { start := i.ReleaseName - if start == "" { - return errors.New("name is required") - } - if len(start) > releaseNameMaxLen { - return errors.Errorf("release name %q exceeds max length of %d", start, releaseNameMaxLen) + if err := chartutil.ValidateReleaseName(start); err != nil { + return errors.Wrapf(err, "release name %q", start) } - if i.DryRun { return nil } diff --git a/pkg/action/install_test.go b/pkg/action/install_test.go index b1844b2ce..3d08d7889 100644 --- a/pkg/action/install_test.go +++ b/pkg/action/install_test.go @@ -132,7 +132,7 @@ func TestInstallRelease_NoName(t *testing.T) { if err == nil { t.Fatal("expected failure when no name is specified") } - assert.Contains(t, err.Error(), "name is required") + assert.Contains(t, err.Error(), "no name provided") } func TestInstallRelease_WithNotes(t *testing.T) { diff --git a/pkg/chartutil/validate_name.go b/pkg/chartutil/validate_name.go index d253731ec..8c45d533b 100644 --- a/pkg/chartutil/validate_name.go +++ b/pkg/chartutil/validate_name.go @@ -52,7 +52,12 @@ var ( ) const ( - // maxNameLen is the maximum length Helm allows for a release name + // According to the Kubernetes docs (https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#rfc-1035-label-names) + // some resource names have a max length of 63 characters while others have a max + // length of 253 characters. As we cannot be sure the resources used in a chart, we + // therefore need to limit it to 63 chars and reserve 10 chars for additional part to name + // of the resource. The reason is that chart maintainers can use release name as part of + // the resource name (and some additional chars). maxReleaseNameLen = 53 // maxMetadataNameLen is the maximum length Kubernetes allows for any name. maxMetadataNameLen = 253