Merge "Nginx ingress config loadbalancer option"
[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 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
39      Usage example:
40       {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
41       {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
42 */}}
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 -}}
49 annotations:
50 {{-   if $annotations }}
51 {{      include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
52 {{-   end }}
53 {{-   if $msb_informations }}
54   msb.onap.org/service-info: '[
55       {
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 }}"
62       }
63       ]'
64 {{-   end}}
65 {{- end }}
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 -}}
69 {{- end -}}
70
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
77 */}}
78 {{- define "common.servicePorts" -}}
79 {{- $serviceType := .serviceType }}
80 {{- $dot := .dot }}
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 }}
88 {{-       else }}
89   name: {{ $port.name }}
90 {{-       end }}
91 {{-       if (eq $serviceType "NodePort") }}
92   nodePort: {{ $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}{{ $port.nodePort }}
93 {{-       end }}
94 {{-     else }}
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 }}
99 {{-       else }}
100   name: {{ $port.name }}
101 {{-       end }}
102 {{-     end }}
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 }}
109 {{-         else }}
110   name: {{ $port.name }}-plain
111 {{-         end }}
112 {{-       end }}
113 {{-     end }}
114 {{-   end }}
115 {{- end -}}
116
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
128 */}}
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 }}
139 apiVersion: v1
140 kind: Service
141 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "dot" $dot) | nindent 2 }}
142 spec:
143   {{- if $headless }}
144   clusterIP: None
145   {{- end }}
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
149   {{- end }}
150   type: {{ $serviceType }}
151   selector: {{- include "common.matchLabels" $dot | nindent 4 }}
152 {{- end -}}
153
154 {{/*
155     Create service template
156     Will create one or two service templates according to this table:
157
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 |
164
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.
167 */}}
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") }}
179 ---
180 {{-       if $suffix }}
181 {{-         $suffix = printf "%s-external" $suffix }}
182 {{-       else }}
183 {{-         $suffix = "external" }}
184 {{-       end }}
185 {{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType) }}
186 {{-     end }}
187 {{-   else }}
188 {{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType) }}
189 {{-   end }}
190 {{- end -}}
191
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 ) }}
199 {{- end -}}
200
201 {{/*
202   Generate the right suffix for headless service
203 */}}
204 {{- define "common._makeHeadlessSuffix" -}}
205 {{-   if hasKey .Values.service.headless "suffix" }}
206 {{-     .Values.service.headless.suffix }}
207 {{-   else }}
208 {{-     print "headless" }}
209 {{-   end }}
210 {{- end -}}
211
212 {{/*
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
218
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  |
230
231 */}}
232 {{- define "common.needTLS" -}}
233 {{-   if hasKey .Values "tlsOverride" }}
234 {{-     if .Values.tlsOverride -}}
235 true
236 {{-       end }}
237 {{-   else }}
238 {{-     if hasKey .Values.global "tlsEnabled" }}
239 {{-       if .Values.global.tlsEnabled }}
240 true
241 {{-       end }}
242 {{-     else }}
243 {{-       if not (include "common.onServiceMesh" .) -}}
244 true
245 {{-       else }}
246 {{-         if not (default false .Values.global.serviceMesh.tls) -}}
247 true
248 {{-         end }}
249 {{-       end }}
250 {{-     end }}
251 {{-   end }}
252 {{- end -}}