[COMMON] Create templates for services and PV
[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      Usage example:
39       {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
40       {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
41 */}}
42 {{- define "common.serviceMetadata" -}}
43   {{- $dot := default . .dot -}}
44   {{- $suffix := default "" .suffix -}}
45   {{- $annotations := default "" .annotations -}}
46 {{- if $annotations -}}
47 annotations: {{- include "common.tplValue" (dict "value" $annotations "context" $dot) | nindent 2 }}
48 {{- end }}
49 name: {{ include "common.servicename" $dot }}{{ if $suffix }}{{ print "-" $suffix }}{{ end }}
50 namespace: {{ include "common.namespace" $dot }}
51 labels: {{- include "common.labels" $dot | nindent 2 -}}
52 {{- end -}}
53
54 {{/* Define the ports of Service
55      The function takes three arguments (inside a dictionary):
56      - .dot : environment (.)
57      - .ports : an array of ports
58      - .portType: the type of the service
59 */}}
60 {{- define "common.servicePorts" -}}
61 {{- $portType := .portType -}}
62 {{- $dot := .dot -}}
63 {{- range $index, $port := .ports }}
64 - port: {{ $port.port }}
65   targetPort: {{ $port.name }}
66   {{- if (eq $portType "NodePort") }}
67   nodePort: {{ $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}{{ $port.nodePort }}
68   {{- end }}
69   name: {{ $port.name }}
70 {{- end -}}
71 {{- end -}}
72
73 {{/* Create generic service template
74      The function takes several arguments (inside a dictionary):
75      - .dot : environment (.)
76      - .ports : an array of ports
77      - .portType: the type of the service
78      - .suffix : a string which will be added at the end of the name (with a '-')
79      - .annotations: the annotations to add
80      - .publishNotReadyAddresses: if we publish not ready address
81      - .headless: if the service is headless
82 */}}
83 {{- define "common.genericService" -}}
84 {{- $dot := default . .dot -}}
85 {{- $suffix := default "" .suffix -}}
86 {{- $annotations := default "" .annotations -}}
87 {{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
88 {{- $portType := .portType -}}
89 {{- $ports := .ports -}}
90 {{- $headless := default false .headless -}}
91 apiVersion: v1
92 kind: Service
93 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "dot" $dot ) | nindent 2 }}
94 spec:
95   {{- if $headless }}
96   clusterIP: None
97   {{- end }}
98   ports: {{- include "common.servicePorts" (dict "portType" $portType "ports" $ports "dot" $dot) | nindent 4 }}
99   {{- if $publishNotReadyAddresses }}
100   publishNotReadyAddresses: true
101   {{- end }}
102   type: {{ $portType }}
103   selector: {{- include "common.matchLabels" $dot | nindent 4 }}
104 {{- end -}}
105
106 {{/* Create service template */}}
107 {{- define "common.service" -}}
108 {{- $suffix := default "" .Values.service.suffix -}}
109 {{- $annotations := default "" .Values.service.annotations -}}
110 {{- $publishNotReadyAddresses := default false .Values.service.publishNotReadyAddresses -}}
111 {{- $portType := .Values.service.type -}}
112 {{- $ports := .Values.service.ports -}}
113 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "portType" $portType) }}
114 {{- end -}}
115
116 {{/* Create headless service template */}}
117 {{- define "common.headlessService" -}}
118 {{- $suffix := include "common._makeHeadlessSuffix" . -}}
119 {{- $annotations := default "" .Values.service.headless.annotations -}}
120 {{- $publishNotReadyAddresses := default false .Values.service.headless.publishNotReadyAddresses -}}
121 {{- $ports := .Values.service.headlessPorts -}}
122 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "portType" "ClusterIP" "headless" true ) }}
123 {{- end -}}
124
125 {{/*
126   Generate the right suffix for headless service
127 */}}
128 {{- define "common._makeHeadlessSuffix" -}}
129 {{-   if hasKey .Values.service.headless "suffix" }}
130 {{-     .Values.service.headless.suffix }}
131 {{-   else }}
132 {{-     print "headless" }}
133 {{-   end }}
134 {{- end -}}