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 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 { if options.stream {
_, _ = fmt.Fprint(dockerCli.Err(), `DEPRECATED: The experimental --stream flag has been removed and the build context _, _ = 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 will be sent non-streaming. Enable BuildKit instead with DOCKER_BUILDKIT=1

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
@ -14,43 +13,28 @@ import (
const ( const (
builderDefaultPlugin = "buildx" builderDefaultPlugin = "buildx"
builderDefaultInstallMsg = `To install buildx, see https://docs.docker.com/go/buildx/` buildxMissingWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
builderErrorMsg = `%s: Required builder component %s is missing or broken.` 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 { func newBuilderError(warn bool, err error) error {
warn bool var errorMsg string
builder string if warn {
err error errorMsg = buildxMissingWarning
}
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))
} else { } else {
errorMsg.WriteString(fmt.Sprintf(builderErrorMsg, "ERROR", e.builder)) errorMsg = buildxMissingError
} }
if e.builder == builderDefaultPlugin { if pluginmanager.IsNotFound(err) {
errorMsg.WriteString(" ") return errors.New(errorMsg)
errorMsg.WriteString(builderDefaultInstallMsg)
} }
if pluginmanager.IsNotFound(e.err) { return fmt.Errorf("%w\n\n%s", err, errorMsg)
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
} }
func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []string) ([]string, []string, error) { 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 perr != nil {
// if builder enforced with DOCKER_BUILDKIT=1, cmd fails if plugin missing or broken // if builder enforced with DOCKER_BUILDKIT=1, cmd fails if plugin missing or broken
if enforcedBuilder { if enforcedBuilder {
return fwargs, fwosargs, newBuilderError(false, builderAlias, perr) return fwargs, fwosargs, newBuilderError(false, perr)
} }
// otherwise, display warning and continue // 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 return args, osargs, nil
} }

View File

@ -37,7 +37,12 @@ func TestBuildFromContextDirectoryWithTag(t *testing.T) {
withWorkingDir(dir)) withWorkingDir(dir))
defer icmd.RunCommand("docker", "image", "rm", "myimage") 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{ output.Assert(t, result.Stdout(), map[int]func(string) error{
0: output.Prefix("Sending build context to Docker daemon"), 0: output.Prefix("Sending build context to Docker daemon"),
1: output.Suffix("Step 1/4 : FROM registry:5000/alpine:3.6"), 1: output.Suffix("Step 1/4 : FROM registry:5000/alpine:3.6"),