-
Notifications
You must be signed in to change notification settings - Fork 86
/
controller.go
636 lines (570 loc) · 25.8 KB
/
controller.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package controller
import (
"context"
"fmt"
"net"
"k8s.io/apimachinery/pkg/util/sets"
configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/cluster-dns-operator/pkg/manifests"
operatorconfig "github.com/openshift/cluster-dns-operator/pkg/operator/config"
"github.com/openshift/cluster-dns-operator/pkg/util/slice"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"github.com/apparentlymart/go-cidr/cidr"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
// DefaultDNSController is the name of the default DNS instance.
DefaultDNSController = "default"
// DNSControllerFinalizer is applied to a DNS before being considered for processing;
// this ensures the operator has a chance to handle all states.
DNSControllerFinalizer = "dns.operator.openshift.io/dns-controller"
controllerName = "dns_controller"
namespaceRunLevelLabel = "openshift.io/run-level" // "0"
namespaceClusterMonitoringLabel = "openshift.io/cluster-monitoring" // "true"
namespacePodSecurityEnforceLabel = "pod-security.kubernetes.io/enforce" // privileged
namespacePodSecurityAuditLabel = "pod-security.kubernetes.io/audit" // privileged
namespacePodSecurityWarnLabel = "pod-security.kubernetes.io/warn" // privileged
)
// managedDNSNamespaceLabels is a set of label keys for labels that the operator manages
// for the operand namespace
var managedDNSNamespaceLabels = sets.NewString(
namespaceClusterMonitoringLabel,
namespaceRunLevelLabel,
namespacePodSecurityAuditLabel,
namespacePodSecurityEnforceLabel,
namespacePodSecurityWarnLabel,
)
// New creates the operator controller from configuration. This is the
// controller that handles all the logic for implementing dns based on
// DNS resources.
//
// The controller will be pre-configured to watch for DNS resources.
func New(mgr manager.Manager, config Config) (controller.Controller, error) {
operatorCache := mgr.GetCache()
reconciler := &reconciler{
Config: config.Config,
client: mgr.GetClient(),
cache: operatorCache,
dnsNameResolverEnabled: config.DNSNameResolverEnabled,
dnsNameResolverNamespaces: config.DNSNameResolverNamespaces,
}
c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: reconciler})
if err != nil {
return nil, err
}
scheme := mgr.GetClient().Scheme()
mapper := mgr.GetClient().RESTMapper()
if err := c.Watch(source.Kind[client.Object](operatorCache, &operatorv1.DNS{}, &handler.EnqueueRequestForObject{})); err != nil {
return nil, err
}
if err := c.Watch(source.Kind[client.Object](operatorCache, &appsv1.DaemonSet{}, handler.EnqueueRequestForOwner(scheme, mapper, &operatorv1.DNS{}))); err != nil {
return nil, err
}
if err := c.Watch(source.Kind[client.Object](operatorCache, &corev1.Service{}, handler.EnqueueRequestForOwner(scheme, mapper, &operatorv1.DNS{}))); err != nil {
return nil, err
}
if err := c.Watch(source.Kind[client.Object](operatorCache, &corev1.ConfigMap{}, handler.EnqueueRequestForOwner(scheme, mapper, &operatorv1.DNS{}))); err != nil {
return nil, err
}
objectToDNS := func(context.Context, client.Object) []reconcile.Request {
return []reconcile.Request{{DefaultDNSNamespaceName()}}
}
isInNS := func(namespace string) func(o client.Object) bool {
return func(o client.Object) bool {
return o.GetNamespace() == namespace
}
}
if err := c.Watch(source.Kind[client.Object](operatorCache, &corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(objectToDNS), predicate.NewPredicateFuncs(isInNS(GlobalUserSpecifiedConfigNamespace)))); err != nil {
return nil, err
}
// If a node is created or deleted, then the controller may need to
// reconcile the DNS service in order to add or remove the
// service.kubernetes.io/topology-aware-hints annotation, but only if
// the node isn't ignored for the purpose of determining whether to
// enable topology-aware hints.
nodePredicate := func(o client.Object) bool {
node := o.(*corev1.Node)
return !ignoreNodeForTopologyAwareHints(node)
}
if err := c.Watch(source.Kind[client.Object](operatorCache, &corev1.Node{}, handler.EnqueueRequestsFromMapFunc(objectToDNS), predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool { return nodePredicate(e.Object) },
DeleteFunc: func(e event.DeleteEvent) bool { return nodePredicate(e.Object) },
UpdateFunc: func(e event.UpdateEvent) bool {
old := e.ObjectOld.(*corev1.Node)
new := e.ObjectNew.(*corev1.Node)
if ignoreNodeForTopologyAwareHints(old) != ignoreNodeForTopologyAwareHints(new) {
return true
}
if !ignoreNodeForTopologyAwareHints(new) && nodeIsValidForTopologyAwareHints(old) != nodeIsValidForTopologyAwareHints(new) {
return true
}
return false
},
GenericFunc: func(e event.GenericEvent) bool { return nodePredicate(e.Object) },
})); err != nil {
return nil, err
}
return c, nil
}
// Config holds all the configuration that must be provided when creating the
// controller.
type Config struct {
// DNSNameResolverEnabled indicates that the "DNSNameResolver" featuregate is enabled.
DNSNameResolverEnabled bool
// DNSNameResolverNamespaces are the namespaces which will be watched for the
// DNSNameResolver resources and updated by the CoreDNS pods if the "DNSNameResolver"
// featuregate is enabled.
DNSNameResolverNamespaces []string
operatorconfig.Config
}
// reconciler handles the actual dns reconciliation logic in response to
// events.
type reconciler struct {
operatorconfig.Config
client client.Client
cache cache.Cache
// dnsNameResolverEnabled indicates that the "DNSNameResolver" featuregate is enabled.
dnsNameResolverEnabled bool
// dnsNameResolverNamespaces are the namespaces which will be watched for the
// DNSNameResolver resources and updated by the CoreDNS pods the "DNSNameResolver"
// featuregate is enabled.
dnsNameResolverNamespaces []string
}
// Reconcile expects request to refer to a dns and will do all the work
// to ensure the dns is in the desired state.
func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
errs := []error{}
result := reconcile.Result{}
logrus.Infof("reconciling request: %v", request)
if request.NamespacedName.Name != DefaultDNSController {
// Return a nil error value to avoid re-triggering the event.
logrus.Errorf("skipping unexpected dns %s", request.NamespacedName.Name)
return result, nil
}
// Get the current dns state.
dns := &operatorv1.DNS{}
if err := r.client.Get(ctx, request.NamespacedName, dns); err != nil {
if errors.IsNotFound(err) {
// This means the dns was already deleted/finalized and there are
// stale queue entries (or something edge triggering from a related
// resource that got deleted async).
logrus.Infof("dns not found; reconciliation will be skipped for request: %v", request)
} else {
errs = append(errs, fmt.Errorf("failed to get dns %s: %v", request, err))
}
dns = nil
}
if dns != nil {
switch dns.Spec.OperatorLogLevel {
case operatorv1.DNSLogLevelNormal:
logrus.SetLevel(logrus.InfoLevel)
case operatorv1.DNSLogLevelDebug:
logrus.SetLevel(logrus.DebugLevel)
case operatorv1.DNSLogLevelTrace:
logrus.SetLevel(logrus.TraceLevel)
default:
logrus.SetLevel(logrus.InfoLevel)
}
switch dns.Spec.ManagementState {
case operatorv1.Unmanaged:
// When the operator is set to unmanaged, it should not make
// changes to the DNS or node resolver pods, but it should still
// update status
//
// TODO: fetch clusterDomain from higher level openshift resource
// when it is exposed
clusterDomain := "cluster.local"
clusterIP, err := r.getClusterIPFromNetworkConfig()
if err != nil {
errs = append(errs, fmt.Errorf("failed to get cluster IP from network config: %v", err))
}
haveDNSDaemonset, dnsDaemonset, err := r.currentDNSDaemonSet(dns)
if err != nil {
errs = append(errs, err)
}
haveNodeResolverDaemonset, nodeResolverDaemonset, err := r.currentNodeResolverDaemonSet()
if err != nil {
errs = append(errs, err)
}
// 2*lameDuckDuration is used for transitionUnchangedToleration to add some room to cover lameDuckDuration when CoreDNS reports unavailable.
// This is eventually used to prevent frequent updates.
if err := r.syncDNSStatus(dns, clusterIP, clusterDomain, haveDNSDaemonset, dnsDaemonset, haveNodeResolverDaemonset, nodeResolverDaemonset, 2*lameDuckDuration, &result); err != nil {
errs = append(errs, fmt.Errorf("failed to sync status of dns %q: %w", dns.Name, err))
}
default:
// Ensure we have all the necessary scaffolding on which to place dns instances.
if err := r.ensureDNSNamespace(); err != nil {
errs = append(errs, fmt.Errorf("failed to ensure dns namespace: %v", err))
}
if dns.DeletionTimestamp != nil {
// Handle deletion.
if err := r.ensureOpenshiftExternalNameServiceDeleted(); err != nil {
errs = append(errs, fmt.Errorf("failed to delete external name for openshift service: %v", err))
}
if err := r.ensureDNSDeleted(dns); err != nil {
errs = append(errs, fmt.Errorf("failed to ensure deletion for dns %s: %v", dns.Name, err))
}
if len(errs) == 0 {
// Clean up the finalizer to allow the dns to be deleted.
if slice.ContainsString(dns.Finalizers, DNSControllerFinalizer) {
updated := dns.DeepCopy()
updated.Finalizers = slice.RemoveString(updated.Finalizers, DNSControllerFinalizer)
if err := r.client.Update(ctx, updated); err != nil {
errs = append(errs, fmt.Errorf("failed to remove finalizer from dns %s: %v", dns.Name, err))
}
}
}
} else if err := r.enforceDNSFinalizer(dns); err != nil {
errs = append(errs, fmt.Errorf("failed to enforce finalizer for dns %s: %v", dns.Name, err))
} else {
// Handle everything else.
if err := r.ensureDNS(dns, &result); err != nil {
errs = append(errs, fmt.Errorf("failed to ensure dns %s: %v", dns.Name, err))
} else if err := r.ensureExternalNameForOpenshiftService(); err != nil {
errs = append(errs, fmt.Errorf("failed to ensure external name for openshift service: %v", err))
}
}
}
}
// Log in case of errors as the controller's logs get eaten.
if len(errs) > 0 {
logrus.Errorf("failed to reconcile request %s: %v", request, utilerrors.NewAggregate(errs))
}
return result, utilerrors.NewAggregate(errs)
}
// ensureExternalNameForOpenshiftService ensures 'openshift.default.svc'
// resolves to 'kubernetes.default.svc'.
// This will ensure backward compatibility with openshift 3.x
func (r *reconciler) ensureExternalNameForOpenshiftService() error {
svc := &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "openshift",
Namespace: "default",
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
ExternalName: "kubernetes.default.svc.cluster.local",
},
}
if err := r.client.Get(context.TODO(), types.NamespacedName{Namespace: svc.Namespace, Name: svc.Name}, svc); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get external name service %s/%s: %v", svc.Namespace, svc.Name, err)
}
if err := r.client.Create(context.TODO(), svc); err != nil {
return fmt.Errorf("failed to create external name service %s/%s: %v", svc.Namespace, svc.Name, err)
}
logrus.Infof("created external name service %s/%s", svc.Namespace, svc.Name)
}
return nil
}
// ensureOpenshiftExternalNameServiceDeleted ensures deletion of 'openshift.default.svc'
func (r *reconciler) ensureOpenshiftExternalNameServiceDeleted() error {
svc := &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "openshift",
Namespace: "default",
},
}
if err := r.client.Delete(context.TODO(), svc); err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to delete external name service %s/%s: %v", svc.Namespace, svc.Name, err)
}
logrus.Infof("deleted external name service %s/%s", svc.Namespace, svc.Name)
return nil
}
// enforceDNSFinalizer adds DNSControllerFinalizer to dns if it doesn't exist.
func (r *reconciler) enforceDNSFinalizer(dns *operatorv1.DNS) error {
if !slice.ContainsString(dns.Finalizers, DNSControllerFinalizer) {
dns.Finalizers = append(dns.Finalizers, DNSControllerFinalizer)
if err := r.client.Update(context.TODO(), dns); err != nil {
return err
}
logrus.Infof("enforced finalizer for dns: %s", dns.Name)
}
return nil
}
// ensureDNSDeleted tries to delete related dns resources.
func (r *reconciler) ensureDNSDeleted(dns *operatorv1.DNS) error {
// DNS specific configmap and service has owner reference to daemonset.
// So deletion of daemonset will trigger garbage collection of corresponding
// configmap and service resources.
if err := r.ensureDNSDaemonSetDeleted(dns); err != nil {
return fmt.Errorf("failed to delete daemonset for dns %s: %v", dns.Name, err)
}
return nil
}
// ensureDNSNamespace ensures all the necessary scaffolding exists for
// dns generally, including a namespace and all RBAC setup.
func (r *reconciler) ensureDNSNamespace() error {
existingNamespace := corev1.Namespace{}
desiredNamespace := manifests.DNSNamespace()
if err := r.client.Get(context.TODO(), DefaultDNSOperandNamespaceName(), &existingNamespace); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get dns namespace %q: %w", desiredNamespace.Name, err)
}
if err := r.client.Create(context.TODO(), desiredNamespace); err != nil {
return fmt.Errorf("failed to create dns namespace %s: %w", desiredNamespace.Name, err)
}
logrus.Infof("created dns namespace: %s", desiredNamespace.Name)
} else {
// Make sure existing DNS namespace has all the labels from the Namespace manifest
changed, updatedNamespace := namespaceLabelsChanged(&existingNamespace, desiredNamespace)
if changed {
if err := r.client.Update(context.TODO(), updatedNamespace); err != nil {
return fmt.Errorf("failed to update dns namespace %s: %w", updatedNamespace.Name, err)
}
logrus.Infof("updated dns namespace: %s", updatedNamespace.Name)
}
}
if _, _, err := r.ensureDNSClusterRole(); err != nil {
return fmt.Errorf("failed to ensure dns cluster role for %s: %v", manifests.DNSClusterRole().Name, err)
}
crb := manifests.DNSClusterRoleBinding()
if err := r.client.Get(context.TODO(), types.NamespacedName{Name: crb.Name}, crb); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get dns cluster role binding %s: %v", crb.Name, err)
}
if err := r.client.Create(context.TODO(), crb); err != nil {
return fmt.Errorf("failed to create dns cluster role binding %s: %v", crb.Name, err)
}
logrus.Infof("created dns cluster role binding: %s", crb.Name)
}
sa := manifests.DNSServiceAccount()
if err := r.client.Get(context.TODO(), types.NamespacedName{Namespace: sa.Namespace, Name: sa.Name}, sa); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get dns service account %s/%s: %v", sa.Namespace, sa.Name, err)
}
if err := r.client.Create(context.TODO(), sa); err != nil {
return fmt.Errorf("failed to create dns service account %s/%s: %v", sa.Namespace, sa.Name, err)
}
logrus.Infof("created dns service account: %s/%s", sa.Namespace, sa.Name)
}
nodeResolverServiceAccount := manifests.NodeResolverServiceAccount()
nodeResolverServiceAccountName := types.NamespacedName{
Namespace: nodeResolverServiceAccount.Namespace,
Name: nodeResolverServiceAccount.Name,
}
if err := r.client.Get(context.TODO(), nodeResolverServiceAccountName, nodeResolverServiceAccount); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get serviceaccount %s: %w", nodeResolverServiceAccountName, err)
}
if err := r.client.Create(context.TODO(), nodeResolverServiceAccount); err != nil {
return fmt.Errorf("failed to create serviceaccount %s: %w", nodeResolverServiceAccountName, err)
}
logrus.Infof("created serviceaccount %s", nodeResolverServiceAccountName)
}
return nil
}
// namespaceLabelsChanged generates a new namespace with the desired labels, and reports true if the existing namespace
// labels need to be updated to match the desired state.
func namespaceLabelsChanged(existingNamespace, desiredNamespace *corev1.Namespace) (bool, *corev1.Namespace) {
changed := false
updatedNamespace := existingNamespace.DeepCopy()
if updatedNamespace.Labels == nil {
updatedNamespace.Labels = map[string]string{}
}
for k := range managedDNSNamespaceLabels {
existingVal, have := existingNamespace.Labels[k]
desiredVal, want := desiredNamespace.Labels[k]
if want && (!have || existingVal != desiredVal) {
updatedNamespace.Labels[k] = desiredNamespace.Labels[k]
changed = true
}
}
return changed, updatedNamespace
}
// ensureMetricsIntegration ensures that dns prometheus metrics are integrated with openshift-monitoring for the given DNS.
func (r *reconciler) ensureMetricsIntegration(dns *operatorv1.DNS, svc *corev1.Service, daemonsetRef metav1.OwnerReference) error {
cr := manifests.MetricsClusterRole()
if err := r.client.Get(context.TODO(), types.NamespacedName{Name: cr.Name}, cr); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get dns metrics cluster role %s: %v", cr.Name, err)
}
if err := r.client.Create(context.TODO(), cr); err != nil {
return fmt.Errorf("failed to create dns metrics cluster role %s: %v", cr.Name, err)
}
logrus.Infof("created dns metrics cluster role %s", cr.Name)
}
crb := manifests.MetricsClusterRoleBinding()
if err := r.client.Get(context.TODO(), types.NamespacedName{Name: crb.Name}, crb); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get dns metrics cluster role binding %s: %v", crb.Name, err)
}
if err := r.client.Create(context.TODO(), crb); err != nil {
return fmt.Errorf("failed to create dns metrics cluster role binding %s: %v", crb.Name, err)
}
logrus.Infof("created dns metrics cluster role binding %s", crb.Name)
}
mr := manifests.MetricsRole()
if err := r.client.Get(context.TODO(), types.NamespacedName{Namespace: mr.Namespace, Name: mr.Name}, mr); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get dns metrics role %s/%s: %v", mr.Namespace, mr.Name, err)
}
if err := r.client.Create(context.TODO(), mr); err != nil {
return fmt.Errorf("failed to create dns metrics role %s/%s: %v", mr.Namespace, mr.Name, err)
}
logrus.Infof("created dns metrics role %s/%s", mr.Namespace, mr.Name)
}
mrb := manifests.MetricsRoleBinding()
if err := r.client.Get(context.TODO(), types.NamespacedName{Namespace: mrb.Namespace, Name: mrb.Name}, mrb); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get dns metrics role binding %s/%s: %v", mrb.Namespace, mrb.Name, err)
}
if err := r.client.Create(context.TODO(), mrb); err != nil {
return fmt.Errorf("failed to create dns metrics role binding %s/%s: %v", mrb.Namespace, mrb.Name, err)
}
logrus.Infof("created dns metrics role binding %s/%s", mrb.Namespace, mrb.Name)
}
if _, _, err := r.ensureServiceMonitor(dns, svc, daemonsetRef); err != nil {
return fmt.Errorf("failed to ensure servicemonitor for %s: %v", dns.Name, err)
}
return nil
}
// ensureDNS ensures all necessary dns resources exist for a given dns.
func (r *reconciler) ensureDNS(dns *operatorv1.DNS, reconcileResult *reconcile.Result) error {
// TODO: fetch this from higher level openshift resource when it is exposed
clusterDomain := "cluster.local"
clusterIP, err := r.getClusterIPFromNetworkConfig()
if err != nil {
return fmt.Errorf("failed to get cluster IP from network config: %v", err)
}
errs := []error{}
if err := r.ensureCABundleConfigMaps(dns); err != nil {
errs = append(errs, fmt.Errorf("failed to create ca bundle configmaps for dns %s: %w", dns.Name, err))
}
// cmMap is a map of ca bundle configmaps' names and their resource versions used in
// the calculation of the path of ca bundles in Corefile and volume mount path in daemonset.
// When the map is empty or missing a configmap because it is not found on the cluster,
// the operator will print a warning, and it will not specify a path in Corefile for this configmap
// which will lead to the default behavior of using the system certificates.
// Also, the operator will not add a volume to daemonset for this configmap.
cmMap := r.caBundleRevisionMap(dns)
haveDNSDaemonset, dnsDaemonset, err := r.ensureDNSDaemonSet(dns, cmMap)
if err != nil {
errs = append(errs, fmt.Errorf("failed to ensure daemonset for dns %s: %v", dns.Name, err))
} else if !haveDNSDaemonset {
errs = append(errs, fmt.Errorf("failed to get daemonset for dns %s", dns.Name))
} else {
trueVar := true
daemonsetRef := metav1.OwnerReference{
APIVersion: "apps/v1",
Kind: "DaemonSet",
Name: dnsDaemonset.Name,
UID: dnsDaemonset.UID,
Controller: &trueVar,
}
if _, _, err := r.ensureDNSConfigMap(dns, clusterDomain, cmMap); err != nil {
errs = append(errs, fmt.Errorf("failed to create configmap for dns %s: %v", dns.Name, err))
}
if haveSvc, svc, err := r.ensureDNSService(dns, clusterIP, daemonsetRef); err != nil {
// Set clusterIP to an empty string to cause ClusterOperator to report
// Available=False and Degraded=True.
clusterIP = ""
errs = append(errs, fmt.Errorf("failed to create service for dns %s: %v", dns.Name, err))
} else if !haveSvc {
errs = append(errs, fmt.Errorf("failed to get service for dns %s", dns.Name))
} else if err := r.ensureMetricsIntegration(dns, svc, daemonsetRef); err != nil {
errs = append(errs, fmt.Errorf("failed to integrate metrics with openshift-monitoring for dns %s: %v", dns.Name, err))
}
}
haveNodeResolverDaemonset, nodeResolverDaemonset, err := r.ensureNodeResolverDaemonSet(dns, clusterIP, clusterDomain)
if err != nil {
errs = append(errs, err)
}
// 2*lameDuckDuration is used for transitionUnchangedToleration to add some room to cover lameDuckDuration when CoreDNS reports unavailable.
// This is eventually used to prevent frequent updates.
if err := r.syncDNSStatus(dns, clusterIP, clusterDomain, haveDNSDaemonset, dnsDaemonset, haveNodeResolverDaemonset, nodeResolverDaemonset, 2*lameDuckDuration, reconcileResult); err != nil {
errs = append(errs, fmt.Errorf("failed to sync status of dns %q: %w", dns.Name, err))
}
return utilerrors.NewAggregate(errs)
}
// caBundleRevisionMap generates a map of ca bundle configmaps with their resource versions
// to be used in Corefile as the path of ca bundles and in daemonset volume mount path.
// Resource version is appended to enable automatic reload of Corefile when there is a change
// in the source ca bundle configmap (e.g. cert rotation). Apart from resource versions,
// server name is also used in the path of ca bundles in case the same ca bundle configmap
// is specified for two different servers.
func (r *reconciler) caBundleRevisionMap(dns *operatorv1.DNS) map[string]string {
caBundleRevisions := map[string]string{}
transportConfig := dns.Spec.UpstreamResolvers.TransportConfig
if transportConfig.Transport == operatorv1.TLSTransport {
if transportConfig.TLS != nil && transportConfig.TLS.CABundle.Name != "" {
name := CABundleConfigMapName(transportConfig.TLS.CABundle.Name)
cm := &corev1.ConfigMap{}
err := r.client.Get(context.TODO(), name, cm)
if err != nil {
logrus.Warningf("failed to get destination ca bundle configmap %s: %v", name.Name, err)
} else {
caBundleRevisions[transportConfig.TLS.CABundle.Name] = fmt.Sprintf("%s-%s", cm.Name, cm.ResourceVersion)
}
}
}
for _, server := range dns.Spec.Servers {
transportConfig := server.ForwardPlugin.TransportConfig
if transportConfig.Transport == operatorv1.TLSTransport {
if transportConfig.TLS != nil && transportConfig.TLS.CABundle.Name != "" {
name := CABundleConfigMapName(transportConfig.TLS.CABundle.Name)
cm := &corev1.ConfigMap{}
err := r.client.Get(context.TODO(), name, cm)
if err != nil {
logrus.Warningf("failed to get destination ca bundle configmap %s: %v", name.Name, err)
} else {
caBundleRevisions[transportConfig.TLS.CABundle.Name] = fmt.Sprintf("%s-%s", cm.Name, cm.ResourceVersion)
}
}
}
}
return caBundleRevisions
}
// getClusterIPFromNetworkConfig will return 10th IP from the service CIDR range
// defined in the cluster network config.
func (r *reconciler) getClusterIPFromNetworkConfig() (string, error) {
networkConfig := &configv1.Network{}
if err := r.client.Get(context.TODO(), types.NamespacedName{Name: "cluster"}, networkConfig); err != nil {
return "", fmt.Errorf("failed to get network 'cluster': %v", err)
}
if len(networkConfig.Status.ServiceNetwork) == 0 {
return "", fmt.Errorf("no service networks found in cluster network config")
}
_, serviceCIDR, err := net.ParseCIDR(networkConfig.Status.ServiceNetwork[0])
if err != nil {
return "", fmt.Errorf("invalid service cidr %s: %v", networkConfig.Status.ServiceNetwork[0], err)
}
dnsClusterIP, err := cidr.Host(serviceCIDR, 10)
if err != nil {
return "", fmt.Errorf("invalid service cidr %v: %v", serviceCIDR, err)
}
return dnsClusterIP.String(), nil
}
func dnsOwnerRef(dns *operatorv1.DNS) metav1.OwnerReference {
trueVar := true
return metav1.OwnerReference{
APIVersion: "operator.openshift.io/v1",
Kind: "DNS",
Name: dns.Name,
UID: dns.UID,
Controller: &trueVar,
}
}