-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
dns.go
288 lines (247 loc) · 7.93 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
/*
Copyright 2019 The Kubernetes Authors.
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 cloudup
import (
"context"
"fmt"
"net"
"os"
"strings"
"k8s.io/klog/v2"
"k8s.io/kops/dns-controller/pkg/dns"
"k8s.io/kops/dnsprovider/pkg/dnsprovider"
"k8s.io/kops/dnsprovider/pkg/dnsprovider/rrstype"
"k8s.io/kops/pkg/apis/kops"
apimodel "k8s.io/kops/pkg/apis/kops/model"
"k8s.io/kops/upup/pkg/fi"
)
const (
// PlaceholderIP is from TEST-NET-3
// https://en.wikipedia.org/wiki/Reserved_IP_addresses
PlaceholderIP = "203.0.113.123"
PlaceholderIPv6 = "fd00:dead:add::"
PlaceholderTTL = 10
// DigitalOcean's DNS servers require a certain minimum TTL (it's 30), keeping 60 here.
PlaceholderTTLDigitialOcean = 60
)
type recordKey struct {
hostname string
rrsType rrstype.RrsType
}
func findZone(cluster *kops.Cluster, cloud fi.Cloud) (dnsprovider.Zone, error) {
dns, err := cloud.DNS()
if err != nil {
return nil, fmt.Errorf("error building DNS provider: %v", err)
}
if dns == nil {
return nil, nil
}
zonesProvider, ok := dns.Zones()
if !ok {
return nil, fmt.Errorf("error getting DNS zones provider")
}
zones, err := zonesProvider.List()
if err != nil {
return nil, fmt.Errorf("error listing DNS zones: %v", err)
}
var matches []dnsprovider.Zone
findName := strings.TrimSuffix(cluster.Spec.DNSZone, ".")
for _, zone := range zones {
id := zone.ID()
name := strings.TrimSuffix(zone.Name(), ".")
if id == cluster.Spec.DNSZone || name == findName {
matches = append(matches, zone)
}
}
if len(matches) == 0 {
return nil, fmt.Errorf("cannot find DNS Zone %q. Please pre-create the zone and set up NS records so that it resolves", cluster.Spec.DNSZone)
}
if len(matches) > 1 {
klog.Infof("Found multiple DNS Zones matching %q, please set the cluster's spec.dnsZone to the desired Zone ID:", cluster.Spec.DNSZone)
for _, zone := range zones {
id := zone.ID()
klog.Infof("\t%s", id)
}
return nil, fmt.Errorf("found multiple DNS Zones matching %q", cluster.Spec.DNSZone)
}
zone := matches[0]
return zone, nil
}
func validateDNS(cluster *kops.Cluster, cloud fi.Cloud) error {
if !cluster.PublishesDNSRecords() || cluster.UsesPrivateDNS() {
klog.V(2).Infof("Skipping DNS validation for non-public DNS")
return nil
}
zone, err := findZone(cluster, cloud)
if err != nil {
return err
}
if zone == nil {
return nil
}
dnsName := strings.TrimSuffix(zone.Name(), ".")
klog.V(2).Infof("Doing DNS lookup to verify NS records for %q", dnsName)
ns, err := net.LookupNS(dnsName)
if err != nil {
return fmt.Errorf("error doing DNS lookup for NS records for %q: %v", dnsName, err)
}
if len(ns) == 0 {
if os.Getenv("DNS_IGNORE_NS_CHECK") == "" {
return fmt.Errorf("NS records not found for %q - please make sure they are correctly configured", dnsName)
}
klog.Warningf("Ignoring failed NS record check because DNS_IGNORE_NS_CHECK is set")
} else {
var hosts []string
for _, n := range ns {
hosts = append(hosts, n.Host)
}
klog.V(2).Infof("Found NS records for %q: %v", dnsName, hosts)
}
return nil
}
func precreateDNS(ctx context.Context, cluster *kops.Cluster, cloud fi.Cloud) error {
// TODO: Move to update
// We precreate some DNS names (where they don't exist), with a dummy IP address
// This avoids hitting negative TTL on DNS lookups, which tend to be very long
// If we get the names wrong here, it doesn't really matter (extra DNS name, slower boot)
// Nothing to do for Gossip clusters and clusters without DNS
if !cluster.PublishesDNSRecords() {
return nil
}
recordKeys := buildPrecreateDNSHostnames(cluster)
if len(recordKeys) == 0 {
klog.V(2).Infof("No DNS records to pre-create")
return nil
}
klog.V(2).Infof("Checking DNS records")
zone, err := findZone(cluster, cloud)
if err != nil {
return err
}
if zone == nil {
return nil
}
rrs, ok := zone.ResourceRecordSets()
if !ok {
return fmt.Errorf("error getting DNS resource records for %q", zone.Name())
}
recordsMap := make(map[string]dnsprovider.ResourceRecordSet)
// TODO: We should change the filter to be a suffix match instead
// records, err := rrs.List("", "")
records, err := rrs.List()
if err != nil {
return fmt.Errorf("error listing DNS resource records for %q: %v", zone.Name(), err)
}
for _, record := range records {
name := dns.EnsureDotSuffix(record.Name())
key := string(record.Type()) + "::" + name
recordsMap[key] = record
}
changeset := rrs.StartChangeset()
// TODO: Add ChangeSet.IsEmpty() method
var created []recordKey
for _, recordKey := range recordKeys {
recordKey.hostname = dns.EnsureDotSuffix(recordKey.hostname)
foundAddress := false
{
dnsRecord := recordsMap[string(recordKey.rrsType)+"::"+recordKey.hostname]
if dnsRecord != nil {
rrdatas := dnsRecord.Rrdatas()
if len(rrdatas) > 0 {
klog.V(4).Infof("Found DNS record %s => %s; won't create", recordKey, rrdatas)
} else {
// This is probably an alias target; leave it alone...
klog.V(4).Infof("Found DNS record %s, but no records", recordKey)
}
foundAddress = true
}
}
foundTXT := false
{
dnsRecord := recordsMap["TXT::"+recordKey.hostname]
if dnsRecord != nil {
foundTXT = true
}
}
if foundAddress && foundTXT {
continue
}
ip := PlaceholderIP
if recordKey.rrsType != rrstype.A {
ip = PlaceholderIPv6
}
klog.V(2).Infof("Pre-creating DNS record %s => %s", recordKey, ip)
if !foundAddress {
if cloud.ProviderID() == kops.CloudProviderDO {
changeset.Add(rrs.New(recordKey.hostname, []string{ip}, PlaceholderTTLDigitialOcean, recordKey.rrsType))
} else {
changeset.Add(rrs.New(recordKey.hostname, []string{ip}, PlaceholderTTL, recordKey.rrsType))
}
}
if !foundTXT {
if cluster.Spec.ExternalDNS != nil && cluster.Spec.ExternalDNS.Provider == kops.ExternalDNSProviderExternalDNS {
domain := recordKey.hostname
if ip == PlaceholderIPv6 {
domain = "aaaa-" + domain
}
changeset.Add(rrs.New(domain, []string{fmt.Sprintf("\"heritage=external-dns,external-dns/owner=kops-%s\"", cluster.ObjectMeta.Name)}, PlaceholderTTL, rrstype.TXT))
}
}
created = append(created, recordKey)
}
if len(created) != 0 {
klog.Infof("Pre-creating DNS records")
err := changeset.Apply(ctx)
if err != nil {
return fmt.Errorf("error pre-creating DNS records: %v", err)
}
klog.V(2).Infof("Pre-created DNS names: %v", created)
}
return nil
}
// buildPrecreateDNSHostnames returns the hostnames we should precreate
func buildPrecreateDNSHostnames(cluster *kops.Cluster) []recordKey {
var recordKeys []recordKey
internalType := rrstype.A
if cluster.Spec.IsIPv6Only() {
internalType = rrstype.AAAA
}
hasAPILoadbalancer := cluster.Spec.API.LoadBalancer != nil
useLBForInternalAPI := hasAPILoadbalancer && cluster.Spec.API.LoadBalancer.UseForInternalAPI
if cluster.Spec.API.PublicName != "" && !hasAPILoadbalancer {
recordKeys = append(recordKeys, recordKey{
hostname: cluster.Spec.API.PublicName,
rrsType: rrstype.A,
})
if internalType != rrstype.A {
recordKeys = append(recordKeys, recordKey{
hostname: cluster.Spec.API.PublicName,
rrsType: internalType,
})
}
}
if !useLBForInternalAPI {
recordKeys = append(recordKeys, recordKey{
hostname: cluster.APIInternalName(),
rrsType: internalType,
})
}
if apimodel.UseKopsControllerForNodeBootstrap(cluster) {
name := "kops-controller.internal." + cluster.ObjectMeta.Name
recordKeys = append(recordKeys, recordKey{
hostname: name,
rrsType: internalType,
})
}
return recordKeys
}