Skip to content

Commit 84ecac1

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 4dd5c23 commit 84ecac1

File tree

146 files changed

+414
-470
lines changed

Some content is hidden

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

146 files changed

+414
-470
lines changed

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: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ 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
87+
// In the future this may be replaced by explicitly passing a context to functions that need it.
88+
baseCtx context.Context
89+
}
90+
91+
// WithContext sets the base context for the cli
92+
// This is used internally for operations may require a context to propagate tracing.
93+
func (cli *DockerCli) WithContext(ctx context.Context) {
94+
cli.baseCtx = ctx
8595
}
8696

8797
// DefaultVersion returns api.defaultVersion.
@@ -323,8 +333,7 @@ func (cli *DockerCli) getInitTimeout() time.Duration {
323333
}
324334

325335
func (cli *DockerCli) initializeFromClient() {
326-
ctx := context.Background()
327-
ctx, cancel := context.WithTimeout(ctx, cli.getInitTimeout())
336+
ctx, cancel := context.WithTimeout(cli.baseCtx, cli.getInitTimeout())
328337
defer cancel()
329338

330339
ping, err := cli.client.Ping(ctx)
@@ -444,6 +453,9 @@ func (cli *DockerCli) initialize() error {
444453
return
445454
}
446455
}
456+
if cli.baseCtx == nil {
457+
cli.baseCtx = context.Background()
458+
}
447459
cli.initializeFromClient()
448460
})
449461
return cli.initErr
@@ -487,7 +499,7 @@ func NewDockerCli(ops ...DockerCliOption) (*DockerCli, error) {
487499
}
488500
ops = append(defaultOps, ops...)
489501

490-
cli := &DockerCli{}
502+
cli := &DockerCli{baseCtx: context.Background()}
491503
if err := cli.Apply(ops...); err != nil {
492504
return nil, err
493505
}

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"
@@ -25,6 +26,15 @@ func WithStandardStreams() DockerCliOption {
2526
}
2627
}
2728

29+
// WithBaseContext sets the base context of a cli.
30+
// This is used to propagate the context from the command line to the client.
31+
func WithBaseContext(ctx context.Context) DockerCliOption {
32+
return func(cli *DockerCli) error {
33+
cli.baseCtx = ctx
34+
return nil
35+
}
36+
}
37+
2838
// WithCombinedStreams uses the same stream for the output and error streams.
2939
func WithCombinedStreams(combined io.Writer) DockerCliOption {
3040
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)