Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues in BuildImage() #2626

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (c *ContainerRequest) Validate() error {
}

// GetContext retrieve the build context for the request
// Must be closed when no longer needed.
func (c *ContainerRequest) GetContext() (io.Reader, error) {
var includes []string = []string{"."}

Expand Down Expand Up @@ -356,12 +357,6 @@ func (c *ContainerRequest) BuildOptions() (types.ImageBuildOptions, error) {
buildOptions.BuildArgs = c.GetBuildArgs()
buildOptions.Dockerfile = c.GetDockerfile()

buildContext, err := c.GetContext()
if err != nil {
return buildOptions, err
}
buildOptions.Context = buildContext

// Make sure the auth configs from the Dockerfile are set right after the user-defined build options.
authsFromDockerfile := getAuthConfigsFromDockerfile(c)

Expand Down Expand Up @@ -400,6 +395,13 @@ func (c *ContainerRequest) BuildOptions() (types.ImageBuildOptions, error) {
buildOptions.Labels = core.DefaultLabels(core.SessionID())
}

// Do this as late as possible to ensure we don't leak the context on error/panic.
buildContext, err := c.GetContext()
if err != nil {
return buildOptions, err
}
buildOptions.Context = buildContext

return buildOptions, nil
}

Expand Down
38 changes: 22 additions & 16 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,35 +882,36 @@ var _ ContainerProvider = (*DockerProvider)(nil)

// BuildImage will build and image from context and Dockerfile, then return the tag
func (p *DockerProvider) BuildImage(ctx context.Context, img ImageBuildInfo) (string, error) {
buildOptions, err := img.BuildOptions()
if err != nil {
return "", err
}
var buildOptions types.ImageBuildOptions
resp, err := backoff.RetryNotifyWithData(
func() (types.ImageBuildResponse, error) {
var err error
buildOptions, err = img.BuildOptions()
if err != nil {
return types.ImageBuildResponse{}, backoff.Permanent(err)
}
defer tryClose(buildOptions.Context) // release resources in any case

var buildError error
var resp types.ImageBuildResponse
err = backoff.RetryNotify(
func() error {
resp, err = p.client.ImageBuild(ctx, buildOptions.Context, buildOptions)
resp, err := p.client.ImageBuild(ctx, buildOptions.Context, buildOptions)
if err != nil {
buildError = errors.Join(buildError, err)
if isPermanentClientError(err) {
return backoff.Permanent(err)
return types.ImageBuildResponse{}, backoff.Permanent(err)
}
return err
return types.ImageBuildResponse{}, err
}
defer p.Close()

return nil
return resp, nil
},
backoff.WithContext(backoff.NewExponentialBackOff(), ctx),
func(err error, duration time.Duration) {
p.Logger.Printf("Failed to build image: %s, will retry", err)
},
)
if err != nil {
return "", errors.Join(buildError, err)
return "", err
}
defer resp.Body.Close()

if img.ShouldPrintBuildLog() {
termFd, isTerm := term.GetFdInfo(os.Stderr)
Expand All @@ -927,8 +928,6 @@ func (p *DockerProvider) BuildImage(ctx context.Context, img ImageBuildInfo) (st
return "", err
}

_ = resp.Body.Close()

// the first tag is the one we want
return buildOptions.Tags[0], nil
}
Expand Down Expand Up @@ -1664,3 +1663,10 @@ func isPermanentClientError(err error) bool {
}
return false
}

func tryClose(r io.Reader) {
rc, ok := r.(io.Closer)
if ok {
_ = rc.Close()
}
}