Merge "[AAI] Add model-loader tracing config"
[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   Helper function to check, if Ingress is enabled
33 */}}
34 {{- define "common.ingress._enabled" -}}
35 {{-   $dot := default . .dot -}}
36 {{-   if $dot.Values.ingress -}}
37 {{-     if $dot.Values.global.ingress -}}
38 {{-       if (default false $dot.Values.global.ingress.enabled) -}}
39 {{-         if (default false $dot.Values.global.ingress.enable_all) -}}
40 true
41 {{-         else -}}
42 {{-           if $dot.Values.ingress.enabled -}}
43 true
44 {{-           end -}}
45 {{-         end -}}
46 {{-       end -}}
47 {{-     end -}}
48 {{-   end -}}
49 {{- end -}}
50
51 {{/*
52   Helper function to check, if TLS redirect is enabled
53 */}}
54 {{- define "common.ingress._tlsRedirect" -}}
55 {{-   $dot := default . .dot -}}
56 {{-   if $dot.Values.global.ingress.config }}
57 {{-     if $dot.Values.global.ingress.config.ssl }}
58 {{-       if eq $dot.Values.global.ingress.config.ssl "redirect" }}
59 true
60 {{-       end -}}
61 {{-     end -}}
62 {{-   end -}}
63 {{- end -}}
64
65 {{/*
66   Helper function to get the Ingress Provider (default is "ingress")
67 */}}
68 {{- define "common.ingress._provider" -}}
69 {{-   $dot := default . .dot -}}
70 {{-   $provider := "ingress" -}}
71 {{-   if $dot.Values.global.ingress -}}
72 {{-     if $dot.Values.global.ingress.provider -}}
73 {{-       if ne $dot.Values.global.ingress.provider "" -}}
74 {{          $provider = $dot.Values.global.ingress.provider }}
75 {{-       end -}}
76 {{-     end -}}
77 {{-   end -}}
78 {{-   $provider -}}
79 {{- end -}}
80
81 {{/*
82   Helper function to get the Ingress Class (default is "nginx")
83 */}}
84 {{- define "common.ingress._class" -}}
85 {{-   $dot := default . .dot -}}
86 {{-   $class := "nginx" -}}
87 {{-   if $dot.Values.global.ingress -}}
88 {{-     if $dot.Values.global.ingress.ingressClass -}}
89 {{-       if ne $dot.Values.global.ingress.ingressClass "" -}}
90 {{          $class = $dot.Values.global.ingress.ingressClass }}
91 {{-       end -}}
92 {{-     end -}}
93 {{-   end -}}
94 {{-   $class -}}
95 {{- end -}}
96
97 {{/*
98   Helper function to get the Ingress Selector (default is "ingress")
99 */}}
100 {{- define "common.ingress._selector" -}}
101 {{-   $dot := default . .dot -}}
102 {{-   $selector := "ingress" -}}
103 {{-   if $dot.Values.global.ingress -}}
104 {{-     if $dot.Values.global.ingress.ingressSelector -}}
105 {{-       if ne $dot.Values.global.ingress.ingressSelector "" -}}
106 {{          $selector = $dot.Values.global.ingress.ingressSelector }}
107 {{-       end -}}
108 {{-     end -}}
109 {{-   end -}}
110 {{-   $selector -}}
111 {{- end -}}
112
113 {{/*
114   Helper function to get the common Gateway, if exists
115 */}}
116 {{- define "common.ingress._commonGateway" -}}
117 {{-   $dot := default . .dot -}}
118 {{-   $gateway := "-" -}}
119 {{-   if $dot.Values.global.ingress -}}
120 {{-     if $dot.Values.global.ingress.commonGateway -}}
121 {{-       if $dot.Values.global.ingress.commonGateway.name -}}
122 {{          $gateway = $dot.Values.global.ingress.commonGateway.name }}
123 {{-       end -}}
124 {{-     end -}}
125 {{-   end -}}
126 {{-   $gateway -}}
127 {{- end -}}
128
129 {{/*
130   Helper function to get the common Gateway HTTP Listener name, if exists
131 */}}
132 {{- define "common.ingress._gatewayHTTPListener" -}}
133 {{-   $dot := default . .dot -}}
134 {{-   $listener := "http-80" -}}
135 {{-   if $dot.Values.global.ingress -}}
136 {{-     if $dot.Values.global.ingress.commonGateway -}}
137 {{-       if $dot.Values.global.ingress.commonGateway.name -}}
138 {{          $listener = $dot.Values.global.ingress.commonGateway.httpListener }}
139 {{-       end -}}
140 {{-     end -}}
141 {{-   end -}}
142 {{-   $listener -}}
143 {{- end -}}
144
145 {{/*
146   Helper function to get the common Gateway HTTPS Listener name, if exists
147 */}}
148 {{- define "common.ingress._gatewayHTTPSListener" -}}
149 {{-   $dot := default . .dot -}}
150 {{-   $listener := "https-443" -}}
151 {{-   if $dot.Values.global.ingress -}}
152 {{-     if $dot.Values.global.ingress.commonGateway -}}
153 {{-       if $dot.Values.global.ingress.commonGateway.name -}}
154 {{          $listener = $dot.Values.global.ingress.commonGateway.httpsListener }}
155 {{-       end -}}
156 {{-     end -}}
157 {{-   end -}}
158 {{-   $listener -}}
159 {{- end -}}
160
161 {{/*
162   Helper function to check the existance of an override value
163 */}}
164 {{- define "common.ingress._overrideIfDefined" -}}
165   {{- $currValue := .currVal }}
166   {{- $parent := .parent }}
167   {{- $var := .var }}
168   {{- if $parent -}}
169     {{- if hasKey $parent $var }}
170       {{- default "" (index $parent $var) }}
171     {{- else -}}
172       {{- default "" $currValue -}}
173     {{- end -}}
174   {{- else -}}
175     {{- default "" $currValue }}
176   {{- end -}}
177 {{- end -}}
178
179 {{/*
180   Helper function to get the protocol of the service
181 */}}
182 {{- define "common.ingress._protocol" -}}
183 {{-   $dot := default . .dot -}}
184 {{-   $protocol := "http" -}}
185 {{-   if $dot.tcpRoutes }}
186 {{-     $protocol = "tcp" -}}
187 {{-   end -}}
188 {{-   if $dot.udpRoutes }}
189 {{-     $protocol = "tcp" -}}
190 {{-   end -}}
191 {{-   if $dot.protocol }}
192 {{-     $protocol = (lower $dot.protocol) -}}
193 {{-   end -}}
194 {{-   $protocol -}}
195 {{- end -}}
196
197 {{/*
198   Create the hostname as concatination <baseaddr>.<baseurl>
199   - baseaddr: from component values: ingress.service.baseaddr
200   - baseurl: from values: global.ingress.virtualhost.baseurl
201     which van be overwritten in the component via: ingress.baseurlOverride
202 */}}
203 {{- define "ingress.config.host" -}}
204 {{-   $dot := default . .dot -}}
205 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
206 {{-   $preaddr := default "" $dot.Values.global.ingress.virtualhost.preaddr -}}
207 {{-   $preaddr := include "common.ingress._overrideIfDefined" (dict "currVal" $preaddr "parent" (default (dict) $dot.Values.ingress) "var" "preaddrOverride") -}}
208 {{-   $postaddr := default "" $dot.Values.global.ingress.virtualhost.postaddr -}}
209 {{-   $postaddr := include "common.ingress._overrideIfDefined" (dict "currVal" $postaddr "parent" (default (dict) $dot.Values.ingress) "var" "postaddrOverride") -}}
210 {{-   $burl := (required "'baseurl' param, set to the generic part of the fqdn, is required." $dot.Values.global.ingress.virtualhost.baseurl) -}}
211 {{-   $burl := include "common.ingress._overrideIfDefined" (dict "currVal" $burl "parent" (default (dict) $dot.Values.ingress) "var" "baseurlOverride") -}}
212 {{ printf "%s%s%s.%s" $preaddr $baseaddr $postaddr $burl }}
213 {{- end -}}
214
215 {{/*
216   Istio Helper function to add the tls route
217 */}}
218 {{- define "istio.config.tls_simple" -}}
219 {{-   $dot := default . .dot -}}
220     tls:
221 {{-   if $dot.Values.global.ingress.config }}
222 {{-     if $dot.Values.global.ingress.config.tls }}
223       credentialName: {{ default "ingress-tls-secret" $dot.Values.global.ingress.config.tls.secret }}
224 {{-     else }}
225       credentialName: "ingress-tls-secret"
226 {{-     end }}
227 {{-   else }}
228       credentialName: "ingress-tls-secret"
229 {{-   end }}
230       mode: SIMPLE
231 {{- end -}}
232
233 {{/*
234   Istio Helper function to add the tls route
235 */}}
236 {{- define "istio.config.tls" -}}
237 {{-   $dot := default . .dot -}}
238 {{-   $service := (required "'service' param, set to the specific service, is required." .service) -}}
239 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
240 {{-   if $service.exposedPort }}
241 {{-     if $service.exposedProtocol }}
242 {{-       if eq $service.exposedProtocol "TLS" }}
243     {{ include "istio.config.tls_simple" (dict "dot" $dot ) }}
244 {{-       end }}
245 {{-     end }}
246 {{-   else }}
247 {{-     if $dot.Values.global.ingress.config }}
248 {{-       if $dot.Values.global.ingress.config.ssl }}
249 {{-         if eq $dot.Values.global.ingress.config.ssl "redirect" }}
250     tls:
251       httpsRedirect: true
252   - port:
253       number: 443
254       name: https
255       protocol: HTTPS
256     {{ include "istio.config.tls_simple" (dict "dot" $dot ) }}
257     hosts:
258     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
259 {{-         end }}
260 {{-       end }}
261 {{-     end }}
262 {{-   end }}
263 {{- end -}}
264
265 {{/*
266   Istio Helper function to add the external port of the service
267 */}}
268 {{- define "istio.config.port" -}}
269 {{-   $dot := default . .dot -}}
270 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
271 {{-   $protocol := (required "'protocol' param, set to the name of the port, is required." .protocol) -}}
272 {{-   if $dot.exposedPort }}
273       number: {{ $dot.exposedPort }}
274 {{-     if $dot.exposedProtocol }}
275       name: {{ $protocol }}-{{ $dot.exposedPort }}
276       protocol: {{ $dot.exposedProtocol }}
277 {{-     else }}
278       name: {{ $protocol }}
279       protocol: HTTP
280 {{-     end -}}
281 {{-   else }}
282       number: 80
283       name: {{ $protocol }}
284       protocol: HTTP
285 {{-   end -}}
286 {{- end -}}
287
288 {{/*
289   Create Port entry in the Gateway resource
290 */}}
291 {{- define "istio.config.gatewayPort" -}}
292 {{-   $dot := default . .dot -}}
293 {{-   $service := (required "'service' param, set to the specific service, is required." .service) -}}
294 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
295 {{-   $protocol := (required "'protocol' param, set to the specific port, is required." .protocol) -}}
296   - port:
297       {{- include "istio.config.port" (dict "dot" $service "baseaddr" $baseaddr "protocol" $protocol) }}
298     hosts:
299     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
300     {{- include "istio.config.tls" (dict "dot" $dot "service" $service "baseaddr" $baseaddr) }}
301 {{- end -}}
302
303 {{/*
304   Helper function to add the route to the service
305 */}}
306 {{- define "ingress.config.port" -}}
307 {{-   $dot := default . .dot -}}
308 {{ range $dot.Values.ingress.service }}
309 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
310   - host: {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
311     http:
312       paths:
313       - backend:
314           service:
315             name: {{ .name }}
316             port:
317             {{- if kindIs "string" .port }}
318               name: {{ .port }}
319             {{- else }}
320               number: {{ .port }}
321             {{- end }}
322         {{- if .path }}
323         path: {{ .path }}
324         {{- end }}
325         pathType: ImplementationSpecific
326 {{- end }}
327 {{- end -}}
328
329 {{/*
330   Istio Helper function to add the route to the service
331 */}}
332 {{- define "istio.config.route" -}}
333 {{- $dot := default . .dot -}}
334 {{- $protocol := (required "'protocol' param, is required." .protocol) -}}
335 {{- if eq $protocol "tcp" }}
336   - match:
337     - port: {{ $dot.exposedPort }}
338     route:
339     - destination:
340         port:
341         {{- if $dot.plain_port }}
342         {{- if kindIs "string" $dot.plain_port }}
343           name: {{ $dot.plain_port }}
344         {{- else }}
345           number: {{ $dot.plain_port }}
346         {{- end }}
347         {{- else }}
348         {{- if kindIs "string" $dot.port }}
349           name: {{ $dot.port }}
350         {{- else }}
351           number: {{ $dot.port }}
352         {{- end }}
353         {{- end }}
354         host: {{ $dot.name }}
355 {{- else if eq $protocol "http" }}
356   - route:
357     - destination:
358         port:
359         {{- if $dot.plain_port }}
360         {{- if kindIs "string" $dot.plain_port }}
361           name: {{ $dot.plain_port }}
362         {{- else }}
363           number: {{ $dot.plain_port }}
364         {{- end }}
365         {{- else }}
366         {{- if kindIs "string" $dot.port }}
367           name: {{ $dot.port }}
368         {{- else }}
369           number: {{ $dot.port }}
370         {{- end }}
371         {{- end }}
372         host: {{ $dot.name }}
373 {{- end -}}
374 {{- end -}}
375
376 {{/*
377   Helper function to add ssl annotations
378 */}}
379 {{- define "ingress.config.annotations.ssl" -}}
380 {{- $class := include "common.ingress._class" (dict "dot" .) }}
381 {{- if .Values.ingress.config -}}
382 {{- if .Values.ingress.config.ssl -}}
383 {{- if eq .Values.ingress.config.ssl "redirect" -}}
384 kubernetes.io/ingress.class: {{ $class }}
385 {{ $class }}.ingress.kubernetes.io/ssl-passthrough: "true"
386 {{ $class }}.ingress.kubernetes.io/ssl-redirect: "true"
387 {{-  else if eq .Values.ingress.config.ssl "native" -}}
388 {{ $class }}.ingress.kubernetes.io/ssl-redirect: "true"
389 {{-  else if eq .Values.ingress.config.ssl "none" -}}
390 {{ $class }}.ingress.kubernetes.io/ssl-redirect: "false"
391 {{- end -}}
392 {{- end -}}
393 {{- end -}}
394 {{- end -}}
395
396
397 {{/*
398   Helper function to add annotations
399 */}}
400 {{- define "ingress.config.annotations" -}}
401 {{- if .Values.ingress -}}
402 {{- if .Values.ingress.annotations -}}
403 {{ toYaml .Values.ingress.annotations | indent 4 | trim }}
404 {{- end -}}
405 {{- end -}}
406 {{ include "ingress.config.annotations.ssl" . | indent 4 | trim }}
407 {{- end -}}
408
409 {{/*
410   Create Istio Ingress resources per defined service
411 */}}
412 {{- define "common.istioIngress" -}}
413 {{- $dot := default . .dot -}}
414 {{- $selector := include "common.ingress._selector" (dict "dot" $dot) }}
415 {{- $gateway := include "common.ingress._commonGateway" (dict "dot" $dot) }}
416 {{  range $dot.Values.ingress.service }}
417 {{    if or ( eq (include "common.ingress._protocol" (dict "dot" .)) "http" ) ( eq (include "common.ingress._protocol" (dict "dot" .)) "tcp" )}}
418 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
419 {{-     if eq $gateway "-" }}
420 ---
421 apiVersion: networking.istio.io/v1beta1
422 kind: Gateway
423 metadata:
424   name: {{ $baseaddr }}-gateway
425 spec:
426   selector:
427     istio: {{ $selector }}
428   servers:
429 {{-       if .tcpRoutes }}
430 {{          range .tcpRoutes }}
431   {{ include "istio.config.gatewayPort" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "tcp") | trim }}
432 {{          end -}}
433 {{-       else }}
434   {{-       if .protocol }}
435   {{ include "istio.config.gatewayPort" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" .protocol) | trim }}
436   {{-       else }}
437   {{ include "istio.config.gatewayPort" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "http") | trim }}
438   {{        end }}
439 {{        end }}
440 {{      end }}
441 ---
442 apiVersion: networking.istio.io/v1beta1
443 kind: VirtualService
444 metadata:
445   name: {{ $baseaddr }}-service
446 spec:
447   hosts:
448     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
449   gateways:
450 {{-   if eq $gateway "-" }}
451   - {{ $baseaddr }}-gateway
452 {{-   else }}
453   - {{ $gateway }}
454 {{-   end }}
455 {{-   if .tcpRoutes }}
456   tcp:
457 {{      range .tcpRoutes }}
458   {{ include "istio.config.route" (dict "dot" . "protocol" "tcp") | trim }}
459 {{      end -}}
460 {{-   else  }}
461   {{-   if .protocol }}
462   {{ .protocol }}:
463   {{ include "istio.config.route" (dict "dot" . "protocol" .protocol) | trim }}
464   {{-   else }}
465   http:
466   {{ include "istio.config.route" (dict "dot" . "protocol" "http") | trim }}
467   {{-   end }}
468 {{-   end }}
469 {{- end }}
470 {{- end }}
471 {{- end -}}
472
473 {{/*
474   GW-API Helper function to add the tls route
475 */}}
476 {{- define "gwapi.config.tls_simple" -}}
477 {{-   $dot := default . .dot -}}
478     tls:
479 {{-   if $dot.Values.global.ingress.config }}
480 {{-     if $dot.Values.global.ingress.config.tls }}
481       certificateRefs:
482         - kind: Secret
483           group: ""
484           name: {{ default "ingress-tls-secret" $dot.Values.global.ingress.config.tls.secret }}
485 {{-     else }}
486       certificateRefs:
487         - kind: Secret
488           group: ""
489           name: "ingress-tls-secret"
490 {{-     end }}
491 {{-   else }}
492       certificateRefs:
493         - kind: Secret
494           group: ""
495           name: "ingress-tls-secret"
496 {{-   end }}
497       mode: Terminate
498 {{- end -}}
499
500 {{/*
501   GW-API Helper function to add the tls route
502 */}}
503 {{- define "gwapi.config.tls" -}}
504 {{-   $dot := default . .dot -}}
505 {{-   $service := (required "'service' param, set to the specific service, is required." .service) -}}
506 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
507 {{-   if $service.exposedPort }}
508 {{-     if $service.exposedProtocol }}
509 {{-       if eq $service.exposedProtocol "TLS" }}
510     {{ include "gwapi.config.tls_simple" (dict "dot" $dot ) }}
511 {{-       end }}
512 {{-     end }}
513 {{-   else }}
514 {{-     if (include "common.ingress._tlsRedirect" (dict "dot" $dot)) }}
515   - name: HTTPS-443
516     port: 443
517     protocol: HTTPS
518     hostname: {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
519     {{ include "gwapi.config.tls_simple" (dict "dot" $dot ) }}
520 {{-     end }}
521 {{-   end }}
522 {{- end -}}
523
524 {{/*
525   Create Listener entry in the Gateway resource
526 */}}
527 {{- define "gwapi.config.listener" -}}
528 {{-   $dot := default . .dot -}}
529 {{-   $service := (required "'service' param, set to the specific service, is required." .service) -}}
530 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
531 {{-   $protocol := (required "'protocol' param, set to the specific port, is required." .protocol) -}}
532 {{-   $port := default 80 $service.exposedPort -}}
533   - name: {{ $protocol }}-{{ $port }}
534     port: {{ $port }}
535 {{-   if $service.exposedProtocol }}
536     protocol: {{ upper $service.exposedProtocol }}
537 {{-   else }}
538     protocol: HTTP
539 {{-   end }}
540     hostname: {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
541     allowedRoutes:
542       namespaces:
543         from: All
544 {{-   if eq $service.protocol "tcp" }}
545         kinds:
546           - kind: TCPRoute
547 {{-   else if eq $service.protocol "tcp" }}
548         kinds:
549           - kind: UDPRoute
550 {{-   end }}
551     {{- include "gwapi.config.tls" (dict "dot" $dot "service" $service "baseaddr" $baseaddr) }}
552 {{- end -}}
553
554 {{/*
555   Create *Route entry for the Gateway-API
556 */}}
557 {{- define "gwapi.config.route" -}}
558 {{-   $dot := default . .dot -}}
559 {{-   $service := (required "'service' param, set to the specific service, is required." .service) -}}
560 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
561 {{-   $protocol := (required "'protocol' param, set to the specific port, is required." .protocol) -}}
562 {{-   $gateway := include "common.ingress._commonGateway" (dict "dot" $dot) -}}
563 {{-   $namespace := default "istio-ingress" $dot.Values.global.ingress.namespace -}}
564 {{-   $path := default "/" $service.path -}}
565 {{-   if eq $protocol "udp" -}}
566 ---
567 apiVersion: gateway.networking.k8s.io/v1alpha2
568 kind: UDPRoute
569 metadata:
570   name: {{ $baseaddr }}-{{ $service.exposedPort }}-route
571 spec:
572   parentRefs:
573     - group: gateway.networking.k8s.io
574       kind: Gateway
575 {{-     if eq $gateway "-" }}
576       name: {{ $baseaddr }}-gateway
577 {{-     else }}
578       name: {{ $gateway }}
579 {{-     end }}
580       namespace: {{ $namespace }}
581       sectionName: udp-{{ $service.exposedPort }}
582   rules:
583     - backendRefs:
584       - group: ''
585         kind: Service
586         name: {{ $service.name }}
587         port: {{ $service.port }}
588         weight: 1
589 {{-   else if eq $protocol "tcp" }}
590 ---
591 apiVersion: gateway.networking.k8s.io/v1alpha2
592 kind: TCPRoute
593 metadata:
594   name: {{ $baseaddr }}-{{ $service.exposedPort }}-route
595 spec:
596   parentRefs:
597     - group: gateway.networking.k8s.io
598       kind: Gateway
599 {{-     if eq $gateway "-" }}
600       name: {{ $baseaddr }}-gateway
601 {{-     else }}
602       name: {{ $gateway }}
603 {{-     end }}
604       namespace: {{ $namespace }}
605       sectionName: tcp-{{ $service.exposedPort }}
606   rules:
607     - backendRefs:
608       - group: ''
609         kind: Service
610         name: {{ $service.name }}
611         port: {{ $service.port }}
612         weight: 1
613 {{-   else if eq $protocol "http" }}
614 ---
615 apiVersion: gateway.networking.k8s.io/v1beta1
616 kind: HTTPRoute
617 metadata:
618   name: {{ $baseaddr }}-http-route
619 spec:
620   parentRefs:
621     - group: gateway.networking.k8s.io
622       kind: Gateway
623 {{-     if eq $gateway "-" }}
624       name: {{ $baseaddr }}-gateway
625 {{-     else }}
626       name: {{ $gateway }}
627 {{-     end }}
628       namespace: {{ $namespace }}
629 {{-     if (include "common.ingress._tlsRedirect" (dict "dot" $dot)) }}
630       sectionName: {{ include "common.ingress._gatewayHTTPSListener" (dict "dot" $dot) }}
631 {{-     else }}
632       sectionName: {{ include "common.ingress._gatewayHTTPListener" (dict "dot" $dot) }}
633 {{-     end }}
634   hostnames:
635     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
636   rules:
637     - backendRefs:
638       - group: ''
639         kind: Service
640         name: {{ $service.name }}
641         port: {{ $service.port }}
642         weight: 1
643       matches:
644         - path:
645             type: PathPrefix
646             value: {{ $path }}
647 {{-     if (include "common.ingress._tlsRedirect" (dict "dot" $dot)) }}
648 ---
649 apiVersion: gateway.networking.k8s.io/v1beta1
650 kind: HTTPRoute
651 metadata:
652   name: {{ $baseaddr }}-redirect-route
653 spec:
654   parentRefs:
655     - group: gateway.networking.k8s.io
656       kind: Gateway
657 {{-       if eq $gateway "-" }}
658       name: {{ $baseaddr }}-gateway
659 {{-       else }}
660       name: {{ $gateway }}
661 {{-       end }}
662       namespace: {{ $namespace }}
663       sectionName: {{ include "common.ingress._gatewayHTTPListener" (dict "dot" $dot) }}
664   hostnames:
665     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
666   rules:
667     - filters:
668       - type: RequestRedirect
669         requestRedirect:
670           scheme: https
671           statusCode: 301
672           port: 443
673       matches:
674         - path:
675             type: PathPrefix
676             value: {{ $path }}
677 {{-     end }}
678 {{-   end }}
679 {{- end -}}
680
681 {{/*
682   Create GW-API Ingress resources per defined service
683 */}}
684 {{- define "common.gwapiIngress" -}}
685 {{- $dot := default . .dot -}}
686 {{- $selector := include "common.ingress._selector" (dict "dot" $dot) }}
687 {{- $gateway := include "common.ingress._commonGateway" (dict "dot" $dot) }}
688 {{  range $dot.Values.ingress.service }}
689 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
690 {{-   if eq $gateway "-" }}
691 ---
692 apiVersion: gateway.networking.k8s.io/v1beta1
693 kind: Gateway
694 metadata:
695   name: {{ $baseaddr }}-gateway
696 spec:
697   gatewayClassName: {{ $dot.Values.global.serviceMesh.engine }}
698   listeners:
699 {{-     if .tcpRoutes }}
700 {{        range .tcpRoutes }}
701   {{ include "gwapi.config.listener" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "tcp") | trim }}
702 {{-        end -}}
703 {{-     else if .udpRoutes }}
704 {{        range .udpRoutes }}
705   {{ include "gwapi.config.listener" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "udp") | trim }}
706 {{-       end -}}
707 {{-     else }}
708 {{-       if .protocol }}
709   {{ include "gwapi.config.listener" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" (lower .protocol)) | trim }}
710 {{-       else }}
711   {{ include "gwapi.config.listener" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "http") | trim }}
712 {{-       end }}
713 {{-     end }}
714 {{-   end }}
715 {{-   if .tcpRoutes }}
716 {{      range .tcpRoutes }}
717 {{ include "gwapi.config.route" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "tcp") | trim }}
718 {{-     end -}}
719 {{-   else if .udpRoutes }}
720 {{      range .udpRoutes }}
721 {{ include "gwapi.config.route" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "udp") | trim }}
722 {{-     end -}}
723 {{-   else }}
724 {{-     if .protocol }}
725 {{ include "gwapi.config.route" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" (lower .protocol)) | trim }}
726 {{-     else }}
727 {{ include "gwapi.config.route" (dict "dot" $dot "service" . "baseaddr" $baseaddr "protocol" "http") | trim }}
728 {{-     end }}
729 {{-   end }}
730 {{- end }}
731 {{- end -}}
732
733 {{/*
734   Create default Ingress resource
735 */}}
736 {{- define "common.nginxIngress" -}}
737 {{- $dot := default . .dot -}}
738 {{  range $dot.Values.ingress.service }}
739 {{    if eq (include "common.ingress._protocol" (dict "dot" $dot)) "http" }}
740 {{      $baseaddr := required "baseaddr" .baseaddr }}
741 apiVersion: networking.k8s.io/v1
742 kind: Ingress
743 metadata:
744   name: {{ include "common.fullname" $dot }}-ingress
745   annotations:
746     {{ include "ingress.config.annotations" $dot }}
747   labels:
748     app: {{ $dot.Chart.Name }}
749     chart: {{ $dot.Chart.Name }}-{{ $dot.Chart.Version | replace "+" "_" }}
750     release: {{ include "common.release" $dot }}
751     heritage: {{ $dot.Release.Service }}
752 spec:
753   rules:
754   {{ include "ingress.config.port" $dot | trim }}
755 {{-     if $dot.Values.ingress.tls }}
756   tls:
757 {{ toYaml $dot.Values.ingress.tls | indent 4 }}
758 {{-     end -}}
759 {{-     if $dot.Values.ingress.config -}}
760 {{-       if $dot.Values.ingress.config.tls }}
761   tls:
762   - hosts:
763     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
764     secretName: {{ required "secret" (tpl (default "" $dot.Values.ingress.config.tls.secret) $dot) }}
765 {{-       end }}
766 {{-     end }}
767 {{-   end }}
768 {{- end }}
769 {{- end -}}
770
771 {{/*
772   Create ingress template
773     Will create ingress template depending on the following values:
774     - .Values.global.ingress.enabled     : enables Ingress globally
775     - .Values.global.ingress.enable_all  : override default Ingress for all charts
776     - .Values.ingress.enabled            : sets Ingress per chart basis
777
778     | global.ingress.enabled | global.ingress.enable_all |ingress.enabled | result     |
779     |------------------------|---------------------------|----------------|------------|
780     | false                  | any                       | any            | no ingress |
781     | true                   | false                     | false          | no ingress |
782     | true                   | true                      | any            | ingress    |
783     | true                   | false                     | true           | ingress    |
784
785     If ServiceMesh (Ingress-Provider: Istio) is enabled the respective resources
786     are created:
787     - Gateway (optional)
788     - VirtualService
789
790     If ServiceMesh (Ingress-Provider: GatewayAPI) is enabled the respective resources
791     are created:
792     - Gateway (optional)
793     - HTTPRoute, TCPRoute, UDPRoute (depending)
794
795     If ServiceMesh is disabled the standard Ingress resource is creates:
796     - Ingress
797 */}}
798 {{- define "common.ingress" -}}
799 {{-   $dot := default . .dot -}}
800 {{-   $provider := include "common.ingress._provider" (dict "dot" $dot) -}}
801 {{-   if (include "common.ingress._enabled" (dict "dot" $dot)) }}
802 {{-     if eq $provider "ingress" -}}
803 {{        include "common.nginxIngress" (dict "dot" $dot) }}
804 {{-     else if eq $provider "istio" -}}
805 {{        include "common.istioIngress" (dict "dot" $dot) }}
806 {{-     else if eq $provider "gw-api" -}}
807 {{        include "common.gwapiIngress" (dict "dot" $dot) }}
808 {{-     end -}}
809 {{-   end -}}
810 {{- end -}}