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