30ef02295f4f2bf4531299f37ff2c9be210a9c1f
[oom.git] / kubernetes / common / common / templates / _ingress.tpl
1 {{/*
2 # Copyright © 2019-2021 Orange, Samsung
3 # Copyright © 2022 Deutsche Telekom
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 */}}
17 {{/*
18   Helper function to check, if Ingress is globally enabled
19 */}}
20 {{- define "common.ingressEnabled" -}}
21 {{-   $dot := default . .dot -}}
22 {{-   if $dot.Values.ingress -}}
23 {{-     if $dot.Values.global.ingress -}}
24 {{-       if (default false $dot.Values.global.ingress.enabled) -}}
25 true
26 {{-       end -}}
27 {{-     end -}}
28 {{-   end -}}
29 {{- end -}}
30
31
32 {{/*
33   Create the hostname as concatination <baseaddr>.<baseurl>
34   - baseaddr: from component values: ingress.service.baseaddr
35   - baseurl: from values: global.ingress.virtualhost.baseurl
36     which van be overwritten in the component via: ingress.baseurlOverride
37 */}}
38 {{- define "ingress.config.host" -}}
39 {{-   $dot := default . .dot -}}
40 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
41 {{-   $preaddr := default "" $dot.Values.global.ingress.virtualhost.preaddr -}}
42 {{-   $preaddr := include "common.ingress._overrideIfDefined" (dict "currVal" $preaddr "parent" (default (dict) $dot.Values.ingress) "var" "preaddrOverride") -}}
43 {{-   $postaddr := default "" $dot.Values.global.ingress.virtualhost.postaddr -}}
44 {{-   $postaddr := include "common.ingress._overrideIfDefined" (dict "currVal" $postaddr "parent" (default (dict) $dot.Values.ingress) "var" "postaddrOverride") -}}
45 {{-   $burl := (required "'baseurl' param, set to the generic part of the fqdn, is required." $dot.Values.global.ingress.virtualhost.baseurl) -}}
46 {{-   $burl := include "common.ingress._overrideIfDefined" (dict "currVal" $burl "parent" (default (dict) $dot.Values.ingress) "var" "baseurlOverride") -}}
47 {{ printf "%s%s%s.%s" $preaddr $baseaddr $postaddr $burl }}
48 {{- end -}}
49
50 {{/*
51   Istio Helper function to add the tls route
52 */}}
53 {{- define "istio.config.tls_simple" -}}
54 {{-   $dot := default . .dot -}}
55     tls:
56 {{-   if $dot.Values.global.ingress.config }}
57 {{-     if $dot.Values.global.ingress.config.tls }}
58       credentialName: {{ default "ingress-tls-secret" $dot.Values.global.ingress.config.tls.secret }}
59 {{-     else }}
60       credentialName: "ingress-tls-secret"
61 {{-     end }}
62 {{-   else }}
63       credentialName: "ingress-tls-secret"
64 {{-   end }}
65       mode: SIMPLE
66 {{- end -}}
67
68 {{/*
69   Istio Helper function to add the tls route
70 */}}
71 {{- define "istio.config.tls" -}}
72 {{-   $dot := default . .dot -}}
73 {{-   $service := (required "'service' param, set to the specific service, is required." .service) -}}
74 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
75 {{-   if $service.exposedPort }}
76 {{-     if $service.exposedProtocol }}
77 {{-       if eq $service.exposedProtocol "TLS" }}
78     {{ include "istio.config.tls_simple" (dict "dot" $dot ) }}
79 {{-       end }}
80 {{-     end }}
81 {{-   else }}
82 {{-     if $dot.Values.global.ingress.config }}
83 {{-       if $dot.Values.global.ingress.config.ssl }}
84 {{-         if eq $dot.Values.global.ingress.config.ssl "redirect" }}
85     tls:
86       httpsRedirect: true
87   - port:
88       number: 443
89       name: https
90       protocol: HTTPS
91     {{ include "istio.config.tls_simple" (dict "dot" $dot ) }}
92     hosts:
93     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
94 {{-         end }}
95 {{-       end }}
96 {{-     end }}
97 {{-   end }}
98 {{- end -}}
99
100 {{/*
101   Istio Helper function to add the external port of the service
102 */}}
103 {{- define "istio.config.port" -}}
104 {{-   $dot := default . .dot -}}
105 {{-   if .exposedPort }}
106       number: {{ .exposedPort }}
107 {{-     if .exposedProtocol }}
108       name: {{ .baseaddr }}
109       protocol: {{ .exposedProtocol }}
110 {{-     else }}
111       name: http
112       protocol: HTTP
113 {{-     end -}}
114 {{-   else }}
115       number: 80
116       name: http
117       protocol: HTTP
118 {{-   end -}}
119 {{- end -}}
120
121 {{/*
122   Helper function to add the route to the service
123 */}}
124 {{- define "ingress.config.port" -}}
125 {{-   $dot := default . .dot -}}
126 {{ range .Values.ingress.service }}
127 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
128   - host: {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
129     http:
130       paths:
131       - backend:
132           service:
133             name: {{ .name }}
134             port:
135             {{- if kindIs "string" .port }}
136               name: {{ .port }}
137             {{- else }}
138               number: {{ .port }}
139             {{- end }}
140         {{- if .path }}
141         path: {{ .path }}
142         {{- end }}
143         pathType: ImplementationSpecific
144 {{- end }}
145 {{- end -}}
146
147 {{/*
148   Istio Helper function to add the route to the service
149 */}}
150 {{- define "istio.config.route" -}}
151 {{-   $dot := default . .dot -}}
152   http:
153   - route:
154     - destination:
155         port:
156         {{- if .plain_port }}
157         {{- if kindIs "string" .plain_port }}
158           name: {{ .plain_port }}
159         {{- else }}
160           number: {{ .plain_port }}
161         {{- end }}
162         {{- else }}
163         {{- if kindIs "string" .port }}
164           name: {{ .port }}
165         {{- else }}
166           number: {{ .port }}
167         {{- end }}
168         {{- end }}
169         host: {{ .name }}
170 {{- end -}}
171
172 {{/*
173   Helper function to add ssl annotations
174 */}}
175 {{- define "ingress.config.annotations.ssl" -}}
176 {{- if .Values.ingress.config -}}
177 {{- if .Values.ingress.config.ssl -}}
178 {{- if eq .Values.ingress.config.ssl "redirect" -}}
179 kubernetes.io/ingress.class: nginx
180 nginx.ingress.kubernetes.io/ssl-passthrough: "true"
181 nginx.ingress.kubernetes.io/ssl-redirect: "true"
182 {{-  else if eq .Values.ingress.config.ssl "native" -}}
183 nginx.ingress.kubernetes.io/ssl-redirect: "true"
184 {{-  else if eq .Values.ingress.config.ssl "none" -}}
185 nginx.ingress.kubernetes.io/ssl-redirect: "false"
186 {{- end -}}
187 {{- end -}}
188 {{- end -}}
189 {{- end -}}
190
191
192 {{/*
193   Helper function to add annotations
194 */}}
195 {{- define "ingress.config.annotations" -}}
196 {{- if .Values.ingress -}}
197 {{- if .Values.ingress.annotations -}}
198 {{ toYaml .Values.ingress.annotations | indent 4 | trim }}
199 {{- end -}}
200 {{- end -}}
201 {{ include "ingress.config.annotations.ssl" . | indent 4 | trim }}
202 {{- end -}}
203
204 {{/*
205   Helper function to check the existance of an override value
206 */}}
207 {{- define "common.ingress._overrideIfDefined" -}}
208   {{- $currValue := .currVal }}
209   {{- $parent := .parent }}
210   {{- $var := .var }}
211   {{- if $parent -}}
212     {{- if hasKey $parent $var }}
213       {{- default "" (index $parent $var) }}
214     {{- else -}}
215       {{- default "" $currValue -}}
216     {{- end -}}
217   {{- else -}}
218     {{- default "" $currValue }}
219   {{- end -}}
220 {{- end -}}
221
222 {{/*
223   Helper function to check, if Ingress is enabled
224 */}}
225 {{- define "common.ingress._enabled" -}}
226 {{-   $dot := default . .dot -}}
227 {{-   if $dot.Values.ingress -}}
228 {{-     if $dot.Values.global.ingress -}}
229 {{-       if (default false $dot.Values.global.ingress.enabled) -}}
230 {{-         if (default false $dot.Values.global.ingress.enable_all) -}}
231 true
232 {{-         else -}}
233 {{-           if $dot.Values.ingress.enabled -}}
234 true
235 {{-           end -}}
236 {{-         end -}}
237 {{-       end -}}
238 {{-     end -}}
239 {{-   end -}}
240 {{- end -}}
241
242 {{/*
243   Create Istio Ingress resources per defined service
244 */}}
245 {{- define "common.istioIngress" -}}
246 {{-   $dot := default . .dot -}}
247 {{    range $dot.Values.ingress.service }}
248 {{-     $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
249 ---
250 apiVersion: networking.istio.io/v1beta1
251 kind: Gateway
252 metadata:
253   name: {{ $baseaddr }}-gateway
254 spec:
255   selector:
256     istio: ingress # use Istio default gateway implementation
257   servers:
258   - port:
259       {{- include "istio.config.port" . }}
260     hosts:
261     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
262     {{- include "istio.config.tls" (dict "dot" $dot "service" . "baseaddr" $baseaddr) }}
263 ---
264 apiVersion: networking.istio.io/v1beta1
265 kind: VirtualService
266 metadata:
267   name: {{ $baseaddr }}-service
268 spec:
269   hosts:
270     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
271   gateways:
272   - {{ $baseaddr }}-gateway
273   {{ include "istio.config.route" . | trim }}
274 {{-   end -}}
275 {{- end -}}
276
277 {{/*
278   Create default Ingress resource
279 */}}
280 {{- define "common.nginxIngress" -}}
281 {{- $dot := default . .dot -}}
282 apiVersion: networking.k8s.io/v1
283 kind: Ingress
284 metadata:
285   name: {{ include "common.fullname" $dot }}-ingress
286   annotations:
287     {{ include "ingress.config.annotations" $dot }}
288   labels:
289     app: {{ $dot.Chart.Name }}
290     chart: {{ $dot.Chart.Name }}-{{ $dot.Chart.Version | replace "+" "_" }}
291     release: {{ include "common.release" $dot }}
292     heritage: {{ $dot.Release.Service }}
293 spec:
294   rules:
295   {{ include "ingress.config.port" $dot | trim }}
296 {{- if $dot.Values.ingress.tls }}
297   tls:
298 {{ toYaml $dot.Values.ingress.tls | indent 4 }}
299 {{- end -}}
300 {{- if $dot.Values.ingress.config -}}
301 {{-   if $dot.Values.ingress.config.tls -}}
302   tls:
303   - hosts:
304   {{-   range $dot.Values.ingress.service }}{{ $baseaddr := required "baseaddr" .baseaddr }}
305     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
306   {{-   end }}
307     secretName: {{ required "secret" (tpl (default "" $dot.Values.ingress.config.tls.secret) $dot) }}
308 {{-   end -}}
309 {{- end -}}
310 {{- end -}}
311
312 {{/*
313   Create ingress template
314     Will create ingress template depending on the following values:
315     - .Values.global.ingress.enabled     : enables Ingress globally
316     - .Values.global.ingress.enable_all  : override default Ingress for all charts
317     - .Values.ingress.enabled            : sets Ingress per chart basis
318
319     | global.ingress.enabled | global.ingress.enable_all |ingress.enabled | result     |
320     |------------------------|---------------------------|----------------|------------|
321     | false                  | any                       | any            | no ingress |
322     | true                   | false                     | false          | no ingress |
323     | true                   | true                      | any            | ingress    |
324     | true                   | false                     | true           | ingress    |
325
326     If ServiceMesh (Istio) is enabled the respective resources are created:
327     - Gateway
328     - VirtualService
329
330     If ServiceMesh is disabled the standard Ingress resource is creates:
331     - Ingress
332 */}}
333 {{- define "common.ingress" -}}
334 {{-   $dot := default . .dot -}}
335 {{-   if (include "common.ingress._enabled" (dict "dot" $dot)) }}
336 {{-     if (include "common.onServiceMesh" .) }}
337 {{-       if eq (default "istio" .Values.global.serviceMesh.engine) "istio" }}
338 {{          include "common.istioIngress" (dict "dot" $dot) }}
339 {{-       end -}}
340 {{-     else -}}
341 {{        include "common.nginxIngress" (dict "dot" $dot) }}
342 {{-     end -}}
343 {{-   end -}}
344 {{- end -}}