builder: simplify error generation, and rephrase error/warning

With this change:

    echo 'FROM busybox' | DOCKER_BUILDKIT=1 docker build -
    ERROR: BuildKit is enabled but the buildx component is missing or broken.
           Install the buildx component to build images with BuildKit:
           https://docs.docker.com/go/buildx/

    echo 'FROM busybox' | docker build -
    DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
                Install the buildx component to build images with BuildKit:
                https://docs.docker.com/go/buildx/

    Sending build context to Docker daemon  2.048kB
    ...

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-01-27 18:42:05 +01:00 committed by CrazyMax
parent 4d8e45782b
commit bce65f0edc
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
3 changed files with 26 additions and 43 deletions

View File

@ -184,12 +184,6 @@ func runBuild(dockerCli command.Cli, options buildOptions) error {
remote string
)
if !options.quiet && dockerCli.ServerInfo().OSType != "windows" {
_, _ = fmt.Fprint(dockerCli.Err(), `DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
`)
}
if options.stream {
_, _ = fmt.Fprint(dockerCli.Err(), `DEPRECATED: The experimental --stream flag has been removed and the build context
will be sent non-streaming. Enable BuildKit instead with DOCKER_BUILDKIT=1

View File

@ -1,7 +1,6 @@
package main
import (
"bytes"
"fmt"
"os"
"strconv"
@ -14,43 +13,28 @@ import (
const (
builderDefaultPlugin = "buildx"
builderDefaultInstallMsg = `To install buildx, see https://docs.docker.com/go/buildx/`
builderErrorMsg = `%s: Required builder component %s is missing or broken.`
buildxMissingWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
Install the buildx component to build images with BuildKit:
https://docs.docker.com/go/buildx/
`
buildxMissingError = `ERROR: BuildKit is enabled but the buildx component is missing or broken.
Install the buildx component to build images with BuildKit:
https://docs.docker.com/go/buildx/
`
)
type builderError struct {
warn bool
builder string
err error
}
func newBuilderError(warn bool, builder string, err error) error {
return &builderError{
warn: warn,
builder: builder,
err: err,
}
}
func (e *builderError) Error() string {
var errorMsg bytes.Buffer
if e.warn {
errorMsg.WriteString(fmt.Sprintf(builderErrorMsg, "WARNING", e.builder))
func newBuilderError(warn bool, err error) error {
var errorMsg string
if warn {
errorMsg = buildxMissingWarning
} else {
errorMsg.WriteString(fmt.Sprintf(builderErrorMsg, "ERROR", e.builder))
errorMsg = buildxMissingError
}
if e.builder == builderDefaultPlugin {
errorMsg.WriteString(" ")
errorMsg.WriteString(builderDefaultInstallMsg)
if pluginmanager.IsNotFound(err) {
return errors.New(errorMsg)
}
if pluginmanager.IsNotFound(e.err) {
return errors.New(errorMsg.String()).Error()
}
return errors.Errorf("%v\n\n%s", e.err, errorMsg.String()).Error()
}
func (e *builderError) Unwrap() error {
return e.err
return fmt.Errorf("%w\n\n%s", err, errorMsg)
}
func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []string) ([]string, []string, error) {
@ -98,10 +82,10 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
if perr != nil {
// if builder enforced with DOCKER_BUILDKIT=1, cmd fails if plugin missing or broken
if enforcedBuilder {
return fwargs, fwosargs, newBuilderError(false, builderAlias, perr)
return fwargs, fwosargs, newBuilderError(false, perr)
}
// otherwise, display warning and continue
_, _ = fmt.Fprintln(dockerCli.Err(), newBuilderError(true, builderAlias, perr).Error())
_, _ = fmt.Fprintln(dockerCli.Err(), newBuilderError(true, perr))
return args, osargs, nil
}

View File

@ -37,7 +37,12 @@ func TestBuildFromContextDirectoryWithTag(t *testing.T) {
withWorkingDir(dir))
defer icmd.RunCommand("docker", "image", "rm", "myimage")
result.Assert(t, icmd.Expected{Err: "DEPRECATED: The legacy builder is deprecated and will be removed in a future release."})
const buildxMissingWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
Install the buildx component to build images with BuildKit:
https://docs.docker.com/go/buildx/
`
result.Assert(t, icmd.Expected{Err: buildxMissingWarning})
output.Assert(t, result.Stdout(), map[int]func(string) error{
0: output.Prefix("Sending build context to Docker daemon"),
1: output.Suffix("Step 1/4 : FROM registry:5000/alpine:3.6"),