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
60 {{- define "common.servicePorts" -}}
61 {{- $portType := .portType -}}
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 }}
69 name: {{ $port.name }}
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
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 -}}
93 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "dot" $dot ) | nindent 2 }}
98 ports: {{- include "common.servicePorts" (dict "portType" $portType "ports" $ports "dot" $dot) | nindent 4 }}
99 {{- if $publishNotReadyAddresses }}
100 publishNotReadyAddresses: true
102 type: {{ $portType }}
103 selector: {{- include "common.matchLabels" $dot | nindent 4 }}
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) }}
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 ) }}
126 Generate the right suffix for headless service
128 {{- define "common._makeHeadlessSuffix" -}}
129 {{- if hasKey .Values.service.headless "suffix" }}
130 {{- .Values.service.headless.suffix }}
132 {{- print "headless" }}