mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
builder: Don't store context in struct
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
parent
a181a825c8
commit
66a0289081
7 changed files with 27 additions and 27 deletions
|
@ -117,8 +117,7 @@ type Builder struct {
|
|||
Aux *streamformatter.AuxFormatter
|
||||
Output io.Writer
|
||||
|
||||
docker builder.Backend
|
||||
clientCtx context.Context
|
||||
docker builder.Backend
|
||||
|
||||
idMapping idtools.IdentityMapping
|
||||
disableCommit bool
|
||||
|
@ -130,19 +129,18 @@ type Builder struct {
|
|||
}
|
||||
|
||||
// newBuilder creates a new Dockerfile builder from an optional dockerfile and a Options.
|
||||
func newBuilder(clientCtx context.Context, options builderOptions) (*Builder, error) {
|
||||
func newBuilder(ctx context.Context, options builderOptions) (*Builder, error) {
|
||||
config := options.Options
|
||||
if config == nil {
|
||||
config = new(types.ImageBuildOptions)
|
||||
}
|
||||
|
||||
imageProber, err := newImageProber(clientCtx, options.Backend, config.CacheFrom, config.NoCache)
|
||||
imageProber, err := newImageProber(ctx, options.Backend, config.CacheFrom, config.NoCache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b := &Builder{
|
||||
clientCtx: clientCtx,
|
||||
options: config,
|
||||
Stdout: options.ProgressWriter.StdoutFormatter,
|
||||
Stderr: options.ProgressWriter.StderrFormatter,
|
||||
|
@ -150,7 +148,7 @@ func newBuilder(clientCtx context.Context, options builderOptions) (*Builder, er
|
|||
Output: options.ProgressWriter.Output,
|
||||
docker: options.Backend,
|
||||
idMapping: options.IDMapping,
|
||||
imageSources: newImageSources(clientCtx, options),
|
||||
imageSources: newImageSources(options),
|
||||
pathCache: options.PathCache,
|
||||
imageProber: imageProber,
|
||||
containerManager: newContainerManager(options.Backend),
|
||||
|
@ -284,7 +282,7 @@ func (b *Builder) dispatchDockerfileWithCancellation(ctx context.Context, parseR
|
|||
fmt.Fprintf(b.Stdout, " ---> %s\n", stringid.TruncateID(dispatchRequest.state.imageID))
|
||||
for _, cmd := range stage.Commands {
|
||||
select {
|
||||
case <-b.clientCtx.Done():
|
||||
case <-ctx.Done():
|
||||
logrus.Debug("Builder: build cancelled!")
|
||||
fmt.Fprint(b.Stdout, "Build cancelled\n")
|
||||
buildsFailed.WithValues(metricsBuildCanceled).Inc()
|
||||
|
|
|
@ -116,7 +116,7 @@ func dispatchCopy(ctx context.Context, d dispatchRequest, c *instructions.CopyCo
|
|||
var im *imageMount
|
||||
var err error
|
||||
if c.From != "" {
|
||||
im, err = d.getImageMount(c.From)
|
||||
im, err = d.getImageMount(ctx, c.From)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "invalid from flag value %s", c.From)
|
||||
}
|
||||
|
@ -134,7 +134,7 @@ func dispatchCopy(ctx context.Context, d dispatchRequest, c *instructions.CopyCo
|
|||
return d.builder.performCopy(ctx, d, copyInstruction)
|
||||
}
|
||||
|
||||
func (d *dispatchRequest) getImageMount(imageRefOrID string) (*imageMount, error) {
|
||||
func (d *dispatchRequest) getImageMount(ctx context.Context, imageRefOrID string) (*imageMount, error) {
|
||||
if imageRefOrID == "" {
|
||||
// TODO: this could return the source in the default case as well?
|
||||
return nil, nil
|
||||
|
@ -149,7 +149,7 @@ func (d *dispatchRequest) getImageMount(imageRefOrID string) (*imageMount, error
|
|||
imageRefOrID = stage.Image
|
||||
localOnly = true
|
||||
}
|
||||
return d.builder.imageSources.Get(imageRefOrID, localOnly, d.builder.platform)
|
||||
return d.builder.imageSources.Get(ctx, imageRefOrID, localOnly, d.builder.platform)
|
||||
}
|
||||
|
||||
// FROM [--platform=platform] imagename[:tag | @digest] [AS build-stage-name]
|
||||
|
@ -173,7 +173,7 @@ func initializeStage(ctx context.Context, d dispatchRequest, cmd *instructions.S
|
|||
platform = &p
|
||||
}
|
||||
|
||||
image, err := d.getFromImage(d.shlex, cmd.BaseName, platform)
|
||||
image, err := d.getFromImage(ctx, d.shlex, cmd.BaseName, platform)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -233,7 +233,7 @@ func (d *dispatchRequest) getExpandedString(shlex *shell.Lex, str string) (strin
|
|||
return name, nil
|
||||
}
|
||||
|
||||
func (d *dispatchRequest) getImageOrStage(name string, platform *specs.Platform) (builder.Image, error) {
|
||||
func (d *dispatchRequest) getImageOrStage(ctx context.Context, name string, platform *specs.Platform) (builder.Image, error) {
|
||||
var localOnly bool
|
||||
if im, ok := d.stages.getByName(name); ok {
|
||||
name = im.Image
|
||||
|
@ -260,13 +260,14 @@ func (d *dispatchRequest) getImageOrStage(name string, platform *specs.Platform)
|
|||
}
|
||||
return builder.Image(imageImage), nil
|
||||
}
|
||||
imageMount, err := d.builder.imageSources.Get(name, localOnly, platform)
|
||||
imageMount, err := d.builder.imageSources.Get(ctx, name, localOnly, platform)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return imageMount.Image(), nil
|
||||
}
|
||||
func (d *dispatchRequest) getFromImage(shlex *shell.Lex, basename string, platform *specs.Platform) (builder.Image, error) {
|
||||
|
||||
func (d *dispatchRequest) getFromImage(ctx context.Context, shlex *shell.Lex, basename string, platform *specs.Platform) (builder.Image, error) {
|
||||
name, err := d.getExpandedString(shlex, basename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -277,7 +278,7 @@ func (d *dispatchRequest) getFromImage(shlex *shell.Lex, basename string, platfo
|
|||
return nil, errors.Errorf("base name (%s) should not be blank", basename)
|
||||
}
|
||||
|
||||
return d.getImageOrStage(name, platform)
|
||||
return d.getImageOrStage(ctx, name, platform)
|
||||
}
|
||||
|
||||
func dispatchOnbuild(ctx context.Context, d dispatchRequest, c *instructions.OnbuildCommand) error {
|
||||
|
@ -369,7 +370,7 @@ func dispatchRun(ctx context.Context, d dispatchRequest, c *instructions.RunComm
|
|||
return err
|
||||
}
|
||||
|
||||
if err := d.builder.containerManager.Run(d.builder.clientCtx, cID, d.builder.Stdout, d.builder.Stderr); err != nil {
|
||||
if err := d.builder.containerManager.Run(ctx, cID, d.builder.Stdout, d.builder.Stderr); err != nil {
|
||||
if err, ok := err.(*statusCodeError); ok {
|
||||
// TODO: change error type, because jsonmessage.JSONError assumes HTTP
|
||||
msg := fmt.Sprintf(
|
||||
|
|
|
@ -36,9 +36,8 @@ func newBuilderWithMockBackend(t *testing.T) *Builder {
|
|||
options: opts,
|
||||
docker: mockBackend,
|
||||
Stdout: new(bytes.Buffer),
|
||||
clientCtx: ctx,
|
||||
disableCommit: true,
|
||||
imageSources: newImageSources(ctx, builderOptions{
|
||||
imageSources: newImageSources(builderOptions{
|
||||
Options: opts,
|
||||
Backend: mockBackend,
|
||||
}),
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type getAndMountFunc func(string, bool, *specs.Platform) (builder.Image, builder.ROLayer, error)
|
||||
type getAndMountFunc func(context.Context, string, bool, *specs.Platform) (builder.Image, builder.ROLayer, error)
|
||||
|
||||
// imageSources mounts images and provides a cache for mounted images. It tracks
|
||||
// all images so they can be unmounted at the end of the build.
|
||||
|
@ -23,8 +23,8 @@ type imageSources struct {
|
|||
getImage getAndMountFunc
|
||||
}
|
||||
|
||||
func newImageSources(ctx context.Context, options builderOptions) *imageSources {
|
||||
getAndMount := func(idOrRef string, localOnly bool, platform *specs.Platform) (builder.Image, builder.ROLayer, error) {
|
||||
func newImageSources(options builderOptions) *imageSources {
|
||||
getAndMount := func(ctx context.Context, idOrRef string, localOnly bool, platform *specs.Platform) (builder.Image, builder.ROLayer, error) {
|
||||
pullOption := backend.PullOptionNoPull
|
||||
if !localOnly {
|
||||
if options.Options.PullParent {
|
||||
|
@ -47,12 +47,12 @@ func newImageSources(ctx context.Context, options builderOptions) *imageSources
|
|||
}
|
||||
}
|
||||
|
||||
func (m *imageSources) Get(idOrRef string, localOnly bool, platform *specs.Platform) (*imageMount, error) {
|
||||
func (m *imageSources) Get(ctx context.Context, idOrRef string, localOnly bool, platform *specs.Platform) (*imageMount, error) {
|
||||
if im, ok := m.byImageID[idOrRef]; ok {
|
||||
return im, nil
|
||||
}
|
||||
|
||||
image, layer, err := m.getImage(idOrRef, localOnly, platform)
|
||||
image, layer, err := m.getImage(ctx, idOrRef, localOnly, platform)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
@ -16,7 +17,7 @@ func getMockImageSource(getImageImage builder.Image, getImageLayer builder.ROLay
|
|||
return &imageSources{
|
||||
byImageID: make(map[string]*imageMount),
|
||||
mounts: []*imageMount{},
|
||||
getImage: func(name string, localOnly bool, platform *ocispec.Platform) (builder.Image, builder.ROLayer, error) {
|
||||
getImage: func(_ context.Context, name string, localOnly bool, platform *ocispec.Platform) (builder.Image, builder.ROLayer, error) {
|
||||
return getImageImage, getImageLayer, getImageError
|
||||
},
|
||||
}
|
||||
|
@ -100,7 +101,8 @@ func TestAddFromScratchPopulatesPlatformIfNil(t *testing.T) {
|
|||
|
||||
func TestImageSourceGetAddsToMounts(t *testing.T) {
|
||||
is := getMockImageSource(nil, nil, nil)
|
||||
_, err := is.Get("test", false, nil)
|
||||
ctx := context.Background()
|
||||
_, err := is.Get(ctx, "test", false, nil)
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, len(is.mounts), 1)
|
||||
}
|
||||
|
|
|
@ -127,7 +127,7 @@ func (b *Builder) performCopy(ctx context.Context, req dispatchRequest, inst cop
|
|||
return err
|
||||
}
|
||||
|
||||
imageMount, err := b.imageSources.Get(state.imageID, true, req.builder.platform)
|
||||
imageMount, err := b.imageSources.Get(ctx, state.imageID, true, req.builder.platform)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to get destination image %q", state.imageID)
|
||||
}
|
||||
|
|
|
@ -90,7 +90,7 @@ func lookupNTAccount(ctx context.Context, builder *Builder, accountName string,
|
|||
stdout := new(bytes.Buffer)
|
||||
stderr := new(bytes.Buffer)
|
||||
|
||||
if err := builder.containerManager.Run(builder.clientCtx, container.ID, stdout, stderr); err != nil {
|
||||
if err := builder.containerManager.Run(ctx, container.ID, stdout, stderr); err != nil {
|
||||
if err, ok := err.(*statusCodeError); ok {
|
||||
return idtools.Identity{}, &jsonmessage.JSONError{
|
||||
Message: stderr.String(),
|
||||
|
|
Loading…
Reference in a new issue