Skip to content

Commit 5400a48

Browse files
cpuguy83thaJeztah
authored andcommitted
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>
1 parent 9eb632d commit 5400a48

146 files changed

Lines changed: 409 additions & 470 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cli/command/builder/prune.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
3030
Short: "Remove build cache",
3131
Args: cli.NoArgs,
3232
RunE: func(cmd *cobra.Command, args []string) error {
33-
spaceReclaimed, output, err := runPrune(dockerCli, options)
33+
spaceReclaimed, output, err := runPrune(cmd.Context(), dockerCli, options)
3434
if err != nil {
3535
return err
3636
}
@@ -58,7 +58,7 @@ const (
5858
allCacheWarning = `WARNING! This will remove all build cache. Are you sure you want to continue?`
5959
)
6060

61-
func runPrune(dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
61+
func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
6262
pruneFilters := options.filter.Value()
6363
pruneFilters = command.PruneFilters(dockerCli, pruneFilters)
6464

@@ -70,7 +70,7 @@ func runPrune(dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint6
7070
return 0, "", nil
7171
}
7272

73-
report, err := dockerCli.Client().BuildCachePrune(context.Background(), types.BuildCachePruneOptions{
73+
report, err := dockerCli.Client().BuildCachePrune(ctx, types.BuildCachePruneOptions{
7474
All: options.all,
7575
KeepStorage: options.keepStorage.Value(),
7676
Filters: pruneFilters,
@@ -93,6 +93,6 @@ func runPrune(dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint6
9393
}
9494

9595
// CachePrune executes a prune command for build cache
96-
func CachePrune(dockerCli command.Cli, all bool, filter opts.FilterOpt) (uint64, string, error) {
97-
return runPrune(dockerCli, pruneOptions{force: true, all: all, filter: filter})
96+
func CachePrune(ctx context.Context, dockerCli command.Cli, all bool, filter opts.FilterOpt) (uint64, string, error) {
97+
return runPrune(ctx, dockerCli, pruneOptions{force: true, all: all, filter: filter})
9898
}

cli/command/checkpoint/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
2828
RunE: func(cmd *cobra.Command, args []string) error {
2929
opts.container = args[0]
3030
opts.checkpoint = args[1]
31-
return runCreate(dockerCli, opts)
31+
return runCreate(cmd.Context(), dockerCli, opts)
3232
},
3333
ValidArgsFunction: completion.NoComplete,
3434
}
@@ -40,8 +40,8 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
4040
return cmd
4141
}
4242

43-
func runCreate(dockerCli command.Cli, opts createOptions) error {
44-
err := dockerCli.Client().CheckpointCreate(context.Background(), opts.container, checkpoint.CreateOptions{
43+
func runCreate(ctx context.Context, dockerCli command.Cli, opts createOptions) error {
44+
err := dockerCli.Client().CheckpointCreate(ctx, opts.container, checkpoint.CreateOptions{
4545
CheckpointID: opts.checkpoint,
4646
CheckpointDir: opts.checkpointDir,
4747
Exit: !opts.leaveRunning,

cli/command/checkpoint/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
2424
Short: "List checkpoints for a container",
2525
Args: cli.ExactArgs(1),
2626
RunE: func(cmd *cobra.Command, args []string) error {
27-
return runList(dockerCli, args[0], opts)
27+
return runList(cmd.Context(), dockerCli, args[0], opts)
2828
},
2929
ValidArgsFunction: completion.ContainerNames(dockerCli, false),
3030
}
@@ -35,8 +35,8 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
3535
return cmd
3636
}
3737

38-
func runList(dockerCli command.Cli, container string, opts listOptions) error {
39-
checkpoints, err := dockerCli.Client().CheckpointList(context.Background(), container, checkpoint.ListOptions{
38+
func runList(ctx context.Context, dockerCli command.Cli, container string, opts listOptions) error {
39+
checkpoints, err := dockerCli.Client().CheckpointList(ctx, container, checkpoint.ListOptions{
4040
CheckpointDir: opts.checkpointDir,
4141
})
4242
if err != nil {

cli/command/checkpoint/remove.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
2222
Short: "Remove a checkpoint",
2323
Args: cli.ExactArgs(2),
2424
RunE: func(cmd *cobra.Command, args []string) error {
25-
return runRemove(dockerCli, args[0], args[1], opts)
25+
return runRemove(cmd.Context(), dockerCli, args[0], args[1], opts)
2626
},
2727
}
2828

@@ -32,8 +32,8 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
3232
return cmd
3333
}
3434

35-
func runRemove(dockerCli command.Cli, container string, checkpointID string, opts removeOptions) error {
36-
return dockerCli.Client().CheckpointDelete(context.Background(), container, checkpoint.DeleteOptions{
35+
func runRemove(ctx context.Context, dockerCli command.Cli, container string, checkpointID string, opts removeOptions) error {
36+
return dockerCli.Client().CheckpointDelete(ctx, container, checkpoint.DeleteOptions{
3737
CheckpointID: checkpointID,
3838
CheckpointDir: opts.checkpointDir,
3939
})

cli/command/cli.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ type DockerCli struct {
8282
dockerEndpoint docker.Endpoint
8383
contextStoreConfig store.Config
8484
initTimeout time.Duration
85+
86+
// baseCtx is the base context used for internal operations. In the future
87+
// this may be replaced by explicitly passing a context to functions that
88+
// need it.
89+
baseCtx context.Context
8590
}
8691

8792
// DefaultVersion returns api.defaultVersion.
@@ -320,8 +325,7 @@ func (cli *DockerCli) getInitTimeout() time.Duration {
320325
}
321326

322327
func (cli *DockerCli) initializeFromClient() {
323-
ctx := context.Background()
324-
ctx, cancel := context.WithTimeout(ctx, cli.getInitTimeout())
328+
ctx, cancel := context.WithTimeout(cli.baseCtx, cli.getInitTimeout())
325329
defer cancel()
326330

327331
ping, err := cli.client.Ping(ctx)
@@ -441,6 +445,9 @@ func (cli *DockerCli) initialize() error {
441445
return
442446
}
443447
}
448+
if cli.baseCtx == nil {
449+
cli.baseCtx = context.Background()
450+
}
444451
cli.initializeFromClient()
445452
})
446453
return cli.initErr
@@ -484,7 +491,7 @@ func NewDockerCli(ops ...CLIOption) (*DockerCli, error) {
484491
}
485492
ops = append(defaultOps, ops...)
486493

487-
cli := &DockerCli{}
494+
cli := &DockerCli{baseCtx: context.Background()}
488495
if err := cli.Apply(ops...); err != nil {
489496
return nil, err
490497
}

cli/command/cli_options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package command
22

33
import (
4+
"context"
45
"io"
56
"os"
67
"strconv"
@@ -35,6 +36,15 @@ func WithStandardStreams() CLIOption {
3536
}
3637
}
3738

39+
// WithBaseContext sets the base context of a cli. It is used to propagate
40+
// the context from the command line to the client.
41+
func WithBaseContext(ctx context.Context) CLIOption {
42+
return func(cli *DockerCli) error {
43+
cli.baseCtx = ctx
44+
return nil
45+
}
46+
}
47+
3848
// WithCombinedStreams uses the same stream for the output and error streams.
3949
func WithCombinedStreams(combined io.Writer) CLIOption {
4050
return func(cli *DockerCli) error {

cli/command/config/create.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
3535
RunE: func(cmd *cobra.Command, args []string) error {
3636
createOpts.Name = args[0]
3737
createOpts.File = args[1]
38-
return RunConfigCreate(dockerCli, createOpts)
38+
return RunConfigCreate(cmd.Context(), dockerCli, createOpts)
3939
},
4040
ValidArgsFunction: completion.NoComplete,
4141
}
@@ -48,9 +48,8 @@ func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
4848
}
4949

5050
// RunConfigCreate creates a config with the given options.
51-
func RunConfigCreate(dockerCli command.Cli, options CreateOptions) error {
51+
func RunConfigCreate(ctx context.Context, dockerCli command.Cli, options CreateOptions) error {
5252
client := dockerCli.Client()
53-
ctx := context.Background()
5453

5554
var in io.Reader = dockerCli.In()
5655
if options.File != "-" {

cli/command/config/inspect.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command {
2727
Args: cli.RequiresMinArgs(1),
2828
RunE: func(cmd *cobra.Command, args []string) error {
2929
opts.Names = args
30-
return RunConfigInspect(dockerCli, opts)
30+
return RunConfigInspect(cmd.Context(), dockerCli, opts)
3131
},
3232
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
3333
return completeNames(dockerCli)(cmd, args, toComplete)
@@ -40,9 +40,8 @@ func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command {
4040
}
4141

4242
// RunConfigInspect inspects the given Swarm config.
43-
func RunConfigInspect(dockerCli command.Cli, opts InspectOptions) error {
43+
func RunConfigInspect(ctx context.Context, dockerCli command.Cli, opts InspectOptions) error {
4444
client := dockerCli.Client()
45-
ctx := context.Background()
4645

4746
if opts.Pretty {
4847
opts.Format = "pretty"

cli/command/config/ls.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func newConfigListCommand(dockerCli command.Cli) *cobra.Command {
3131
Short: "List configs",
3232
Args: cli.NoArgs,
3333
RunE: func(cmd *cobra.Command, args []string) error {
34-
return RunConfigList(dockerCli, listOpts)
34+
return RunConfigList(cmd.Context(), dockerCli, listOpts)
3535
},
3636
ValidArgsFunction: completion.NoComplete,
3737
}
@@ -45,9 +45,8 @@ func newConfigListCommand(dockerCli command.Cli) *cobra.Command {
4545
}
4646

4747
// RunConfigList lists Swarm configs.
48-
func RunConfigList(dockerCli command.Cli, options ListOptions) error {
48+
func RunConfigList(ctx context.Context, dockerCli command.Cli, options ListOptions) error {
4949
client := dockerCli.Client()
50-
ctx := context.Background()
5150

5251
configs, err := client.ConfigList(ctx, types.ConfigListOptions{Filters: options.Filter.Value()})
5352
if err != nil {

cli/command/config/remove.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func newConfigRemoveCommand(dockerCli command.Cli) *cobra.Command {
2626
opts := RemoveOptions{
2727
Names: args,
2828
}
29-
return RunConfigRemove(dockerCli, opts)
29+
return RunConfigRemove(cmd.Context(), dockerCli, opts)
3030
},
3131
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
3232
return completeNames(dockerCli)(cmd, args, toComplete)
@@ -35,9 +35,8 @@ func newConfigRemoveCommand(dockerCli command.Cli) *cobra.Command {
3535
}
3636

3737
// RunConfigRemove removes the given Swarm configs.
38-
func RunConfigRemove(dockerCli command.Cli, opts RemoveOptions) error {
38+
func RunConfigRemove(ctx context.Context, dockerCli command.Cli, opts RemoveOptions) error {
3939
client := dockerCli.Client()
40-
ctx := context.Background()
4140

4241
var errs []string
4342

0 commit comments

Comments
 (0)