Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix external name not solving by reloading resolv.conf. #225

Merged
merged 3 commits into from
Apr 16, 2018

Conversation

grayluck
Copy link
Contributor

Now skydns will be configured with resolv.conf when nameserver is empty.

Fixes#224 (#224)

\assign @MrHohn

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 14, 2018
@MrHohn
Copy link
Member

MrHohn commented Apr 14, 2018

/ok-to-test

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Apr 14, 2018
pkg/dns/dns.go Outdated
kd.SkyDNSConfig.Nameservers = nameServers
if len(nameServers) == 0 {
kd.SkyDNSConfig.Nameservers = kd.loadDefaultNameserver()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like kd.SkyDNSConfig.Nameservers = nameServers is still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed..

Now skydns will be configured with resolv.conf when nameserver is empty.
pkg/dns/dns.go Outdated
@@ -139,6 +140,19 @@ func NewKubeDNS(client clientset.Interface, clusterDomain string, timeout time.D
return kd
}

func (kd *KubeDNS) loadDefaultNameserver() []string {
nameservers := []string{}
if c, err := dns.ClientConfigFromFile("/etc/resolv.conf"); !os.IsNotExist(err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is not exist different from parse error in this case? I think we should return empty array in both cases (feel like there are some redundant logic)?

Can we log if ClientConfigFromFile returns error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logged using verbose level 1.

pkg/dns/dns.go Outdated
}
return nameservers
}

func (kd *KubeDNS) updateConfig(nextConfig *config.Config) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a unit test for updating nameservers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit test for updateConfig added.

@MrHohn
Copy link
Member

MrHohn commented Apr 16, 2018

Keep @bowei in the loop.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 16, 2018
@grayluck grayluck force-pushed the externalname branch 2 times, most recently from 4834ebb to bf3f6e8 Compare April 16, 2018 20:20
@grayluck
Copy link
Contributor Author

Done. Unit test added. PTAL thx

pkg/dns/dns.go Outdated
nameservers = append(nameservers, net.JoinHostPort(s, c.Port))
}
} else {
glog.V(1).Infof("Load nameserver from resolv.conf failed: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use glog.Errorf() here. Can we move the error case up and early return?

if c, err := dns.ClientConfigFromFile(defaultResolvFile); err != nil {
// log error
return
}

// do the rest

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

pkg/dns/dns.go Outdated
@@ -148,12 +164,20 @@ func (kd *KubeDNS) updateConfig(nextConfig *config.Config) {
for _, nameServer := range nextConfig.UpstreamNameservers {
if ip, port, err := util.ValidateNameserverIpAndPort(nameServer); err != nil {
glog.V(1).Infof("Invalid nameserver %q: %v", nameServer, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glog.Errorf()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Now skyDNS nameservers will fall back to resolv.conf whenever it's empty.
if c, err := dns.ClientConfigFromFile(defaultResolvFile); err != nil {
glog.Errorf("Load nameserver from resolv.conf failed: %v", err)
return []string{}
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else block seems unnecessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, we are using thec.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, let keep this as is.

pkg/dns/dns.go Outdated
glog.Errorf("Invalid nameserver %q: %v", nameServer, err)
if len(kd.SkyDNSConfig.Nameservers) == 0 {
// Fall back to resolv.conf on initialization failed.
kd.SkyDNSConfig.Nameservers = kd.loadDefaultNameserver()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should fall through here --- append whatever valid nameservers are passed in, and reuse the 0 length detection logic below:

if ip, port, err := util.ValidateNameserverIpAndPort(nameServer); err != nil {
   glog.Errorf("Invalid nameserver %q: %v", nameServer, err)
} else {
   nameServers = append(nameServers, net.JoinHostPort(ip, port))
}
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is, we don't want to pollute kd.config with invalid input. (But the truth is, kd.config.UpstreamNameservers are never used so it's okay to be polluted)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, though having duplicated logic here seems bad. What about:

	if kd.SkyDNSConfig != nil {
		var nameServers []string
		for _, nameServer := range nextConfig.UpstreamNameservers {
			if ip, port, err := util.ValidateNameserverIpAndPort(nameServer); err != nil {
				glog.Errorf("Invalid nameserver %q: %v", nameServer, err)
				// Don't populate nameServers if invalid input is detected.
				nameServers = []string{}
				break
			} else {
				nameServers = append(nameServers, net.JoinHostPort(ip, port))
			}
		}
		if len(nameServers) == 0 {
			...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry what I wrote is wrong. I got what you were saying.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can still do the partially update and leave kd.config untouched. Do you want me to do that?
I am kind of against this since kd.SkyDNSConfig is partially updated and kd.config is not which might be confusing in some extreme cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am kind of against this since kd.SkyDNSConfig is partially updated and kd.config is not which might be confusing in some extreme cases.

Agreed, that sounds bad. Let's keep this.

Copy link
Member

@MrHohn MrHohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 16, 2018
pkg/dns/dns.go Outdated
glog.V(1).Infof("Invalid nameserver %q: %v", nameServer, err)
glog.Errorf("Invalid nameserver %q: %v", nameServer, err)
if len(kd.SkyDNSConfig.Nameservers) == 0 {
// Fall back to resolv.conf on initialization failed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: initialization failed -> initialization failure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

pkg/dns/dns.go Outdated
glog.Errorf("Invalid nameserver %q: %v", nameServer, err)
if len(kd.SkyDNSConfig.Nameservers) == 0 {
// Fall back to resolv.conf on initialization failed.
kd.SkyDNSConfig.Nameservers = kd.loadDefaultNameserver()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am kind of against this since kd.SkyDNSConfig is partially updated and kd.config is not which might be confusing in some extreme cases.

Agreed, that sounds bad. Let's keep this.

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 16, 2018
@grayluck
Copy link
Contributor Author

Thanks for review!

Copy link
Member

@MrHohn MrHohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 16, 2018
@MrHohn
Copy link
Member

MrHohn commented Apr 16, 2018

Will merge this once travis passes.

@MrHohn MrHohn merged commit 80fdd88 into kubernetes:master Apr 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants