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