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 "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.port_protocol }}
125 name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
127 name: {{ $port.name }}
129 {{- if (eq $serviceType "NodePort") }}
130 nodePort: {{ include "common.nodePortPrefix" (dict "dot" $dot "portNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
133 - port: {{ default $port.port $port.plain_port }}
134 targetPort: {{ $port.name }}
135 {{- if $port.port_protocol }}
136 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
138 name: {{ $port.name }}
141 {{- if (and (and (include "common.needTLS" $dot) $add_plain_port) $port.plain_port) }}
142 {{- if (eq $serviceType "ClusterIP") }}
143 - port: {{ $port.plain_port }}
144 targetPort: {{ $port.name }}-plain
145 {{- if $port.port_protocol }}
146 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
148 name: {{ $port.name }}-plain
155 {{/* Create generic service template
156 The function takes several arguments (inside a dictionary):
157 - .dot : environment (.)
158 - .ports : an array of ports
159 - .serviceType: the type of the service
160 - .suffix : a string which will be added at the end of the name (with a '-')
161 - .annotations: the annotations to add
162 - .msb_informations: msb information in order to create msb annotation
163 - .publishNotReadyAddresses: if we publish not ready address
164 - .headless: if the service is headless
165 - .add_plain_port: add tls port AND plain port
166 - .labels : labels to add (dict)
167 - .matchLabels: selectors/machLabels to add (dict)
168 - .sessionAffinity: ClientIP - enables sticky sessions based on client IP, default: None
170 {{- define "common.genericService" -}}
171 {{- $dot := default . .dot -}}
172 {{- $suffix := default "" .suffix -}}
173 {{- $annotations := default "" .annotations -}}
174 {{- $msb_informations := default "" .msb_informations -}}
175 {{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
176 {{- $serviceType := .serviceType -}}
177 {{- $ports := .ports -}}
178 {{- $headless := default false .headless -}}
179 {{- $add_plain_port := default false .add_plain_port }}
180 {{- $labels := default (dict) .labels -}}
181 {{- $matchLabels := default (dict) .matchLabels -}}
182 {{- $sessionAffinity := default "None" $dot.Values.service.sessionAffinity -}}
185 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
190 ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot "add_plain_port" $add_plain_port) | nindent 4 }}
191 {{- if $publishNotReadyAddresses }}
192 publishNotReadyAddresses: true
194 type: {{ $serviceType }}
195 selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
196 sessionAffinity: {{ $sessionAffinity }}
200 Create service template
201 Will create one or two service templates according to this table:
203 | serviceType | both_tls_and_plain | result |
204 |---------------|--------------------|--------------|
205 | ClusterIP | any | one Service |
206 | Not ClusterIP | not present | one Service |
207 | Not ClusterIP | false | one Service |
208 | Not ClusterIP | true | two Services |
210 If two services are created, one is ClusterIP with both crypted and plain
211 ports and the other one is NodePort (or LoadBalancer) with crypted port only.
213 {{- define "common.service" -}}
214 {{- $dot := default . .dot -}}
215 {{- $suffix := default "" $dot.Values.service.suffix -}}
216 {{- $annotations := default "" $dot.Values.service.annotations -}}
217 {{- $publishNotReadyAddresses := default false $dot.Values.service.publishNotReadyAddresses -}}
218 {{- $msb_informations := default "" $dot.Values.service.msb -}}
219 {{- $serviceType := $dot.Values.service.type -}}
220 {{- $ports := $dot.Values.service.ports -}}
221 {{- $both_tls_and_plain:= default false $dot.Values.service.both_tls_and_plain }}
222 {{- $labels := default (dict) .labels -}}
223 {{- $matchLabels := default (dict) .matchLabels -}}
225 {{- if (and (include "common.needTLS" .) $both_tls_and_plain) }}
226 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "add_plain_port" true $labels "matchLabels" $matchLabels) }}
227 {{- if (ne $serviceType "ClusterIP") }}
230 {{- $suffix = printf "%s-external" $suffix }}
232 {{- $suffix = "external" }}
234 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
237 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
241 {{/* Create headless service template */}}
242 {{- define "common.headlessService" -}}
243 {{- $dot := default . .dot -}}
244 {{- $suffix := include "common._makeHeadlessSuffix" $dot -}}
245 {{- $annotations := default "" $dot.Values.service.headless.annotations -}}
246 {{- $publishNotReadyAddresses := default false $dot.Values.service.headless.publishNotReadyAddresses -}}
247 {{- $ports := $dot.Values.service.headlessPorts -}}
248 {{- $labels := default (dict) .labels -}}
249 {{- $matchLabels := default (dict) .matchLabels -}}
250 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
254 Generate the right suffix for headless service
256 {{- define "common._makeHeadlessSuffix" -}}
257 {{- if hasKey .Values.service.headless "suffix" }}
258 {{- .Values.service.headless.suffix }}
260 {{- print "headless" }}
265 Calculate if we need to use TLS ports.
266 We use TLS by default unless we're on service mesh with TLS.
267 We can also override this behavior with override toggles:
268 - .Values.global.tlsEnabled : override default TLS behavior for all charts
269 - .Values.tlsOverride : override global and default TLS on a per chart basis
271 this will give these combinations:
272 | tlsOverride | global.tlsEnabled | global.serviceMesh.enabled | global.serviceMesh.tls | result |
273 |-------------|-------------------|----------------------------|------------------------|--------|
274 | not present | not present | not present | any | true |
275 | not present | not present | false | any | true |
276 | not present | not present | true | false | true |
277 | not present | not present | true | true | false |
278 | not present | true | any | any | true |
279 | not present | false | any | any | false |
280 | true | any | any | any | true |
281 | false | any | any | any | false |
284 {{- define "common.needTLS" -}}
285 {{- if hasKey .Values "tlsOverride" }}
286 {{- if .Values.tlsOverride -}}
290 {{- if hasKey .Values.global "tlsEnabled" }}
291 {{- if .Values.global.tlsEnabled }}
295 {{- if not (include "common.onServiceMesh" .) -}}
298 {{- if not (default false .Values.global.serviceMesh.tls) -}}