db276d546cc88e1ff20cd19518d0b7e112f89bbc
[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 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
106 {{-   $protocol := (required "'protocol' param, set to the name of the port, is required." .protocol) -}}
107 {{-   if $dot.exposedPort }}
108       number: {{ $dot.exposedPort }}
109 {{-     if $dot.exposedProtocol }}
110       name: {{ $protocol }}-{{ $dot.exposedPort }}
111       protocol: {{ $dot.exposedProtocol }}
112 {{-     else }}
113       name: {{ $protocol }}
114       protocol: HTTP
115 {{-     end -}}
116 {{-   else }}
117       number: 80
118       name: {{ $protocol }}
119       protocol: HTTP
120 {{-   end -}}
121 {{- end -}}
122
123 {{/*
124   Helper function to add the route to the service
125 */}}
126 {{- define "ingress.config.port" -}}
127 {{-   $dot := default . .dot -}}
128 {{ range .Values.ingress.service }}
129 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
130   - host: {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
131     http:
132       paths:
133       - backend:
134           service:
135             name: {{ .name }}
136             port:
137             {{- if kindIs "string" .port }}
138               name: {{ .port }}
139             {{- else }}
140               number: {{ .port }}
141             {{- end }}
142         {{- if .path }}
143         path: {{ .path }}
144         {{- end }}
145         pathType: ImplementationSpecific
146 {{- end }}
147 {{- end -}}
148
149 {{/*
150   Istio Helper function to add the route to the service
151 */}}
152 {{- define "istio.config.route" -}}
153 {{- $dot := default . .dot -}}
154 {{- $protocol := (required "'protocol' param, is required." .protocol) -}}
155 {{- if eq $protocol "tcp" }}
156   - match:
157     - port: {{ $dot.exposedPort }}
158     route:
159     - destination:
160         port:
161         {{- if $dot.plain_port }}
162         {{- if kindIs "string" $dot.plain_port }}
163           name: {{ $dot.plain_port }}
164         {{- else }}
165           number: {{ $dot.plain_port }}
166         {{- end }}
167         {{- else }}
168         {{- if kindIs "string" $dot.port }}
169           name: {{ $dot.port }}
170         {{- else }}
171           number: {{ $dot.port }}
172         {{- end }}
173         {{- end }}
174         host: {{ $dot.name }}
175 {{- else if eq $protocol "http" }}
176   - route:
177     - destination:
178         port:
179         {{- if $dot.plain_port }}
180         {{- if kindIs "string" $dot.plain_port }}
181           name: {{ $dot.plain_port }}
182         {{- else }}
183           number: {{ $dot.plain_port }}
184         {{- end }}
185         {{- else }}
186         {{- if kindIs "string" $dot.port }}
187           name: {{ $dot.port }}
188         {{- else }}
189           number: {{ $dot.port }}
190         {{- end }}
191         {{- end }}
192         host: {{ $dot.name }}
193 {{- end -}}
194 {{- end -}}
195
196 {{/*
197   Helper function to add ssl annotations
198 */}}
199 {{- define "ingress.config.annotations.ssl" -}}
200 {{- if .Values.ingress.config -}}
201 {{- if .Values.ingress.config.ssl -}}
202 {{- if eq .Values.ingress.config.ssl "redirect" -}}
203 kubernetes.io/ingress.class: nginx
204 nginx.ingress.kubernetes.io/ssl-passthrough: "true"
205 nginx.ingress.kubernetes.io/ssl-redirect: "true"
206 {{-  else if eq .Values.ingress.config.ssl "native" -}}
207 nginx.ingress.kubernetes.io/ssl-redirect: "true"
208 {{-  else if eq .Values.ingress.config.ssl "none" -}}
209 nginx.ingress.kubernetes.io/ssl-redirect: "false"
210 {{- end -}}
211 {{- end -}}
212 {{- end -}}
213 {{- end -}}
214
215
216 {{/*
217   Helper function to add annotations
218 */}}
219 {{- define "ingress.config.annotations" -}}
220 {{- if .Values.ingress -}}
221 {{- if .Values.ingress.annotations -}}
222 {{ toYaml .Values.ingress.annotations | indent 4 | trim }}
223 {{- end -}}
224 {{- end -}}
225 {{ include "ingress.config.annotations.ssl" . | indent 4 | trim }}
226 {{- end -}}
227
228 {{/*
229   Helper function to check the existance of an override value
230 */}}
231 {{- define "common.ingress._overrideIfDefined" -}}
232   {{- $currValue := .currVal }}
233   {{- $parent := .parent }}
234   {{- $var := .var }}
235   {{- if $parent -}}
236     {{- if hasKey $parent $var }}
237       {{- default "" (index $parent $var) }}
238     {{- else -}}
239       {{- default "" $currValue -}}
240     {{- end -}}
241   {{- else -}}
242     {{- default "" $currValue }}
243   {{- end -}}
244 {{- end -}}
245
246 {{/*
247   Helper function to check, if Ingress is enabled
248 */}}
249 {{- define "common.ingress._enabled" -}}
250 {{-   $dot := default . .dot -}}
251 {{-   if $dot.Values.ingress -}}
252 {{-     if $dot.Values.global.ingress -}}
253 {{-       if (default false $dot.Values.global.ingress.enabled) -}}
254 {{-         if (default false $dot.Values.global.ingress.enable_all) -}}
255 true
256 {{-         else -}}
257 {{-           if $dot.Values.ingress.enabled -}}
258 true
259 {{-           end -}}
260 {{-         end -}}
261 {{-       end -}}
262 {{-     end -}}
263 {{-   end -}}
264 {{- end -}}
265
266 {{/*
267   Create Port entry in the Gateway resource
268 */}}
269 {{- define "istio.config.gatewayPort" -}}
270 {{-   $dot := default . .dot -}}
271 {{-   $service := (required "'service' param, set to the specific service, is required." .service) -}}
272 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
273 {{-   $protocol := (required "'protocol' param, set to the specific port, is required." .protocol) -}}
274   - port:
275       {{- include "istio.config.port" (dict "dot" $service "baseaddr" $baseaddr "protocol" $protocol) }}
276     hosts:
277     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
278     {{- include "istio.config.tls" (dict "dot" $dot "service" $service "baseaddr" $baseaddr) }}
279 {{- end -}}
280
281 {{/*
282   Create Istio Ingress resources per defined service
283 */}}
284 {{- define "common.istioIngress" -}}
285 {{- $dot := default . .dot -}}
286 {{  range $dot.Values.ingress.service }}
287 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
288 ---
289 apiVersion: networking.istio.io/v1beta1
290 kind: Gateway
291 metadata:
292   name: {{ $baseaddr }}-gateway
293 spec:
294   selector:
295     istio: ingress # use Istio default gateway implementation
296   servers:
297 {{-   if .tcpRoutes }}
298 {{      range .tcpRoutes }}
299   {{ include "istio.config.gatewayPort" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "tcp") | trim }}
300 {{      end -}}
301 {{-   else }}
302   {{-   if .protocol }}
303   {{ include "istio.config.gatewayPort" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" .protocol) | trim }}
304   {{-   else }}
305   {{ include "istio.config.gatewayPort" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "http") | trim }}
306   {{    end }}
307 {{    end }}
308 ---
309 apiVersion: networking.istio.io/v1beta1
310 kind: VirtualService
311 metadata:
312   name: {{ $baseaddr }}-service
313 spec:
314   hosts:
315     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
316   gateways:
317   - {{ $baseaddr }}-gateway
318 {{-   if .tcpRoutes }}
319   tcp:
320 {{      range .tcpRoutes }}
321   {{ include "istio.config.route" (dict "dot" . "protocol" "tcp") | trim }}
322 {{      end -}}
323 {{-   else  }}
324   {{-   if .protocol }}
325   {{ .protocol }}:
326   {{ include "istio.config.route" (dict "dot" . "protocol" .protocol) | trim }}
327   {{-   else }}
328   http:
329   {{ include "istio.config.route" (dict "dot" . "protocol" "http") | trim }}
330   {{    end }}
331 {{    end }}
332 {{- end -}}
333 {{- end -}}
334
335 {{/*
336   Create default Ingress resource
337 */}}
338 {{- define "common.nginxIngress" -}}
339 {{- $dot := default . .dot -}}
340 apiVersion: networking.k8s.io/v1
341 kind: Ingress
342 metadata:
343   name: {{ include "common.fullname" $dot }}-ingress
344   annotations:
345     {{ include "ingress.config.annotations" $dot }}
346   labels:
347     app: {{ $dot.Chart.Name }}
348     chart: {{ $dot.Chart.Name }}-{{ $dot.Chart.Version | replace "+" "_" }}
349     release: {{ include "common.release" $dot }}
350     heritage: {{ $dot.Release.Service }}
351 spec:
352   rules:
353   {{ include "ingress.config.port" $dot | trim }}
354 {{- if $dot.Values.ingress.tls }}
355   tls:
356 {{ toYaml $dot.Values.ingress.tls | indent 4 }}
357 {{- end -}}
358 {{- if $dot.Values.ingress.config -}}
359 {{-   if $dot.Values.ingress.config.tls }}
360   tls:
361   - hosts:
362   {{-   range $dot.Values.ingress.service }}{{ $baseaddr := required "baseaddr" .baseaddr }}
363     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
364   {{-   end }}
365     secretName: {{ required "secret" (tpl (default "" $dot.Values.ingress.config.tls.secret) $dot) }}
366 {{-   end -}}
367 {{- end -}}
368 {{- end -}}
369
370 {{/*
371   Create ingress template
372     Will create ingress template depending on the following values:
373     - .Values.global.ingress.enabled     : enables Ingress globally
374     - .Values.global.ingress.enable_all  : override default Ingress for all charts
375     - .Values.ingress.enabled            : sets Ingress per chart basis
376
377     | global.ingress.enabled | global.ingress.enable_all |ingress.enabled | result     |
378     |------------------------|---------------------------|----------------|------------|
379     | false                  | any                       | any            | no ingress |
380     | true                   | false                     | false          | no ingress |
381     | true                   | true                      | any            | ingress    |
382     | true                   | false                     | true           | ingress    |
383
384     If ServiceMesh (Istio) is enabled the respective resources are created:
385     - Gateway
386     - VirtualService
387
388     If ServiceMesh is disabled the standard Ingress resource is creates:
389     - Ingress
390 */}}
391 {{- define "common.ingress" -}}
392 {{-   $dot := default . .dot -}}
393 {{-   if (include "common.ingress._enabled" (dict "dot" $dot)) }}
394 {{-     if (include "common.onServiceMesh" .) }}
395 {{-       if eq (default "istio" .Values.global.serviceMesh.engine) "istio" }}
396 {{          include "common.istioIngress" (dict "dot" $dot) }}
397 {{-       end -}}
398 {{-     else -}}
399 {{        include "common.nginxIngress" (dict "dot" $dot) }}
400 {{-     end -}}
401 {{-   end -}}
402 {{- end -}}