aboutsummaryrefslogtreecommitdiffstats
path: root/kubernetes/common/common/templates/_service.tpl
blob: 8b430ef2bd772ee7a81788c1c17483dd28d98659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
{{/*
# Copyright © 2017 Amdocs, Bell Canada
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/}}

{{/*
  Resolve the name of a chart's service.

  The default will be the chart name (or .Values.nameOverride if set).
  And the use of .Values.service.name overrides all.

  - .Values.service.name: override default service (ie. chart) name
*/}}
{{/*
  Expand the service name for a chart.
*/}}
{{- define "common.servicename" -}}
  {{- $name := default .Chart.Name .Values.nameOverride -}}
  {{- default $name .Values.service.name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/* Define the metadata of Service
     The function takes from one to four arguments (inside a dictionary):
     - .dot : environment (.)
     - .suffix : a string which will be added at the end of the name (with a '-').
     - .annotations: the annotations to add
     - .msb_informations: msb information in order to create msb annotation
     - .labels : labels to add
     Usage example:
      {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
      {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
*/}}
{{- define "common.serviceMetadata" -}}
  {{- $dot := default . .dot -}}
  {{- $suffix := default "" .suffix -}}
  {{- $annotations := default "" .annotations -}}
  {{- $msb_informations := default "" .msb_informations -}}
  {{- $labels := default (dict) .labels -}}
{{- if or $annotations $msb_informations -}}
annotations:
{{-   if $annotations }}
{{      include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
{{-   end }}
{{-   if $msb_informations }}
  msb.onap.org/service-info: '[
      {
          "serviceName": "{{ include "common.servicename" $dot }}",
          "version": "{{ default "v1" $msb_informations.version }}",
          "url": "{{ default "/" $msb_informations.url }}",
          "protocol": "{{ default "REST" $msb_informations.protocol }}",
          "port": "{{ $msb_informations.port }}",
          "visualRange":"{{ default "1" $msb_informations.visualRange }}"
      }
      ]'
{{-   end}}
{{- end }}
name: {{ include "common.servicename" $dot }}{{ if $suffix }}{{ print "-" $suffix }}{{ end }}
namespace: {{ include "common.namespace" $dot }}
labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 2 -}}
{{- end -}}

{{/* Define the ports of Service
     The function takes three arguments (inside a dictionary):
     - .dot : environment (.)
     - .ports : an array of ports
     - .serviceType: the type of the service
     - .add_plain_port: add tls port AND plain port
*/}}
{{- define "common.servicePorts" -}}
{{- $serviceType := .serviceType }}
{{- $dot := .dot }}
{{- $add_plain_port := default false .add_plain_port }}
{{-   range $index, $port := .ports }}
{{-     if (include "common.needTLS" $dot) }}
- port: {{ $port.port }}
  targetPort: {{ $port.name }}
{{-       if $port.port_protocol }}
  name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
{{-       else }}
  name: {{ $port.name }}
{{-       end }}
{{-       if (eq $serviceType "NodePort") }}
  nodePort: {{ $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}{{ $port.nodePort }}
{{-       end }}
{{-     else }}
- port: {{ default $port.port $port.plain_port }}
  targetPort: {{ $port.name }}
{{-       if $port.port_protocol }}
  name: {{ printf "%s-%s" $port.port_protocol $port.name }}
{{-       else }}
  name: {{ $port.name }}
{{-       end }}
{{-     end }}
{{-     if (and (and (include "common.needTLS" $dot) $add_plain_port) $port.plain_port)  }}
{{-       if (eq $serviceType "ClusterIP")  }}
- port: {{ $port.plain_port }}
  targetPort: {{ $port.name }}-plain
{{-         if $port.port_protocol }}
  name: {{ printf "%s-%s" $port.port_protocol $port.name }}
{{-         else }}
  name: {{ $port.name }}-plain
{{-         end }}
{{-       end }}
{{-     end }}
{{-   end }}
{{- end -}}

{{/* Create generic service template
     The function takes several arguments (inside a dictionary):
     - .dot : environment (.)
     - .ports : an array of ports
     - .serviceType: the type of the service
     - .suffix : a string which will be added at the end of the name (with a '-')
     - .annotations: the annotations to add
     - .msb_informations: msb information in order to create msb annotation
     - .publishNotReadyAddresses: if we publish not ready address
     - .headless: if the service is headless
     - .add_plain_port: add tls port AND plain port
     - .labels : labels to add (dict)
     - .matchLabels: selectors/machLabels to add (dict)
     - .sessionAffinity: ClientIP  - enables sticky sessions based on client IP, default: None
*/}}
{{- define "common.genericService" -}}
{{- $dot := default . .dot -}}
{{- $suffix := default "" .suffix -}}
{{- $annotations := default "" .annotations -}}
{{- $msb_informations := default "" .msb_informations -}}
{{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
{{- $serviceType := .serviceType -}}
{{- $ports := .ports -}}
{{- $headless := default false .headless -}}
{{- $add_plain_port := default false .add_plain_port }}
{{- $labels := default (dict) .labels -}}
{{- $matchLabels := default (dict) .matchLabels -}}
{{- $sessionAffinity := default "None" $dot.Values.service.sessionAffinity -}}
apiVersion: v1
kind: Service
metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
spec:
  {{- if $headless }}
  clusterIP: None
  {{- end }}
  ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot "add_plain_port" $add_plain_port) | nindent 4 }}
  {{- if $publishNotReadyAddresses }}
  publishNotReadyAddresses: true
  {{- end }}
  type: {{ $serviceType }}
  selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
  sessionAffinity: {{ $sessionAffinity }}
{{- end -}}

