blob: 9c50032b78712f53ec9b5f968d989bca1adcb6d0 [file] [log] [blame]
Giorgi Lekveishviliec6b3cc2023-12-01 16:30:04 +04001{{/* vim: set filetype=mustache: */}}
2{{/*
3Expand the name of the chart.
4*/}}
5{{- define "coredns.name" -}}
6{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
7{{- end -}}
8
9{{/*
10Create a default fully qualified app name.
11We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
12*/}}
13{{- define "coredns.fullname" -}}
14{{- if .Values.fullnameOverride -}}
15{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
16{{- else -}}
17{{- $name := default .Chart.Name .Values.nameOverride -}}
18{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
19{{- end -}}
20{{- end -}}
21
22{{/*
23Common labels
24*/}}
25{{- define "coredns.labels" -}}
26app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
27app.kubernetes.io/instance: {{ .Release.Name | quote }}
28helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
29{{- if .Values.isClusterService }}
30k8s-app: {{ template "coredns.k8sapplabel" . }}
31kubernetes.io/cluster-service: "true"
32kubernetes.io/name: "CoreDNS"
33{{- end }}
34app.kubernetes.io/name: {{ template "coredns.name" . }}
35{{- end -}}
36
37{{/*
38Common labels with autoscaler
39*/}}
40{{- define "coredns.labels.autoscaler" -}}
41app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
42app.kubernetes.io/instance: {{ .Release.Name | quote }}
43helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
44{{- if .Values.isClusterService }}
45k8s-app: {{ template "coredns.k8sapplabel" . }}-autoscaler
46kubernetes.io/cluster-service: "true"
47kubernetes.io/name: "CoreDNS"
48{{- end }}
49app.kubernetes.io/name: {{ template "coredns.name" . }}-autoscaler
50{{- end -}}
51
52{{/*
53Allow k8s-app label to be overridden
54*/}}
55{{- define "coredns.k8sapplabel" -}}
56{{- default .Chart.Name .Values.k8sAppLabelOverride | trunc 63 | trimSuffix "-" -}}
57{{- end -}}
58
59{{/*
60Generate the list of ports automatically from the server definitions
61*/}}
62{{- define "coredns.servicePorts" -}}
63 {{/* Set ports to be an empty dict */}}
64 {{- $ports := dict -}}
65 {{/* Iterate through each of the server blocks */}}
66 {{- range .Values.servers -}}
67 {{/* Capture port to avoid scoping awkwardness */}}
68 {{- $port := toString .port -}}
69
70 {{/* If none of the server blocks has mentioned this port yet take note of it */}}
71 {{- if not (hasKey $ports $port) -}}
72 {{- $ports := set $ports $port (dict "istcp" false "isudp" false) -}}
73 {{- end -}}
74 {{/* Retrieve the inner dict that holds the protocols for a given port */}}
75 {{- $innerdict := index $ports $port -}}
76
77 {{/*
78 Look at each of the zones and check which protocol they serve
79 At the moment the following are supported by CoreDNS:
80 UDP: dns://
81 TCP: tls://, grpc://
82 */}}
83 {{- range .zones -}}
84 {{- if has (default "" .scheme) (list "dns://") -}}
85 {{/* Optionally enable tcp for this service as well */}}
86 {{- if eq (default false .use_tcp) true }}
87 {{- $innerdict := set $innerdict "istcp" true -}}
88 {{- end }}
89 {{- $innerdict := set $innerdict "isudp" true -}}
90 {{- end -}}
91
92 {{- if has (default "" .scheme) (list "tls://" "grpc://") -}}
93 {{- $innerdict := set $innerdict "istcp" true -}}
94 {{- end -}}
95 {{- end -}}
96
97 {{/* If none of the zones specify scheme, default to dns:// on both tcp & udp */}}
98 {{- if and (not (index $innerdict "istcp")) (not (index $innerdict "isudp")) -}}
99 {{- $innerdict := set $innerdict "isudp" true -}}
100 {{- $innerdict := set $innerdict "istcp" true -}}
101 {{- end -}}
102
103 {{- if .nodePort -}}
104 {{- $innerdict := set $innerdict "nodePort" .nodePort -}}
105 {{- end -}}
106
107 {{/* Write the dict back into the outer dict */}}
108 {{- $ports := set $ports $port $innerdict -}}
109 {{- end -}}
110
111 {{/* Write out the ports according to the info collected above */}}
112 {{- range $port, $innerdict := $ports -}}
113 {{- $portList := list -}}
114 {{- if index $innerdict "isudp" -}}
115 {{- $portList = append $portList (dict "port" ($port | int) "protocol" "UDP" "name" (printf "udp-%s" $port)) -}}
116 {{- end -}}
117 {{- if index $innerdict "istcp" -}}
118 {{- $portList = append $portList (dict "port" ($port | int) "protocol" "TCP" "name" (printf "tcp-%s" $port)) -}}
119 {{- end -}}
120
121 {{- range $portDict := $portList -}}
122 {{- if index $innerdict "nodePort" -}}
123 {{- $portDict := set $portDict "nodePort" (get $innerdict "nodePort" | int) -}}
124 {{- end -}}
125
126 {{- printf "- %s\n" (toJson $portDict) -}}
127 {{- end -}}
128 {{- end -}}
129{{- end -}}
130
131{{/*
132Generate the list of ports automatically from the server definitions
133*/}}
134{{- define "coredns.containerPorts" -}}
135 {{/* Set ports to be an empty dict */}}
136 {{- $ports := dict -}}
137 {{/* Iterate through each of the server blocks */}}
138 {{- range .Values.servers -}}
139 {{/* Capture port to avoid scoping awkwardness */}}
140 {{- $port := toString .port -}}
141
142 {{/* If none of the server blocks has mentioned this port yet take note of it */}}
143 {{- if not (hasKey $ports $port) -}}
144 {{- $ports := set $ports $port (dict "istcp" false "isudp" false) -}}
145 {{- end -}}
146 {{/* Retrieve the inner dict that holds the protocols for a given port */}}
147 {{- $innerdict := index $ports $port -}}
148
149 {{/*
150 Look at each of the zones and check which protocol they serve
151 At the moment the following are supported by CoreDNS:
152 UDP: dns://
153 TCP: tls://, grpc://
154 */}}
155 {{- range .zones -}}
156 {{- if has (default "" .scheme) (list "dns://") -}}
157 {{/* Optionally enable tcp for this service as well */}}
158 {{- if eq (default false .use_tcp) true }}
159 {{- $innerdict := set $innerdict "istcp" true -}}
160 {{- end }}
161 {{- $innerdict := set $innerdict "isudp" true -}}
162 {{- end -}}
163
164 {{- if has (default "" .scheme) (list "tls://" "grpc://") -}}
165 {{- $innerdict := set $innerdict "istcp" true -}}
166 {{- end -}}
167 {{- end -}}
168
169 {{/* If none of the zones specify scheme, default to dns:// on both tcp & udp */}}
170 {{- if and (not (index $innerdict "istcp")) (not (index $innerdict "isudp")) -}}
171 {{- $innerdict := set $innerdict "isudp" true -}}
172 {{- $innerdict := set $innerdict "istcp" true -}}
173 {{- end -}}
174
175 {{- if .hostPort -}}
176 {{- $innerdict := set $innerdict "hostPort" .hostPort -}}
177 {{- end -}}
178
179 {{/* Write the dict back into the outer dict */}}
180 {{- $ports := set $ports $port $innerdict -}}
181
182 {{/* Fetch port from the configuration if the prometheus section exists */}}
183 {{- range .plugins -}}
184 {{- if eq .name "prometheus" -}}
185 {{- $prometheus_addr := toString .parameters -}}
186 {{- $prometheus_addr_list := regexSplit ":" $prometheus_addr -1 -}}
187 {{- $prometheus_port := index $prometheus_addr_list 1 -}}
188 {{- $ports := set $ports $prometheus_port (dict "istcp" true "isudp" false) -}}
189 {{- end -}}
190 {{- end -}}
191 {{- end -}}
192
193 {{/* Write out the ports according to the info collected above */}}
194 {{- range $port, $innerdict := $ports -}}
195 {{- $portList := list -}}
196 {{- if index $innerdict "isudp" -}}
197 {{- $portList = append $portList (dict "containerPort" ($port | int) "protocol" "UDP" "name" (printf "udp-%s" $port)) -}}
198 {{- end -}}
199 {{- if index $innerdict "istcp" -}}
200 {{- $portList = append $portList (dict "containerPort" ($port | int) "protocol" "TCP" "name" (printf "tcp-%s" $port)) -}}
201 {{- end -}}
202
203 {{- range $portDict := $portList -}}
204 {{- if index $innerdict "hostPort" -}}
205 {{- $portDict := set $portDict "hostPort" (get $innerdict "hostPort" | int) -}}
206 {{- end -}}
207
208 {{- printf "- %s\n" (toJson $portDict) -}}
209 {{- end -}}
210 {{- end -}}
211{{- end -}}
212
213{{/*
214Create the name of the service account to use
215*/}}
216{{- define "coredns.serviceAccountName" -}}
217{{- if .Values.serviceAccount.create -}}
218 {{ default (include "coredns.fullname" .) .Values.serviceAccount.name }}
219{{- else -}}
220 {{ default "default" .Values.serviceAccount.name }}
221{{- end -}}
222{{- end -}}