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.port_protocol }}
130 name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
132 name: {{ $port.name }}
134 {{- if (eq $serviceType "NodePort") }}
135 nodePort: {{ include "common.nodePortPrefix" (dict "dot" $dot "useNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
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.port_protocol }}
146 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
148 name: {{ $port.name }}
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 }}
158 protocol: {{ default "TCP" $port.l4_protocol }}
160 {{- if $port.port_protocol }}
161 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
163 name: {{ $port.name }}-plain
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
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 -}}
202 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
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 }}
211 {{- if $publishNotReadyAddresses }}
212 publishNotReadyAddresses: true
214 type: {{ $serviceType }}
215 selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
216 sessionAffinity: {{ $sessionAffinity }}
220 Create service template
221 Will create one or two service templates according to this table:
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 |
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.
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 -}}
245 {{- if (and (include "common.needTLS" $dot) $both_tls_and_plain) }}
246 {{ 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) }}
247 {{- if (ne $serviceType "ClusterIP") }}
250 {{- $suffix = printf "%s-external" $suffix }}
252 {{- $suffix = "external" }}
254 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
257 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
261 {{/* Create headless service template */}}
262 {{- define "common.headlessService" -}}
263 {{- $dot := default . .dot -}}
264 {{- $suffix := include "common._makeHeadlessSuffix" $dot -}}
265 {{- $annotations := default "" $dot.Values.service.headless.annotations -}}
266 {{- $publishNotReadyAddresses := default false $dot.Values.service.headless.publishNotReadyAddresses -}}
267 {{- $ports := $dot.Values.service.headlessPorts -}}
268 {{- $labels := default (dict) .labels -}}
269 {{- $matchLabels := default (dict) .matchLabels -}}
270 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
274 Generate the right suffix for headless service
276 {{- define "common._makeHeadlessSuffix" -}}
277 {{- if hasKey .Values.service.headless "suffix" }}
278 {{- .Values.service.headless.suffix }}
280 {{- print "headless" }}
285 Calculate if we need to use TLS ports.
286 We use TLS by default unless we're on service mesh with TLS.
287 We can also override this behavior with override toggles:
288 - .Values.global.tlsEnabled : override default TLS behavior for all charts
289 - .Values.tlsOverride : override global and default TLS on a per chart basis
291 this will give these combinations:
292 | tlsOverride | global.tlsEnabled | global.serviceMesh.enabled | global.serviceMesh.tls | result |
293 |-------------|-------------------|----------------------------|------------------------|--------|
294 | not present | not present | not present | any | true |
295 | not present | not present | false | any | true |
296 | not present | not present | true | false | true |
297 | not present | not present | true | true | false |
298 | not present | true | any | any | true |
299 | not present | false | any | any | false |
300 | true | any | any | any | true |
301 | false | any | any | any | false |
304 {{- define "common.needTLS" -}}
305 {{- if hasKey .Values "tlsOverride" }}
306 {{- if .Values.tlsOverride -}}
310 {{- if hasKey .Values.global "tlsEnabled" }}
311 {{- if .Values.global.tlsEnabled }}
315 {{- if not (include "common.onServiceMesh" .) -}}
318 {{- if not (default false .Values.global.serviceMesh.tls) -}}
327 generate needed scheme:
332 {{- define "common.scheme" -}}
333 {{- ternary "https" "http" (eq "true" (include "common.needTLS" .)) }}
336 {{- define "common.port.buildCache" -}}
338 {{- if not $global.Values._DmaapDrNodePortsCache }}
339 {{- $portCache := dict }}
340 {{- range $port := .Values.service.ports }}
341 {{- $_ := set $portCache $port.name (dict "port" $port.port "plain_port" $port.plain_port) }}
343 {{- $_ := set $global.Values "_DmaapDrNodePortsCache" $portCache }}
348 Get Port value according to its name and if we want tls or plain port.
349 The template takes below arguments:
350 - .global: environment (.)
351 - .name: name of the port
352 - .getPlain: boolean allowing to choose between tls (false, default) or
354 If plain_port is not set and we ask for plain, it will return empty.
356 {{- define "common.getPort" -}}
357 {{- $global := .global }}
358 {{- $name := .name }}
359 {{- $getPlain := default false .getPlain }}
360 {{- include "common.port.buildCache" $global }}
361 {{- $portCache := $global.Values._DmaapDrNodePortsCache }}
362 {{- $port := index $portCache $name }}
363 {{- ternary $port.plain_port $port.port $getPlain }}