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 <alexandre.beslic@gmail.com>
This commit is contained in:
Alexandre Beslic 2016-08-05 15:51:32 -07:00
parent 6401bd65b1
commit 5479df79c7
3 changed files with 16 additions and 0 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)