forked from docker/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As described in our ROADMAP.md, introduce new Swarm management commands to call to the corresponding API endpoints. This PR is fully backward compatible (joining a Swarm is an optional feature of the Engine, and existing commands are not impacted). Signed-off-by: Daniel Nephin <[email protected]> Signed-off-by: Victor Vieux <[email protected]> Signed-off-by: Tonis Tiigi <[email protected]>
- Loading branch information
1 parent
d4abe1d
commit 12a00e6
Showing
36 changed files
with
2,612 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package idresolver | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/docker/engine-api/client" | ||
"github.com/docker/engine-api/types/swarm" | ||
) | ||
|
||
// IDResolver provides ID to Name resolution. | ||
type IDResolver struct { | ||
client client.APIClient | ||
noResolve bool | ||
cache map[string]string | ||
} | ||
|
||
// New creates a new IDResolver. | ||
func New(client client.APIClient, noResolve bool) *IDResolver { | ||
return &IDResolver{ | ||
client: client, | ||
noResolve: noResolve, | ||
cache: make(map[string]string), | ||
} | ||
} | ||
|
||
func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string, error) { | ||
switch t.(type) { | ||
case swarm.Node: | ||
node, err := r.client.NodeInspect(ctx, id) | ||
if err != nil { | ||
return id, nil | ||
} | ||
if node.Spec.Annotations.Name != "" { | ||
return node.Spec.Annotations.Name, nil | ||
} | ||
if node.Description.Hostname != "" { | ||
return node.Description.Hostname, nil | ||
} | ||
return id, nil | ||
case swarm.Service: | ||
service, err := r.client.ServiceInspect(ctx, id) | ||
if err != nil { | ||
return id, nil | ||
} | ||
return service.Spec.Annotations.Name, nil | ||
default: | ||
return "", fmt.Errorf("unsupported type") | ||
} | ||
|
||
} | ||
|
||
// Resolve will attempt to resolve an ID to a Name by querying the manager. | ||
// Results are stored into a cache. | ||
// If the `-n` flag is used in the command-line, resolution is disabled. | ||
func (r *IDResolver) Resolve(ctx context.Context, t interface{}, id string) (string, error) { | ||
if r.noResolve { | ||
return id, nil | ||
} | ||
if name, ok := r.cache[id]; ok { | ||
return name, nil | ||
} | ||
name, err := r.get(ctx, t, id) | ||
if err != nil { | ||
return "", err | ||
} | ||
r.cache[id] = name | ||
return name, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package node | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/docker/docker/api/client" | ||
"github.com/docker/docker/cli" | ||
"github.com/docker/engine-api/types/swarm" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
func newAcceptCommand(dockerCli *client.DockerCli) *cobra.Command { | ||
var flags *pflag.FlagSet | ||
|
||
cmd := &cobra.Command{ | ||
Use: "accept NODE [NODE...]", | ||
Short: "Accept a node in the swarm", | ||
Args: cli.RequiresMinArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runAccept(dockerCli, flags, args) | ||
}, | ||
} | ||
|
||
flags = cmd.Flags() | ||
return cmd | ||
} | ||
|
||
func runAccept(dockerCli *client.DockerCli, flags *pflag.FlagSet, args []string) error { | ||
for _, id := range args { | ||
if err := runUpdate(dockerCli, id, func(node *swarm.Node) { | ||
node.Spec.Membership = swarm.NodeMembershipAccepted | ||
}); err != nil { | ||
return err | ||
} | ||
fmt.Println(id, "attempting to accept a node in the swarm.") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package node | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/docker/docker/api/client" | ||
"github.com/docker/docker/cli" | ||
apiclient "github.com/docker/engine-api/client" | ||
) | ||
|
||
// NewNodeCommand returns a cobra command for `node` subcommands | ||
func NewNodeCommand(dockerCli *client.DockerCli) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "node", | ||
Short: "Manage docker swarm nodes", | ||
Args: cli.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString()) | ||
}, | ||
} | ||
cmd.AddCommand( | ||
newAcceptCommand(dockerCli), | ||
newDemoteCommand(dockerCli), | ||
newInspectCommand(dockerCli), | ||
newListCommand(dockerCli), | ||
newPromoteCommand(dockerCli), | ||
newRemoveCommand(dockerCli), | ||
newTasksCommand(dockerCli), | ||
newUpdateCommand(dockerCli), | ||
) | ||
return cmd | ||
} | ||
|
||
func nodeReference(client apiclient.APIClient, ctx context.Context, ref string) (string, error) { | ||
// The special value "self" for a node reference is mapped to the current | ||
// node, hence the node ID is retrieved using the `/info` endpoint. | ||
if ref == "self" { | ||
info, err := client.Info(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
return info.Swarm.NodeID, nil | ||
} | ||
return ref, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package node | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/docker/docker/api/client" | ||
"github.com/docker/docker/cli" | ||
"github.com/docker/engine-api/types/swarm" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
func newDemoteCommand(dockerCli *client.DockerCli) *cobra.Command { | ||
var flags *pflag.FlagSet | ||
|
||
cmd := &cobra.Command{ | ||
Use: "demote NODE [NODE...]", | ||
Short: "Demote a node from manager in the swarm", | ||
Args: cli.RequiresMinArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runDemote(dockerCli, flags, args) | ||
}, | ||
} | ||
|
||
flags = cmd.Flags() | ||
return cmd | ||
} | ||
|
||
func runDemote(dockerCli *client.DockerCli, flags *pflag.FlagSet, args []string) error { | ||
for _, id := range args { | ||
if err := runUpdate(dockerCli, id, func(node *swarm.Node) { | ||
node.Spec.Role = swarm.NodeRoleWorker | ||
}); err != nil { | ||
return err | ||
} | ||
fmt.Println(id, "attempting to demote a manager in the swarm.") | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.