From 2268e028a24cbac6a876884d320a706de300313c Mon Sep 17 00:00:00 2001 From: Stuart Wallace Date: Tue, 19 Mar 2019 23:28:43 +0000 Subject: [PATCH] Add ability to override flannel interface --- pkg/agent/config/config.go | 9 +++++++++ pkg/agent/flannel/flannel.go | 17 ++++++++++------- pkg/agent/flannel/setup.go | 2 +- pkg/cli/cmds/agent.go | 7 +++++++ pkg/cli/cmds/server.go | 1 + pkg/daemons/config/types.go | 1 + 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/pkg/agent/config/config.go b/pkg/agent/config/config.go index 2ef55a322809..7c3fc89dc00a 100644 --- a/pkg/agent/config/config.go +++ b/pkg/agent/config/config.go @@ -206,11 +206,20 @@ func get(envInfo *cmds.Agent) (*config.Node, error) { return nil, errors.Wrapf(err, "failed to find host-local") } + var flannelIface *sysnet.Interface + if !envInfo.NoFlannel && len(envInfo.FlannelIface) > 0 { + flannelIface, err = sysnet.InterfaceByName(envInfo.FlannelIface) + if err != nil { + return nil, errors.Wrapf(err, "unable to find interface") + } + } + nodeConfig := &config.Node{ Docker: envInfo.Docker, NoFlannel: envInfo.NoFlannel, ContainerRuntimeEndpoint: envInfo.ContainerRuntimeEndpoint, } + nodeConfig.FlannelIface = flannelIface nodeConfig.LocalAddress = localAddress(controlConfig) nodeConfig.Images = filepath.Join(envInfo.DataDir, "images") nodeConfig.AgentConfig.NodeIP = nodeIP diff --git a/pkg/agent/flannel/flannel.go b/pkg/agent/flannel/flannel.go index 030cdaed5e9b..53f9d57d16dd 100644 --- a/pkg/agent/flannel/flannel.go +++ b/pkg/agent/flannel/flannel.go @@ -36,8 +36,8 @@ const ( subnetFile = "/run/flannel/subnet.env" ) -func flannel(ctx context.Context, flannelConf, kubeConfigFile string) error { - extIface, err := LookupExtIface() +func flannel(ctx context.Context, flannelIface *net.Interface, flannelConf, kubeConfigFile string) error { + extIface, err := LookupExtIface(flannelIface) if err != nil { return err } @@ -81,14 +81,17 @@ func flannel(ctx context.Context, flannelConf, kubeConfigFile string) error { return nil } -func LookupExtIface() (*backend.ExternalInterface, error) { - var iface *net.Interface +func LookupExtIface(iface *net.Interface) (*backend.ExternalInterface, error) { var ifaceAddr net.IP var err error - log.Info("Determining IP address of default interface") - if iface, err = ip.GetDefaultGatewayIface(); err != nil { - return nil, fmt.Errorf("failed to get default interface: %s", err) + if iface == nil { + log.Info("Determining IP address of default interface") + if iface, err = ip.GetDefaultGatewayIface(); err != nil { + return nil, fmt.Errorf("failed to get default interface: %s", err) + } + } else { + log.Info("Determining IP address of specified interface: ", iface.Name) } ifaceAddr, err = ip.GetIfaceIP4Addr(iface) diff --git a/pkg/agent/flannel/setup.go b/pkg/agent/flannel/setup.go index 8fb5d29165b5..e90158174ed8 100644 --- a/pkg/agent/flannel/setup.go +++ b/pkg/agent/flannel/setup.go @@ -79,7 +79,7 @@ func Run(ctx context.Context, config *config.Node) error { } go func() { - err := flannel(ctx, config.FlannelConf, config.AgentConfig.KubeConfig) + err := flannel(ctx, config.FlannelIface, config.FlannelConf, config.AgentConfig.KubeConfig) logrus.Fatalf("flannel exited: %v", err) }() diff --git a/pkg/cli/cmds/agent.go b/pkg/cli/cmds/agent.go index 0b02acee1925..cc8b4391870d 100644 --- a/pkg/cli/cmds/agent.go +++ b/pkg/cli/cmds/agent.go @@ -19,6 +19,7 @@ type Agent struct { Docker bool ContainerRuntimeEndpoint string NoFlannel bool + FlannelIface string Debug bool Rootless bool AgentShared @@ -54,6 +55,11 @@ var ( Usage: "(agent) Disable embedded flannel", Destination: &AgentConfig.NoFlannel, } + FlannelIfaceFlag = cli.StringFlag{ + Name: "flannel-iface", + Usage: "(agent) Override default flannel interface", + Destination: &AgentConfig.FlannelIface, + } CRIEndpointFlag = cli.StringFlag{ Name: "container-runtime-endpoint", Usage: "(agent) Disable embedded containerd and use alternative CRI implementation", @@ -121,6 +127,7 @@ func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command { }, DockerFlag, FlannelFlag, + FlannelIfaceFlag, NodeNameFlag, NodeIPFlag, CRIEndpointFlag, diff --git a/pkg/cli/cmds/server.go b/pkg/cli/cmds/server.go index 62e8a77d422f..5c230b5a6b9e 100644 --- a/pkg/cli/cmds/server.go +++ b/pkg/cli/cmds/server.go @@ -134,6 +134,7 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command { NodeNameFlag, DockerFlag, FlannelFlag, + FlannelIfaceFlag, CRIEndpointFlag, ResolvConfFlag, ExtraKubeletArgs, diff --git a/pkg/daemons/config/types.go b/pkg/daemons/config/types.go index 5c07ac3ce87a..822fc1d48a53 100644 --- a/pkg/daemons/config/types.go +++ b/pkg/daemons/config/types.go @@ -15,6 +15,7 @@ type Node struct { ContainerRuntimeEndpoint string NoFlannel bool FlannelConf string + FlannelIface *net.Interface LocalAddress string Containerd Containerd Images string