Merge "[DOC] update list of host to set"
[oom.git] / kubernetes / common / common / templates / _service.tpl
1 {{/*
2 # Copyright © 2017 Amdocs, Bell Canada
3 #
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
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 */}}
16
17 {{/*
18   Resolve the name of a chart's service.
19
20   The default will be the chart name (or .Values.nameOverride if set).
21   And the use of .Values.service.name overrides all.
22
23   - .Values.service.name: override default service (ie. chart) name
24 */}}
25 {{/*
26   Expand the service name for a chart.
27 */}}
28 {{- define "common.servicename" -}}
29   {{- $name := default .Chart.Name .Values.nameOverride -}}
30   {{- default $name .Values.service.name | trunc 63 | trimSuffix "-" -}}
31 {{- end -}}
32
33 {{/*
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
37                                             will be use locally;
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
45                                             service or on port;
46   - .Values.service.nodePortPrefix        : value used on a pert chart basis if
47                                             no other version exists.
48
49   The function takes two arguments (inside a dictionary):
50      - .dot : environment (.)
51      - .useNodePortExt : does the port use the "extended" nodeport part or the
52                          normal one?
53 */}}
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 }}
59 {{-   else -}}
60 {{      $dot.Values.service.nodePortPrefixOverride | default $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}
61 {{-   end -}}
62 {{- end -}}
63
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
71      Usage example:
72       {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
73       {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
74 */}}
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 -}}
82 annotations:
83 {{-   if $annotations }}
84 {{      include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
85 {{-   end }}
86 {{-   if $msb_informations }}
87   msb.onap.org/service-info: '[
88 {{-     range $index, $msb_information := $msb_informations }}
89 {{-       if ne $index 0 }}
90       ,
91 {{-       end }}
92       {
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 }}"
99       }
100 {{-    end }}
101     ]'
102 {{-   end}}
103 {{- end }}
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 -}}
107 {{- end -}}
108
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
115 */}}
116 {{- define "common.servicePorts" -}}
117 {{- $serviceType := .serviceType }}
118 {{- $dot := .dot }}
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 }}
126 {{-       else }}
127   name: {{ $port.name }}
128 {{-       end }}
129 {{-       if (eq $serviceType "NodePort") }}
130   nodePort: {{ include "common.nodePortPrefix" (dict "dot" $dot "portNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
131 {{-       end }}
132 {{-     else }}
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 }}
137 {{-       else }}
138   name: {{ $port.name }}
139 {{-       end }}
140 {{-     end }}
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 }}
147 {{-         else }}
148   name: {{ $port.name }}-plain
149 {{-         end }}
150 {{-       end }}
151 {{-     end }}
152 {{-   end }}
153 {{- end -}}
154
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
169 */}}
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 -}}
183 apiVersion: v1
184 kind: Service
185 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
186 spec:
187   {{- if $headless }}
188   clusterIP: None
189   {{- end }}
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
193   {{- end }}
194   type: {{ $serviceType }}
195   selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
196   sessionAffinity: {{ $sessionAffinity }}
197 {{- end -}}
198
199 {{/*
200     Create service template
201     Will create one or two service templates according to this table:
202
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 |
209
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.
212 */}}
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 -}}
224
225 {{-   if (and (include "common.needTLS" $dot) $both_tls_and_plain) }}
226 {{      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) }}
227 {{-     if (ne $serviceType "ClusterIP") }}
228 ---
229 {{-       if $suffix }}
230 {{-         $suffix = printf "%s-external" $suffix }}
231 {{-       else }}
232 {{-         $suffix = "external" }}
233 {{-       end }}
234 {{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
235 {{-     end }}
236 {{-   else }}
237 {{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
238 {{-   end }}
239 {{- end -}}
240
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) }}
251 {{- end -}}
252
253 {{/*
254   Generate the right suffix for headless service
255 */}}
256 {{- define "common._makeHeadlessSuffix" -}}
257 {{-   if hasKey .Values.service.headless "suffix" }}
258 {{-     .Values.service.headless.suffix }}
259 {{-   else }}
260 {{-     print "headless" }}
261 {{-   end }}
262 {{- end -}}
263
264 {{/*
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
270
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  |
282
283 */}}
284 {{- define "common.needTLS" -}}
285 {{-   if hasKey .Values "tlsOverride" }}
286 {{-     if .Values.tlsOverride -}}
287 true
288 {{-       end }}
289 {{-   else }}
290 {{-     if hasKey .Values.global "tlsEnabled" }}
291 {{-       if .Values.global.tlsEnabled }}
292 true
293 {{-       end }}
294 {{-     else }}
295 {{-       if not (include "common.onServiceMesh" .) -}}
296 true
297 {{-       else }}
298 {{-         if not (default false .Values.global.serviceMesh.tls) -}}
299 true
300 {{-         end }}
301 {{-       end }}
302 {{-     end }}
303 {{-   end }}
304 {{- end -}}
305
306 {{- define "common.port.buildCache" -}}
307   {{- $global := . }}
308   {{- if not $global.Values._DmaapDrNodePortsCache }}
309     {{- $portCache := dict }}
310     {{- range $port := .Values.service.ports }}
311       {{- $_ := set $portCache $port.name (dict "port" $port.port "plain_port" $port.plain_port) }}
312     {{- end }}
313     {{- $_ := set $global.Values "_DmaapDrNodePortsCache" $portCache }}
314   {{- end }}
315 {{- end -}}
316
317 {/*
318   Get Port value according to its name and if we want tls or plain port.
319   The template takes below arguments:
320     - .global: environment (.)
321     - .name: name of the port
322     - .getPlain: boolean allowing to choose between tls (false, default) or
323                  plain (true)
324     If plain_port is not set and we ask for plain, it will return empty.
325 */}
326 {{- define "common.getPort" -}}
327   {{- $global := .global }}
328   {{- $name := .name }}
329   {{- $getPlain := default false .getPlain }}
330   {{- include "common.port.buildCache" $global }}
331   {{- $portCache := $global.Values._DmaapDrNodePortsCache }}
332   {{- $port := index $portCache $name }}
333   {{- ternary $port.plain_port $port.port $getPlain }}
334 {{- end -}}