-
Notifications
You must be signed in to change notification settings - Fork 473
/
dns.go
572 lines (511 loc) · 18.8 KB
/
dns.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
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package botanist
import (
"context"
"errors"
"fmt"
"strings"
"time"
gardencore "github.com/gardener/gardener/pkg/apis/core"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
extensionsv1alpha1helper "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1/helper"
"github.com/gardener/gardener/pkg/operation/botanist/component"
"github.com/gardener/gardener/pkg/operation/botanist/component/extensions/dns"
"github.com/gardener/gardener/pkg/utils/flow"
gutil "github.com/gardener/gardener/pkg/utils/gardener"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
dnsv1alpha1 "github.com/gardener/external-dns-management/pkg/apis/dns/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// DNSInternalName is a constant for a DNS resources used for the internal domain name.
DNSInternalName = "internal"
// DNSExternalName is a constant for a DNS resources used for the external domain name.
DNSExternalName = "external"
// DNSOwnerName is a constant for a DNS resources used for the owner domain name.
DNSOwnerName = "owner"
// DNSProviderRoleAdditional is a constant for additionally managed DNS providers.
DNSProviderRoleAdditional = "managed-dns-provider"
// DNSRealmAnnotation is the annotation key for restricting provider access for shoot DNS entries
DNSRealmAnnotation = "dns.gardener.cloud/realms"
// DNSRecordSecretPrefix is a constant for prefixing secrets referenced by DNSRecords
DNSRecordSecretPrefix = "dnsrecord"
)
// DeployExternalDNS deploys the external DNSOwner, DNSProvider, and DNSEntry resources.
func (b *Botanist) DeployExternalDNS(ctx context.Context) error {
if b.NeedsExternalDNS() {
if b.isRestorePhase() {
return dnsRestoreDeployer{
provider: b.Shoot.Components.Extensions.DNS.ExternalProvider,
entry: b.Shoot.Components.Extensions.DNS.ExternalEntry,
owner: b.Shoot.Components.Extensions.DNS.ExternalOwner,
}.Deploy(ctx)
}
return component.OpWaiter(
b.Shoot.Components.Extensions.DNS.ExternalOwner,
b.Shoot.Components.Extensions.DNS.ExternalProvider,
b.Shoot.Components.Extensions.DNS.ExternalEntry,
).Deploy(ctx)
}
return component.OpWaiter(
b.Shoot.Components.Extensions.DNS.ExternalEntry,
b.Shoot.Components.Extensions.DNS.ExternalProvider,
b.Shoot.Components.Extensions.DNS.ExternalOwner,
).Deploy(ctx)
}
// DeployInternalDNS deploys the internal DNSOwner, DNSProvider, and DNSEntry resources.
func (b *Botanist) DeployInternalDNS(ctx context.Context) error {
if b.NeedsInternalDNS() {
if b.isRestorePhase() {
return dnsRestoreDeployer{
provider: b.Shoot.Components.Extensions.DNS.InternalProvider,
entry: b.Shoot.Components.Extensions.DNS.InternalEntry,
owner: b.Shoot.Components.Extensions.DNS.InternalOwner,
}.Deploy(ctx)
}
return component.OpWaiter(
b.Shoot.Components.Extensions.DNS.InternalOwner,
b.Shoot.Components.Extensions.DNS.InternalProvider,
b.Shoot.Components.Extensions.DNS.InternalEntry,
).Deploy(ctx)
}
return component.OpWaiter(
b.Shoot.Components.Extensions.DNS.InternalEntry,
b.Shoot.Components.Extensions.DNS.InternalProvider,
b.Shoot.Components.Extensions.DNS.InternalOwner,
).Deploy(ctx)
}
func (b *Botanist) enableDNSProviderForShootDNSEntries() map[string]string {
return map[string]string{DNSRealmAnnotation: fmt.Sprintf("%s,", b.Shoot.SeedNamespace)}
}
// DefaultExternalDNSProvider returns the external DNSProvider if external DNS is
// enabled and if not DeployWaiter which removes the external DNSProvider.
func (b *Botanist) DefaultExternalDNSProvider() component.DeployWaiter {
if b.NeedsExternalDNS() {
return dns.NewProvider(
b.Logger,
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.ProviderValues{
Name: DNSExternalName,
Purpose: DNSExternalName,
Provider: b.Shoot.ExternalDomain.Provider,
SecretData: b.Shoot.ExternalDomain.SecretData,
Domains: &dns.IncludeExclude{
Include: sets.NewString(append(b.Shoot.ExternalDomain.IncludeDomains, *b.Shoot.ExternalClusterDomain)...).List(),
Exclude: b.Shoot.ExternalDomain.ExcludeDomains,
},
Zones: &dns.IncludeExclude{
Include: b.Shoot.ExternalDomain.IncludeZones,
Exclude: b.Shoot.ExternalDomain.ExcludeZones,
},
Annotations: b.enableDNSProviderForShootDNSEntries(),
},
)
}
return component.OpDestroy(dns.NewProvider(
b.Logger,
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.ProviderValues{
Name: DNSExternalName,
Purpose: DNSExternalName,
},
))
}
// DefaultExternalDNSEntry returns DeployWaiter which removes the external DNSEntry.
func (b *Botanist) DefaultExternalDNSEntry() component.DeployWaiter {
return component.OpDestroy(dns.NewEntry(
b.Logger,
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.EntryValues{
Name: DNSExternalName,
TTL: *b.Config.Controllers.Shoot.DNSEntryTTLSeconds,
},
))
}
// DefaultExternalDNSOwner returns DeployWaiter which removes the external DNSOwner.
func (b *Botanist) DefaultExternalDNSOwner() component.DeployWaiter {
return component.OpDestroy(dns.NewOwner(
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.OwnerValues{
Name: DNSExternalName,
},
))
}
// DefaultInternalDNSProvider returns the internal DNSProvider if internal DNS is
// enabled and if not, DeployWaiter which removes the internal DNSProvider.
func (b *Botanist) DefaultInternalDNSProvider() component.DeployWaiter {
if b.NeedsInternalDNS() {
return dns.NewProvider(
b.Logger,
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.ProviderValues{
Name: DNSInternalName,
Purpose: DNSInternalName,
Provider: b.Garden.InternalDomain.Provider,
SecretData: b.Garden.InternalDomain.SecretData,
Domains: &dns.IncludeExclude{
Include: []string{b.Shoot.InternalClusterDomain},
},
Zones: &dns.IncludeExclude{
Include: b.Garden.InternalDomain.IncludeZones,
Exclude: b.Garden.InternalDomain.ExcludeZones,
},
},
)
}
return component.OpDestroy(dns.NewProvider(
b.Logger,
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.ProviderValues{
Name: DNSInternalName,
Purpose: DNSInternalName,
},
))
}
// DefaultInternalDNSEntry returns DeployWaiter which removes the internal DNSEntry.
func (b *Botanist) DefaultInternalDNSEntry() component.DeployWaiter {
return component.OpDestroy(dns.NewEntry(
b.Logger,
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.EntryValues{
Name: DNSInternalName,
TTL: *b.Config.Controllers.Shoot.DNSEntryTTLSeconds,
},
))
}
// DefaultInternalDNSOwner returns a DeployWaiter which removes the internal DNSOwner.
func (b *Botanist) DefaultInternalDNSOwner() component.DeployWaiter {
return component.OpDestroy(dns.NewOwner(
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.OwnerValues{
Name: DNSInternalName,
},
))
}
// AdditionalDNSProviders returns a map containing DNSProviders where the key is the provider name.
// Providers and DNSEntries which are no longer needed / or in use, contain a DeployWaiter which removes
// said DNSEntry / DNSProvider.
func (b *Botanist) AdditionalDNSProviders(ctx context.Context) (map[string]component.DeployWaiter, error) {
seedClient := b.K8sSeedClient.Client()
additionalProviders := map[string]component.DeployWaiter{}
if b.NeedsAdditionalDNSProviders() {
for i, provider := range b.Shoot.GetInfo().Spec.DNS.Providers {
p := provider
if p.Primary != nil && *p.Primary {
continue
}
var includeDomains, excludeDomains, includeZones, excludeZones []string
if domains := p.Domains; domains != nil {
includeDomains = domains.Include
excludeDomains = domains.Exclude
}
if zones := p.Zones; zones != nil {
includeZones = zones.Include
excludeZones = zones.Exclude
}
providerType := p.Type
if providerType == nil {
return nil, fmt.Errorf("dns provider[%d] doesn't specify a type", i)
}
if *providerType == gardencore.DNSUnmanaged {
b.Logger.Infof("Skipping deployment of DNS provider[%d] since it specifies type %q", i, gardencore.DNSUnmanaged)
continue
}
secretName := p.SecretName
if secretName == nil {
return nil, fmt.Errorf("dns provider[%d] doesn't specify a secretName", i)
}
secret := &corev1.Secret{}
if err := b.K8sGardenClient.Client().Get(
ctx,
kutil.Key(b.Shoot.GetInfo().Namespace, *secretName),
secret,
); err != nil {
return nil, fmt.Errorf("could not get dns provider secret %q: %+v", *secretName, err)
}
providerName := gutil.GenerateDNSProviderName(*secretName, *providerType)
additionalProviders[providerName] = dns.NewProvider(
b.Logger,
seedClient,
b.Shoot.SeedNamespace,
&dns.ProviderValues{
Name: providerName,
Purpose: providerName,
Labels: map[string]string{v1beta1constants.GardenRole: DNSProviderRoleAdditional},
SecretData: secret.Data,
Provider: *p.Type,
Domains: &dns.IncludeExclude{
Include: includeDomains,
Exclude: excludeDomains,
},
Zones: &dns.IncludeExclude{
Include: includeZones,
Exclude: excludeZones,
},
Annotations: b.enableDNSProviderForShootDNSEntries(),
},
)
}
}
// Clean-up old providers
providerList := &dnsv1alpha1.DNSProviderList{}
if err := seedClient.List(
ctx,
providerList,
client.InNamespace(b.Shoot.SeedNamespace),
client.MatchingLabels{v1beta1constants.GardenRole: DNSProviderRoleAdditional},
); err != nil {
return nil, err
}
for _, p := range providerList.Items {
if _, ok := additionalProviders[p.Name]; !ok {
additionalProviders[p.Name] = component.OpDestroy(dns.NewProvider(
b.Logger,
seedClient,
b.Shoot.SeedNamespace,
&dns.ProviderValues{
Name: p.Name,
Purpose: p.Name,
Labels: map[string]string{v1beta1constants.GardenRole: DNSProviderRoleAdditional},
},
))
}
}
return additionalProviders, nil
}
// NeedsExternalDNS returns true if the Shoot cluster needs external DNS.
func (b *Botanist) NeedsExternalDNS() bool {
return !b.Shoot.DisableDNS &&
b.Shoot.GetInfo().Spec.DNS != nil &&
b.Shoot.GetInfo().Spec.DNS.Domain != nil &&
b.Shoot.ExternalClusterDomain != nil &&
!strings.HasSuffix(*b.Shoot.ExternalClusterDomain, ".nip.io") &&
b.Shoot.ExternalDomain != nil &&
b.Shoot.ExternalDomain.Provider != "unmanaged"
}
// NeedsInternalDNS returns true if the Shoot cluster needs internal DNS.
func (b *Botanist) NeedsInternalDNS() bool {
return !b.Shoot.DisableDNS &&
b.Garden.InternalDomain != nil &&
b.Garden.InternalDomain.Provider != "unmanaged"
}
// NeedsAdditionalDNSProviders returns true if additional DNS providers
// are needed.
func (b *Botanist) NeedsAdditionalDNSProviders() bool {
return !b.Shoot.DisableDNS &&
b.Shoot.GetInfo().Spec.DNS != nil &&
len(b.Shoot.GetInfo().Spec.DNS.Providers) > 0
}
// APIServerSNIPodMutatorEnabled returns false if the value of the Shoot annotation
// 'alpha.featuregates.shoot.gardener.cloud/apiserver-sni-pod-injector' is 'disable' or
// APIServerSNI feature is disabled.
func (b *Botanist) APIServerSNIPodMutatorEnabled() bool {
sniEnabled := b.APIServerSNIEnabled()
if !sniEnabled {
return false
}
vs, ok := b.Shoot.GetInfo().GetAnnotations()[v1beta1constants.AnnotationShootAPIServerSNIPodInjector]
if !ok {
return true
}
return vs != v1beta1constants.AnnotationShootAPIServerSNIPodInjectorDisableValue
}
// DeployAdditionalDNSProviders deploys all additional DNS providers in the shoot namespace of the seed.
func (b *Botanist) DeployAdditionalDNSProviders(ctx context.Context) error {
return b.DeployDNSProviders(ctx, b.Shoot.Components.Extensions.DNS.AdditionalProviders)
}
// DeployDNSProviders deploys the specified DNS providers in the shoot namespace of the seed.
func (b *Botanist) DeployDNSProviders(ctx context.Context, dnsProviders map[string]component.DeployWaiter) error {
fns := make([]flow.TaskFn, 0, len(dnsProviders))
for _, v := range dnsProviders {
dnsProvider := v
fns = append(fns, func(ctx context.Context) error {
return component.OpWaiter(dnsProvider).Deploy(ctx)
})
}
return flow.Parallel(fns...)(ctx)
}
// DeleteDNSProviders deletes all DNS providers in the shoot namespace of the seed.
func (b *Botanist) DeleteDNSProviders(ctx context.Context) error {
if err := b.K8sSeedClient.Client().DeleteAllOf(
ctx,
&dnsv1alpha1.DNSProvider{},
client.InNamespace(b.Shoot.SeedNamespace),
); err != nil {
return err
}
timeoutCtx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()
return kutil.WaitUntilResourcesDeleted(
timeoutCtx,
b.K8sSeedClient.Client(),
&dnsv1alpha1.DNSProviderList{},
5*time.Second,
client.InNamespace(b.Shoot.SeedNamespace),
)
}
// DestroyInternalDNS destroys the internal DNSEntry, DNSOwner, and DNSProvider resources.
func (b *Botanist) DestroyInternalDNS(ctx context.Context) error {
return component.OpDestroyAndWait(
b.Shoot.Components.Extensions.DNS.InternalEntry,
b.Shoot.Components.Extensions.DNS.InternalProvider,
b.Shoot.Components.Extensions.DNS.InternalOwner,
).Destroy(ctx)
}
// DestroyExternalDNS destroys the external DNSEntry, DNSOwner, and DNSProvider resources.
func (b *Botanist) DestroyExternalDNS(ctx context.Context) error {
return component.OpDestroyAndWait(
b.Shoot.Components.Extensions.DNS.ExternalEntry,
b.Shoot.Components.Extensions.DNS.ExternalProvider,
b.Shoot.Components.Extensions.DNS.ExternalOwner,
).Destroy(ctx)
}
// MigrateInternalDNS destroys the internal DNSEntry, DNSOwner, and DNSProvider resources,
// without removing the entry from the DNS provider.
func (b *Botanist) MigrateInternalDNS(ctx context.Context) error {
return component.OpDestroyAndWait(
b.Shoot.Components.Extensions.DNS.InternalOwner,
b.Shoot.Components.Extensions.DNS.InternalProvider,
b.Shoot.Components.Extensions.DNS.InternalEntry,
).Destroy(ctx)
}
// MigrateExternalDNS destroys the external DNSEntry, DNSOwner, and DNSProvider resources,
// without removing the entry from the DNS provider.
func (b *Botanist) MigrateExternalDNS(ctx context.Context, keepProvider bool) error {
if keepProvider {
// Delete the DNSOwner and DNSEntry resources in this order to make sure that the actual DNS record is preserved
if err := component.OpDestroyAndWait(
b.Shoot.Components.Extensions.DNS.ExternalOwner,
b.Shoot.Components.Extensions.DNS.ExternalEntry,
).Destroy(ctx); err != nil {
return err
}
// Deploy the DNSProvider resource
return component.OpWaiter(b.Shoot.Components.Extensions.DNS.ExternalProvider).Deploy(ctx)
}
return component.OpDestroyAndWait(
b.Shoot.Components.Extensions.DNS.ExternalOwner,
b.Shoot.Components.Extensions.DNS.ExternalProvider,
b.Shoot.Components.Extensions.DNS.ExternalEntry,
).Destroy(ctx)
}
// dnsRestoreDeployer implements special deploy logic for DNS providers, entries, and owners to be executed only
// during the restore phase.
type dnsRestoreDeployer struct {
provider component.DeployWaiter
entry component.DeployWaiter
owner component.DeployWaiter
}
func (d dnsRestoreDeployer) Deploy(ctx context.Context) error {
// Deploy the provider and wait for it to become ready
if d.provider != nil {
if err := d.provider.Deploy(ctx); err != nil {
return err
}
if err := d.provider.Wait(ctx); err != nil {
return err
}
}
// Deploy the entry and wait for it to be reconciled, but ignore any errors due to Invalid or Error status
// This is done in order to ensure that the entry exists and has been reconciled before the owner is reconciled
if err := d.entry.Deploy(ctx); err != nil {
return err
}
if err := d.entry.Wait(ctx); err != nil {
var errWithState dns.ErrorWithDNSState
if errors.As(err, &errWithState) {
if errWithState.DNSState() != dnsv1alpha1.STATE_ERROR && errWithState.DNSState() != dnsv1alpha1.STATE_INVALID && errWithState.DNSState() != dnsv1alpha1.STATE_STALE {
return err
}
} else {
return err
}
}
// Deploy the owner and wait for it to become ready
if err := d.owner.Deploy(ctx); err != nil {
return err
}
if err := d.owner.Wait(ctx); err != nil {
return err
}
// Wait for the entry to become ready
return d.entry.Wait(ctx)
}
func (d dnsRestoreDeployer) Destroy(_ context.Context) error { return nil }
func (b *Botanist) newDNSComponentsTargetingAPIServerAddress() {
if b.NeedsInternalDNS() {
ownerID := *b.Shoot.GetInfo().Status.ClusterIdentity + "-" + DNSInternalName
b.Shoot.Components.Extensions.DNS.InternalOwner = dns.NewOwner(
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.OwnerValues{
Name: DNSInternalName,
Active: pointer.Bool(true),
OwnerID: ownerID,
},
)
b.Shoot.Components.Extensions.DNS.InternalEntry = dns.NewEntry(
b.Logger,
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.EntryValues{
Name: DNSInternalName,
DNSName: gutil.GetAPIServerDomain(b.Shoot.InternalClusterDomain),
Targets: []string{b.APIServerAddress},
OwnerID: ownerID,
TTL: *b.Config.Controllers.Shoot.DNSEntryTTLSeconds,
},
)
b.Shoot.Components.Extensions.InternalDNSRecord.SetRecordType(extensionsv1alpha1helper.GetDNSRecordType(b.APIServerAddress))
b.Shoot.Components.Extensions.InternalDNSRecord.SetValues([]string{b.APIServerAddress})
}
if b.NeedsExternalDNS() {
ownerID := *b.Shoot.GetInfo().Status.ClusterIdentity + "-" + DNSExternalName
b.Shoot.Components.Extensions.DNS.ExternalOwner = dns.NewOwner(
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.OwnerValues{
Name: DNSExternalName,
Active: pointer.Bool(true),
OwnerID: ownerID,
},
)
b.Shoot.Components.Extensions.DNS.ExternalEntry = dns.NewEntry(
b.Logger,
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
&dns.EntryValues{
Name: DNSExternalName,
DNSName: gutil.GetAPIServerDomain(*b.Shoot.ExternalClusterDomain),
Targets: []string{b.APIServerAddress},
OwnerID: ownerID,
TTL: *b.Config.Controllers.Shoot.DNSEntryTTLSeconds,
},
)
b.Shoot.Components.Extensions.ExternalDNSRecord.SetRecordType(extensionsv1alpha1helper.GetDNSRecordType(b.APIServerAddress))
b.Shoot.Components.Extensions.ExternalDNSRecord.SetValues([]string{b.APIServerAddress})
}
}