From 7d950520e5467c2d12d0aa7686063a01072afd5b Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Wed, 21 Dec 2016 18:06:16 -0800 Subject: [PATCH] Allow swarm join with `--availability=drain` This fix tries to address the issue raised in 24596 where it was not possible to join as manager only (`--availability=drain`). This fix adds a new flag `--availability` to `swarm join`. Related documentation has been updated. An integration test has been added. NOTE: Additional pull request for swarmkit and engine-api will be created separately. This fix fixes 24596. Signed-off-by: Yong Tang Upstream-commit: ab2635ead050ea2f283da637ed5115d7cfb7f694 Component: cli --- components/cli/command/swarm/join.go | 21 ++++++++++++++++++--- components/cli/command/swarm/opts.go | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/components/cli/command/swarm/join.go b/components/cli/command/swarm/join.go index 3ea1462df4..40fc5c192f 100644 --- a/components/cli/command/swarm/join.go +++ b/components/cli/command/swarm/join.go @@ -2,12 +2,15 @@ package swarm import ( "fmt" + "strings" + + "golang.org/x/net/context" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/cli" "github.com/docker/docker/cli/command" "github.com/spf13/cobra" - "golang.org/x/net/context" + "github.com/spf13/pflag" ) type joinOptions struct { @@ -16,6 +19,7 @@ type joinOptions struct { // Not a NodeAddrOption because it has no default port. advertiseAddr string token string + availability string } func newJoinCommand(dockerCli command.Cli) *cobra.Command { @@ -29,7 +33,7 @@ func newJoinCommand(dockerCli command.Cli) *cobra.Command { Args: cli.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.remote = args[0] - return runJoin(dockerCli, opts) + return runJoin(dockerCli, cmd.Flags(), opts) }, } @@ -37,10 +41,11 @@ func newJoinCommand(dockerCli command.Cli) *cobra.Command { flags.Var(&opts.listenAddr, flagListenAddr, "Listen address (format: [:port])") flags.StringVar(&opts.advertiseAddr, flagAdvertiseAddr, "", "Advertised address (format: [:port])") flags.StringVar(&opts.token, flagToken, "", "Token for entry into the swarm") + flags.StringVar(&opts.availability, flagAvailability, "active", "Availability of the node (active/pause/drain)") return cmd } -func runJoin(dockerCli command.Cli, opts joinOptions) error { +func runJoin(dockerCli command.Cli, flags *pflag.FlagSet, opts joinOptions) error { client := dockerCli.Client() ctx := context.Background() @@ -50,6 +55,16 @@ func runJoin(dockerCli command.Cli, opts joinOptions) error { AdvertiseAddr: opts.advertiseAddr, RemoteAddrs: []string{opts.remote}, } + if flags.Changed(flagAvailability) { + availability := swarm.NodeAvailability(strings.ToLower(opts.availability)) + switch availability { + case swarm.NodeAvailabilityActive, swarm.NodeAvailabilityPause, swarm.NodeAvailabilityDrain: + req.Availability = availability + default: + return fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability) + } + } + err := client.SwarmJoin(ctx, req) if err != nil { return err diff --git a/components/cli/command/swarm/opts.go b/components/cli/command/swarm/opts.go index 9db46dcf55..40f88a4412 100644 --- a/components/cli/command/swarm/opts.go +++ b/components/cli/command/swarm/opts.go @@ -28,6 +28,7 @@ const ( flagSnapshotInterval = "snapshot-interval" flagLockKey = "lock-key" flagAutolock = "autolock" + flagAvailability = "availability" ) type swarmOptions struct {