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 three 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
40 {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
41 {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
43 {{- define "common.serviceMetadata" -}}
44 {{- $dot := default . .dot -}}
45 {{- $suffix := default "" .suffix -}}
46 {{- $annotations := default "" .annotations -}}
47 {{- $msb_informations := default "" .msb_informations -}}
48 {{- if or $annotations $msb_informations -}}
50 {{- if $annotations }}
51 {{ include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
53 {{- if $msb_informations }}
54 msb.onap.org/service-info: '[
56 "serviceName": "{{ include "common.servicename" $dot }}",
57 "version": "{{ default "v1" $msb_informations.version }}",
58 "url": "{{ default "/" $msb_informations.url }}",
59 "protocol": "{{ default "REST" $msb_informations.protocol }}",
60 "port": "{{ $msb_informations.port }}",
61 "visualRange":"{{ default "1" $msb_informations.visualRange }}"
66 name: {{ include "common.servicename" $dot }}{{ if $suffix }}{{ print "-" $suffix }}{{ end }}
67 namespace: {{ include "common.namespace" $dot }}
68 labels: {{- include "common.labels" $dot | nindent 2 -}}
71 {{/* Define the ports of Service
72 The function takes three arguments (inside a dictionary):
73 - .dot : environment (.)
74 - .ports : an array of ports
75 - .serviceType: the type of the service
76 - .add_plain_port: add tls port AND plain port
78 {{- define "common.servicePorts" -}}
79 {{- $serviceType := .serviceType }}
81 {{- $add_plain_port := default false .add_plain_port }}
82 {{- range $index, $port := .ports }}
83 {{- if (include "common.needTLS" $dot) }}
84 - port: {{ $port.port }}
85 targetPort: {{ $port.name }}
86 {{- if $port.port_protocol }}
87 name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
89 name: {{ $port.name }}
91 {{- if (eq $serviceType "NodePort") }}
92 nodePort: {{ $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}{{ $port.nodePort }}
95 - port: {{ default $port.port $port.plain_port }}
96 targetPort: {{ $port.name }}
97 {{- if $port.port_protocol }}
98 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
100 name: {{ $port.name }}
103 {{- if (and (and (include "common.needTLS" $dot) $add_plain_port) $port.plain_port) }}
104 {{- if (eq $serviceType "ClusterIP") }}
105 - port: {{ $port.plain_port }}
106 targetPort: {{ $port.name }}-plain
107 {{- if $port.port_protocol }}
108 name: {{ printf "%s-%s" $port.port_protocol $port.name }}
110 name: {{ $port.name }}-plain
117 {{/* Create generic service template
118 The function takes several arguments (inside a dictionary):
119 - .dot : environment (.)
120 - .ports : an array of ports
121 - .serviceType: the type of the service
122 - .suffix : a string which will be added at the end of the name (with a '-')
123 - .annotations: the annotations to add
124 - .msb_informations: msb information in order to create msb annotation
125 - .publishNotReadyAddresses: if we publish not ready address
126 - .headless: if the service is headless
127 - .add_plain_port: add tls port AND plain port
129 {{- define "common.genericService" -}}
130 {{- $dot := default . .dot -}}
131 {{- $suffix := default "" .suffix -}}
132 {{- $annotations := default "" .annotations -}}
133 {{- $msb_informations := default "" .msb_informations -}}
134 {{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
135 {{- $serviceType := .serviceType -}}
136 {{- $ports := .ports -}}
137 {{- $headless := default false .headless -}}
138 {{- $add_plain_port := default false .add_plain_port }}
141 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "dot" $dot) | nindent 2 }}
146 ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot "add_plain_port" $add_plain_port) | nindent 4 }}
147 {{- if $publishNotReadyAddresses }}
148 publishNotReadyAddresses: true
150 type: {{ $serviceType }}
151 selector: {{- include "common.matchLabels" $dot | nindent 4 }}
155 Create service template
156 Will create one or two service templates according to this table:
158 | serviceType | both_tls_and_plain | result |
159 |---------------|--------------------|--------------|
160 | ClusterIP | any | one Service |
161 | Not ClusterIP | not present | one Service |
162 | Not ClusterIP | false | one Service |
163 | Not ClusterIP | true | two Services |
165 If two services are created, one is ClusterIP with both crypted and plain
166 ports and the other one is NodePort (or LoadBalancer) with crypted port only.
168 {{- define "common.service" -}}
169 {{- $suffix := default "" .Values.service.suffix -}}
170 {{- $annotations := default "" .Values.service.annotations -}}
171 {{- $publishNotReadyAddresses := default false .Values.service.publishNotReadyAddresses -}}
172 {{- $msb_informations := default "" .Values.service.msb -}}
173 {{- $serviceType := .Values.service.type -}}
174 {{- $ports := .Values.service.ports -}}
175 {{- $both_tls_and_plain:= default false .Values.service.both_tls_and_plain }}
176 {{- if (and (include "common.needTLS" .) $both_tls_and_plain) }}
177 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "add_plain_port" true) }}
178 {{- if (ne $serviceType "ClusterIP") }}
181 {{- $suffix = printf "%s-external" $suffix }}
183 {{- $suffix = "external" }}
185 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType) }}
188 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType) }}
192 {{/* Create headless service template */}}
193 {{- define "common.headlessService" -}}
194 {{- $suffix := include "common._makeHeadlessSuffix" . -}}
195 {{- $annotations := default "" .Values.service.headless.annotations -}}
196 {{- $publishNotReadyAddresses := default false .Values.service.headless.publishNotReadyAddresses -}}
197 {{- $ports := .Values.service.headlessPorts -}}
198 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true ) }}
202 Generate the right suffix for headless service
204 {{- define "common._makeHeadlessSuffix" -}}
205 {{- if hasKey .Values.service.headless "suffix" }}
206 {{- .Values.service.headless.suffix }}
208 {{- print "headless" }}
213 Calculate if we need to use TLS ports.
214 We use TLS by default unless we're on service mesh with TLS.
215 We can also override this behavior with override toggles:
216 - .Values.global.tlsEnabled : override default TLS behavior for all charts
217 - .Values.tlsOverride : override global and default TLS on a per chart basis
219 this will give these combinations:
220 | tlsOverride | global.tlsEnabled | global.serviceMesh.enabled | global.serviceMesh.tls | result |
221 |-------------|-------------------|----------------------------|------------------------|--------|
222 | not present | not present | not present | any | true |
223 | not present | not present | false | any | true |
224 | not present | not present | true | false | true |
225 | not present | not present | true | true | false |
226 | not present | true | any | any | true |
227 | not present | false | any | any | false |
228 | true | any | any | any | true |
229 | false | any | any | any | false |
232 {{- define "common.needTLS" -}}
233 {{- if hasKey .Values "tlsOverride" }}
234 {{- if .Values.tlsOverride -}}
238 {{- if hasKey .Values.global "tlsEnabled" }}
239 {{- if .Values.global.tlsEnabled }}
243 {{- if not (include "common.onServiceMesh" .) -}}
246 {{- if not (default false .Values.global.serviceMesh.tls) -}}