Skip to content

Commit def549c

Browse files
ndeloofvvoland
andcommitted
imageservice: Add context to various methods
Co-authored-by: Paweł Gronowski <[email protected]> Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 4c07d58 commit def549c

40 files changed

Lines changed: 303 additions & 256 deletions

api/server/router/container/backend.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ type copyBackend interface {
3232

3333
// stateBackend includes functions to implement to provide container state lifecycle functionality.
3434
type stateBackend interface {
35-
ContainerCreate(config types.ContainerCreateConfig) (container.CreateResponse, error)
35+
ContainerCreate(ctx context.Context, config types.ContainerCreateConfig) (container.CreateResponse, error)
3636
ContainerKill(name string, signal string) error
3737
ContainerPause(name string) error
3838
ContainerRename(oldName, newName string) error
3939
ContainerResize(name string, height, width int) error
4040
ContainerRestart(ctx context.Context, name string, options container.StopOptions) error
4141
ContainerRm(name string, config *types.ContainerRmConfig) error
42-
ContainerStart(name string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
42+
ContainerStart(ctx context.Context, name string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
4343
ContainerStop(ctx context.Context, name string, options container.StopOptions) error
4444
ContainerUnpause(name string) error
4545
ContainerUpdate(name string, hostConfig *container.HostConfig) (container.ContainerUpdateOKBody, error)
@@ -54,7 +54,7 @@ type monitorBackend interface {
5454
ContainerStats(ctx context.Context, name string, config *backend.ContainerStatsConfig) error
5555
ContainerTop(name string, psArgs string) (*container.ContainerTopOKBody, error)
5656

57-
Containers(config *types.ContainerListOptions) ([]*types.Container, error)
57+
Containers(ctx context.Context, config *types.ContainerListOptions) ([]*types.Container, error)
5858
}
5959

6060
// attachBackend includes function to implement to provide container attaching functionality.
@@ -68,7 +68,7 @@ type systemBackend interface {
6868
}
6969

7070
type commitBackend interface {
71-
CreateImageFromContainer(name string, config *backend.CreateImageConfig) (imageID string, err error)
71+
CreateImageFromContainer(ctx context.Context, name string, config *backend.CreateImageConfig) (imageID string, err error)
7272
}
7373

7474
// Backend is all the methods that need to be implemented to provide container specific functionality.

api/server/router/container/container_routes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (s *containerRouter) postCommit(ctx context.Context, w http.ResponseWriter,
5858
Changes: r.Form["changes"],
5959
}
6060

61-
imgID, err := s.backend.CreateImageFromContainer(r.Form.Get("container"), commitCfg)
61+
imgID, err := s.backend.CreateImageFromContainer(ctx, r.Form.Get("container"), commitCfg)
6262
if err != nil {
6363
return err
6464
}
@@ -91,7 +91,7 @@ func (s *containerRouter) getContainersJSON(ctx context.Context, w http.Response
9191
config.Limit = limit
9292
}
9393

94-
containers, err := s.backend.Containers(config)
94+
containers, err := s.backend.Containers(ctx, config)
9595
if err != nil {
9696
return err
9797
}
@@ -214,7 +214,7 @@ func (s *containerRouter) postContainersStart(ctx context.Context, w http.Respon
214214

215215
checkpoint := r.Form.Get("checkpoint")
216216
checkpointDir := r.Form.Get("checkpoint-dir")
217-
if err := s.backend.ContainerStart(vars["name"], hostConfig, checkpoint, checkpointDir); err != nil {
217+
if err := s.backend.ContainerStart(ctx, vars["name"], hostConfig, checkpoint, checkpointDir); err != nil {
218218
return err
219219
}
220220

@@ -578,7 +578,7 @@ func (s *containerRouter) postContainersCreate(ctx context.Context, w http.Respo
578578
hostConfig.PidsLimit = nil
579579
}
580580

581-
ccr, err := s.backend.ContainerCreate(types.ContainerCreateConfig{
581+
ccr, err := s.backend.ContainerCreate(ctx, types.ContainerCreateConfig{
582582
Name: name,
583583
Config: config,
584584
HostConfig: hostConfig,

api/server/router/image/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type imageBackend interface {
3131

3232
type importExportBackend interface {
3333
LoadImage(ctx context.Context, inTar io.ReadCloser, outStream io.Writer, quiet bool) error
34-
ImportImage(src string, repository string, platform *specs.Platform, tag string, msg string, inConfig io.ReadCloser, outStream io.Writer, changes []string) error
34+
ImportImage(ctx context.Context, src string, repository string, platform *specs.Platform, tag string, msg string, inConfig io.ReadCloser, outStream io.Writer, changes []string) error
3535
ExportImage(ctx context.Context, names []string, outStream io.Writer) error
3636
}
3737

api/server/router/image/image_routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (ir *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWrit
6868
progressErr = ir.backend.PullImage(ctx, img, tag, platform, metaHeaders, authConfig, output)
6969
} else { // import
7070
src := r.Form.Get("fromSrc")
71-
progressErr = ir.backend.ImportImage(src, repo, platform, tag, message, r.Body, output, r.Form["changes"])
71+
progressErr = ir.backend.ImportImage(ctx, src, repo, platform, tag, message, r.Body, output, r.Form["changes"])
7272
}
7373
if progressErr != nil {
7474
if !output.Flushed() {

api/server/router/swarm/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
type Backend interface {
1313
Init(req types.InitRequest) (string, error)
1414
Join(req types.JoinRequest) error
15-
Leave(force bool) error
15+
Leave(ctx context.Context, force bool) error
1616
Inspect() (types.Swarm, error)
1717
Update(uint64, types.Spec, types.UpdateFlags) error
1818
GetUnlockKey() (string, error)

api/server/router/swarm/cluster_routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (sr *swarmRouter) leaveCluster(ctx context.Context, w http.ResponseWriter,
5656
}
5757

5858
force := httputils.BoolValue(r, "force")
59-
return sr.backend.Leave(force)
59+
return sr.backend.Leave(ctx, force)
6060
}
6161

6262
func (sr *swarmRouter) inspectCluster(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

builder/builder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ type ExecBackend interface {
6060
// ContainerAttachRaw attaches to container.
6161
ContainerAttachRaw(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool, attached chan struct{}) error
6262
// ContainerCreateIgnoreImagesArgsEscaped creates a new Docker container and returns potential warnings
63-
ContainerCreateIgnoreImagesArgsEscaped(config types.ContainerCreateConfig) (container.CreateResponse, error)
63+
ContainerCreateIgnoreImagesArgsEscaped(ctx context.Context, config types.ContainerCreateConfig) (container.CreateResponse, error)
6464
// ContainerRm removes a container specified by `id`.
6565
ContainerRm(name string, config *types.ContainerRmConfig) error
6666
// ContainerKill stops the container execution abruptly.
6767
ContainerKill(containerID string, sig string) error
6868
// ContainerStart starts a new container
69-
ContainerStart(containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
69+
ContainerStart(ctx context.Context, containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
7070
// ContainerWait stops processing until the given container is stopped.
7171
ContainerWait(ctx context.Context, name string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error)
7272
}
@@ -80,7 +80,7 @@ type Result struct {
8080
// ImageCacheBuilder represents a generator for stateful image cache.
8181
type ImageCacheBuilder interface {
8282
// MakeImageCache creates a stateful image cache.
83-
MakeImageCache(cacheFrom []string) ImageCache
83+
MakeImageCache(ctx context.Context, cacheFrom []string) (ImageCache, error)
8484
}
8585

8686
// ImageCache abstracts an image cache.

builder/dockerfile/builder.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (bm *BuildManager) Build(ctx context.Context, config backend.BuildConfig) (
9595
if err != nil {
9696
return nil, err
9797
}
98-
return b.build(source, dockerfile)
98+
return b.build(ctx, source, dockerfile)
9999
}
100100

101101
// builderOptions are the dependencies required by the builder
@@ -136,6 +136,11 @@ func newBuilder(clientCtx context.Context, options builderOptions) (*Builder, er
136136
config = new(types.ImageBuildOptions)
137137
}
138138

139+
imageProber, err := newImageProber(clientCtx, options.Backend, config.CacheFrom, config.NoCache)
140+
if err != nil {
141+
return nil, err
142+
}
143+
139144
b := &Builder{
140145
clientCtx: clientCtx,
141146
options: config,
@@ -147,7 +152,7 @@ func newBuilder(clientCtx context.Context, options builderOptions) (*Builder, er
147152
idMapping: options.IDMapping,
148153
imageSources: newImageSources(clientCtx, options),
149154
pathCache: options.PathCache,
150-
imageProber: newImageProber(options.Backend, config.CacheFrom, config.NoCache),
155+
imageProber: imageProber,
151156
containerManager: newContainerManager(options.Backend),
152157
}
153158

@@ -181,7 +186,7 @@ func buildLabelOptions(labels map[string]string, stages []instructions.Stage) {
181186

182187
// Build runs the Dockerfile builder by parsing the Dockerfile and executing
183188
// the instructions from the file.
184-
func (b *Builder) build(source builder.Source, dockerfile *parser.Result) (*builder.Result, error) {
189+
func (b *Builder) build(ctx context.Context, source builder.Source, dockerfile *parser.Result) (*builder.Result, error) {
185190
defer b.imageSources.Unmount()
186191

187192
stages, metaArgs, err := instructions.Parse(dockerfile.AST)
@@ -205,7 +210,7 @@ func (b *Builder) build(source builder.Source, dockerfile *parser.Result) (*buil
205210
buildLabelOptions(b.options.Labels, stages)
206211

207212
dockerfile.PrintWarnings(b.Stderr)
208-
dispatchState, err := b.dispatchDockerfileWithCancellation(stages, metaArgs, dockerfile.EscapeToken, source)
213+
dispatchState, err := b.dispatchDockerfileWithCancellation(ctx, stages, metaArgs, dockerfile.EscapeToken, source)
209214
if err != nil {
210215
return nil, err
211216
}
@@ -244,7 +249,7 @@ func printCommand(out io.Writer, currentCommandIndex int, totalCommands int, cmd
244249
return currentCommandIndex + 1
245250
}
246251

247-
func (b *Builder) dispatchDockerfileWithCancellation(parseResult []instructions.Stage, metaArgs []instructions.ArgCommand, escapeToken rune, source builder.Source) (*dispatchState, error) {
252+
func (b *Builder) dispatchDockerfileWithCancellation(ctx context.Context, parseResult []instructions.Stage, metaArgs []instructions.ArgCommand, escapeToken rune, source builder.Source) (*dispatchState, error) {
248253
dispatchRequest := dispatchRequest{}
249254
buildArgs := NewBuildArgs(b.options.BuildArgs)
250255
totalCommands := len(metaArgs) + len(parseResult)
@@ -272,7 +277,7 @@ func (b *Builder) dispatchDockerfileWithCancellation(parseResult []instructions.
272277
dispatchRequest = newDispatchRequest(b, escapeToken, source, buildArgs, stagesResults)
273278

274279
currentCommandIndex = printCommand(b.Stdout, currentCommandIndex, totalCommands, stage.SourceCode)
275-
if err := initializeStage(dispatchRequest, &stage); err != nil {
280+
if err := initializeStage(ctx, dispatchRequest, &stage); err != nil {
276281
return nil, err
277282
}
278283
dispatchRequest.state.updateRunConfig()
@@ -290,7 +295,7 @@ func (b *Builder) dispatchDockerfileWithCancellation(parseResult []instructions.
290295

291296
currentCommandIndex = printCommand(b.Stdout, currentCommandIndex, totalCommands, cmd)
292297

293-
if err := dispatch(dispatchRequest, cmd); err != nil {
298+
if err := dispatch(ctx, dispatchRequest, cmd); err != nil {
294299
return nil, err
295300
}
296301
dispatchRequest.state.updateRunConfig()
@@ -317,7 +322,7 @@ func (b *Builder) dispatchDockerfileWithCancellation(parseResult []instructions.
317322
// coming from the query parameter of the same name.
318323
//
319324
// TODO: Remove?
320-
func BuildFromConfig(config *container.Config, changes []string, os string) (*container.Config, error) {
325+
func BuildFromConfig(ctx context.Context, config *container.Config, changes []string, os string) (*container.Config, error) {
321326
if len(changes) == 0 {
322327
return config, nil
323328
}
@@ -327,7 +332,7 @@ func BuildFromConfig(config *container.Config, changes []string, os string) (*co
327332
return nil, errdefs.InvalidParameter(err)
328333
}
329334

330-
b, err := newBuilder(context.Background(), builderOptions{
335+
b, err := newBuilder(ctx, builderOptions{
331336
Options: &types.ImageBuildOptions{NoCache: true},
332337
})
333338
if err != nil {
@@ -360,7 +365,7 @@ func BuildFromConfig(config *container.Config, changes []string, os string) (*co
360365
dispatchRequest.state.imageID = config.Image
361366
dispatchRequest.state.operatingSystem = os
362367
for _, cmd := range commands {
363-
err := dispatch(dispatchRequest, cmd)
368+
err := dispatch(ctx, dispatchRequest, cmd)
364369
if err != nil {
365370
return nil, errdefs.InvalidParameter(err)
366371
}

builder/dockerfile/containerbackend.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func newContainerManager(docker builder.ExecBackend) *containerManager {
2828
}
2929

3030
// Create a container
31-
func (c *containerManager) Create(runConfig *container.Config, hostConfig *container.HostConfig) (container.CreateResponse, error) {
32-
container, err := c.backend.ContainerCreateIgnoreImagesArgsEscaped(types.ContainerCreateConfig{
31+
func (c *containerManager) Create(ctx context.Context, runConfig *container.Config, hostConfig *container.HostConfig) (container.CreateResponse, error) {
32+
container, err := c.backend.ContainerCreateIgnoreImagesArgsEscaped(ctx, types.ContainerCreateConfig{
3333
Config: runConfig,
3434
HostConfig: hostConfig,
3535
})
@@ -69,7 +69,7 @@ func (c *containerManager) Run(ctx context.Context, cID string, stdout, stderr i
6969
}
7070
}()
7171

72-
if err := c.backend.ContainerStart(cID, nil, "", ""); err != nil {
72+
if err := c.backend.ContainerStart(ctx, cID, nil, "", ""); err != nil {
7373
close(finished)
7474
logCancellationError(cancelErrCh, "error from ContainerStart: "+err.Error())
7575
return err

0 commit comments

Comments
 (0)