4fc2e4b1c9e384b7812483980a4042e4564a2629
[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 {{- define "ingress.config.host" -}}
18 {{-   $dot := default . .dot -}}
19 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
20 {{-   $burl := (required "'baseurl' param, set to the generic part of the fqdn, is required." $dot.Values.global.ingress.virtualhost.baseurl) -}}
21 {{ printf "%s.%s" $baseaddr $burl }}
22 {{- end -}}
23
24 {{- define "ingress.config.port" -}}
25 {{-   $dot := default . .dot -}}
26 {{ range .Values.ingress.service }}
27 {{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
28   - host: {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
29     http:
30       paths:
31       - backend:
32           service:
33             name: {{ .name }}
34             port:
35             {{- if kindIs "string" .port }}
36               name: {{ .port }}
37             {{- else }}
38               number: {{ .port }}
39             {{- end }}
40         {{- if .path }}
41         path: {{ .path }}
42         {{- end }}
43         pathType: ImplementationSpecific
44 {{- end }}
45 {{- end -}}
46
47 {{- define "istio.config.route" -}}
48 {{-   $dot := default . .dot -}}
49 {{ range .Values.ingress.service }}
50   http:
51   - route:
52     - destination:
53         port:
54         {{- if .plain_port }}
55         {{- if kindIs "string" .plain_port }}
56           name: {{ .plain_port }}
57         {{- else }}
58           number: {{ .plain_port }}
59         {{- end }}
60         {{- else }}
61         {{- if kindIs "string" .port }}
62           name: {{ .port }}
63         {{- else }}
64           number: {{ .port }}
65         {{- end }}
66         {{- end }}
67         host: {{ .name }}
68 {{- end -}}
69 {{- end -}}
70
71 {{- define "ingress.config.annotations.ssl" -}}
72 {{- if .Values.ingress.config -}}
73 {{- if .Values.ingress.config.ssl -}}
74 {{- if eq .Values.ingress.config.ssl "redirect" -}}
75 kubernetes.io/ingress.class: nginx
76 nginx.ingress.kubernetes.io/ssl-passthrough: "true"
77 nginx.ingress.kubernetes.io/ssl-redirect: "true"
78 {{-  else if eq .Values.ingress.config.ssl "native" -}}
79 nginx.ingress.kubernetes.io/ssl-redirect: "true"
80 {{-  else if eq .Values.ingress.config.ssl "none" -}}
81 nginx.ingress.kubernetes.io/ssl-redirect: "false"
82 {{- end -}}
83 {{- end -}}
84 {{- end -}}
85 {{- end -}}
86
87
88 {{- define "ingress.config.annotations" -}}
89 {{- if .Values.ingress -}}
90 {{- if .Values.ingress.annotations -}}
91 {{ toYaml .Values.ingress.annotations | indent 4 | trim }}
92 {{- end -}}
93 {{- end -}}
94 {{ include "ingress.config.annotations.ssl" . | indent 4 | trim }}
95 {{- end -}}
96
97 {{- define "common.ingress._overrideIfDefined" -}}
98   {{- $currValue := .currVal }}
99   {{- $parent := .parent }}
100   {{- $var := .var }}
101   {{- if $parent -}}
102     {{- if hasKey $parent $var }}
103       {{- default "" (index $parent $var) }}
104     {{- else -}}
105       {{- default "" $currValue -}}
106     {{- end -}}
107   {{- else -}}
108     {{- default "" $currValue }}
109   {{- end -}}
110 {{- end -}}
111
112 {{- define "common.ingress" -}}
113 {{-   $dot := default . .dot -}}
114 {{- if .Values.ingress -}}
115   {{- $ingressEnabled := default false .Values.ingress.enabled -}}
116   {{- $ingressEnabled := include "common.ingress._overrideIfDefined" (dict "currVal" $ingressEnabled "parent" (default (dict) .Values.global.ingress) "var" "enabled") }}
117   {{- $ingressEnabled := include "common.ingress._overrideIfDefined" (dict "currVal" $ingressEnabled "parent" .Values.ingress "var" "enabledOverride") }}
118 {{- if $ingressEnabled }}
119 {{- if (include "common.onServiceMesh" .) }}
120 {{- if eq (default "istio" .Values.global.serviceMesh.engine) "istio" }}
121       {{-   $dot := default . .dot -}}
122 apiVersion: networking.istio.io/v1beta1
123 kind: Gateway
124 metadata:
125   name: {{ include "common.fullname" . }}-gateway
126 spec:
127   selector:
128     istio: ingressgateway # use Istio default gateway implementation
129   servers:
130   - port:
131       number: 80
132       name: http
133       protocol: HTTP
134     hosts:
135     {{- range .Values.ingress.service }}{{ $baseaddr := required "baseaddr" .baseaddr }}
136     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
137     {{- end }}
138 {{- if .Values.global.ingress.config }}
139 {{- if .Values.global.ingress.config.ssl }}
140 {{- if eq .Values.global.ingress.config.ssl "redirect" }}
141     tls:
142       httpsRedirect: true
143   - port:
144       number: 443
145       name: https
146       protocol: HTTPS
147     tls:
148 {{- if .Values.global.ingress.config }}
149 {{- if .Values.global.ingress.config.tls }}
150       credentialName: {{ default "ingress-tls-secret" .Values.global.ingress.config.tls.secret }}
151 {{- else }}
152       credentialName: "ingress-tls-secret"
153 {{- end }}
154 {{- else }}
155       credentialName: "ingress-tls-secret"
156 {{- end }}
157       mode: SIMPLE
158     hosts:
159     {{- range .Values.ingress.service }}{{ $baseaddr := required "baseaddr" .baseaddr }}
160     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
161     {{- end }}
162 {{- end }}
163 {{- end }}
164 {{- end }}
165 ---
166 apiVersion: networking.istio.io/v1beta1
167 kind: VirtualService
168 metadata:
169   name: {{ include "common.fullname" . }}-service
170 spec:
171   hosts:
172   {{- range .Values.ingress.service }}{{ $baseaddr := required "baseaddr" .baseaddr }}
173     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
174   {{- end }}
175   gateways:
176   - {{ include "common.fullname" . }}-gateway
177   {{ include "istio.config.route" . | trim }}
178 {{- end -}}
179 {{- else -}}
180 apiVersion: networking.k8s.io/v1
181 kind: Ingress
182 metadata:
183   name: {{ include "common.fullname" . }}-ingress
184   annotations:
185     {{ include "ingress.config.annotations" . }}
186   labels:
187     app: {{ .Chart.Name }}
188     chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
189     release: {{ include "common.release" . }}
190     heritage: {{ .Release.Service }}
191 spec:
192   rules:
193   {{ include "ingress.config.port" . | trim }}
194 {{- if .Values.ingress.tls }}
195   tls:
196 {{ toYaml .Values.ingress.tls | indent 4 }}
197 {{- end -}}
198 {{- if .Values.ingress.config -}}
199 {{- if .Values.ingress.config.tls -}}
200   tls:
201   - hosts:
202   {{- range .Values.ingress.service }}{{ $baseaddr := required "baseaddr" .baseaddr }}
203     - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
204   {{- end }}
205     secretName: {{ required "secret" (tpl (default "" .Values.ingress.config.tls.secret) $dot) }}
206 {{- end -}}
207 {{- end -}}
208 {{- end -}}
209 {{- end -}}
210 {{- end -}}
211 {{- end -}}