From 43e48c75c7dc92b23c02414c2529a837daba863d Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Fri, 1 May 2020 18:42:50 -0600 Subject: [PATCH] ignore template comments, fix bad test data Signed-off-by: Matt Butcher --- pkg/chartutil/create.go | 2 +- pkg/lint/rules/template.go | 10 ++++++++-- pkg/lint/rules/template_test.go | 1 + .../rules/testdata/albatross/templates/_helpers.tpl | 8 ++++---- pkg/lint/rules/testdata/albatross/templates/svc.yaml | 6 +++--- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/chartutil/create.go b/pkg/chartutil/create.go index 0e87c7b47..3636d278c 100644 --- a/pkg/chartutil/create.go +++ b/pkg/chartutil/create.go @@ -377,7 +377,7 @@ const defaultNotes = `1. Get the application URL by running these commands: {{- else if contains "LoadBalancer" .Values.service.type }} NOTE: It may take a few minutes for the LoadBalancer IP to be available. You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include ".fullname" . }}' - export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include ".fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include ".fullname" . }} --template "{{ "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}" }}") echo http://$SERVICE_IP:{{ .Values.service.port }} {{- else if contains "ClusterIP" .Values.service.type }} export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include ".name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") diff --git a/pkg/lint/rules/template.go b/pkg/lint/rules/template.go index 778c616c0..3728c98c8 100644 --- a/pkg/lint/rules/template.go +++ b/pkg/lint/rules/template.go @@ -188,10 +188,16 @@ func validateNoReleaseTime(manifest []byte) error { // TODO: I strongly suspect that there are better regexps than these two. var ( - badTplStart = regexp.MustCompile(`{{-?[^-\s]+`) - badTplEnd = regexp.MustCompile(`[^\s-]+-?}}`) + badTplStart = regexp.MustCompile(`{{-?[^-\s\/]+`) + badTplEnd = regexp.MustCompile(`[^\s-\/]+-?}}`) ) +// validateWhitespaceAroundTemplateDirectives checks for formatting errors on tpl directives +// +// The recommendation is that templates add at least one whitespace character between the +// directive marker ({{ or {{-) and the directive. This enforces that. +// +// See https://github.com/helm/helm/issues/5763 func validateWhitespaceAroundTemplateDirectives(template string) error { badMatches := []string{} if matches := badTplStart.FindAllString(template, 10); matches != nil { diff --git a/pkg/lint/rules/template_test.go b/pkg/lint/rules/template_test.go index ff2d174e6..f466b1fa0 100644 --- a/pkg/lint/rules/template_test.go +++ b/pkg/lint/rules/template_test.go @@ -244,6 +244,7 @@ func TestValidateWhitespaceAroundTemplateDirectives(t *testing.T) { `{{ legal }}{{illegal }}`: false, `{{ legal }}{{- legal -}}`: true, "{{\nlegal\n}}": true, + "{{/* comment */}}": true, } { if err := validateWhitespaceAroundTemplateDirectives(example); (err == nil) != success { st := "failure" diff --git a/pkg/lint/rules/testdata/albatross/templates/_helpers.tpl b/pkg/lint/rules/testdata/albatross/templates/_helpers.tpl index 24f76db73..307e6118c 100644 --- a/pkg/lint/rules/testdata/albatross/templates/_helpers.tpl +++ b/pkg/lint/rules/testdata/albatross/templates/_helpers.tpl @@ -2,7 +2,7 @@ {{/* Expand the name of the chart. */}} -{{define "name"}}{{default "nginx" .Values.nameOverride | trunc 63 | trimSuffix "-" }}{{end}} +{{ define "name" }}{{ default "nginx" .Values.nameOverride | trunc 63 | trimSuffix "-" }}{{ end }} {{/* Create a default fully qualified app name. @@ -10,7 +10,7 @@ Create a default fully qualified app name. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). */}} -{{define "fullname"}} +{{ define "fullname" }} {{- $name := default "nginx" .Values.nameOverride -}} -{{printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} -{{end}} +{{ printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{ end }} diff --git a/pkg/lint/rules/testdata/albatross/templates/svc.yaml b/pkg/lint/rules/testdata/albatross/templates/svc.yaml index 16bb27d55..20ca4bfd7 100644 --- a/pkg/lint/rules/testdata/albatross/templates/svc.yaml +++ b/pkg/lint/rules/testdata/albatross/templates/svc.yaml @@ -7,13 +7,13 @@ metadata: labels: app.kubernetes.io/managed-by: {{ .Release.Service | quote }} app.kubernetes.io/instance: {{ .Release.Name | quote }} - helm.sh/chart: "{{.Chart.Name}}-{{.Chart.Version}}" + helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" kubeVersion: {{ .Capabilities.KubeVersion.Major }} spec: ports: - - port: {{default 80 .Values.httpPort | quote}} + - port: {{ default 80 .Values.httpPort | quote }} targetPort: 80 protocol: TCP name: http selector: - app.kubernetes.io/name: {{template "fullname" .}} + app.kubernetes.io/name: {{ template "fullname" . }}