aboutsummaryrefslogtreecommitdiffstats
path: root/kubernetes/common/common/templates/_ingress.tpl
blob: d8a944712a598ad1fea4073189b0dd692c38105d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
{{/*
# Copyright © 2019-2021 Orange, Samsung
# Copyright © 2022 Deutsche Telekom
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/}}
{{/*
  Create the hostname as concatination <baseaddr>.<baseurl>
  - baseaddr: from component values: ingress.service.baseaddr
  - baseurl: from values: global.ingress.virtualhost.baseurl
    which van be overwritten in the component via: ingress.baseurlOverride
*/}}
{{- define "ingress.config.host" -}}
{{-   $dot := default . .dot -}}
{{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
{{-   $burl := (required "'baseurl' param, set to the generic part of the fqdn, is required." $dot.Values.global.ingress.virtualhost.baseurl) -}}
{{-   $burl := include "common.ingress._overrideIfDefined" (dict "currVal" $burl "parent" (default (dict) $dot.Values.ingress) "var" "baseurlOverride") -}}
{{ printf "%s.%s" $baseaddr $burl }}
{{- end -}}

{{/*
  Helper function to add the tls route
*/}}
{{- define "ingress.config.tls" -}}
{{-   $dot := default . .dot -}}
{{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) -}}
{{-   if $dot.Values.global.ingress.config }}
{{-     if $dot.Values.global.ingress.config.ssl }}
{{-       if eq $dot.Values.global.ingress.config.ssl "redirect" }}
    tls:
      httpsRedirect: true
  - port:
      number: 443
      name: https
      protocol: HTTPS
    tls:
{{-         if $dot.Values.global.ingress.config }}
{{-           if $dot.Values.global.ingress.config.tls }}
      credentialName: {{ default "ingress-tls-secret" $dot.Values.global.ingress.config.tls.secret }}
{{-           else }}
      credentialName: "ingress-tls-secret"
{{-           end }}
{{-         else }}
      credentialName: "ingress-tls-secret"
{{-         end }}
      mode: SIMPLE
    hosts:
    - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
{{-       end }}
{{-     end }}
{{-   end }}
{{- end -}}

{{/*
  Helper function to add the route to the service
*/}}
{{- define "ingress.config.port" -}}
{{-   $dot := default . .dot -}}
{{ range .Values.ingress.service }}
{{-   $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
  - host: {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
    http:
      paths:
      - backend:
          service:
            name: {{ .name }}
            port:
            {{- if kindIs "string" .port }}
              name: {{ .port }}
            {{- else }}
              number: {{ .port }}
            {{- end }}
        {{- if .path }}
        path: {{ .path }}
        {{- end }}
        pathType: ImplementationSpecific
{{- end }}
{{- end -}}

{{/*
  Helper function to add the route to the service
*/}}
{{- define "istio.config.route" -}}
{{-   $dot := default . .dot -}}
  http:
  - route:
    - destination:
        port:
        {{- if .plain_port }}
        {{- if kindIs "string" .plain_port }}
          name: {{ .plain_port }}
        {{- else }}
          number: {{ .plain_port }}
        {{- end }}
        {{- else }}
        {{- if kindIs "string" .port }}
          name: {{ .port }}
        {{- else }}
          number: {{ .port }}
        {{- end }}
        {{- end }}
        host: {{ .name }}
{{- end -}}

{{/*
  Helper function to add ssl annotations
*/}}
{{- define "ingress.config.annotations.ssl" -}}
{{- if .Values.ingress.config -}}
{{- if .Values.ingress.config.ssl -}}
{{- if eq .Values.ingress.config.ssl "redirect" -}}
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-passthrough: "true"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
{{-  else if eq .Values.ingress.config.ssl "native" -}}
nginx.ingress.kubernetes.io/ssl-redirect: "true"
{{-  else if eq .Values.ingress.config.ssl "none" -}}
nginx.ingress.kubernetes.io/ssl-redirect: "false"
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}


{{/*
  Helper function to add annotations
*/}}
{{- define "ingress.config.annotations" -}}
{{- if .Values.ingress -}}
{{- if .Values.ingress.annotations -}}
{{ toYaml .Values.ingress.annotations | indent 4 | trim }}
{{- end -}}
{{- end -}}
{{ include "ingress.config.annotations.ssl" . | indent 4 | trim }}
{{- end -}}

{{/*
  Helper function to check the existance of an override value
*/}}
{{- define "common.ingress._overrideIfDefined" -}}
  {{- $currValue := .currVal }}
  {{- $parent := .parent }}
  {{- $var := .var }}
  {{- if $parent -}}
    {{- if hasKey $parent $var }}
      {{- default "" (index $parent $var) }}
    {{- else -}}
      {{- default "" $currValue -}}
    {{- end -}}
  {{- else -}}
    {{- default "" $currValue }}
  {{- end -}}
{{- end -}}

{{/*
  Helper function to check, if Ingress is enabled
*/}}
{{- define "common.ingress._enabled" -}}
{{-   $dot := default . .dot -}}
{{-   if $dot.Values.ingress -}}
{{-     if $dot.Values.global.ingress -}}
{{-       if (default false $dot.Values.global.ingress.enabled) -}}
{{-         if (default false $dot.Values.global.ingress.enable_all) -}}
true
{{-         else -}}
{{-           if $dot.Values.ingress.enabled -}}
true
{{-           end -}}
{{-         end -}}
{{-       end -}}
{{-     end -}}
{{-   end -}}
{{- end -}}

{{/*
  Create Istio Ingress resources per defined service
*/}}
{{- define "common.istioIngress" -}}
{{-   $dot := default . .dot -}}
{{    range $dot.Values.ingress.service }}
{{-     $baseaddr := (required "'baseaddr' param, set to the specific part of the fqdn, is required." .baseaddr) }}
---
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: {{ $baseaddr }}-gateway
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
    {{ include "ingress.config.tls" (dict "dot" $dot "baseaddr" $baseaddr) }}
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: {{ $baseaddr }}-service
spec:
  hosts:
    - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
  gateways:
  - {{ $baseaddr }}-gateway
  {{ include "istio.config.route" . | trim }}
{{-   end -}}
{{- end -}}

{{/*
  Create default Ingress resource
*/}}
{{- define "common.nginxIngress" -}}
{{- $dot := default . .dot -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "common.fullname" $dot }}-ingress
  annotations:
    {{ include "ingress.config.annotations" $dot }}
  labels:
    app: {{ $dot.Chart.Name }}
    chart: {{ $dot.Chart.Name }}-{{ $dot.Chart.Version | replace "+" "_" }}
    release: {{ include "common.release" $dot }}
    heritage: {{ $dot.Release.Service }}
spec:
  rules:
  {{ include "ingress.config.port" $dot | trim }}
{{- if $dot.Values.ingress.tls }}
  tls:
{{ toYaml $dot.Values.ingress.tls | indent 4 }}
{{- end -}}
{{- if $dot.Values.ingress.config -}}
{{-   if $dot.Values.ingress.config.tls -}}
  tls:
  - hosts:
  {{-   range $dot.Values.ingress.service }}{{ $baseaddr := required "baseaddr" .baseaddr }}
    - {{ include "ingress.config.host" (dict "dot" $dot "baseaddr" $baseaddr) }}
  {{-   end }}
    secretName: {{ required "secret" (tpl (default "" $dot.Values.ingress.config.tls.secret) $dot) }}
{{-   end -}}
{{- end -}}
{{- end -}}

{{/*
  Create ingress template
    Will create ingress template depending on the following values:
    - .Values.global.ingress.enabled     : enables Ingress globally
    - .Values.global.ingress.enable_all  : override default Ingress for all charts
    - .Values.ingress.enabled            : sets Ingress per chart basis

    | global.ingress.enabled | global.ingress.enable_all |ingress.enabled | result     |
    |------------------------|---------------------------|----------------|------------|
    | false                  | any                       | any            | no ingress |
    | true                   | false                     | false          | no ingress |
    | true                   | true                      | any            | ingress    |
    | true                   | false                     | true           | ingress    |

    If ServiceMesh (Istio) is enabled the respective resources are created:
    - Gateway
    - VirtualService

    If ServiceMesh is disabled the standard Ingress resource is creates:
    - Ingress
*/}}
{{- define "common.ingress" -}}
{{-   $dot := default . .dot -}}
{{-   if (include "common.ingress._enabled" (dict "dot" $dot)) }}
{{-     if (include "common.onServiceMesh" .) }}
{{-       if eq (default "istio" .Values.global.serviceMesh.engine) "istio" }}
{{          include "common.istioIngress" (dict "dot" $dot) }}
{{-       end -}}
{{-     else -}}
{{        include "common.nginxIngress" (dict "dot" $dot) }}
{{-     end -}}
{{-   end -}}
{{- end -}}