Skip to content

Commit fb222e9

Browse files
committed
Fix fallback DNS for IMDS and IPV6-only
Signed-off-by: Brad Davidson <[email protected]>
1 parent be729fc commit fb222e9

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

pkg/agent/config/config.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const (
4747
DefaultPodManifestPath = "pod-manifests"
4848
)
4949

50+
var InstanceMetadataServiceIP = net.ParseIP("169.254.169.254")
51+
5052
// Get returns a pointer to a completed Node configuration struct,
5153
// containing a merging of the local CLI configuration with settings from the server.
5254
// Node configuration includes client certificates, which requires node password verification,
@@ -390,8 +392,7 @@ func isValidResolvConf(resolvConfFile string) bool {
390392
for scanner.Scan() {
391393
ipMatch := nameserver.FindStringSubmatch(scanner.Text())
392394
if len(ipMatch) == 2 {
393-
ip := net.ParseIP(ipMatch[1])
394-
if ip == nil || !ip.IsGlobalUnicast() {
395+
if !isValidNameserver(ipMatch[1]) {
395396
return false
396397
} else {
397398
foundNameserver = true
@@ -404,6 +405,21 @@ func isValidResolvConf(resolvConfFile string) bool {
404405
return foundNameserver
405406
}
406407

408+
// isValidNameserver returns a boolean indicating whether or not the IP is a valid
409+
// upstream resolver address. Resolver IPs must be valid global unicast addresses, with
410+
// the exception of the instance metadata service IP, which some cloud providers require
411+
// traffic be forwarded to in order for private DNS to work properly.
412+
func isValidNameserver(addr string) bool {
413+
ip := net.ParseIP(addr)
414+
if ip == nil {
415+
return false
416+
}
417+
if !ip.IsGlobalUnicast() && !ip.Equal(InstanceMetadataServiceIP) {
418+
return false
419+
}
420+
return true
421+
}
422+
407423
func locateOrGenerateResolvConf(envInfo *cmds.Agent) string {
408424
if envInfo.ResolvConf != "" {
409425
return envInfo.ResolvConf
@@ -416,11 +432,11 @@ func locateOrGenerateResolvConf(envInfo *cmds.Agent) string {
416432
}
417433

418434
resolvConf := filepath.Join(envInfo.DataDir, "agent", "etc", "resolv.conf")
419-
if err := agentutil.WriteFile(resolvConf, "nameserver 8.8.8.8\n"); err != nil {
435+
if err := agentutil.WriteFile(resolvConf, "nameserver 8.8.8.8\nnameserver 2001:4860:4860::8888\n"); err != nil {
420436
logrus.Errorf("Failed to write %s: %v", resolvConf, err)
421437
return ""
422438
}
423-
logrus.Warnf("Host resolv.conf includes loopback or multicast nameservers - kubelet will use autogenerated resolv.conf with nameserver 8.8.8.8")
439+
logrus.Warnf("Host resolv.conf includes loopback, multicast, or link-local nameservers - kubelet will use autogenerated resolv.conf with nameservers 8.8.8.8 2001:4860:4860::8888")
424440
return resolvConf
425441
}
426442

pkg/agent/config/config_internal_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ func Test_isValidResolvConf(t *testing.T) {
1313
}{
1414
{name: "Valid ResolvConf", fileContent: "nameserver 8.8.8.8\nnameserver 2001:4860:4860::8888\n", expectedResult: true},
1515
{name: "Invalid ResolvConf", fileContent: "nameserver 999.999.999.999\nnameserver not.an.ip\n", expectedResult: false},
16-
{name: "Wrong Nameserver", fileContent: "search example.com\n", expectedResult: false},
16+
{name: "No Nameserver", fileContent: "search example.com\n", expectedResult: false},
1717
{name: "One valid nameserver", fileContent: "test test.com\nnameserver 8.8.8.8", expectedResult: true},
1818
{name: "Non GlobalUnicast", fileContent: "nameserver ::1\nnameserver 169.254.0.1\nnameserver fe80::1\n", expectedResult: false},
19+
{name: "Instance Metadata", fileContent: "nameserver 169.254.169.254", expectedResult: true},
1920
{name: "Empty File", fileContent: "", expectedResult: false},
2021
}
2122

0 commit comments

Comments
 (0)