Skip to content

Commit

Permalink
Merge pull request #225 from grayluck/externalname
Browse files Browse the repository at this point in the history
Fix external name not solving by reloading resolv.conf.
  • Loading branch information
MrHohn authored Apr 16, 2018
2 parents 6df9916 + 969920f commit 80fdd88
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
6 changes: 5 additions & 1 deletion cmd/kube-dns/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ func NewKubeDNSServerDefault(config *options.KubeDNSConfig) *KubeDNSServer {

default:
glog.V(0).Infof("ConfigMap and ConfigDir not configured, using values from command line flags")
configSync = dnsconfig.NewNopSync(&dnsconfig.Config{Federations: config.Federations, UpstreamNameservers: strings.Split(config.NameServers, ",")})
conf := dnsconfig.Config{Federations: config.Federations}
if len(config.NameServers) > 0 {
conf.UpstreamNameservers = strings.Split(config.NameServers, ",")
}
configSync = dnsconfig.NewNopSync(&conf)
}

return &KubeDNSServer{
Expand Down
29 changes: 27 additions & 2 deletions pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ const (
resyncPeriod = 5 * time.Minute
)

var (
defaultResolvFile = "/etc/resolv.conf"
)

type KubeDNS struct {
// kubeClient makes calls to API Server and registers calls with API Server
// to get Endpoints and Service objects.
Expand Down Expand Up @@ -139,6 +143,19 @@ func NewKubeDNS(client clientset.Interface, clusterDomain string, timeout time.D
return kd
}

func (kd *KubeDNS) loadDefaultNameserver() []string {
if c, err := dns.ClientConfigFromFile(defaultResolvFile); err != nil {
glog.Errorf("Load nameserver from resolv.conf failed: %v", err)
return []string{}
} else {
nameservers := []string{}
for _, s := range c.Servers {
nameservers = append(nameservers, net.JoinHostPort(s, c.Port))
}
return nameservers
}
}

func (kd *KubeDNS) updateConfig(nextConfig *config.Config) {
kd.configLock.Lock()
defer kd.configLock.Unlock()
Expand All @@ -147,13 +164,21 @@ func (kd *KubeDNS) updateConfig(nextConfig *config.Config) {
var nameServers []string
for _, nameServer := range nextConfig.UpstreamNameservers {
if ip, port, err := util.ValidateNameserverIpAndPort(nameServer); err != nil {
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 failure.
kd.SkyDNSConfig.Nameservers = kd.loadDefaultNameserver()
}
return
} else {
nameServers = append(nameServers, net.JoinHostPort(ip, port))
}
}
kd.SkyDNSConfig.Nameservers = nameServers
if len(nameServers) == 0 {
kd.SkyDNSConfig.Nameservers = kd.loadDefaultNameserver()
} else {
kd.SkyDNSConfig.Nameservers = nameServers
}
}
kd.config = nextConfig
glog.V(2).Infof("Configuration updated: %+v", *kd.config)
Expand Down
33 changes: 33 additions & 0 deletions pkg/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package dns

import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
Expand Down Expand Up @@ -702,6 +705,36 @@ func TestConfigSyncInitialMap(t *testing.T) {
checkConfigEqual(t, kd, &config.Config{Federations: map[string]string{"name3": "domain3"}})
}

func TestUpdateConfig(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test")
defaultResolvFile = filepath.Join(tmpdir, "resolv.conf")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)

kd := newKubeDNS()
kd.SkyDNSConfig = new(skyserver.Config)

nextConfig := &config.Config{UpstreamNameservers: []string{"badNameserver"}}
kd.updateConfig(nextConfig)
assert.NotEqual(t, nextConfig, kd.config)
assert.Equal(t, []string{}, kd.SkyDNSConfig.Nameservers)

err = ioutil.WriteFile(defaultResolvFile, []byte("nameserver 127.0.0.1"), 0666)
require.NoError(t, err)

kd.updateConfig(nextConfig)
assert.Equal(t, []string{"127.0.0.1:53"}, kd.SkyDNSConfig.Nameservers)

nextConfig = &config.Config{UpstreamNameservers: []string{"192.0.2.123:10086", "192.0.2.123"}}
kd.updateConfig(nextConfig)
assert.Equal(t, nextConfig, kd.config)
assert.Equal(t, []string{"192.0.2.123:10086", "192.0.2.123:53"}, kd.SkyDNSConfig.Nameservers)

nextConfig = new(config.Config)
kd.updateConfig(nextConfig)
assert.Equal(t, []string{"127.0.0.1:53"}, kd.SkyDNSConfig.Nameservers)
}

func newNodes() *v1.NodeList {
return &v1.NodeList{
Items: []v1.Node{
Expand Down

0 comments on commit 80fdd88

Please sign in to comment.