Plumb contexts through commands

This is to prepare for otel support.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Brian Goff
2023-09-09 22:27:44 +00:00
committed by Sebastiaan van Stijn
parent 9eb632dc7c
commit 5400a48aaf
146 changed files with 409 additions and 470 deletions

View File

@ -1,6 +1,7 @@
package node
import (
"context"
"fmt"
"github.com/docker/cli/cli"
@ -15,12 +16,12 @@ func newDemoteCommand(dockerCli command.Cli) *cobra.Command {
Short: "Demote one or more nodes from manager in the swarm",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runDemote(dockerCli, args)
return runDemote(cmd.Context(), dockerCli, args)
},
}
}
func runDemote(dockerCli command.Cli, nodes []string) error {
func runDemote(ctx context.Context, dockerCli command.Cli, 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)
@ -32,5 +33,5 @@ func runDemote(dockerCli command.Cli, nodes []string) error {
success := func(nodeID string) {
fmt.Fprintf(dockerCli.Out(), "Manager %s demoted in the swarm.\n", nodeID)
}
return updateNodes(dockerCli, nodes, demote, success)
return updateNodes(ctx, dockerCli, nodes, demote, success)
}

View File

@ -27,7 +27,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.nodeIds = args
return runInspect(dockerCli, opts)
return runInspect(cmd.Context(), dockerCli, opts)
},
}
@ -37,9 +37,8 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}
func runInspect(dockerCli command.Cli, opts inspectOptions) error {
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
client := dockerCli.Client()
ctx := context.Background()
if opts.pretty {
opts.format = "pretty"

View File

@ -31,7 +31,7 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
Short: "List nodes in the swarm",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runList(dockerCli, options)
return runList(cmd.Context(), dockerCli, options)
},
ValidArgsFunction: completion.NoComplete,
}
@ -43,9 +43,8 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}
func runList(dockerCli command.Cli, options listOptions) error {
func runList(ctx context.Context, dockerCli command.Cli, options listOptions) error {
client := dockerCli.Client()
ctx := context.Background()
nodes, err := client.NodeList(
ctx,

View File

@ -1,6 +1,7 @@
package node
import (
"context"
"fmt"
"github.com/docker/cli/cli"
@ -15,12 +16,12 @@ func newPromoteCommand(dockerCli command.Cli) *cobra.Command {
Short: "Promote one or more nodes to manager in the swarm",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runPromote(dockerCli, args)
return runPromote(cmd.Context(), dockerCli, args)
},
}
}
func runPromote(dockerCli command.Cli, nodes []string) error {
func runPromote(ctx context.Context, dockerCli command.Cli, 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)
@ -32,5 +33,5 @@ func runPromote(dockerCli command.Cli, nodes []string) error {
success := func(nodeID string) {
fmt.Fprintf(dockerCli.Out(), "Node %s promoted to a manager in the swarm.\n", nodeID)
}
return updateNodes(dockerCli, nodes, promote, success)
return updateNodes(ctx, dockerCli, nodes, promote, success)
}

View File

@ -39,7 +39,7 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
options.nodeIDs = args
}
return runPs(dockerCli, options)
return runPs(cmd.Context(), dockerCli, options)
},
ValidArgsFunction: completion.NoComplete,
}
@ -53,9 +53,8 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}
func runPs(dockerCli command.Cli, options psOptions) error {
func runPs(ctx context.Context, dockerCli command.Cli, options psOptions) error {
client := dockerCli.Client()
ctx := context.Background()
var (
errs []string

View File

@ -25,7 +25,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
Short: "Remove one or more nodes from the swarm",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runRemove(dockerCli, args, opts)
return runRemove(cmd.Context(), dockerCli, args, opts)
},
}
flags := cmd.Flags()
@ -33,9 +33,8 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}
func runRemove(dockerCli command.Cli, args []string, opts removeOptions) error {
func runRemove(ctx context.Context, dockerCli command.Cli, args []string, opts removeOptions) error {
client := dockerCli.Client()
ctx := context.Background()
var errs []string

View File

@ -23,7 +23,7 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
Short: "Update a node",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runUpdate(dockerCli, cmd.Flags(), args[0])
return runUpdate(cmd.Context(), dockerCli, cmd.Flags(), args[0])
},
}
@ -36,16 +36,15 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
return cmd
}
func runUpdate(dockerCli command.Cli, flags *pflag.FlagSet, nodeID string) error {
func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, nodeID string) error {
success := func(_ string) {
fmt.Fprintln(dockerCli.Out(), nodeID)
}
return updateNodes(dockerCli, []string{nodeID}, mergeNodeUpdate(flags), success)
return updateNodes(ctx, dockerCli, []string{nodeID}, mergeNodeUpdate(flags), success)
}
func updateNodes(dockerCli command.Cli, nodes []string, mergeNode func(node *swarm.Node) error, success func(nodeID string)) error {
func updateNodes(ctx context.Context, dockerCli command.Cli, nodes []string, mergeNode func(node *swarm.Node) error, success func(nodeID string)) error {
client := dockerCli.Client()
ctx := context.Background()
for _, nodeID := range nodes {
node, _, err := client.NodeInspectWithRaw(ctx, nodeID)