-
Notifications
You must be signed in to change notification settings - Fork 82
/
controller_dns_configmap.go
450 lines (412 loc) · 15.1 KB
/
controller_dns_configmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package controller
import (
"bytes"
"context"
"fmt"
"net"
"strings"
"text/template"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/cluster-dns-operator/pkg/manifests"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
resolvConf = "/etc/resolv.conf"
defaultDNSPort = 53
lameDuckDuration = 20 * time.Second
// cacheDefaultMaxPositiveTTLSeconds is the default maximum TTL that the
// operator configures CoreDNS to enforce for positive (NOERROR)
// responses.
cacheDefaultMaxPositiveTTLSeconds = 900
// cacheDefaultMaxNegativeTTLSeconds is the default maximum TTL that the
// operator configures CoreDNS to enforce for negative (NXDOMAIN)
// responses.
cacheDefaultMaxNegativeTTLSeconds = 30
)
var errInvalidNetworkUpstream = fmt.Errorf("The address field is mandatory for upstream of type Network, but was not provided")
var errTransportTLSConfiguredWithoutServerName = fmt.Errorf("The ServerName field is mandatory when configuring TLS as the DNS Transport")
var errTransportTLSConfiguredForNonIP = fmt.Errorf("Only IP addresses are allowed when configuring TLS as the DNS Transport")
var errTransportTLSConfiguredForSysResConf = fmt.Errorf("Using system resolv config is not allowed when configuring TLS as the DNS Transport")
var corefileTemplate = template.Must(template.New("Corefile").Funcs(template.FuncMap{
"CoreDNSForwardingPolicy": coreDNSPolicy, "UpstreamResolver": coreDNSResolver,
}).Parse(`{{range .Servers -}}
# {{.Name}}
{{range .Zones}}{{.}}:5353 {{end}}{
{{with $fp:=.ForwardPlugin -}}
prometheus 127.0.0.1:9153
forward .{{range $fp.Upstreams}} {{if eq "TLS" $fp.TransportConfig.Transport}}tls://{{end}}{{.}}{{end}} {
{{- with $tls := .TransportConfig.TLS }}
{{- with $serverName := $tls.ServerName }}
tls_servername {{$serverName}}
tls {{- with $.CABundleRevisionMap }}{{- with $cm := (index $.CABundleRevisionMap $tls.CABundle.Name) }} /etc/pki/{{$serverName}}-{{ $cm }}/{{ $.CABundleFileName }}{{end}}{{end}}
{{- end}}
{{- end}}
policy {{ CoreDNSForwardingPolicy .Policy }}
{{- if eq "TCP" $fp.ProtocolStrategy }}
force_tcp
{{- end}}
}
{{- end}}
errors
log . {
{{$.LogLevel}}
}
bufsize 1232
cache {{ $.PositiveTTL }} {
denial 9984 {{ $.NegativeTTL }}
}
{{- if eq true $.OCPDNSNameResolver}}
ocp_dnsnameresolver {
namespaces {{range $.DNSNameResolverNamespaces}}{{.}} {{end}}
}
{{- end}}
}
{{end -}}
.:5353 {
bufsize 1232
errors
log . {
{{.LogLevel}}
}
health {
lameduck {{.LameDuckDuration}}
}
ready
kubernetes {{.ClusterDomain}} in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
prometheus 127.0.0.1:9153
{{- with .UpstreamResolvers }}
forward .{{range .Upstreams}} {{if eq "TLS" $.UpstreamResolvers.TransportConfig.Transport}}tls://{{end}}{{UpstreamResolver .}}{{end}} {
{{- with $tls := .TransportConfig.TLS }}
{{- with $serverName := $tls.ServerName }}
tls_servername {{$serverName}}
tls {{- with $.CABundleRevisionMap }}{{- with $cm := (index $.CABundleRevisionMap $tls.CABundle.Name) }} /etc/pki/{{$serverName}}-{{ $cm }}/{{ $.CABundleFileName }}{{end}}{{end}}
{{- end}}
{{- end}}
policy {{ CoreDNSForwardingPolicy .Policy }}
{{- if eq "TCP" $.UpstreamResolvers.ProtocolStrategy }}
force_tcp
{{- end}}
}
{{- end}}
cache {{ .PositiveTTL }} {
denial 9984 {{ .NegativeTTL }}
}
reload
{{- if eq true $.OCPDNSNameResolver}}
ocp_dnsnameresolver {
namespaces {{range $.DNSNameResolverNamespaces}}{{.}} {{end}}
}
{{- end}}
}
hostname.bind:5353 {
chaos
}
`))
// ensureDNSConfigMap ensures that a configmap exists for a given DNS.
func (r *reconciler) ensureDNSConfigMap(dns *operatorv1.DNS, clusterDomain string, caBundleRevisionMap map[string]string) (bool, *corev1.ConfigMap, error) {
haveCM, current, err := r.currentDNSConfigMap(dns)
if err != nil {
return false, nil, fmt.Errorf("failed to get configmap: %v", err)
}
desired, err := desiredDNSConfigMap(dns, clusterDomain, caBundleRevisionMap, r.dnsNameResolverEnabled, r.dnsNameResolverNamespaces)
if err != nil {
return haveCM, current, fmt.Errorf("failed to build configmap: %v", err)
}
switch {
case !haveCM:
if err := r.client.Create(context.TODO(), desired); err != nil {
return false, nil, fmt.Errorf("failed to create configmap: %v", err)
}
logrus.Infof("created configmap: %s", desired.Name)
return r.currentDNSConfigMap(dns)
case haveCM:
if updated, err := r.updateDNSConfigMap(current, desired); err != nil {
return true, current, err
} else if updated {
return r.currentDNSConfigMap(dns)
}
}
return true, current, nil
}
func (r *reconciler) currentDNSConfigMap(dns *operatorv1.DNS) (bool, *corev1.ConfigMap, error) {
current := &corev1.ConfigMap{}
err := r.client.Get(context.TODO(), DNSConfigMapName(dns), current)
if err != nil {
if errors.IsNotFound(err) {
return false, nil, nil
}
return false, nil, err
}
return true, current, nil
}
func desiredDNSConfigMap(dns *operatorv1.DNS, clusterDomain string, caBundleRevisionMap map[string]string, dnsNameResolverEnabled bool, dnsNameResolverNamespaces []string) (*corev1.ConfigMap, error) {
if len(clusterDomain) == 0 {
clusterDomain = "cluster.local"
}
dns, err := sanitizeTLSSettings(dns)
if err != nil {
return nil, err
}
upstreamResolvers := operatorv1.UpstreamResolvers{
Upstreams: []operatorv1.Upstream{
{
Type: operatorv1.SystemResolveConfType,
},
},
Policy: operatorv1.SequentialForwardingPolicy,
TransportConfig: dns.Spec.UpstreamResolvers.TransportConfig,
ProtocolStrategy: dns.Spec.UpstreamResolvers.ProtocolStrategy,
}
if len(dns.Spec.UpstreamResolvers.Upstreams) > 0 {
//Upstreams are defined, we can remove the default one
upstreamResolvers.Upstreams = []operatorv1.Upstream{}
for _, upstream := range dns.Spec.UpstreamResolvers.Upstreams {
if upstream.Type == operatorv1.NetworkResolverType {
if upstream.Address == "" {
return nil, errInvalidNetworkUpstream
}
}
upstreamCopy := *upstream.DeepCopy()
//appending only if there are no duplicates
if !contains(upstreamResolvers.Upstreams, upstream) {
upstreamResolvers.Upstreams = append(upstreamResolvers.Upstreams, upstreamCopy)
}
}
}
if dns.Spec.UpstreamResolvers.Policy != "" {
upstreamResolvers.Policy = dns.Spec.UpstreamResolvers.Policy
}
// Calculate the caching values (in seconds) for use in the Corefile
pTTL, nTTL := coreDNSCache(dns)
corefileParameters := struct {
ClusterDomain string
Servers interface{}
UpstreamResolvers operatorv1.UpstreamResolvers
PolicyStr func(policy operatorv1.ForwardingPolicy) string
LogLevel string
CABundleRevisionMap map[string]string
CABundleFileName string
LameDuckDuration time.Duration
PositiveTTL uint32
NegativeTTL uint32
OCPDNSNameResolver bool
DNSNameResolverNamespaces []string
}{
ClusterDomain: clusterDomain,
Servers: dns.Spec.Servers,
UpstreamResolvers: upstreamResolvers,
PolicyStr: coreDNSPolicy,
LogLevel: coreDNSLogLevel(dns),
CABundleRevisionMap: caBundleRevisionMap,
CABundleFileName: caBundleFileName,
LameDuckDuration: lameDuckDuration,
PositiveTTL: pTTL,
NegativeTTL: nTTL,
OCPDNSNameResolver: dnsNameResolverEnabled,
DNSNameResolverNamespaces: dnsNameResolverNamespaces,
}
corefile := new(bytes.Buffer)
if err := corefileTemplate.Execute(corefile, corefileParameters); err != nil {
return nil, err
}
name := DNSConfigMapName(dns)
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name.Name,
Namespace: name.Namespace,
Labels: map[string]string{
manifests.OwningDNSLabel: DNSDaemonSetLabel(dns),
},
},
Data: map[string]string{
"Corefile": corefile.String(),
},
}
cm.SetOwnerReferences([]metav1.OwnerReference{dnsOwnerRef(dns)})
return cm, nil
}
// sanitizeTLSSettings sanitizes TLS settings by setting ServerName to empty string when TLS is not configured, and by
// setting ClearText as the default value when Transport is not set. It also makes sure TLS is configured only for IP addresses.
func sanitizeTLSSettings(dns *operatorv1.DNS) (*operatorv1.DNS, error) {
updated := dns.DeepCopy()
for i, server := range updated.Spec.Servers {
transport := server.ForwardPlugin.TransportConfig.Transport
tls := server.ForwardPlugin.TransportConfig.TLS
if transport == operatorv1.TLSTransport {
// tls cannot be configured without a ServerName
if tls == nil || tls.ServerName == "" {
return updated, errTransportTLSConfiguredWithoutServerName
}
// tls can only be configured for ip addresses
for _, upstream := range server.ForwardPlugin.Upstreams {
addr := upstream
if v, _, err := net.SplitHostPort(upstream); err == nil {
addr = v
}
if res := net.ParseIP(addr); res == nil {
return updated, errTransportTLSConfiguredForNonIP
}
}
}
// When Transport is "", set the default as "Cleartext".
if transport == "" {
transport = operatorv1.CleartextTransport
updated.Spec.Servers[i].ForwardPlugin.TransportConfig.Transport = transport
}
// When Transport is "Cleartext" and a ServerName is set, the Corefile will ignore any other TLS settings.
if transport == operatorv1.CleartextTransport && tls != nil && tls.ServerName != "" {
logrus.Warningf("ServerName is set in server %q but Transport is not set to TLS. ServerName will be ignored", server.Name)
updated.Spec.Servers[i].ForwardPlugin.TransportConfig.TLS.ServerName = ""
}
}
transport := updated.Spec.UpstreamResolvers.TransportConfig.Transport
tls := updated.Spec.UpstreamResolvers.TransportConfig.TLS
if transport == operatorv1.TLSTransport {
// tls cannot be configured without a ServerName
if tls == nil || tls.ServerName == "" {
return updated, errTransportTLSConfiguredWithoutServerName
}
// if there is no upstream, system resolv conf will be used as default,
// and tls can only be configured for ip addresses
if len(updated.Spec.UpstreamResolvers.Upstreams) < 1 {
return updated, errTransportTLSConfiguredForSysResConf
}
// tls can only be configured for ip addresses
for _, upstream := range updated.Spec.UpstreamResolvers.Upstreams {
if upstream.Type == operatorv1.SystemResolveConfType {
return updated, errTransportTLSConfiguredForSysResConf
}
}
}
// When Transport is "", set the default as cleartext.
if transport == "" {
transport = operatorv1.CleartextTransport
updated.Spec.UpstreamResolvers.TransportConfig.Transport = transport
}
// When Transport is "" or "Cleartext" and a ServerName is set, the Corefile will ignore any other TLS settings.
if transport == operatorv1.CleartextTransport && tls != nil && tls.ServerName != "" {
logrus.Warningf("ServerName is set but Transport is not set to tls. ServerName will be ignored")
updated.Spec.UpstreamResolvers.TransportConfig.TLS.ServerName = ""
}
return updated, nil
}
func (r *reconciler) updateDNSConfigMap(current, desired *corev1.ConfigMap) (bool, error) {
changed, updated := corefileChanged(current, desired)
if !changed {
return false, nil
}
// Diff before updating because the client may mutate the object.
diff := cmp.Diff(current, updated, cmpopts.EquateEmpty())
if err := r.client.Update(context.TODO(), updated); err != nil {
return false, fmt.Errorf("failed to update configmap: %v", err)
}
logrus.Infof("updated configmap %s/%s: %v", updated.Namespace, updated.Name, diff)
return true, nil
}
func corefileChanged(current, expected *corev1.ConfigMap) (bool, *corev1.ConfigMap) {
if cmp.Equal(current.Data, expected.Data, cmpopts.EquateEmpty()) {
return false, current
}
updated := current.DeepCopy()
updated.Data = expected.Data
return true, updated
}
func coreDNSResolver(upstream operatorv1.Upstream) (string, error) {
if upstream.Type == operatorv1.NetworkResolverType {
if upstream.Address == "" {
return "", errInvalidNetworkUpstream
}
if upstream.Port > 0 {
return net.JoinHostPort(strings.ToUpper(upstream.Address), fmt.Sprintf("%d", upstream.Port)), nil
} else {
return strings.ToUpper(upstream.Address), nil
}
}
return resolvConf, nil
}
func coreDNSPolicy(policy operatorv1.ForwardingPolicy) string {
switch policy {
case operatorv1.RandomForwardingPolicy:
return "random"
case operatorv1.RoundRobinForwardingPolicy:
return "round_robin"
case operatorv1.SequentialForwardingPolicy:
return "sequential"
}
return "random"
}
func coreDNSLogLevel(dns *operatorv1.DNS) string {
switch dns.Spec.LogLevel {
case operatorv1.DNSLogLevelNormal:
return "class error"
case operatorv1.DNSLogLevelDebug:
return "class denial error"
case operatorv1.DNSLogLevelTrace:
return "class all"
}
return "class error"
}
// coreDNSCache reads the TTL values set in spec.cache and returns integer representations of those values in seconds.
// This returns uint32 values because CoreDNS won't allow setting the maxTTLs to 0 and there's no use case for setting a
// negative maxTTL value.
func coreDNSCache(dns *operatorv1.DNS) (positiveTTL, negativeTTL uint32) {
// We're using Round() here so if a fractional value is provided it'll abide by
// normal rounding rules (e.g. halfway or greater, round up, else round down). Seconds()
// returns the number of seconds as a float64, and then we convert that to an integer for
// use in the Corefile.
configuredPosTTL := uint32(dns.Spec.Cache.PositiveTTL.Round(time.Second).Seconds())
configuredNegTTL := uint32(dns.Spec.Cache.NegativeTTL.Round(time.Second).Seconds())
// For values <=0, Round() will return them as-is so we'll use the default if that happens
// because CoreDNS won't allow setting a negative value.
if configuredPosTTL <= 0 {
positiveTTL = cacheDefaultMaxPositiveTTLSeconds
} else {
positiveTTL = configuredPosTTL
}
if configuredNegTTL <= 0 {
negativeTTL = cacheDefaultMaxNegativeTTLSeconds
} else {
negativeTTL = configuredNegTTL
}
return positiveTTL, negativeTTL
}
func contains(upstreams []operatorv1.Upstream, upstream operatorv1.Upstream) bool {
for _, anUpstream := range upstreams {
if cmp.Equal(upstream, anUpstream, cmp.Comparer(cmpPort), cmp.Comparer(cmpAddress), cmp.Comparer(cmpUpstreamType)) {
return true
}
}
return false
}
func cmpUpstreamType(a, b operatorv1.UpstreamType) bool {
if a == "" {
a = operatorv1.SystemResolveConfType
}
if b == "" {
b = operatorv1.SystemResolveConfType
}
return a == b
}
func cmpPort(a, b uint32) bool {
aVal := uint32(defaultDNSPort)
if a != 0 {
aVal = a
}
bVal := uint32(defaultDNSPort)
if b != 0 {
bVal = b
}
return aVal == bVal
}
func cmpAddress(a, b string) bool {
return strings.EqualFold(a, b)
}