{{/*
    Create service template
    Will create one or two service templates according to this table:

    | serviceType   | both_tls_and_plain | result       |
    |---------------|--------------------|--------------|
    | ClusterIP     | any                | one Service  |
    | Not ClusterIP | not present        | one Service  |
    | Not ClusterIP | false              | one Service  |
    | Not ClusterIP | true               | two Services |

    If two services are created, one is ClusterIP with both crypted and plain
    ports and the other one is NodePort (or LoadBalancer) with crypted port only.
*/}}
{{- define "common.service" -}}
{{-   $dot := default . .dot -}}
{{-   $suffix := default "" $dot.Values.service.suffix -}}
{{-   $annotations := default "" $dot.Values.service.annotations -}}
{{-   $publishNotReadyAddresses := default false $dot.Values.service.publishNotReadyAddresses -}}
{{-   $msb_informations := default "" $dot.Values.service.msb -}}
{{-   $serviceType := $dot.Values.service.type -}}
{{-   $ports := $dot.Values.service.ports -}}
{{-   $both_tls_and_plain:= default false $dot.Values.service.both_tls_and_plain }}
{{-   $labels := default (dict) .labels -}}
{{-   $matchLabels := default (dict) .matchLabels -}}

{{-   if (and (include "common.needTLS" .) $both_tls_and_plain) }}
{{      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) }}
{{-     if (ne $serviceType "ClusterIP") }}
---
{{-       if $suffix }}
{{-         $suffix = printf "%s-external" $suffix }}
{{-       else }}
{{-         $suffix = "external" }}
{{-       end }}
{{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
{{-     end }}
{{-   else }}
{{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
{{-   end }}
{{- end -}}

{{/* Create headless service template */}}
{{- define "common.headlessService" -}}
{{- $dot := default . .dot -}}
{{- $suffix := include "common._makeHeadlessSuffix" $dot -}}
{{- $annotations := default "" $dot.Values.service.headless.annotations -}}
{{- $publishNotReadyAddresses := default false $dot.Values.service.headless.publishNotReadyAddresses -}}
{{- $ports := $dot.Values.service.headlessPorts -}}
{{- $labels := default (dict) .labels -}}
{{- $matchLabels := default (dict) .matchLabels -}}
{{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
{{- end -}}

{{/*
  Generate the right suffix for headless service
*/}}
{{- define "common._makeHeadlessSuffix" -}}
{{-   if hasKey .Values.service.headless "suffix" }}
{{-     .Values.service.headless.suffix }}
{{-   else }}
{{-     print "headless" }}
{{-   end }}
{{- end -}}

{{/*
  Calculate if we need to use TLS ports.
  We use TLS by default unless we're on service mesh with TLS.
  We can also override this behavior with override toggles:
  - .Values.global.tlsEnabled  : override default TLS behavior for all charts
  - .Values.tlsOverride : override global and default TLS on a per chart basis

  this will give these combinations:
  | tlsOverride | global.tlsEnabled | global.serviceMesh.enabled | global.serviceMesh.tls | result |
  |-------------|-------------------|----------------------------|------------------------|--------|
  | not present | not present       | not present                | any                    | true   |
  | not present | not present       | false                      | any                    | true   |
  | not present | not present       | true                       | false                  | true   |
  | not present | not present       | true                       | true                   | false  |
  | not present | true              | any                        | any                    | true   |
  | not present | false             | any                        | any                    | false  |
  | true        | any               | any                        | any                    | true   |
  | false       | any               | any                        | any                    | false  |

*/}}
{{- define "common.needTLS" -}}
{{-   if hasKey .Values "tlsOverride" }}
{{-     if .Values.tlsOverride -}}
true
{{-       end }}
{{-   else }}
{{-     if hasKey .Values.global "tlsEnabled" }}
{{-       if .Values.global.tlsEnabled }}
true
{{-       end }}
{{-     else }}
{{-       if not (include "common.onServiceMesh" .) -}}
true
{{-       else }}
{{-         if not (default false .Values.global.serviceMesh.tls) -}}
true
{{-         end }}
{{-       end }}
{{-     end }}
{{-   end }}
{{- end -}}