2 # Copyright © 2017 Amdocs, Bell Canada
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 Resolve the name of a chart's service.
20 The default will be the chart name (or .Values.nameOverride if set).
21 And the use of .Values.service.name overrides all.
23 - .Values.service.name: override default service (ie. chart) name
26 Expand the service name for a chart.
28 {{- define "common.servicename" -}}
29 {{- $name := default .Chart.Name .Values.nameOverride -}}
30 {{- default $name .Values.service.name | trunc 63 | trimSuffix "-" -}}
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
39 {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
40 {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
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 }}
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 -}}
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 - .prefix: NodePort prefix to be used
62 {{- define "common.servicePorts" -}}
63 {{- $portType := .portType -}}
65 {{- range $index, $port := .ports }}
66 {{- $portPrefix := default "nodePortPrefix" $port.prefix }}
67 - port: {{ $port.port }}
68 targetPort: {{ $port.name }}
69 {{- if (eq $portType "NodePort") }}
70 nodePort: {{ index $dot.Values "global" $portPrefix | default (index $dot.Values $portPrefix) }}{{ $port.nodePort }}
72 name: {{ $port.name }}
76 {{/* Create generic service template
77 The function takes several arguments (inside a dictionary):
78 - .dot : environment (.)
79 - .ports : an array of ports
80 - .portType: the type of the service
81 - .suffix : a string which will be added at the end of the name (with a '-')
82 - .annotations: the annotations to add
83 - .publishNotReadyAddresses: if we publish not ready address
84 - .headless: if the service is headless
86 {{- define "common.genericService" -}}
87 {{- $dot := default . .dot -}}
88 {{- $suffix := default "" .suffix -}}
89 {{- $annotations := default "" .annotations -}}
90 {{- $publishNotReadyAddresses := default false .publishNotReadyAddresses -}}
91 {{- $portType := .portType -}}
92 {{- $ports := .ports -}}
93 {{- $headless := default false .headless -}}
96 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "dot" $dot ) | nindent 2 }}
101 ports: {{- include "common.servicePorts" (dict "portType" $portType "ports" $ports "dot" $dot) | nindent 4 }}
102 {{- if $publishNotReadyAddresses }}
103 publishNotReadyAddresses: true
105 type: {{ $portType }}
106 selector: {{- include "common.matchLabels" $dot | nindent 4 }}
109 {{/* Create service template */}}
110 {{- define "common.service" -}}
111 {{- $suffix := default "" .Values.service.suffix -}}
112 {{- $annotations := default "" .Values.service.annotations -}}
113 {{- $publishNotReadyAddresses := default false .Values.service.publishNotReadyAddresses -}}
114 {{- $portType := .Values.service.type -}}
115 {{- $ports := .Values.service.ports -}}
116 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "portType" $portType) }}
119 {{/* Create headless service template */}}
120 {{- define "common.headlessService" -}}
121 {{- $suffix := include "common._makeHeadlessSuffix" . -}}
122 {{- $annotations := default "" .Values.service.headless.annotations -}}
123 {{- $publishNotReadyAddresses := default false .Values.service.headless.publishNotReadyAddresses -}}
124 {{- $ports := .Values.service.headlessPorts -}}
125 {{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "portType" "ClusterIP" "headless" true ) }}
129 Generate the right suffix for headless service
131 {{- define "common._makeHeadlessSuffix" -}}
132 {{- if hasKey .Values.service.headless "suffix" }}
133 {{- .Values.service.headless.suffix }}
135 {{- print "headless" }}