diff --git a/charts/common/Chart.yaml b/charts/common/Chart.yaml index 48b9015d..72d30844 100644 --- a/charts/common/Chart.yaml +++ b/charts/common/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 name: common description: Function library for k8s-at-home charts type: library -version: 2.4.0 +version: 2.5.0 keywords: - k8s-at-home - common diff --git a/charts/common/templates/_notes.tpl b/charts/common/templates/_notes.tpl index 07904900..4cba1996 100644 --- a/charts/common/templates/_notes.tpl +++ b/charts/common/templates/_notes.tpl @@ -6,7 +6,7 @@ Default NOTES.txt content. 1. Get the application URL by running these commands: {{- if .Values.ingress.enabled }} {{- range .Values.ingress.hosts }} - http{{ if $.Values.ingress.tls }}s{{ end }}://{{ .host }}{{ (first .paths).path }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{- if .hostTpl }}{{ tpl .hostTpl $ }}{{ else }}{{ .host }}{{ end }}{{ (first .paths).path }} {{- end }} {{- else if contains "NodePort" .Values.service.type }} export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "common.names.fullname" . }}) diff --git a/charts/common/templates/classes/_ingress.tpl b/charts/common/templates/classes/_ingress.tpl index 713d3a3b..4f641c25 100644 --- a/charts/common/templates/classes/_ingress.tpl +++ b/charts/common/templates/classes/_ingress.tpl @@ -38,12 +38,23 @@ spec: {{- range .hosts }} - {{ . | quote }} {{- end }} + {{- range .hostsTpl }} + - {{ tpl . $ | quote }} + {{- end }} + {{- if .secretNameTpl }} + secretName: {{ tpl .secretNameTpl $ | quote}} + {{- else }} secretName: {{ .secretName }} + {{- end }} {{- end }} {{- end }} rules: {{- range $values.hosts }} + {{- if .hostTpl }} + - host: {{ tpl .hostTpl $ | quote }} + {{- else }} - host: {{ .host | quote }} + {{- end }} http: paths: {{- range .paths }} diff --git a/charts/common/values.yaml b/charts/common/values.yaml index ceb206e2..10df27c8 100644 --- a/charts/common/values.yaml +++ b/charts/common/values.yaml @@ -178,14 +178,21 @@ ingress: labels: {} hosts: - host: chart-example.local + ## Or a tpl that is evaluated + # hostTpl: '{{ include "common.names.fullname" . }}.{{ .Release.Namespace }}.{{ .Values.ingress.domainname }}' paths: - path: / # Ignored if not kubeVersion >= 1.14-0 pathType: Prefix tls: [] # - secretName: chart-example-tls + ## Or if you need a dynamic secretname + # - secretNameTpl: '{{ include "common.names.fullname" . }}-ingress' # hosts: # - chart-example.local + ## Or a tpl that is evaluated + # hostTpl: + # - '{{ include "common.names.fullname" . }}.{{ .Release.Namespace }}.{{ .Values.ingress.domainname }}' additionalIngresses: [] # - enabled: false # nameSuffix: "api" diff --git a/test/charts/common-test_spec.rb b/test/charts/common-test_spec.rb index 1afebff5..50caf750 100644 --- a/test/charts/common-test_spec.rb +++ b/test/charts/common-test_spec.rb @@ -186,5 +186,133 @@ class Test < ChartTest jq('.spec.volumeClaimTemplates[0].spec.storageClassName', resource('StatefulSet')).must_equal values[:volumeClaimTemplates][0][:storageClass] end end + + describe 'ingress' do + it 'should be disabled when ingress.enabled: false' do + values = { + ingress: { + enabled: false + } + } + chart.value values + assert_nil(resource('Ingress')) + end + + it 'should be enabled when ingress.enabled: true' do + values = { + ingress: { + enabled: true + } + } + + chart.value values + refute_nil(resource('Ingress')) + end + + it 'ingress with hosts' do + values = { + ingress: { + hosts: [ + { + host: 'hostname', + paths: [ + { + path: '/' + } + ] + } + ] + } + } + + chart.value values + jq('.spec.rules[0].host', resource('Ingress')).must_equal values[:ingress][:hosts][0][:host] + jq('.spec.rules[0].http.paths[0].path', resource('Ingress')).must_equal values[:ingress][:hosts][0][:paths][0][:path] + end + + it 'ingress with hosts template is evaluated' do + expectedHostName = 'common-test.hostname' + values = { + ingress: { + hosts: [ + { + hostTpl: '{{ .Release.Name }}.hostname', + paths: [ + { + path: '/' + } + ] + } + ] + } + } + + chart.value values + jq('.spec.rules[0].host', resource('Ingress')).must_equal expectedHostName + jq('.spec.rules[0].http.paths[0].path', resource('Ingress')).must_equal values[:ingress][:hosts][0][:paths][0][:path] + end + + it 'ingress with hosts and tls' do + values = { + ingress: { + enabled: true, + hosts: [ + { + host: 'hostname', + paths: [ + { + path: '/' + } + ] + } + ], + tls: [ + { + hosts: [ 'hostname' ], + secretName: 'hostname-secret-name' + } + ] + } + } + + chart.value values + jq('.spec.rules[0].host', resource('Ingress')).must_equal values[:ingress][:hosts][0][:host] + jq('.spec.rules[0].http.paths[0].path', resource('Ingress')).must_equal values[:ingress][:hosts][0][:paths][0][:path] + jq('.spec.tls[0].hosts[0]', resource('Ingress')).must_equal values[:ingress][:tls][0][:hosts][0] + jq('.spec.tls[0].secretName', resource('Ingress')).must_equal values[:ingress][:tls][0][:secretName] + end + + it 'ingress with hosts and tls templates is evaluated' do + expectedHostName = 'common-test.hostname' + expectedSecretName = 'common-test-hostname-secret-name' + values = { + ingress: { + enabled: true, + hosts: [ + { + hostTpl: '{{ .Release.Name }}.hostname', + paths: [ + { + path: '/' + } + ] + } + ], + tls: [ + { + hostsTpl: [ '{{ .Release.Name }}.hostname' ], + secretNameTpl: '{{ .Release.Name }}-hostname-secret-name' + } + ] + } + } + + chart.value values + jq('.spec.rules[0].host', resource('Ingress')).must_equal expectedHostName + jq('.spec.rules[0].http.paths[0].path', resource('Ingress')).must_equal values[:ingress][:hosts][0][:paths][0][:path] + jq('.spec.tls[0].hosts[0]', resource('Ingress')).must_equal expectedHostName + jq('.spec.tls[0].secretName', resource('Ingress')).must_equal expectedSecretName + end + end end end