From 3cf087c30077ddb17afee13d5915e4ae905c4665 Mon Sep 17 00:00:00 2001 From: fanjiyun Date: Fri, 15 May 2020 17:41:20 +0800 Subject: [PATCH] control network interface order for containers Signed-off-by: fanjiyun --- cli/command/container/opts.go | 1 + cli/command/network/connect.go | 3 +++ opts/network.go | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index f53ae5960de4..c23d56e1eb88 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -804,6 +804,7 @@ func parseNetworkAttachmentOpt(ep opts.NetworkAttachmentOpts) (*networktypes.End LinkLocalIPs: ep.LinkLocalIPs, } } + epConfig.Priority = ep.Priority return epConfig, nil } diff --git a/cli/command/network/connect.go b/cli/command/network/connect.go index 04108e20fadb..81571090edf0 100644 --- a/cli/command/network/connect.go +++ b/cli/command/network/connect.go @@ -21,6 +21,7 @@ type connectOptions struct { aliases []string linklocalips []string driverOpts []string + priority int } func newConnectCommand(dockerCli command.Cli) *cobra.Command { @@ -46,6 +47,7 @@ func newConnectCommand(dockerCli command.Cli) *cobra.Command { flags.StringSliceVar(&options.aliases, "alias", []string{}, "Add network-scoped alias for the container") flags.StringSliceVar(&options.linklocalips, "link-local-ip", []string{}, "Add a link-local address for the container") flags.StringSliceVar(&options.driverOpts, "driver-opt", []string{}, "driver options for the network") + flags.IntVar(&options.priority, "priority", 0, "Set network priority (default 0)") return cmd } @@ -65,6 +67,7 @@ func runConnect(dockerCli command.Cli, options connectOptions) error { Links: options.links.GetAll(), Aliases: options.aliases, DriverOpts: driverOpts, + Priority: options.priority, } return client.NetworkConnect(context.Background(), options.network, options.container, epConfig) diff --git a/opts/network.go b/opts/network.go index ce7370ee0eea..48dbfb3b8b12 100644 --- a/opts/network.go +++ b/opts/network.go @@ -4,6 +4,7 @@ import ( "encoding/csv" "fmt" "regexp" + "strconv" "strings" ) @@ -13,6 +14,7 @@ const ( networkOptIPv4Address = "ip" networkOptIPv6Address = "ip6" driverOpt = "driver-opt" + networkPriority = "priority" ) // NetworkAttachmentOpts represents the network options for endpoint creation @@ -24,6 +26,7 @@ type NetworkAttachmentOpts struct { IPv4Address string IPv6Address string LinkLocalIPs []string // TODO add support for LinkLocalIPs in the csv notation of `--network` ? + Priority int } // NetworkOpt represents a network config in swarm mode. @@ -76,6 +79,12 @@ func (n *NetworkOpt) Set(value string) error { } else { return err } + case networkPriority: + prior, err := strconv.Atoi(value) + if err != nil { + return fmt.Errorf("invalid priority value %s", value) + } + netOpt.Priority = prior default: return fmt.Errorf("invalid field key %s", key) }