[common] Add ingress unittest and tpl functionality (#568)

* Add support for tpl in ingress handling
This commit is contained in:
Mikael Sennerholm 2021-02-11 18:59:30 +01:00 committed by GitHub
parent 1c7d6d403e
commit 11ef99d20a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 148 additions and 2 deletions

View File

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

View File

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

View File

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

View File

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

View File

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