Merge "[AAI] Add model-loader tracing config"
[oom.git] / kubernetes / common / common / templates / _service.tpl
1 {{/*
2 # Copyright © 2017 Amdocs, Bell Canada
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 */}}
16 {{/*
17   Resolve the name of a chart's service.
18
19   The default will be the chart name (or .Values.nameOverride if set).
20   And the use of .Values.service.name overrides all.
21
22   - .Values.service.name: override default service (ie. chart) name
23 */}}
24 {{/*
25   Expand the service name for a chart.
26 */}}
27 {{- define "common.servicename" -}}
28   {{- $name := default .Chart.Name .Values.nameOverride -}}
29   {{- default $name .Values.service.name | trunc 63 | trimSuffix "-" -}}
30 {{- end -}}
31
32 {{/*
33   Resolve the prefix node port to use. We look at these different values in
34   order of priority (first found, first chosen)
35   - .Values.service.nodePortPrefixOverride: override value for nodePort which
36                                             will be use locally;
37   - .Values.global.nodePortPrefix         : global value for nodePort which will
38                                             be used for all charts (unless
39                                             previous one is used);
40   - .Values.global.nodePortPrefixExt      : global value for nodePort which will
41                                             be used for all charts (unless
42                                             previous one is used) if
43                                             useNodePortExt is set to true in
44                                             service or on port;
45   - .Values.service.nodePortPrefix        : value used on a pert chart basis if
46                                             no other version exists.
47
48   The function takes two arguments (inside a dictionary):
49      - .dot : environment (.)
50      - .useNodePortExt : does the port use the "extended" nodeport part or the
51                          normal one?
52 */}}
53 {{- define "common.nodePortPrefix" -}}
54 {{-   $dot := default . .dot -}}
55 {{-   $useNodePortExt := default false .useNodePortExt -}}
56 {{-   if or $useNodePortExt $dot.Values.service.useNodePortExt -}}
57 {{      $dot.Values.service.nodePortPrefixOverride | default $dot.Values.global.nodePortPrefixExt | default $dot.Values.nodePortPrefix }}
58 {{-   else -}}
59 {{      $dot.Values.service.nodePortPrefixOverride | default $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}
60 {{-   end -}}
61 {{- end -}}
62
63 {{/* Define the metadata of Service
64      The function takes from one to four arguments (inside a dictionary):
65      - .dot : environment (.)
66      - .suffix : a string which will be added at the end of the name (with a '-').
67      - .annotations: the annotations to add
68      - .msb_informations: msb information in order to create msb annotation
69      - .labels : labels to add
70      Usage example:
71       {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
72       {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
73 */}}
74 {{- define "common.serviceMetadata" -}}
75   {{- $dot := default . .dot -}}
76   {{- $suffix := default "" .suffix -}}
77   {{- $annotations := default "" .annotations -}}
78   {{- $msb_informations := default "" .msb_informations -}}
79   {{- $labels := default (dict) .labels -}}
80 {{- if or $annotations $msb_informations -}}
81 annotations:
82 {{-   if $annotations }}
83 {{      include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
84 {{-   end }}
85 {{-   if $msb_informations }}
86   msb.onap.org/service-info: '[
87 {{-     range $index, $msb_information := $msb_informations }}
88 {{-       if ne $index 0 }}
89       ,
90 {{-       end }}
91       {
92         "serviceName": "{{ default (include "common.servicename" $dot) $msb_information.serviceName  }}",
93         "version": "{{ default "v1" $msb_information.version }}",
94         "url": "{{ default "/" $msb_information.url }}",
95         "protocol": "{{ default "REST" $msb_information.protocol }}",
96         "enable_ssl": {{ default false $msb_information.enable_ssl }},
97         "port": "{{ $msb_information.port }}",
98         "visualRange":"{{ default "1" $msb_information.visualRange }}"
99       }
100 {{-    end }}
101     ]'
102 {{-   end}}
103 {{- end }}
104 name: {{ include "common.servicename" $dot }}{{ if $suffix }}{{ print "-" $suffix }}{{ end }}
105 namespace: {{ include "common.namespace" $dot }}
106 labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 2 -}}
107 {{- end -}}
108
109 {{/* Define the ports of Service
110      The function takes three arguments (inside a dictionary):
111      - .dot : environment (.)
112      - .ports : an array of ports
113      - .serviceType: the type of the service
114      - .add_plain_port: add tls port AND plain port
115 */}}
116 {{- define "common.servicePorts" -}}
117 {{- $serviceType := .serviceType }}
118 {{- $dot := .dot }}
119 {{- $add_plain_port := default false .add_plain_port }}
120 {{-   range $index, $port := .ports }}
121 {{-     if (include "common.needTLS" $dot) }}
122 - port: {{ $port.port }}
123   targetPort: {{ $port.name }}
124 {{-       if $port.l4_protocol }}
125   protocol: {{ $port.l4_protocol }}
126 {{-       else }}
127   protocol: TCP
128 {{-       end }}
129 {{-       if $port.app_protocol }}
130   appProtocol: {{ $port.app_protocol }}
131 {{-       end }}
132 {{-       if $port.port_protocol }}
133   name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
134 {{-       else }}
135   name: {{ $port.name }}
136 {{-       end }}
137 {{-     else }}
138 - port: {{ default $port.port $port.plain_port }}
139   targetPort: {{ $port.name }}
140 {{-       if $port.plain_port_l4_protocol }}
141   protocol: {{ $port.plain_port_l4_protocol }}
142 {{-       else }}
143   protocol: {{ default "TCP" $port.l4_protocol  }}
144 {{-       end }}
145 {{-       if $port.app_protocol }}
146   appProtocol: {{ $port.app_protocol }}
147 {{-       end }}
148 {{-       if $port.port_protocol }}
149   name: {{ printf "%s-%s" $port.port_protocol $port.name }}
150 {{-       else }}
151   name: {{ $port.name }}
152 {{-       end }}
153 {{-     end }}
154 {{-     if (eq $serviceType "NodePort") }}
155   nodePort: {{ include "common.nodePortPrefix" (dict "dot" $dot "useNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
156 {{-     end }}
157 {{-     if (and (and (include "common.needTLS" $dot) $add_plain_port) $port.plain_port)  }}
158 {{-       if (eq $serviceType "ClusterIP")  }}
159 - port: {{ $port.plain_port }}
160   targetPort: {{ $port.name }}-plain
161 {{-         if $port.plain_l4_port_protocol }}
162   protocol: {{ $port.plain_port_l4_protocol }}
163 {{-         else }}
164   protocol: {{ default "TCP" $port.l4_protocol  }}
165 {{-         end }}
166 {{-       if $port.app_protocol }}
167   appProtocol: {{ $port.app_protocol }}
168 {{-       end }}
169 {{-         if $port.port_protocol }}
170   name: {{ printf "%s-%s" $port.port_protocol $port.name }}
171 {{-         else }}
172   name: {{ $port.name }}-plain
173 {{-         end }}
174 {{-       end }}
175 {{-     end }}
176 {{-   end }}
177 {{- end -}}
178
179 {{/* Create generic service template
180      The function takes several arguments (inside a dictionary):
181      - .dot : environment (.)
182      - .ports : an array of ports
183      - .serviceType: the type of the service
184      - .suffix : a string which will be added at the end of the name (with a '-')
185      - .annotations: the annotations to add
186      - .msb_informations: msb information in order to create msb annotation
187      - .publishNotReadyAddresses: if we publish not ready address
188      - .headless: if the service is headless
189      - .add_plain_port: add tls port AND plain port
190      - .labels : labels to add (dict)
191      - .matchLabels: selectors/machLabels to add (dict)
192      - .sessionAffinity: ClientIP  - enables sticky sessions based on client IP, default: None
193 */}}
194 {{- define "common.genericService" -}}
195 {{- $dot := default . .dot -}}
196 {{- $suffix := default "" .suffix -}}
197 {{- $annotations := default "" .annotations -}}
198 {{- $msb_informations := default "" .msb_informations -}}
199 {{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
200 {{- $serviceType := .serviceType -}}
201 {{- $ports := .ports -}}
202 {{- $headless := default false .headless -}}
203 {{- $add_plain_port := default false .add_plain_port }}
204 {{- $labels := default (dict) .labels -}}
205 {{- $matchLabels := default (dict) .matchLabels -}}
206 {{- $sessionAffinity := default "None" $dot.Values.service.sessionAffinity -}}
207 {{- $kubeTargetVersion := default $dot.Capabilities.KubeVersion.Version | trimPrefix "v" -}}
208 {{- $ipFamilyPolicy := default "PreferDualStack" $dot.Values.service.ipFamilyPolicy -}}
209 apiVersion: v1
210 kind: Service
211 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
212 spec:
213   {{- if $headless }}
214   clusterIP: None
215   {{- end }}
216   ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot "add_plain_port" $add_plain_port) | nindent 4 }}
217   {{- if semverCompare ">=1.20.0" $kubeTargetVersion }}
218   ipFamilyPolicy: {{ $ipFamilyPolicy }}
219   {{- end }}
220   {{- if $publishNotReadyAddresses }}
221   publishNotReadyAddresses: true
222   {{- end }}
223   type: {{ $serviceType }}
224   selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
225   sessionAffinity: {{ $sessionAffinity }}
226 {{- end -}}
227
228 {{/*
229     Create service template
230     Will create one or two service templates according to this table:
231
232     | serviceType   | both_tls_and_plain | result       |
233     |---------------|--------------------|--------------|
234     | ClusterIP     | any                | one Service  |
235     | Not ClusterIP | not present        | one Service  |
236     | Not ClusterIP | false              | one Service  |
237     | Not ClusterIP | true               | two Services |
238
239     If two services are created, one is ClusterIP with both crypted and plain
240     ports and the other one is NodePort (or LoadBalancer) with crypted port only.
241 */}}
242 {{- define "common.service" -}}
243 {{-   $dot := default . .dot -}}
244 {{-   $suffix := default "" $dot.Values.service.suffix -}}
245 {{-   $annotations := default "" $dot.Values.service.annotations -}}
246 {{-   $publishNotReadyAddresses := default false $dot.Values.service.publishNotReadyAddresses -}}
247 {{-   $msb_informations := default "" $dot.Values.service.msb -}}
248 {{-   $serviceType := $dot.Values.service.type -}}
249 {{-   $ports := $dot.Values.service.ports -}}
250 {{-   $both_tls_and_plain:= default false $dot.Values.service.both_tls_and_plain }}
251 {{-   $labels := default (dict) .labels -}}
252 {{-   $matchLabels := default (dict) .matchLabels -}}
253 {{-   if and (include "common.ingressEnabled" $dot) (eq $serviceType "NodePort") -}}
254 {{-     $serviceType = "ClusterIP" }}
255 {{-   end }}
256
257 {{-   if (and (include "common.needTLS" $dot) $both_tls_and_plain) }}
258 {{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "add_plain_port" true $labels "matchLabels" $matchLabels) }}
259 {{-     if (ne $serviceType "ClusterIP") }}
260 ---
261 {{-       if $suffix }}
262 {{-         $suffix = printf "%s-external" $suffix }}
263 {{-       else }}
264 {{-         $suffix = "external" }}
265 {{-       end }}
266 {{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
267 {{-     end }}
268 {{-   else }}
269 {{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
270 {{-   end }}
271 {{- end -}}
272
273 {{/* Create headless service template */}}
274 {{- define "common.headlessService" -}}
275 {{- $dot := default . .dot -}}
276 {{- $suffix := include "common._makeHeadlessSuffix" $dot -}}
277 {{- $annotations := default "" $dot.Values.service.headless.annotations -}}
278 {{- $publishNotReadyAddresses := default false $dot.Values.service.headless.publishNotReadyAddresses -}}
279 {{- $ports := $dot.Values.service.headlessPorts -}}
280 {{- $labels := default (dict) .labels -}}
281 {{- $matchLabels := default (dict) .matchLabels -}}
282 {{- if ($dot.Values.metrics) }}
283 {{-   range $index, $metricPort := $dot.Values.metrics.ports }}
284 {{-     $ports = append $ports $metricPort }}
285 {{-   end }}
286 {{- end }}
287 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
288 {{- end -}}
289
290 {{/*
291   Generate the right suffix for headless service
292 */}}
293 {{- define "common._makeHeadlessSuffix" -}}
294 {{-   if hasKey .Values.service.headless "suffix" }}
295 {{-     .Values.service.headless.suffix }}
296 {{-   else }}
297 {{-     print "headless" }}
298 {{-   end }}
299 {{- end -}}
300
301 {{/*
302   Calculate if we need to use TLS ports.
303   We use TLS by default unless we're on service mesh with TLS.
304   We can also override this behavior with override toggles:
305   - .Values.global.tlsEnabled  : override default TLS behavior for all charts
306   - .Values.tlsOverride : override global and default TLS on a per chart basis
307
308   this will give these combinations:
309   | tlsOverride | global.tlsEnabled | global.serviceMesh.enabled | global.serviceMesh.tls | result |
310   |-------------|-------------------|----------------------------|------------------------|--------|
311   | not present | not present       | not present                | any                    | true   |
312   | not present | not present       | false                      | any                    | true   |
313   | not present | not present       | true                       | false                  | true   |
314   | not present | not present       | true                       | true                   | false  |
315   | not present | true              | any                        | any                    | true   |
316   | not present | false             | any                        | any                    | false  |
317   | true        | any               | any                        | any                    | true   |
318   | false       | any               | any                        | any                    | false  |
319
320 */}}
321 {{- define "common.needTLS" -}}
322 {{-   if hasKey .Values "tlsOverride" }}
323 {{-     if .Values.tlsOverride -}}
324 true
325 {{-       end }}
326 {{-   else }}
327 {{-     if hasKey .Values.global "tlsEnabled" }}
328 {{-       if .Values.global.tlsEnabled }}
329 true
330 {{-       end }}
331 {{-     else }}
332 {{-       if not (include "common.onServiceMesh" .) -}}
333 true
334 {{-       else }}
335 {{-         if not (default false .Values.global.serviceMesh.tls) -}}
336 true
337 {{-         end }}
338 {{-       end }}
339 {{-     end }}
340 {{-   end }}
341 {{- end -}}
342
343 {{/*
344   generate needed scheme:
345     - https if needTLS
346     - http if not
347 */}}
348
349 {{- define "common.scheme" -}}
350   {{- ternary "https" "http" (eq "true" (include "common.needTLS" .)) }}
351 {{- end -}}
352
353 {{- define "common.port.buildCache" -}}
354   {{- $global := . }}
355   {{- if not $global.Values._DmaapDrNodePortsCache }}
356     {{- $portCache := dict }}
357     {{- range $port := .Values.service.ports }}
358       {{- $_ := set $portCache $port.name (dict "port" $port.port "plain_port" $port.plain_port) }}
359     {{- end }}
360     {{- $_ := set $global.Values "_DmaapDrNodePortsCache" $portCache }}
361   {{- end }}
362 {{- end -}}
363
364 {/*
365   Get Port value according to its name and if we want tls or plain port.
366   The template takes below arguments:
367     - .global: environment (.)
368     - .name: name of the port
369     - .getPlain: boolean allowing to choose between tls (false, default) or
370                  plain (true)
371     If plain_port is not set and we ask for plain, it will return empty.
372 */}
373 {{- define "common.getPort" -}}
374   {{- $global := .global }}
375   {{- $name := .name }}
376   {{- $getPlain := default false .getPlain }}
377   {{- include "common.port.buildCache" $global }}
378   {{- $portCache := $global.Values._DmaapDrNodePortsCache }}
379   {{- $port := index $portCache $name }}
380   {{- ternary $port.plain_port $port.port $getPlain }}
381 {{- end -}}