2 # Copyright © 2017 Amdocs, Bell Canada
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 Resolve the name of a chart's service.
19 The default will be the chart name (or .Values.nameOverride if set).
20 And the use of .Values.service.name overrides all.
22 - .Values.service.name: override default service (ie. chart) name
25 Expand the service name for a chart.
27 {{- define "common.servicename" -}}
28 {{- $name := default .Chart.Name .Values.nameOverride -}}
29 {{- default $name .Values.service.name | trunc 63 | trimSuffix "-" -}}
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
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
45 - .Values.service.nodePortPrefix : value used on a pert chart basis if
46 no other version exists.
48 The function takes two arguments (inside a dictionary):
49 - .dot : environment (.)
50 - .useNodePortExt : does the port use the "extended" nodeport part or the
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 }}
59 {{ $dot.Values.service.nodePortPrefixOverride | default $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}
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
71 {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
72 {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
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 -}}
82 {{- if $annotations }}
83 {{ include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
85 {{- if $msb_informations }}
86 msb.onap.org/service-info: '[
87 {{- range $index, $msb_information := $msb_informations }}
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 }}"
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 -}}
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
116 {{- define "common.servicePorts" -}}
117 {{- $serviceType := .serviceType }}
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 }}
129 {{- if $port.app_protocol }}
130 appProtocol: {{ $port.app_protocol }}
132 {{- if $port.port_protocol }}
133 name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
135 name: {{ $port.name }}
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 }}
143 protocol: {{ default "TCP" $port.l4_protocol }}
145 {{- if $port.app_protocol }}
146 appProtocol: {{ $port.app_protocol }}
148 {{- if $port.port_protocol }}
149 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
151 name: {{ $port.name }}
154 {{- if (eq $serviceType "NodePort") }}
155 nodePort: {{ include "common.nodePortPrefix" (dict "dot" $dot "useNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
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 }}
164 protocol: {{ default "TCP" $port.l4_protocol }}
166 {{- if $port.app_protocol }}
167 appProtocol: {{ $port.app_protocol }}
169 {{- if $port.port_protocol }}
170 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
172 name: {{ $port.name }}-plain
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
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 -}}
211 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
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 }}
220 {{- if $publishNotReadyAddresses }}
221 publishNotReadyAddresses: true
223 type: {{ $serviceType }}
224 selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
225 sessionAffinity: {{ $sessionAffinity }}
229 Create service template
230 Will create one or two service templates according to this table:
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 |
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.
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" }}
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") }}
262 {{- $suffix = printf "%s-external" $suffix }}
264 {{- $suffix = "external" }}
266 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
269 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
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 }}
287 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
291 Generate the right suffix for headless service
293 {{- define "common._makeHeadlessSuffix" -}}
294 {{- if hasKey .Values.service.headless "suffix" }}
295 {{- .Values.service.headless.suffix }}
297 {{- print "headless" }}
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
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 |
321 {{- define "common.needTLS" -}}
322 {{- if hasKey .Values "tlsOverride" }}
323 {{- if .Values.tlsOverride -}}
327 {{- if hasKey .Values.global "tlsEnabled" }}
328 {{- if .Values.global.tlsEnabled }}
332 {{- if not (include "common.onServiceMesh" .) -}}
335 {{- if not (default false .Values.global.serviceMesh.tls) -}}
344 generate needed scheme:
349 {{- define "common.scheme" -}}
350 {{- ternary "https" "http" (eq "true" (include "common.needTLS" .)) }}
353 {{- define "common.port.buildCache" -}}
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) }}
360 {{- $_ := set $global.Values "_DmaapDrNodePortsCache" $portCache }}
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
371 If plain_port is not set and we ask for plain, it will return empty.
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 }}