[COMMON] Disable Nodeports in _service template
[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.port_protocol }}
130   name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
131 {{-       else }}
132   name: {{ $port.name }}
133 {{-       end }}
134 {{-       if (eq $serviceType "NodePort") }}
135   nodePort: {{ include "common.nodePortPrefix" (dict "dot" $dot "useNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
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.port_protocol }}
146   name: {{ printf "%s-%s" $port.port_protocol $port.name }}
147 {{-       else }}
148   name: {{ $port.name }}
149 {{-       end }}
150 {{-     end }}
151 {{-     if (and (and (include "common.needTLS" $dot) $add_plain_port) $port.plain_port)  }}
152 {{-       if (eq $serviceType "ClusterIP")  }}
153 - port: {{ $port.plain_port }}
154   targetPort: {{ $port.name }}-plain
155 {{-       if $port.plain_l4_port_protocol }}
156   protocol: {{ $port.plain_port_l4_protocol }}
157 {{-       else }}
158   protocol: {{ default "TCP" $port.l4_protocol  }}
159 {{-       end }}
160 {{-         if $port.port_protocol }}
161   name: {{ printf "%s-%s" $port.port_protocol $port.name }}
162 {{-         else }}
163   name: {{ $port.name }}-plain
164 {{-         end }}
165 {{-       end }}
166 {{-     end }}
167 {{-   end }}
168 {{- end -}}
169
170 {{/* Create generic service template
171      The function takes several arguments (inside a dictionary):
172      - .dot : environment (.)
173      - .ports : an array of ports
174      - .serviceType: the type of the service
175      - .suffix : a string which will be added at the end of the name (with a '-')
176      - .annotations: the annotations to add
177      - .msb_informations: msb information in order to create msb annotation
178      - .publishNotReadyAddresses: if we publish not ready address
179      - .headless: if the service is headless
180      - .add_plain_port: add tls port AND plain port
181      - .labels : labels to add (dict)
182      - .matchLabels: selectors/machLabels to add (dict)
183      - .sessionAffinity: ClientIP  - enables sticky sessions based on client IP, default: None
184 */}}
185 {{- define "common.genericService" -}}
186 {{- $dot := default . .dot -}}
187 {{- $suffix := default "" .suffix -}}
188 {{- $annotations := default "" .annotations -}}
189 {{- $msb_informations := default "" .msb_informations -}}
190 {{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
191 {{- $serviceType := .serviceType -}}
192 {{- $ports := .ports -}}
193 {{- $headless := default false .headless -}}
194 {{- $add_plain_port := default false .add_plain_port }}
195 {{- $labels := default (dict) .labels -}}
196 {{- $matchLabels := default (dict) .matchLabels -}}
197 {{- $sessionAffinity := default "None" $dot.Values.service.sessionAffinity -}}
198 {{- $kubeTargetVersion := default $dot.Capabilities.KubeVersion.Version | trimPrefix "v" -}}
199 {{- $ipFamilyPolicy := default "PreferDualStack" $dot.Values.service.ipFamilyPolicy -}}
200 apiVersion: v1
201 kind: Service
202 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
203 spec:
204   {{- if $headless }}
205   clusterIP: None
206   {{- end }}
207   ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot "add_plain_port" $add_plain_port) | nindent 4 }}
208   {{- if semverCompare ">=1.20.0" $kubeTargetVersion }}
209   ipFamilyPolicy: {{ $ipFamilyPolicy }}
210   {{- end }}
211   {{- if $publishNotReadyAddresses }}
212   publishNotReadyAddresses: true
213   {{- end }}
214   type: {{ $serviceType }}
215   selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
216   sessionAffinity: {{ $sessionAffinity }}
217 {{- end -}}
218
219 {{/*
220     Create service template
221     Will create one or two service templates according to this table:
222
223     | serviceType   | both_tls_and_plain | result       |
224     |---------------|--------------------|--------------|
225     | ClusterIP     | any                | one Service  |
226     | Not ClusterIP | not present        | one Service  |
227     | Not ClusterIP | false              | one Service  |
228     | Not ClusterIP | true               | two Services |
229
230     If two services are created, one is ClusterIP with both crypted and plain
231     ports and the other one is NodePort (or LoadBalancer) with crypted port only.
232 */}}
233 {{- define "common.service" -}}
234 {{-   $dot := default . .dot -}}
235 {{-   $suffix := default "" $dot.Values.service.suffix -}}
236 {{-   $annotations := default "" $dot.Values.service.annotations -}}
237 {{-   $publishNotReadyAddresses := default false $dot.Values.service.publishNotReadyAddresses -}}
238 {{-   $msb_informations := default "" $dot.Values.service.msb -}}
239 {{-   $serviceType := $dot.Values.service.type -}}
240 {{-   $ports := $dot.Values.service.ports -}}
241 {{-   $both_tls_and_plain:= default false $dot.Values.service.both_tls_and_plain }}
242 {{-   $labels := default (dict) .labels -}}
243 {{-   $matchLabels := default (dict) .matchLabels -}}
244 {{-   if and (include "common.onServiceMesh" $dot) (eq $serviceType "NodePort") }}
245 {{-   $serviceType = "ClusterIP" }}
246 {{-   end }}
247
248 {{-   if (and (include "common.needTLS" $dot) $both_tls_and_plain) }}
249 {{      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) }}
250 {{-     if (ne $serviceType "ClusterIP") }}
251 ---
252 {{-       if $suffix }}
253 {{-         $suffix = printf "%s-external" $suffix }}
254 {{-       else }}
255 {{-         $suffix = "external" }}
256 {{-       end }}
257 {{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
258 {{-     end }}
259 {{-   else }}
260 {{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
261 {{-   end }}
262 {{- end -}}
263
264 {{/* Create headless service template */}}
265 {{- define "common.headlessService" -}}
266 {{- $dot := default . .dot -}}
267 {{- $suffix := include "common._makeHeadlessSuffix" $dot -}}
268 {{- $annotations := default "" $dot.Values.service.headless.annotations -}}
269 {{- $publishNotReadyAddresses := default false $dot.Values.service.headless.publishNotReadyAddresses -}}
270 {{- $ports := $dot.Values.service.headlessPorts -}}
271 {{- $labels := default (dict) .labels -}}
272 {{- $matchLabels := default (dict) .matchLabels -}}
273 {{- if ($dot.Values.metrics) }}
274 {{-   range $index, $metricPort := $dot.Values.metrics.ports }}
275 {{-     $ports = append $ports $metricPort }}
276 {{-   end }}
277 {{- end }}
278 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
279 {{- end -}}
280
281 {{/*
282   Generate the right suffix for headless service
283 */}}
284 {{- define "common._makeHeadlessSuffix" -}}
285 {{-   if hasKey .Values.service.headless "suffix" }}
286 {{-     .Values.service.headless.suffix }}
287 {{-   else }}
288 {{-     print "headless" }}
289 {{-   end }}
290 {{- end -}}
291
292 {{/*
293   Calculate if we need to use TLS ports.
294   We use TLS by default unless we're on service mesh with TLS.
295   We can also override this behavior with override toggles:
296   - .Values.global.tlsEnabled  : override default TLS behavior for all charts
297   - .Values.tlsOverride : override global and default TLS on a per chart basis
298
299   this will give these combinations:
300   | tlsOverride | global.tlsEnabled | global.serviceMesh.enabled | global.serviceMesh.tls | result |
301   |-------------|-------------------|----------------------------|------------------------|--------|
302   | not present | not present       | not present                | any                    | true   |
303   | not present | not present       | false                      | any                    | true   |
304   | not present | not present       | true                       | false                  | true   |
305   | not present | not present       | true                       | true                   | false  |
306   | not present | true              | any                        | any                    | true   |
307   | not present | false             | any                        | any                    | false  |
308   | true        | any               | any                        | any                    | true   |
309   | false       | any               | any                        | any                    | false  |
310
311 */}}
312 {{- define "common.needTLS" -}}
313 {{-   if hasKey .Values "tlsOverride" }}
314 {{-     if .Values.tlsOverride -}}
315 true
316 {{-       end }}
317 {{-   else }}
318 {{-     if hasKey .Values.global "tlsEnabled" }}
319 {{-       if .Values.global.tlsEnabled }}
320 true
321 {{-       end }}
322 {{-     else }}
323 {{-       if not (include "common.onServiceMesh" .) -}}
324 true
325 {{-       else }}
326 {{-         if not (default false .Values.global.serviceMesh.tls) -}}
327 true
328 {{-         end }}
329 {{-       end }}
330 {{-     end }}
331 {{-   end }}
332 {{- end -}}
333
334 {{/*
335   generate needed scheme:
336     - https if needTLS
337     - http if not
338 */}}
339
340 {{- define "common.scheme" -}}
341   {{- ternary "https" "http" (eq "true" (include "common.needTLS" .)) }}
342 {{- end -}}
343
344 {{- define "common.port.buildCache" -}}
345   {{- $global := . }}
346   {{- if not $global.Values._DmaapDrNodePortsCache }}
347     {{- $portCache := dict }}
348     {{- range $port := .Values.service.ports }}
349       {{- $_ := set $portCache $port.name (dict "port" $port.port "plain_port" $port.plain_port) }}
350     {{- end }}
351     {{- $_ := set $global.Values "_DmaapDrNodePortsCache" $portCache }}
352   {{- end }}
353 {{- end -}}
354
355 {/*
356   Get Port value according to its name and if we want tls or plain port.
357   The template takes below arguments:
358     - .global: environment (.)
359     - .name: name of the port
360     - .getPlain: boolean allowing to choose between tls (false, default) or
361                  plain (true)
362     If plain_port is not set and we ask for plain, it will return empty.
363 */}
364 {{- define "common.getPort" -}}
365   {{- $global := .global }}
366   {{- $name := .name }}
367   {{- $getPlain := default false .getPlain }}
368   {{- include "common.port.buildCache" $global }}
369   {{- $portCache := $global.Values._DmaapDrNodePortsCache }}
370   {{- $port := index $portCache $name }}
371   {{- ternary $port.plain_port $port.port $getPlain }}
372 {{- end -}}