Merge "[SO] Correct connection configuration to sdnc"
[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 {{/* 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
40      Usage example:
41       {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
42       {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
43 */}}
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 -}}
51 annotations:
52 {{-   if $annotations }}
53 {{      include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
54 {{-   end }}
55 {{-   if $msb_informations }}
56   msb.onap.org/service-info: '[
57 {{-     range $index, $msb_information := $msb_informations }}
58 {{-       if ne $index 0 }}
59       ,
60 {{-       end }}
61       {
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 }}"
68       }
69 {{-    end }}
70     ]'
71 {{-   end}}
72 {{- end }}
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 -}}
76 {{- end -}}
77
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
84 */}}
85 {{- define "common.servicePorts" -}}
86 {{- $serviceType := .serviceType }}
87 {{- $dot := .dot }}
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 }}
95 {{-       else }}
96   name: {{ $port.name }}
97 {{-       end }}
98 {{-       if (eq $serviceType "NodePort") }}
99   nodePort: {{ $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}{{ $port.nodePort }}
100 {{-       end }}
101 {{-     else }}
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 }}
106 {{-       else }}
107   name: {{ $port.name }}
108 {{-       end }}
109 {{-     end }}
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 }}
116 {{-         else }}
117   name: {{ $port.name }}-plain
118 {{-         end }}
119 {{-       end }}
120 {{-     end }}
121 {{-   end }}
122 {{- end -}}
123
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
138 */}}
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 -}}
152 apiVersion: v1
153 kind: Service
154 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
155 spec:
156   {{- if $headless }}
157   clusterIP: None
158   {{- end }}
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
162   {{- end }}
163   type: {{ $serviceType }}
164   selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
165   sessionAffinity: {{ $sessionAffinity }}
166 {{- end -}}
167
168 {{/*
169     Create service template
170     Will create one or two service templates according to this table:
171
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 |
178
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.
181 */}}
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 -}}
193
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") }}
197 ---
198 {{-       if $suffix }}
199 {{-         $suffix = printf "%s-external" $suffix }}
200 {{-       else }}
201 {{-         $suffix = "external" }}
202 {{-       end }}
203 {{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
204 {{-     end }}
205 {{-   else }}
206 {{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
207 {{-   end }}
208 {{- end -}}
209
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) }}
220 {{- end -}}
221
222 {{/*
223   Generate the right suffix for headless service
224 */}}
225 {{- define "common._makeHeadlessSuffix" -}}
226 {{-   if hasKey .Values.service.headless "suffix" }}
227 {{-     .Values.service.headless.suffix }}
228 {{-   else }}
229 {{-     print "headless" }}
230 {{-   end }}
231 {{- end -}}
232
233 {{/*
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
239
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  |
251
252 */}}
253 {{- define "common.needTLS" -}}
254 {{-   if hasKey .Values "tlsOverride" }}
255 {{-     if .Values.tlsOverride -}}
256 true
257 {{-       end }}
258 {{-   else }}
259 {{-     if hasKey .Values.global "tlsEnabled" }}
260 {{-       if .Values.global.tlsEnabled }}
261 true
262 {{-       end }}
263 {{-     else }}
264 {{-       if not (include "common.onServiceMesh" .) -}}
265 true
266 {{-       else }}
267 {{-         if not (default false .Values.global.serviceMesh.tls) -}}
268 true
269 {{-         end }}
270 {{-       end }}
271 {{-     end }}
272 {{-   end }}
273 {{- end -}}