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