Update Kong ingress controller to version 1.4. It fixes the occasional 404 error.
[ric-plt/ric-dep.git] / helm / infrastructure / subcharts / kong / templates / _helpers.tpl
1 {{/* vim: set filetype=mustache: */}}
2 {{/*
3 Create a default fully qualified app name.
4 We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
5 */}}
6
7 {{- define "kong.name" -}}
8 {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
9 {{- end -}}
10
11 {{- define "kong.fullname" -}}
12 {{- $name := default .Chart.Name .Values.nameOverride -}}
13 {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
14 {{- end -}}
15
16 {{- define "kong.chart" -}}
17 {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
18 {{- end -}}
19
20 {{- define "kong.metaLabels" -}}
21 app.kubernetes.io/name: {{ template "kong.name" . }}
22 helm.sh/chart: {{ template "kong.chart" . }}
23 app.kubernetes.io/instance: "{{ .Release.Name }}"
24 app.kubernetes.io/managed-by: "{{ .Release.Service }}"
25 app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
26 {{- end -}}
27
28 {{- define "kong.selectorLabels" -}}
29 app.kubernetes.io/name: {{ template "kong.name" . }}
30 app.kubernetes.io/component: app
31 app.kubernetes.io/instance: "{{ .Release.Name }}"
32 {{- end -}}
33
34 {{- define "kong.postgresql.fullname" -}}
35 {{- $name := default "postgresql" .Values.postgresql.nameOverride -}}
36 {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
37 {{- end -}}
38
39 {{- define "kong.dblessConfig.fullname" -}}
40 {{- $name := default "kong-custom-dbless-config" .Values.dblessConfig.nameOverride -}}
41 {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
42 {{- end -}}
43
44 {{/*
45 Create the name of the service account to use
46 */}}
47 {{- define "kong.serviceAccountName" -}}
48 {{- if .Values.ingressController.serviceAccount.create -}}
49     {{ default (include "kong.fullname" .) .Values.ingressController.serviceAccount.name }}
50 {{- else -}}
51     {{ default "default" .Values.ingressController.serviceAccount.name }}
52 {{- end -}}
53 {{- end -}}
54
55 {{/*
56 Create the KONG_PROXY_LISTEN value string
57 */}}
58 {{- define "kong.kongProxyListenValue" -}}
59
60 {{- if and .Values.proxy.http.enabled .Values.proxy.tls.enabled -}}
61    0.0.0.0:{{ .Values.proxy.http.containerPort }},0.0.0.0:{{ .Values.proxy.tls.containerPort }} ssl
62 {{- else -}}
63 {{- if .Values.proxy.http.enabled -}}
64    0.0.0.0:{{ .Values.proxy.http.containerPort }}
65 {{- end -}}
66 {{- if .Values.proxy.tls.enabled -}}
67    0.0.0.0:{{ .Values.proxy.tls.containerPort }} ssl
68 {{- end -}}
69 {{- end -}}
70
71 {{- end }}
72
73 {{/*
74 Create the KONG_ADMIN_GUI_LISTEN value string
75 */}}
76 {{- define "kong.kongManagerListenValue" -}}
77
78 {{- if and .Values.manager.http.enabled .Values.manager.tls.enabled -}}
79    0.0.0.0:{{ .Values.manager.http.containerPort }},0.0.0.0:{{ .Values.manager.tls.containerPort }} ssl
80 {{- else -}}
81 {{- if .Values.manager.http.enabled -}}
82    0.0.0.0:{{ .Values.manager.http.containerPort }}
83 {{- end -}}
84 {{- if .Values.manager.tls.enabled -}}
85    0.0.0.0:{{ .Values.manager.tls.containerPort }} ssl
86 {{- end -}}
87 {{- end -}}
88
89 {{- end }}
90
91 {{/*
92 Create the KONG_PORTAL_GUI_LISTEN value string
93 */}}
94 {{- define "kong.kongPortalListenValue" -}}
95
96 {{- if and .Values.portal.http.enabled .Values.portal.tls.enabled -}}
97    0.0.0.0:{{ .Values.portal.http.containerPort }},0.0.0.0:{{ .Values.portal.tls.containerPort }} ssl
98 {{- else -}}
99 {{- if .Values.portal.http.enabled -}}
100    0.0.0.0:{{ .Values.portal.http.containerPort }}
101 {{- end -}}
102 {{- if .Values.portal.tls.enabled -}}
103    0.0.0.0:{{ .Values.portal.tls.containerPort }} ssl
104 {{- end -}}
105 {{- end -}}
106
107 {{- end }}
108
109 {{/*
110 Create the KONG_PORTAL_API_LISTEN value string
111 */}}
112 {{- define "kong.kongPortalApiListenValue" -}}
113
114 {{- if and .Values.portalapi.http.enabled .Values.portalapi.tls.enabled -}}
115    0.0.0.0:{{ .Values.portalapi.http.containerPort }},0.0.0.0:{{ .Values.portalapi.tls.containerPort }} ssl
116 {{- else -}}
117 {{- if .Values.portalapi.http.enabled -}}
118    0.0.0.0:{{ .Values.portalapi.http.containerPort }}
119 {{- end -}}
120 {{- if .Values.portalapi.tls.enabled -}}
121    0.0.0.0:{{ .Values.portalapi.tls.containerPort }} ssl
122 {{- end -}}
123 {{- end -}}
124
125 {{- end }}
126
127 {{/*
128 Create the ingress servicePort value string
129 */}}
130
131 {{- define "kong.ingress.servicePort" -}}
132 {{- if .tls.enabled -}}
133    {{ .tls.servicePort }}
134 {{- else -}}
135    {{ .http.servicePort }}
136 {{- end -}}
137 {{- end -}}
138
139 {{/*
140 Generate an appropriate external URL from a Kong service's ingress configuration
141 Strips trailing slashes from the path. Manager at least does not handle these
142 intelligently and will append its own slash regardless, and the admin API cannot handle
143 the extra slash.
144 */}}
145
146 {{- define "kong.ingress.serviceUrl" -}}
147 {{- if .tls -}}
148     https://{{ .hostname }}{{ .path | trimSuffix "/" }}
149 {{- else -}}
150     http://{{ .hostname }}{{ .path | trimSuffix "/" }}
151 {{- end -}}
152 {{- end -}}
153
154 {{/*
155 The name of the service used for the ingress controller's validation webhook
156 */}}
157
158 {{- define "kong.service.validationWebhook" -}}
159 {{ include "kong.fullname" . }}-validation-webhook
160 {{- end -}}
161
162 {{- define "kong.env" -}}
163 {{- range $key, $val := .Values.env }}
164 - name: KONG_{{ $key | upper}}
165 {{- $valueType := printf "%T" $val -}}
166 {{ if eq $valueType "map[string]interface {}" }}
167 {{ toYaml $val | indent 2 -}}
168 {{- else }}
169   value: {{ $val | quote -}}
170 {{- end -}}
171 {{- end -}}
172 {{- end -}}
173
174 {{- define "kong.ingressController.env" -}}
175 {{- range $key, $val := .Values.ingressController.env }}
176 - name: CONTROLLER_{{ $key | upper}}
177 {{- $valueType := printf "%T" $val -}}
178 {{ if eq $valueType "map[string]interface {}" }}
179 {{ toYaml $val | indent 2 -}}
180 {{- else }}
181   value: {{ $val | quote -}}
182 {{- end -}}
183 {{- end -}}
184 {{- end -}}
185
186 {{- define "kong.volumes" -}}
187 - name: {{ template "kong.fullname" . }}-prefix-dir
188   emptyDir: {}
189 - name: {{ template "kong.fullname" . }}-tmp
190   emptyDir: {}
191 {{- range .Values.plugins.configMaps }}
192 - name: kong-plugin-{{ .pluginName }}
193   configMap:
194     name: {{ .name }}
195 {{- end }}
196 {{- range .Values.plugins.secrets }}
197 - name: kong-plugin-{{ .pluginName }}
198   secret:
199     secretName: {{ .name }}
200 {{- end }}
201 - name: custom-nginx-template-volume
202   configMap:
203     name: {{ template "kong.fullname" . }}-default-custom-server-blocks
204 {{- if (and (not .Values.ingressController.enabled) (eq .Values.env.database "off")) }}
205 - name: kong-custom-dbless-config-volume
206   configMap:
207     {{- if .Values.dblessConfig.configMap }}
208     name: {{ .Values.dblessConfig.configMap }}
209     {{- else }}
210     name: {{ template "kong.dblessConfig.fullname" . }}
211     {{- end }}
212 {{- end }}
213 {{- if .Values.ingressController.admissionWebhook.enabled }}
214 - name: webhook-cert
215   secret:
216     secretName: {{ template "kong.fullname" . }}-validation-webhook-keypair
217 {{- end }}
218 {{- range $secretVolume := .Values.secretVolumes }}
219 - name: {{ . }}
220   secret:
221     secretName: {{ . }}
222 {{- end }}
223 {{- end -}}
224
225 {{- define "kong.volumeMounts" -}}
226 - name: {{ template "kong.fullname" . }}-prefix-dir
227   mountPath: /kong_prefix/
228 - name: {{ template "kong.fullname" . }}-tmp
229   mountPath: /tmp
230 - name: custom-nginx-template-volume
231   mountPath: /kong
232 {{- if (and (not .Values.ingressController.enabled) (eq .Values.env.database "off")) }}
233 - name: kong-custom-dbless-config-volume
234   mountPath: /kong_dbless/
235 {{- end }}
236 {{- range .Values.secretVolumes }}
237 - name:  {{ . }}
238   mountPath: /etc/secrets/{{ . }}
239 {{- end }}
240 {{- range .Values.plugins.configMaps }}
241 - name:  kong-plugin-{{ .pluginName }}
242   mountPath: /opt/kong/plugins/{{ .pluginName }}
243   readOnly: true
244 {{- end }}
245 {{- range .Values.plugins.secrets }}
246 - name:  kong-plugin-{{ .pluginName }}
247   mountPath: /opt/kong/plugins/{{ .pluginName }}
248   readOnly: true
249 {{- end }}
250 {{- end -}}
251
252 {{- define "kong.plugins" -}}
253 {{ $myList := list "bundled" }}
254 {{- range .Values.plugins.configMaps -}}
255 {{- $myList = append $myList .pluginName -}}
256 {{- end -}}
257 {{- range .Values.plugins.secrets -}}
258   {{ $myList = append $myList .pluginName -}}
259 {{- end }}
260 {{- $myList | join "," -}}
261 {{- end -}}
262
263 {{- define "kong.wait-for-db" -}}
264 - name: wait-for-db
265   image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
266   imagePullPolicy: {{ .Values.image.pullPolicy }}
267   env:
268   {{- if .Values.enterprise.enabled }}
269   {{- include "kong.license" . | nindent 2 }}
270   {{- end }}
271   {{- if .Values.postgresql.enabled }}
272   - name: KONG_PG_HOST
273     value: {{ template "kong.postgresql.fullname" . }}
274   - name: KONG_PG_PORT
275     value: "{{ .Values.postgresql.service.port }}"
276   - name: KONG_PG_PASSWORD
277     valueFrom:
278       secretKeyRef:
279         name: {{ template "kong.postgresql.fullname" . }}
280         key: postgresql-password
281   {{- end }}
282   - name: KONG_LUA_PACKAGE_PATH
283     value: "/opt/?.lua;;"
284   - name: KONG_PLUGINS
285     value: {{ template "kong.plugins" . }}
286   {{- include "kong.env" .  | nindent 2 }}
287   command: [ "/bin/sh", "-c", "until kong start; do echo 'waiting for db'; sleep 1; done; kong stop" ]
288   volumeMounts:
289   {{- include "kong.volumeMounts" . | nindent 4 }}
290 {{- end -}}
291
292 {{- define "kong.controller-container" -}}
293 - name: ingress-controller
294   args:
295   - /kong-ingress-controller
296   # Service from were we extract the IP address/es to use in Ingress status
297   - --publish-service={{ .Release.Namespace }}/{{ template "kong.fullname" . }}-proxy
298   # Set the ingress class
299   - --ingress-class={{ .Values.ingressController.ingressClass }}
300   - --election-id=kong-ingress-controller-leader-{{ .Values.ingressController.ingressClass }}
301   # the kong URL points to the kong admin api server
302   {{- if .Values.admin.useTLS }}
303   - --kong-url=https://localhost:{{ .Values.admin.containerPort }}
304   - --admin-tls-skip-verify # TODO make this configurable
305   {{- else }}
306   - --kong-url=http://localhost:{{ .Values.admin.containerPort }}
307   {{- end }}
308   {{- if .Values.ingressController.admissionWebhook.enabled }}
309   - --admission-webhook-listen=0.0.0.0:{{ .Values.ingressController.admissionWebhook.port }}
310   {{- end }}
311   env:
312   - name: POD_NAME
313     valueFrom:
314       fieldRef:
315         apiVersion: v1
316         fieldPath: metadata.name
317   - name: POD_NAMESPACE
318     valueFrom:
319       fieldRef:
320         apiVersion: v1
321         fieldPath: metadata.namespace
322 {{- include "kong.ingressController.env" .  | indent 2 }}
323   image: "{{ .Values.ingressController.image.repository }}:{{ .Values.ingressController.image.tag }}"
324   imagePullPolicy: {{ .Values.image.pullPolicy }}
325   readinessProbe:
326 {{ toYaml .Values.ingressController.readinessProbe | indent 4 }}
327   livenessProbe:
328 {{ toYaml .Values.ingressController.livenessProbe | indent 4 }}
329   resources:
330 {{ toYaml .Values.ingressController.resources | indent 4 }}
331 {{- if .Values.ingressController.admissionWebhook.enabled }}
332   volumeMounts:
333   - name: webhook-cert
334     mountPath: /admission-webhook
335     readOnly: true
336 {{- end }}
337 {{- end -}}
338
339 {{/*
340 Retrieve Kong Enterprise license from a secret and make it available in env vars
341 */}}
342 {{- define "kong.license" -}}
343 - name: KONG_LICENSE_DATA
344   valueFrom:
345     secretKeyRef:
346       name: {{ .Values.enterprise.license_secret }}
347       key: license
348 {{- end -}}
349
350 {{/*
351 Use the Pod security context defined in Values or set the UID by default
352 */}}
353 {{- define "kong.podsecuritycontext" -}}
354 {{ .Values.securityContext | toYaml }}
355 {{- end -}}
356
357 {{/*
358 The environment values passed to Kong; this should come after all
359 the template that it itself is using form the above sections.
360 */}}
361 {{- define "kong.final_env" -}}
362 - name: KONG_LUA_PACKAGE_PATH
363   value: "/opt/?.lua;;"
364 {{- if not .Values.env.admin_listen }}
365 {{- if .Values.admin.useTLS }}
366 - name: KONG_ADMIN_LISTEN
367   value: "0.0.0.0:{{ .Values.admin.containerPort }} ssl"
368 {{- else }}
369 - name: KONG_ADMIN_LISTEN
370   value: 0.0.0.0:{{ .Values.admin.containerPort }}
371 {{- end }}
372 {{- end }}
373 {{- if .Values.admin.ingress.enabled }}
374 - name: KONG_ADMIN_API_URI
375   value: {{ include "kong.ingress.serviceUrl" .Values.admin.ingress }}
376 {{- end }}
377 {{- if not .Values.env.proxy_listen }}
378 - name: KONG_PROXY_LISTEN
379   value: {{ template "kong.kongProxyListenValue" . }}
380 {{- end }}
381 {{- if and (not .Values.env.admin_gui_listen) (.Values.enterprise.enabled) }}
382 - name: KONG_ADMIN_GUI_LISTEN
383   value: {{ template "kong.kongManagerListenValue" . }}
384 {{- end }}
385 {{- if and (.Values.manager.ingress.enabled) (.Values.enterprise.enabled) }}
386 - name: KONG_ADMIN_GUI_URL
387   value: {{ include "kong.ingress.serviceUrl" .Values.manager.ingress }}
388 {{- end }}
389 {{- if and (not .Values.env.portal_gui_listen) (.Values.enterprise.enabled) (.Values.enterprise.portal.enabled) }}
390 - name: KONG_PORTAL_GUI_LISTEN
391   value: {{ template "kong.kongPortalListenValue" . }}
392 {{- end }}
393 {{- if and (.Values.portal.ingress.enabled) (.Values.enterprise.enabled) (.Values.enterprise.portal.enabled) }}
394 - name: KONG_PORTAL_GUI_HOST
395   value: {{ .Values.portal.ingress.hostname }}
396 {{- if .Values.portal.ingress.tls }}
397 - name: KONG_PORTAL_GUI_PROTOCOL
398   value: https
399 {{- else }}
400 - name: KONG_PORTAL_GUI_PROTOCOL
401   value: http
402 {{- end }}
403 {{- end }}
404 {{- if and (not .Values.env.portal_api_listen) (.Values.enterprise.enabled) (.Values.enterprise.portal.enabled) }}
405 - name: KONG_PORTAL_API_LISTEN
406   value: {{ template "kong.kongPortalApiListenValue" . }}
407 {{- end }}
408 {{- if and (.Values.portalapi.ingress.enabled) (.Values.enterprise.enabled) (.Values.enterprise.portal.enabled) }}
409 - name: KONG_PORTAL_API_URL
410   value: {{ include "kong.ingress.serviceUrl" .Values.portalapi.ingress }}
411 {{- end }}
412 - name: KONG_NGINX_DAEMON
413   value: "off"
414 {{- if .Values.enterprise.enabled }}
415 {{- if not .Values.enterprise.vitals.enabled }}
416 - name: KONG_VITALS
417   value: "off"
418 {{- end }}
419 {{- if .Values.enterprise.portal.enabled }}
420 - name: KONG_PORTAL
421   value: "on"
422 {{- if .Values.enterprise.portal.portal_auth }}
423 - name: KONG_PORTAL_AUTH
424   value: {{ .Values.enterprise.portal.portal_auth }}
425 - name: KONG_PORTAL_SESSION_CONF
426   valueFrom:
427     secretKeyRef:
428       name: {{ .Values.enterprise.portal.session_conf_secret }}
429       key: portal_session_conf
430 {{- end }}
431 {{- end }}
432 {{- if .Values.enterprise.rbac.enabled }}
433 - name: KONG_ENFORCE_RBAC
434   value: "on"
435 - name: KONG_ADMIN_GUI_AUTH
436   value: {{ .Values.enterprise.rbac.admin_gui_auth | default "basic-auth" }}
437 {{- if not (eq .Values.enterprise.rbac.admin_gui_auth "basic-auth") }}
438 - name: KONG_ADMIN_GUI_AUTH_CONF
439   valueFrom:
440     secretKeyRef:
441       name: {{ .Values.enterprise.rbac.admin_gui_auth_conf_secret }}
442       key: admin_gui_auth_conf
443 {{- end }}
444 - name: KONG_ADMIN_GUI_SESSION_CONF
445   valueFrom:
446     secretKeyRef:
447       name: {{ .Values.enterprise.rbac.session_conf_secret }}
448       key: admin_gui_session_conf
449 {{- end }}
450 {{- if .Values.enterprise.smtp.enabled }}
451 - name: KONG_PORTAL_EMAILS_FROM
452   value: {{ .Values.enterprise.smtp.portal_emails_from }}
453 - name: KONG_PORTAL_EMAILS_REPLY_TO
454   value: {{ .Values.enterprise.smtp.portal_emails_reply_to }}
455 - name: KONG_ADMIN_EMAILS_FROM
456   value: {{ .Values.enterprise.smtp.admin_emails_from }}
457 - name: KONG_ADMIN_EMAILS_REPLY_TO
458   value: {{ .Values.enterprise.smtp.admin_emails_reply_to }}
459 - name: KONG_SMTP_HOST
460   value: {{ .Values.enterprise.smtp.smtp_host }}
461 - name: KONG_SMTP_PORT
462   value: {{ .Values.enterprise.smtp.smtp_port | quote }}
463 - name: KONG_SMTP_STARTTLS
464   value: {{ .Values.enterprise.smtp.smtp_starttls | quote }}
465 {{- if .Values.enterprise.smtp.auth.smtp_username }}
466 - name: KONG_SMTP_USERNAME
467   value: {{ .Values.enterprise.smtp.auth.smtp_username }}
468 - name: KONG_SMTP_PASSWORD
469   valueFrom:
470     secretKeyRef:
471       name: {{ .Values.enterprise.smtp.auth.smtp_password_secret }}
472       key: smtp_password
473 {{- end }}
474 {{- else }}
475 - name: KONG_SMTP_MOCK
476   value: "on"
477 {{- end }}
478 {{ include "kong.license" . }}
479 {{- end }}
480 - name: KONG_NGINX_HTTP_INCLUDE
481   value: /kong/servers.conf
482 {{- if .Values.postgresql.enabled }}
483 - name: KONG_PG_HOST
484   value: {{ template "kong.postgresql.fullname" . }}
485 - name: KONG_PG_PORT
486   value: "{{ .Values.postgresql.service.port }}"
487 - name: KONG_PG_PASSWORD
488   valueFrom:
489     secretKeyRef:
490       name: {{ template "kong.postgresql.fullname" . }}
491       key: postgresql-password
492 {{- end }}
493 {{- if (and (not .Values.ingressController.enabled) (eq .Values.env.database "off")) }}
494 - name: KONG_DECLARATIVE_CONFIG
495   value: "/kong_dbless/kong.yml"
496 {{- end }}
497 - name: KONG_PLUGINS
498   value: {{ template "kong.plugins" . }}
499 {{- include "kong.env" . }}
500 {{- end -}}
501
502 {{- define "kong.wait-for-postgres" -}}
503 - name: wait-for-postgres
504   image: "{{ .Values.waitImage.repository }}:{{ .Values.waitImage.tag }}"
505   imagePullPolicy: {{ .Values.waitImage.pullPolicy }}
506   env:
507   {{- include "kong.final_env" . | nindent 2 }}
508   command: [ "/bin/sh", "-c", "until nc -zv $KONG_PG_HOST $KONG_PG_PORT -w1; do echo 'waiting for db'; sleep 1; done" ]
509 {{- end -}}