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.
18 Resolve the name of a chart's service.
20 The default will be the chart name (or .Values.nameOverride if set).
21 And the use of .Values.service.name overrides all.
23 - .Values.service.name: override default service (ie. chart) name
26 Expand the service name for a chart.
28 {{- define "common.servicename" -}}
29 {{- $name := default .Chart.Name .Values.nameOverride -}}
30 {{- default $name .Values.service.name | trunc 63 | trimSuffix "-" -}}
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
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
46 - .Values.service.nodePortPrefix : value used on a pert chart basis if
47 no other version exists.
49 The function takes two arguments (inside a dictionary):
50 - .dot : environment (.)
51 - .useNodePortExt : does the port use the "extended" nodeport part or the
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 }}
60 {{ $dot.Values.service.nodePortPrefixOverride | default $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}
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
72 {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
73 {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
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 -}}
83 {{- if $annotations }}
84 {{ include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
86 {{- if $msb_informations }}
87 msb.onap.org/service-info: '[
88 {{- range $index, $msb_information := $msb_informations }}
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 }}"
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 -}}
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
117 {{- define "common.servicePorts" -}}
118 {{- $serviceType := .serviceType }}
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 }}
128 name: {{ $port.name }}
130 {{- if (eq $serviceType "NodePort") }}
131 nodePort: {{ include "common.nodePortPrefix" (dict "dot" $dot "useNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
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 }}
139 name: {{ $port.name }}
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 }}
149 name: {{ $port.name }}-plain
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
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 -}}
186 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
191 ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot "add_plain_port" $add_plain_port) | nindent 4 }}
192 {{- if $publishNotReadyAddresses }}
193 publishNotReadyAddresses: true
195 type: {{ $serviceType }}
196 selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
197 sessionAffinity: {{ $sessionAffinity }}
201 Create service template
202 Will create one or two service templates according to this table:
204 | serviceType | both_tls_and_plain | result |
205 |---------------|--------------------|--------------|
206 | ClusterIP | any | one Service |
207 | Not ClusterIP | not present | one Service |
208 | Not ClusterIP | false | one Service |
209 | Not ClusterIP | true | two Services |
211 If two services are created, one is ClusterIP with both crypted and plain
212 ports and the other one is NodePort (or LoadBalancer) with crypted port only.
214 {{- define "common.service" -}}
215 {{- $dot := default . .dot -}}
216 {{- $suffix := default "" $dot.Values.service.suffix -}}
217 {{- $annotations := default "" $dot.Values.service.annotations -}}
218 {{- $publishNotReadyAddresses := default false $dot.Values.service.publishNotReadyAddresses -}}
219 {{- $msb_informations := default "" $dot.Values.service.msb -}}
220 {{- $serviceType := $dot.Values.service.type -}}
221 {{- $ports := $dot.Values.service.ports -}}
222 {{- $both_tls_and_plain:= default false $dot.Values.service.both_tls_and_plain }}
223 {{- $labels := default (dict) .labels -}}
224 {{- $matchLabels := default (dict) .matchLabels -}}
226 {{- if (and (include "common.needTLS" $dot) $both_tls_and_plain) }}
227 {{ 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) }}
228 {{- if (ne $serviceType "ClusterIP") }}
231 {{- $suffix = printf "%s-external" $suffix }}
233 {{- $suffix = "external" }}
235 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
238 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
242 {{/* Create headless service template */}}
243 {{- define "common.headlessService" -}}
244 {{- $dot := default . .dot -}}
245 {{- $suffix := include "common._makeHeadlessSuffix" $dot -}}
246 {{- $annotations := default "" $dot.Values.service.headless.annotations -}}
247 {{- $publishNotReadyAddresses := default false $dot.Values.service.headless.publishNotReadyAddresses -}}
248 {{- $ports := $dot.Values.service.headlessPorts -}}
249 {{- $labels := default (dict) .labels -}}
250 {{- $matchLabels := default (dict) .matchLabels -}}
251 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
255 Generate the right suffix for headless service
257 {{- define "common._makeHeadlessSuffix" -}}
258 {{- if hasKey .Values.service.headless "suffix" }}
259 {{- .Values.service.headless.suffix }}
261 {{- print "headless" }}
266 Calculate if we need to use TLS ports.
267 We use TLS by default unless we're on service mesh with TLS.
268 We can also override this behavior with override toggles:
269 - .Values.global.tlsEnabled : override default TLS behavior for all charts
270 - .Values.tlsOverride : override global and default TLS on a per chart basis
272 this will give these combinations:
273 | tlsOverride | global.tlsEnabled | global.serviceMesh.enabled | global.serviceMesh.tls | result |
274 |-------------|-------------------|----------------------------|------------------------|--------|
275 | not present | not present | not present | any | true |
276 | not present | not present | false | any | true |
277 | not present | not present | true | false | true |
278 | not present | not present | true | true | false |
279 | not present | true | any | any | true |
280 | not present | false | any | any | false |
281 | true | any | any | any | true |
282 | false | any | any | any | false |
285 {{- define "common.needTLS" -}}
286 {{- if hasKey .Values "tlsOverride" }}
287 {{- if .Values.tlsOverride -}}
291 {{- if hasKey .Values.global "tlsEnabled" }}
292 {{- if .Values.global.tlsEnabled }}
296 {{- if not (include "common.onServiceMesh" .) -}}
299 {{- if not (default false .Values.global.serviceMesh.tls) -}}
307 {{- define "common.port.buildCache" -}}
309 {{- if not $global.Values._DmaapDrNodePortsCache }}
310 {{- $portCache := dict }}
311 {{- range $port := .Values.service.ports }}
312 {{- $_ := set $portCache $port.name (dict "port" $port.port "plain_port" $port.plain_port) }}
314 {{- $_ := set $global.Values "_DmaapDrNodePortsCache" $portCache }}
319 Get Port value according to its name and if we want tls or plain port.
320 The template takes below arguments:
321 - .global: environment (.)
322 - .name: name of the port
323 - .getPlain: boolean allowing to choose between tls (false, default) or
325 If plain_port is not set and we ask for plain, it will return empty.
327 {{- define "common.getPort" -}}
328 {{- $global := .global }}
329 {{- $name := .name }}
330 {{- $getPlain := default false .getPlain }}
331 {{- include "common.port.buildCache" $global }}
332 {{- $portCache := $global.Values._DmaapDrNodePortsCache }}
333 {{- $port := index $portCache $name }}
334 {{- ternary $port.plain_port $port.port $getPlain }}