From 5479df79c7859d5a09f42c22ddb5fa3e56ceeb7c Mon Sep 17 00:00:00 2001 From: Alexandre Beslic Date: Fri, 5 Aug 2016 15:51:32 -0700 Subject: [PATCH] swarm: improve cli output on node promote/demote for unchanged role As of now promoting (or demoting) a node that has its role left unchanged will always print a successful message. This PR fixes the issue by matching the behavior on swarmkit's swarmctl command and printing a message when desired role is the current role of the node. As a result this also avoids calling update when it is not necessary. Signed-off-by: Alexandre Beslic --- api/client/node/demote.go | 4 ++++ api/client/node/promote.go | 4 ++++ api/client/node/update.go | 8 ++++++++ 3 files changed, 16 insertions(+) diff --git a/api/client/node/demote.go b/api/client/node/demote.go index 3819b1a399..6acf5c3eb2 100644 --- a/api/client/node/demote.go +++ b/api/client/node/demote.go @@ -22,6 +22,10 @@ func newDemoteCommand(dockerCli *client.DockerCli) *cobra.Command { func runDemote(dockerCli *client.DockerCli, nodes []string) error { demote := func(node *swarm.Node) error { + if node.Spec.Role == swarm.NodeRoleWorker { + fmt.Fprintf(dockerCli.Out(), "Node %s is already a worker.\n", node.ID) + return errNoRoleChange + } node.Spec.Role = swarm.NodeRoleWorker return nil } diff --git a/api/client/node/promote.go b/api/client/node/promote.go index 39e284f937..9bfc4a277f 100644 --- a/api/client/node/promote.go +++ b/api/client/node/promote.go @@ -22,6 +22,10 @@ func newPromoteCommand(dockerCli *client.DockerCli) *cobra.Command { func runPromote(dockerCli *client.DockerCli, nodes []string) error { promote := func(node *swarm.Node) error { + if node.Spec.Role == swarm.NodeRoleManager { + fmt.Fprintf(dockerCli.Out(), "Node %s is already a manager.\n", node.ID) + return errNoRoleChange + } node.Spec.Role = swarm.NodeRoleManager return nil } diff --git a/api/client/node/update.go b/api/client/node/update.go index 1d070dd536..9437853ca4 100644 --- a/api/client/node/update.go +++ b/api/client/node/update.go @@ -1,6 +1,7 @@ package node import ( + "errors" "fmt" "github.com/docker/docker/api/client" @@ -13,6 +14,10 @@ import ( "golang.org/x/net/context" ) +var ( + errNoRoleChange = errors.New("role was already set to the requested value") +) + func newUpdateCommand(dockerCli *client.DockerCli) *cobra.Command { nodeOpts := newNodeOptions() @@ -53,6 +58,9 @@ func updateNodes(dockerCli *client.DockerCli, nodes []string, mergeNode func(nod err = mergeNode(&node) if err != nil { + if err == errNoRoleChange { + continue + } return err } err = client.NodeUpdate(ctx, node.ID, node.Version, node.Spec)