Skip to content

Commit

Permalink
Merge pull request #265 from erikwilson/kubelet-resolv-conf
Browse files Browse the repository at this point in the history
Kubelet resolv.conf DNS update
  • Loading branch information
ibuildthecloud authored Mar 26, 2019
2 parents 028b8a4 + bb14bcb commit 93cc646
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion manifests/coredns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ data:
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
proxy . 1.1.1.1
proxy . /etc/resolv.conf
cache 30
loop
reload
Expand Down
47 changes: 47 additions & 0 deletions pkg/agent/config/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package config

import (
"bufio"
"context"
"crypto/tls"
"encoding/pem"
"fmt"
"io/ioutil"
sysnet "net"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -115,6 +118,49 @@ func writeKubeConfig(envInfo *cmds.Agent, info clientaccess.Info, controlConfig
return kubeConfigPath, info.WriteKubeConfig(kubeConfigPath)
}

func isValidResolvConf(resolvConfFile string) bool {
file, err := os.Open(resolvConfFile)
if err != nil {
return false
}
defer file.Close()

nameserver := regexp.MustCompile(`^nameserver\s+([^\s]*)`)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ipMatch := nameserver.FindStringSubmatch(scanner.Text())
if len(ipMatch) == 2 {
ip := sysnet.ParseIP(ipMatch[1])
if ip == nil || !ip.IsGlobalUnicast() {
return false
}
}
}
if err := scanner.Err(); err != nil {
return false
}
return true
}

func locateOrGenerateResolvConf(envInfo *cmds.Agent) string {
if envInfo.ResolvConf != "" {
return envInfo.ResolvConf
}
resolvConfs := []string{"/etc/resolv.conf", "/run/systemd/resolve/resolv.conf"}
for _, conf := range resolvConfs {
if isValidResolvConf(conf) {
return conf
}
}

tmpConf := filepath.Join(os.TempDir(), "k3s-resolv.conf")
if err := ioutil.WriteFile(tmpConf, []byte("nameserver 8.8.8.8\n"), 0444); err != nil {
logrus.Error(err)
return ""
}
return tmpConf
}

func get(envInfo *cmds.Agent) (*config.Node, error) {
if envInfo.Debug {
logrus.SetLevel(logrus.DebugLevel)
Expand Down Expand Up @@ -170,6 +216,7 @@ func get(envInfo *cmds.Agent) (*config.Node, error) {
nodeConfig.AgentConfig.NodeIP = nodeIP
nodeConfig.AgentConfig.NodeName = nodeName
nodeConfig.AgentConfig.ClusterDNS = controlConfig.ClusterDNS
nodeConfig.AgentConfig.ResolvConf = locateOrGenerateResolvConf(envInfo)
nodeConfig.AgentConfig.CACertPath = clientCA
nodeConfig.AgentConfig.ListenAddress = "127.0.0.1"
nodeConfig.AgentConfig.KubeConfig = kubeConfig
Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/cmds/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Agent struct {
Token string
TokenFile string
ServerURL string
ResolvConf string
DataDir string
NodeIP string
NodeName string
Expand Down Expand Up @@ -55,6 +56,12 @@ var (
Usage: "(agent) Disable embedded containerd and use alternative CRI implementation",
Destination: &AgentConfig.ContainerRuntimeEndpoint,
}
ResolvConfFlag = cli.StringFlag{
Name: "resolv-conf",
Usage: "Kubelet resolv.conf file",
EnvVar: "K3S_RESOLV_CONF",
Destination: &AgentConfig.ResolvConf,
}
)

func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command {
Expand Down Expand Up @@ -99,6 +106,7 @@ func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command {
NodeNameFlag,
NodeIPFlag,
CRIEndpointFlag,
ResolvConfFlag,
},
}
}
1 change: 1 addition & 0 deletions pkg/cli/cmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command {
DockerFlag,
FlannelFlag,
CRIEndpointFlag,
ResolvConfFlag,
},
}
}
3 changes: 3 additions & 0 deletions pkg/daemons/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func kubelet(cfg *config.Agent) {
if len(cfg.ClusterDNS) > 0 {
args = append(args, "--cluster-dns", cfg.ClusterDNS.String())
}
if cfg.ResolvConf != "" {
args = append(args, "--resolv-conf", cfg.ResolvConf)
}
if cfg.RuntimeSocket != "" {
args = append(args, "--container-runtime", "remote")
args = append(args, "--container-runtime-endpoint", cfg.RuntimeSocket)
Expand Down
1 change: 1 addition & 0 deletions pkg/daemons/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Agent struct {
NodeName string
ClusterCIDR net.IPNet
ClusterDNS net.IP
ResolvConf string
RootDir string
KubeConfig string
NodeIP string
Expand Down
2 changes: 1 addition & 1 deletion pkg/deploy/zz_generated_bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 93cc646

Please sign in to comment.