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