From a1ce08d4f11854375b6dc60be8241811cf3b5e36 Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Mon, 8 Jul 2019 16:02:06 -0700 Subject: [PATCH] Default node-ip from flannel-iface --- pkg/cli/agent/agent.go | 5 ++++ pkg/cli/server/server.go | 6 +++++ pkg/netutil/iface.go | 57 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 pkg/netutil/iface.go diff --git a/pkg/cli/agent/agent.go b/pkg/cli/agent/agent.go index 5f45f8d349..b8896509be 100644 --- a/pkg/cli/agent/agent.go +++ b/pkg/cli/agent/agent.go @@ -13,6 +13,7 @@ import ( "github.com/rancher/k3s/pkg/agent" "github.com/rancher/k3s/pkg/cli/cmds" "github.com/rancher/k3s/pkg/datadir" + "github.com/rancher/k3s/pkg/netutil" "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -56,6 +57,10 @@ func Run(ctx *cli.Context) error { return fmt.Errorf("--server is required") } + if cmds.AgentConfig.FlannelIface != "" && cmds.AgentConfig.NodeIP == "" { + cmds.AgentConfig.NodeIP = netutil.GetIPFromInterface(cmds.AgentConfig.FlannelIface) + } + logrus.Infof("Starting k3s agent %s", ctx.App.Version) dataDir, err := datadir.LocalHome(cmds.AgentConfig.DataDir, cmds.AgentConfig.Rootless) diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index 05d06fed94..eae5f9df8a 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -10,6 +10,8 @@ import ( "strings" "time" + "github.com/rancher/k3s/pkg/netutil" + systemd "github.com/coreos/go-systemd/daemon" "github.com/docker/docker/pkg/reexec" "github.com/natefinch/lumberjack" @@ -126,6 +128,10 @@ func run(app *cli.Context, cfg *cmds.Server) error { serverConfig.ControlConfig.AdvertisePort = cfg.AdvertisePort serverConfig.ControlConfig.BootstrapType = cfg.BootstrapType + if cmds.AgentConfig.FlannelIface != "" && cmds.AgentConfig.NodeIP == "" { + cmds.AgentConfig.NodeIP = netutil.GetIPFromInterface(cmds.AgentConfig.FlannelIface) + } + if serverConfig.ControlConfig.AdvertiseIP == "" && cmds.AgentConfig.NodeIP != "" { serverConfig.ControlConfig.AdvertiseIP = cmds.AgentConfig.NodeIP } diff --git a/pkg/netutil/iface.go b/pkg/netutil/iface.go new file mode 100644 index 0000000000..64de04bc94 --- /dev/null +++ b/pkg/netutil/iface.go @@ -0,0 +1,57 @@ +package netutil + +import ( + "fmt" + "net" + + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +func GetIPFromInterface(ifaceName string) string { + ip, err := getIPFromInterface(ifaceName) + if err != nil { + logrus.Warn(errors.Wrap(err, "unable to get global unicast ip from interface name")) + } else { + logrus.Infof("found ip %s from iface %s", ip, ifaceName) + } + return ip +} + +func getIPFromInterface(ifaceName string) (string, error) { + iface, err := net.InterfaceByName(ifaceName) + if err != nil { + return "", err + } + addrs, err := iface.Addrs() + if err != nil { + return "", err + } + if iface.Flags&net.FlagUp == 0 { + return "", fmt.Errorf("the interface %s is not up", ifaceName) + } + + globalUnicasts := []string{} + for _, addr := range addrs { + ip, _, err := net.ParseCIDR(addr.String()) + if err != nil { + return "", errors.Wrapf(err, "unable to parse CIDR for interface %s", iface.Name) + } + // skipping if not ipv4 + if ip.To4() == nil { + continue + } + if ip.IsGlobalUnicast() { + globalUnicasts = append(globalUnicasts, ip.String()) + } + } + + if len(globalUnicasts) > 1 { + return "", fmt.Errorf("multiple global unicast addresses defined for %s, please set ip from one of %v", ifaceName, globalUnicasts) + } + if len(globalUnicasts) == 1 { + return globalUnicasts[0], nil + } + + return "", fmt.Errorf("can't find ip for interface %s", ifaceName) +}