Merge "[CONSUL] Add limits to consul chart."
[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         "enable_ssl": {{ default false $msb_information.enable_ssl }},
98         "port": "{{ $msb_information.port }}",
99         "visualRange":"{{ default "1" $msb_information.visualRange }}"
100       }
101 {{-    end }}
102     ]'
103 {{-   end}}
104 {{- end }}
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 -}}
108 {{- end -}}
109
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
116 */}}
117 {{- define "common.servicePorts" -}}
118 {{- $serviceType := .serviceType }}
119 {{- $dot := .dot }}
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 }}
127 {{-       else }}
128   name: {{ $port.name }}
129 {{-       end }}
130 {{-       if (eq $serviceType "NodePort") }}
131   nodePort: {{ include "common.nodePortPrefix" (dict "dot" $dot "portNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
132 {{-       end }}
133 {{-     else }}
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 }}
138 {{-       else }}
139   name: {{ $port.name }}
140 {{-       end }}
141 {{-     end }}
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 }}
148 {{-         else }}
149   name: {{ $port.name }}-plain
150 {{-         end }}
151 {{-       end }}
152 {{-     end }}
153 {{-   end }}
154 {{- end -}}
155
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
170 */}}
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 -}}
184 apiVersion: v1
185 kind: Service
186 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
187 spec:
188   {{- if $headless }}
189   clusterIP: None
190   {{- end }}
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
194   {{- end }}
195   type: {{ $serviceType }}
196   selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
197   sessionAffinity: {{ $sessionAffinity }}
198 {{- end -}}
199
200 {{/*
201     Create service template
202     Will create one or two service templates according to this table:
203
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 |
210
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.
213 */}}
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 -}}
225
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") }}
229 ---
230 {{-       if $suffix }}
231 {{-         $suffix = printf "%s-external" $suffix }}
232 {{-       else }}
233 {{-         $suffix = "external" }}
234 {{-       end }}
235 {{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
236 {{-     end }}
237 {{-   else }}
238 {{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
239 {{-   end }}
240 {{- end -}}
241
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) }}
252 {{- end -}}
253
254 {{/*
255   Generate the right suffix for headless service
256 */}}
257 {{- define "common._makeHeadlessSuffix" -}}
258 {{-   if hasKey .Values.service.headless "suffix" }}
259 {{-     .Values.service.headless.suffix }}
260 {{-   else }}
261 {{-     print "headless" }}
262 {{-   end }}
263 {{- end -}}
264
265 {{/*
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
271
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  |
283
284 */}}
285 {{- define "common.needTLS" -}}
286 {{-   if hasKey .Values "tlsOverride" }}
287 {{-     if .Values.tlsOverride -}}
288 true
289 {{-       end }}
290 {{-   else }}
291 {{-     if hasKey .Values.global "tlsEnabled" }}
292 {{-       if .Values.global.tlsEnabled }}
293 true
294 {{-       end }}
295 {{-     else }}
296 {{-       if not (include "common.onServiceMesh" .) -}}
297 true
298 {{-       else }}
299 {{-         if not (default false .Values.global.serviceMesh.tls) -}}
300 true
301 {{-         end }}
302 {{-       end }}
303 {{-     end }}
304 {{-   end }}
305 {{- end -}}
306
307 {{- define "common.port.buildCache" -}}
308   {{- $global := . }}
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) }}
313     {{- end }}
314     {{- $_ := set $global.Values "_DmaapDrNodePortsCache" $portCache }}
315   {{- end }}
316 {{- end -}}
317
318 {/*
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
324                  plain (true)
325     If plain_port is not set and we ask for plain, it will return empty.
326 */}
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 }}
335 {{- end -}}