diff --git a/cmd/helm/template_test.go b/cmd/helm/template_test.go index f8cd31347..3d27baa99 100644 --- a/cmd/helm/template_test.go +++ b/cmd/helm/template_test.go @@ -94,6 +94,11 @@ func TestTemplateCmd(t *testing.T) { cmd: fmt.Sprintf("template '%s' --show-only templates/service.yaml --show-only charts/subcharta/templates/service.yaml", chartPath), golden: "output/template-show-only-multiple.txt", }, + { + name: "check chart with multiple dependencies that share global value", + cmd: fmt.Sprintf("template '%s' --set global.ingress.annotations.test=global", "testdata/testcharts/chart-with-subchart-global"), + golden: "output/template-with-subchart-global-value.txt", + }, { name: "template with show-only glob", cmd: fmt.Sprintf("template '%s' --show-only templates/subdir/role*", chartPath), diff --git a/cmd/helm/testdata/output/template-with-subchart-global-value.txt b/cmd/helm/testdata/output/template-with-subchart-global-value.txt new file mode 100644 index 000000000..395703614 --- /dev/null +++ b/cmd/helm/testdata/output/template-with-subchart-global-value.txt @@ -0,0 +1,48 @@ +--- +# Source: chart-with-subchart-global/charts/dep1/templates/ingress.yaml +apiVersion: networking.k8s.io/v1beta1 +kind: Ingress +metadata: + name: RELEASE-NAME-dep1 + labels: + helm.sh/chart: dep1-0.1.0 + app.kubernetes.io/name: dep1 + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "1.16.0" + app.kubernetes.io/managed-by: Helm + annotations: + some-annotation: dep1 + test: global +spec: + rules: + - host: "localhost1" + http: + paths: + - path: / + backend: + serviceName: RELEASE-NAME-dep1 + servicePort: 80 +--- +# Source: chart-with-subchart-global/charts/dep2/templates/ingress.yaml +apiVersion: networking.k8s.io/v1beta1 +kind: Ingress +metadata: + name: RELEASE-NAME-dep2 + labels: + helm.sh/chart: dep2-0.1.0 + app.kubernetes.io/name: dep2 + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/version: "1.16.0" + app.kubernetes.io/managed-by: Helm + annotations: + some-annotation: dep2 + test: global +spec: + rules: + - host: "localhost2" + http: + paths: + - path: / + backend: + serviceName: RELEASE-NAME-dep2 + servicePort: 80 diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/Chart.lock b/cmd/helm/testdata/testcharts/chart-with-subchart-global/Chart.lock new file mode 100644 index 000000000..ddd8169de --- /dev/null +++ b/cmd/helm/testdata/testcharts/chart-with-subchart-global/Chart.lock @@ -0,0 +1,9 @@ +dependencies: +- name: dep1 + repository: file://../dep1 + version: 0.1.0 +- name: dep2 + repository: file://../dep2 + version: 0.1.0 +digest: sha256:3278a7071cc4f6cecc966ec807fd378018d2f7f07f5f136b538596c1d9066c51 +generated: "2020-08-03T18:59:45.008862+03:00" diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/Chart.yaml b/cmd/helm/testdata/testcharts/chart-with-subchart-global/Chart.yaml new file mode 100644 index 000000000..9a4b93203 --- /dev/null +++ b/cmd/helm/testdata/testcharts/chart-with-subchart-global/Chart.yaml @@ -0,0 +1,15 @@ +apiVersion: v2 +name: chart-with-subchart-global +description: A Helm chart for Kubernetes +type: application +version: 0.1.0 +appVersion: 1.16.0 + +dependencies: + - name: dep1 + version: 0.1.0 + repository: file://./dep1 + - name: dep2 + version: 0.1.0 + repository: file://./dep2 + diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/charts/dep1-0.1.0.tgz b/cmd/helm/testdata/testcharts/chart-with-subchart-global/charts/dep1-0.1.0.tgz new file mode 100644 index 000000000..d870e96cc Binary files /dev/null and b/cmd/helm/testdata/testcharts/chart-with-subchart-global/charts/dep1-0.1.0.tgz differ diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/charts/dep2-0.1.0.tgz b/cmd/helm/testdata/testcharts/chart-with-subchart-global/charts/dep2-0.1.0.tgz new file mode 100644 index 000000000..d79bd2387 Binary files /dev/null and b/cmd/helm/testdata/testcharts/chart-with-subchart-global/charts/dep2-0.1.0.tgz differ diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep1/Chart.yaml b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep1/Chart.yaml new file mode 100644 index 000000000..fbdd3e7dc --- /dev/null +++ b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep1/Chart.yaml @@ -0,0 +1,23 @@ +apiVersion: v2 +name: dep1 +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +appVersion: 1.16.0 diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep1/templates/ingress.yaml b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep1/templates/ingress.yaml new file mode 100644 index 000000000..d22c917bd --- /dev/null +++ b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep1/templates/ingress.yaml @@ -0,0 +1,42 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "dep1.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "dep1.labels" . | nindent 4 }} +{{/* // Here I'm try to merge global & unique for chart annotations*/}} + {{- with merge .Values.global.ingress.annotations .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ . }} + backend: + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep1/values.yaml b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep1/values.yaml new file mode 100644 index 000000000..41bd48bb5 --- /dev/null +++ b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep1/values.yaml @@ -0,0 +1,23 @@ + + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: true + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: [] + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep2/Chart.yaml b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep2/Chart.yaml new file mode 100644 index 000000000..2bd659297 --- /dev/null +++ b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep2/Chart.yaml @@ -0,0 +1,23 @@ +apiVersion: v2 +name: dep2 +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +appVersion: 1.16.0 diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep2/templates/ingress.yaml b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep2/templates/ingress.yaml new file mode 100644 index 000000000..2dce86651 --- /dev/null +++ b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep2/templates/ingress.yaml @@ -0,0 +1,42 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "dep2.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "dep2.labels" . | nindent 4 }} + {{/* // Here I'm try to merge global & unique for chart annotations*/}} + {{- with merge .Values.global.ingress.annotations .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ . }} + backend: + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep2/values.yaml b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep2/values.yaml new file mode 100644 index 000000000..116b40067 --- /dev/null +++ b/cmd/helm/testdata/testcharts/chart-with-subchart-global/dep2/values.yaml @@ -0,0 +1,24 @@ + + + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: true + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: [] + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local diff --git a/cmd/helm/testdata/testcharts/chart-with-subchart-global/values.yaml b/cmd/helm/testdata/testcharts/chart-with-subchart-global/values.yaml new file mode 100644 index 000000000..c80094219 --- /dev/null +++ b/cmd/helm/testdata/testcharts/chart-with-subchart-global/values.yaml @@ -0,0 +1,22 @@ +global: + ingress: + annotations: {} + +dep1: + ingress: + enabled: true + annotations: + "some-annotation": "dep1" + hosts: + - host: localhost1 + paths: + - / +dep2: + ingress: + enabled: true + annotations: + "some-annotation": "dep2" + hosts: + - host: localhost2 + paths: + - / diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 5aa0ed8ec..a581a4d33 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -26,6 +26,7 @@ import ( "strings" "text/template" + "github.com/mitchellh/copystructure" "github.com/pkg/errors" "k8s.io/client-go/rest" @@ -244,7 +245,11 @@ func (e Engine) renderWithReferences(tpls, referenceTpls map[string]renderable) continue } // At render time, add information about the template that is being rendered. - vals := tpls[filename].vals + structureCopy, err := copystructure.Copy(tpls[filename].vals) + if err != nil { + return map[string]string{}, err + } + vals := structureCopy.(chartutil.Values) vals["Template"] = chartutil.Values{"Name": filename, "BasePath": tpls[filename].basePath} var buf strings.Builder if err := t.ExecuteTemplate(&buf, filename, vals); err != nil {