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 "-" -}}
33 {{/* Define the metadata of Service
34 The function takes from one to four arguments (inside a dictionary):
35 - .dot : environment (.)
36 - .suffix : a string which will be added at the end of the name (with a '-').
37 - .annotations: the annotations to add
38 - .msb_informations: msb information in order to create msb annotation
39 - .labels : labels to add
41 {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
42 {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
44 {{- define "common.serviceMetadata" -}}
45 {{- $dot := default . .dot -}}
46 {{- $suffix := default "" .suffix -}}
47 {{- $annotations := default "" .annotations -}}
48 {{- $msb_informations := default "" .msb_informations -}}
49 {{- $labels := default (dict) .labels -}}
50 {{- if or $annotations $msb_informations -}}
52 {{- if $annotations }}
53 {{ include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
55 {{- if $msb_informations }}
56 msb.onap.org/service-info: '[
57 {{- range $index, $msb_information := $msb_informations }}
62 "serviceName": "{{ default (include "common.servicename" $dot) $msb_information.serviceName }}",
63 "version": "{{ default "v1" $msb_information.version }}",
64 "url": "{{ default "/" $msb_information.url }}",
65 "protocol": "{{ default "REST" $msb_information.protocol }}",
66 "port": "{{ $msb_information.port }}",
67 "visualRange":"{{ default "1" $msb_information.visualRange }}"
73 name: {{ include "common.servicename" $dot }}{{ if $suffix }}{{ print "-" $suffix }}{{ end }}
74 namespace: {{ include "common.namespace" $dot }}
75 labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 2 -}}
78 {{/* Define the ports of Service
79 The function takes three arguments (inside a dictionary):
80 - .dot : environment (.)
81 - .ports : an array of ports
82 - .serviceType: the type of the service
83 - .add_plain_port: add tls port AND plain port
85 {{- define "common.servicePorts" -}}
86 {{- $serviceType := .serviceType }}
88 {{- $add_plain_port := default false .add_plain_port }}
89 {{- range $index, $port := .ports }}
90 {{- if (include "common.needTLS" $dot) }}
91 - port: {{ $port.port }}
92 targetPort: {{ $port.name }}
93 {{- if $port.port_protocol }}
94 name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
96 name: {{ $port.name }}
98 {{- if (eq $serviceType "NodePort") }}
99 nodePort: {{ $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}{{ $port.nodePort }}
102 - port: {{ default $port.port $port.plain_port }}
103 targetPort: {{ $port.name }}
104 {{- if $port.port_protocol }}
105 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
107 name: {{ $port.name }}
110 {{- if (and (and (include "common.needTLS" $dot) $add_plain_port) $port.plain_port) }}
111 {{- if (eq $serviceType "ClusterIP") }}
112 - port: {{ $port.plain_port }}
113 targetPort: {{ $port.name }}-plain
114 {{- if $port.port_protocol }}
115 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
117 name: {{ $port.name }}-plain
124 {{/* Create generic service template
125 The function takes several arguments (inside a dictionary):
126 - .dot : environment (.)
127 - .ports : an array of ports
128 - .serviceType: the type of the service
129 - .suffix : a string which will be added at the end of the name (with a '-')
130 - .annotations: the annotations to add
131 - .msb_informations: msb information in order to create msb annotation
132 - .publishNotReadyAddresses: if we publish not ready address
133 - .headless: if the service is headless
134 - .add_plain_port: add tls port AND plain port
135 - .labels : labels to add (dict)
136 - .matchLabels: selectors/machLabels to add (dict)
137 - .sessionAffinity: ClientIP - enables sticky sessions based on client IP, default: None
139 {{- define "common.genericService" -}}
140 {{- $dot := default . .dot -}}
141 {{- $suffix := default "" .suffix -}}
142 {{- $annotations := default "" .annotations -}}
143 {{- $msb_informations := default "" .msb_informations -}}
144 {{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
145 {{- $serviceType := .serviceType -}}
146 {{- $ports := .ports -}}
147 {{- $headless := default false .headless -}}
148 {{- $add_plain_port := default false .add_plain_port }}
149 {{- $labels := default (dict) .labels -}}
150 {{- $matchLabels := default (dict) .matchLabels -}}
151 {{- $sessionAffinity := default "None" $dot.Values.service.sessionAffinity -}}
154 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
159 ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot "add_plain_port" $add_plain_port) | nindent 4 }}
160 {{- if $publishNotReadyAddresses }}
161 publishNotReadyAddresses: true
163 type: {{ $serviceType }}
164 selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
165 sessionAffinity: {{ $sessionAffinity }}
169 Create service template
170 Will create one or two service templates according to this table:
172 | serviceType | both_tls_and_plain | result |
173 |---------------|--------------------|--------------|
174 | ClusterIP | any | one Service |
175 | Not ClusterIP | not present | one Service |
176 | Not ClusterIP | false | one Service |
177 | Not ClusterIP | true | two Services |
179 If two services are created, one is ClusterIP with both crypted and plain
180 ports and the other one is NodePort (or LoadBalancer) with crypted port only.
182 {{- define "common.service" -}}
183 {{- $dot := default . .dot -}}
184 {{- $suffix := default "" $dot.Values.service.suffix -}}
185 {{- $annotations := default "" $dot.Values.service.annotations -}}
186 {{- $publishNotReadyAddresses := default false $dot.Values.service.publishNotReadyAddresses -}}
187 {{- $msb_informations := default "" $dot.Values.service.msb -}}
188 {{- $serviceType := $dot.Values.service.type -}}
189 {{- $ports := $dot.Values.service.ports -}}
190 {{- $both_tls_and_plain:= default false $dot.Values.service.both_tls_and_plain }}
191 {{- $labels := default (dict) .labels -}}
192 {{- $matchLabels := default (dict) .matchLabels -}}
194 {{- if (and (include "common.needTLS" .) $both_tls_and_plain) }}
195 {{ 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) }}
196 {{- if (ne $serviceType "ClusterIP") }}
199 {{- $suffix = printf "%s-external" $suffix }}
201 {{- $suffix = "external" }}
203 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
206 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
210 {{/* Create headless service template */}}
211 {{- define "common.headlessService" -}}
212 {{- $dot := default . .dot -}}
213 {{- $suffix := include "common._makeHeadlessSuffix" $dot -}}
214 {{- $annotations := default "" $dot.Values.service.headless.annotations -}}
215 {{- $publishNotReadyAddresses := default false $dot.Values.service.headless.publishNotReadyAddresses -}}
216 {{- $ports := $dot.Values.service.headlessPorts -}}
217 {{- $labels := default (dict) .labels -}}
218 {{- $matchLabels := default (dict) .matchLabels -}}
219 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
223 Generate the right suffix for headless service
225 {{- define "common._makeHeadlessSuffix" -}}
226 {{- if hasKey .Values.service.headless "suffix" }}
227 {{- .Values.service.headless.suffix }}
229 {{- print "headless" }}
234 Calculate if we need to use TLS ports.
235 We use TLS by default unless we're on service mesh with TLS.
236 We can also override this behavior with override toggles:
237 - .Values.global.tlsEnabled : override default TLS behavior for all charts
238 - .Values.tlsOverride : override global and default TLS on a per chart basis
240 this will give these combinations:
241 | tlsOverride | global.tlsEnabled | global.serviceMesh.enabled | global.serviceMesh.tls | result |
242 |-------------|-------------------|----------------------------|------------------------|--------|
243 | not present | not present | not present | any | true |
244 | not present | not present | false | any | true |
245 | not present | not present | true | false | true |
246 | not present | not present | true | true | false |
247 | not present | true | any | any | true |
248 | not present | false | any | any | false |
249 | true | any | any | any | true |
250 | false | any | any | any | false |
253 {{- define "common.needTLS" -}}
254 {{- if hasKey .Values "tlsOverride" }}
255 {{- if .Values.tlsOverride -}}
259 {{- if hasKey .Values.global "tlsEnabled" }}
260 {{- if .Values.global.tlsEnabled }}
264 {{- if not (include "common.onServiceMesh" .) -}}
267 {{- if not (default false .Values.global.serviceMesh.tls) -}}