[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       {
58           "serviceName": "{{ include "common.servicename" $dot }}",
59           "version": "{{ default "v1" $msb_informations.version }}",
60           "url": "{{ default "/" $msb_informations.url }}",
61           "protocol": "{{ default "REST" $msb_informations.protocol }}",
62           "port": "{{ $msb_informations.port }}",
63           "visualRange":"{{ default "1" $msb_informations.visualRange }}"
64       }
65       ]'
66 {{-   end}}
67 {{- end }}
68 name: {{ include "common.servicename" $dot }}{{ if $suffix }}{{ print "-" $suffix }}{{ end }}
69 namespace: {{ include "common.namespace" $dot }}
70 labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 2 -}}
71 {{- end -}}
72
73 {{/* Define the ports of Service
74      The function takes three arguments (inside a dictionary):
75      - .dot : environment (.)
76      - .ports : an array of ports
77      - .serviceType: the type of the service
78      - .add_plain_port: add tls port AND plain port
79 */}}
80 {{- define "common.servicePorts" -}}
81 {{- $serviceType := .serviceType }}
82 {{- $dot := .dot }}
83 {{- $add_plain_port := default false .add_plain_port }}
84 {{-   range $index, $port := .ports }}
85 {{-     if (include "common.needTLS" $dot) }}
86 - port: {{ $port.port }}
87   targetPort: {{ $port.name }}
88 {{-       if $port.port_protocol }}
89   name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
90 {{-       else }}
91   name: {{ $port.name }}
92 {{-       end }}
93 {{-       if (eq $serviceType "NodePort") }}
94   nodePort: {{ $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}{{ $port.nodePort }}
95 {{-       end }}
96 {{-     else }}
97 - port: {{ default $port.port $port.plain_port }}
98   targetPort: {{ $port.name }}
99 {{-       if $port.port_protocol }}
100   name: {{ printf "%s-%s" $port.port_protocol $port.name }}
101 {{-       else }}
102   name: {{ $port.name }}
103 {{-       end }}
104 {{-     end }}
105 {{-     if (and (and (include "common.needTLS" $dot) $add_plain_port) $port.plain_port)  }}
106 {{-       if (eq $serviceType "ClusterIP")  }}
107 - port: {{ $port.plain_port }}
108   targetPort: {{ $port.name }}-plain
109 {{-         if $port.port_protocol }}
110   name: {{ printf "%s-%s" $port.port_protocol $port.name }}
111 {{-         else }}
112   name: {{ $port.name }}-plain
113 {{-         end }}
114 {{-       end }}
115 {{-     end }}
116 {{-   end }}
117 {{- end -}}
118
119 {{/* Create generic service template
120      The function takes several arguments (inside a dictionary):
121      - .dot : environment (.)
122      - .ports : an array of ports
123      - .serviceType: the type of the service
124      - .suffix : a string which will be added at the end of the name (with a '-')
125      - .annotations: the annotations to add
126      - .msb_informations: msb information in order to create msb annotation
127      - .publishNotReadyAddresses: if we publish not ready address
128      - .headless: if the service is headless
129      - .add_plain_port: add tls port AND plain port
130      - .labels : labels to add (dict)
131      - .matchLabels: selectors/machLabels to add (dict)
132      - .sessionAffinity: ClientIP  - enables sticky sessions based on client IP, default: None
133 */}}
134 {{- define "common.genericService" -}}
135 {{- $dot := default . .dot -}}
136 {{- $suffix := default "" .suffix -}}
137 {{- $annotations := default "" .annotations -}}
138 {{- $msb_informations := default "" .msb_informations -}}
139 {{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
140 {{- $serviceType := .serviceType -}}
141 {{- $ports := .ports -}}
142 {{- $headless := default false .headless -}}
143 {{- $add_plain_port := default false .add_plain_port }}
144 {{- $labels := default (dict) .labels -}}
145 {{- $matchLabels := default (dict) .matchLabels -}}
146 {{- $sessionAffinity := default "None" $dot.Values.service.sessionAffinity -}}
147 apiVersion: v1
148 kind: Service
149 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
150 spec:
151   {{- if $headless }}
152   clusterIP: None
153   {{- end }}
154   ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot "add_plain_port" $add_plain_port) | nindent 4 }}
155   {{- if $publishNotReadyAddresses }}
156   publishNotReadyAddresses: true
157   {{- end }}
158   type: {{ $serviceType }}
159   selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
160   sessionAffinity: {{ $sessionAffinity }}
161 {{- end -}}
162
163 {{/*
164     Create service template
165     Will create one or two service templates according to this table:
166
167     | serviceType   | both_tls_and_plain | result       |
168     |---------------|--------------------|--------------|
169     | ClusterIP     | any                | one Service  |
170     | Not ClusterIP | not present        | one Service  |
171     | Not ClusterIP | false              | one Service  |
172     | Not ClusterIP | true               | two Services |
173
174     If two services are created, one is ClusterIP with both crypted and plain
175     ports and the other one is NodePort (or LoadBalancer) with crypted port only.
176 */}}
177 {{- define "common.service" -}}
178 {{-   $dot := default . .dot -}}
179 {{-   $suffix := default "" $dot.Values.service.suffix -}}
180 {{-   $annotations := default "" $dot.Values.service.annotations -}}
181 {{-   $publishNotReadyAddresses := default false $dot.Values.service.publishNotReadyAddresses -}}
182 {{-   $msb_informations := default "" $dot.Values.service.msb -}}
183 {{-   $serviceType := $dot.Values.service.type -}}
184 {{-   $ports := $dot.Values.service.ports -}}
185 {{-   $both_tls_and_plain:= default false $dot.Values.service.both_tls_and_plain }}
186 {{-   $labels := default (dict) .labels -}}
187 {{-   $matchLabels := default (dict) .matchLabels -}}
188
189 {{-   if (and (include "common.needTLS" .) $both_tls_and_plain) }}
190 {{      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) }}
191 {{-     if (ne $serviceType "ClusterIP") }}
192 ---
193 {{-       if $suffix }}
194 {{-         $suffix = printf "%s-external" $suffix }}
195 {{-       else }}
196 {{-         $suffix = "external" }}
197 {{-       end }}
198 {{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
199 {{-     end }}
200 {{-   else }}
201 {{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
202 {{-   end }}
203 {{- end -}}
204
205 {{/* Create headless service template */}}
206 {{- define "common.headlessService" -}}
207 {{- $dot := default . .dot -}}
208 {{- $suffix := include "common._makeHeadlessSuffix" $dot -}}
209 {{- $annotations := default "" $dot.Values.service.headless.annotations -}}
210 {{- $publishNotReadyAddresses := default false $dot.Values.service.headless.publishNotReadyAddresses -}}
211 {{- $ports := $dot.Values.service.headlessPorts -}}
212 {{- $labels := default (dict) .labels -}}
213 {{- $matchLabels := default (dict) .matchLabels -}}
214 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
215 {{- end -}}
216
217 {{/*
218   Generate the right suffix for headless service
219 */}}
220 {{- define "common._makeHeadlessSuffix" -}}
221 {{-   if hasKey .Values.service.headless "suffix" }}
222 {{-     .Values.service.headless.suffix }}
223 {{-   else }}
224 {{-     print "headless" }}
225 {{-   end }}
226 {{- end -}}
227
228 {{/*
229   Calculate if we need to use TLS ports.
230   We use TLS by default unless we're on service mesh with TLS.
231   We can also override this behavior with override toggles:
232   - .Values.global.tlsEnabled  : override default TLS behavior for all charts
233   - .Values.tlsOverride : override global and default TLS on a per chart basis
234
235   this will give these combinations:
236   | tlsOverride | global.tlsEnabled | global.serviceMesh.enabled | global.serviceMesh.tls | result |
237   |-------------|-------------------|----------------------------|------------------------|--------|
238   | not present | not present       | not present                | any                    | true   |
239   | not present | not present       | false                      | any                    | true   |
240   | not present | not present       | true                       | false                  | true   |
241   | not present | not present       | true                       | true                   | false  |
242   | not present | true              | any                        | any                    | true   |
243   | not present | false             | any                        | any                    | false  |
244   | true        | any               | any                        | any                    | true   |
245   | false       | any               | any                        | any                    | false  |
246
247 */}}
248 {{- define "common.needTLS" -}}
249 {{-   if hasKey .Values "tlsOverride" }}
250 {{-     if .Values.tlsOverride -}}
251 true
252 {{-       end }}
253 {{-   else }}
254 {{-     if hasKey .Values.global "tlsEnabled" }}
255 {{-       if .Values.global.tlsEnabled }}
256 true
257 {{-       end }}
258 {{-     else }}
259 {{-       if not (include "common.onServiceMesh" .) -}}
260 true
261 {{-       else }}
262 {{-         if not (default false .Values.global.serviceMesh.tls) -}}
263 true
264 {{-         end }}
265 {{-       end }}
266 {{-     end }}
267 {{-   end }}
268 {{- end -}}