2022-02-03 04:37:55 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
pluginmanager "github.com/docker/cli/cli-plugins/manager"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-01-27 12:42:05 -05:00
|
|
|
builderDefaultPlugin = "buildx"
|
|
|
|
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/
|
|
|
|
`
|
2022-02-03 04:37:55 -05:00
|
|
|
|
2022-01-27 12:42:05 -05:00
|
|
|
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/
|
|
|
|
`
|
|
|
|
)
|
2022-02-03 04:37:55 -05:00
|
|
|
|
2022-01-27 12:42:05 -05:00
|
|
|
func newBuilderError(warn bool, err error) error {
|
|
|
|
var errorMsg string
|
|
|
|
if warn {
|
|
|
|
errorMsg = buildxMissingWarning
|
2022-01-27 13:30:06 -05:00
|
|
|
} else {
|
2022-01-27 12:42:05 -05:00
|
|
|
errorMsg = buildxMissingError
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
2022-01-27 12:42:05 -05:00
|
|
|
if pluginmanager.IsNotFound(err) {
|
|
|
|
return errors.New(errorMsg)
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
2022-02-03 13:11:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w\n\n%s", err, errorMsg)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s", errorMsg)
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
|
2022-01-27 13:30:06 -05:00
|
|
|
func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []string) ([]string, []string, error) {
|
2022-02-03 13:11:05 -05:00
|
|
|
var useLegacy bool
|
|
|
|
var useBuilder bool
|
|
|
|
|
2022-02-03 04:37:55 -05:00
|
|
|
// check DOCKER_BUILDKIT env var is present and
|
2022-02-03 13:11:05 -05:00
|
|
|
// if not assume we want to use the builder component
|
2022-02-03 04:37:55 -05:00
|
|
|
if v, ok := os.LookupEnv("DOCKER_BUILDKIT"); ok {
|
|
|
|
enabled, err := strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
2022-01-27 13:30:06 -05:00
|
|
|
return args, osargs, errors.Wrap(err, "DOCKER_BUILDKIT environment variable expects boolean value")
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
if !enabled {
|
2022-02-03 13:11:05 -05:00
|
|
|
useLegacy = true
|
|
|
|
} else {
|
|
|
|
useBuilder = true
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if a builder alias is defined, use it instead
|
|
|
|
// of the default one
|
|
|
|
builderAlias := builderDefaultPlugin
|
|
|
|
aliasMap := dockerCli.ConfigFile().Aliases
|
|
|
|
if v, ok := aliasMap[keyBuilderAlias]; ok {
|
2022-02-03 13:11:05 -05:00
|
|
|
useBuilder = true
|
2022-02-03 04:37:55 -05:00
|
|
|
builderAlias = v
|
|
|
|
}
|
|
|
|
|
2022-02-03 13:11:05 -05:00
|
|
|
// is this a build that should be forwarded to the builder?
|
|
|
|
fwargs, fwosargs, forwarded := forwardBuilder(builderAlias, args, osargs)
|
|
|
|
if !forwarded {
|
2022-01-27 13:30:06 -05:00
|
|
|
return args, osargs, nil
|
|
|
|
}
|
|
|
|
|
2022-02-03 13:11:05 -05:00
|
|
|
if useLegacy {
|
|
|
|
// display warning if not wcow and continue
|
|
|
|
if dockerCli.ServerInfo().OSType != "windows" {
|
|
|
|
_, _ = fmt.Fprintln(dockerCli.Err(), newBuilderError(true, nil))
|
|
|
|
}
|
2022-01-27 13:30:06 -05:00
|
|
|
return args, osargs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check plugin is available if cmd forwarded
|
|
|
|
plugin, perr := pluginmanager.GetPlugin(builderAlias, dockerCli, cmd.Root())
|
|
|
|
if perr == nil && plugin != nil {
|
|
|
|
perr = plugin.Err
|
|
|
|
}
|
|
|
|
if perr != nil {
|
2022-02-03 13:11:05 -05:00
|
|
|
// if builder enforced with DOCKER_BUILDKIT=1, cmd must fail if plugin missing or broken
|
|
|
|
if useBuilder {
|
|
|
|
return args, osargs, newBuilderError(false, perr)
|
2022-01-27 13:30:06 -05:00
|
|
|
}
|
|
|
|
// otherwise, display warning and continue
|
2022-01-27 12:42:05 -05:00
|
|
|
_, _ = fmt.Fprintln(dockerCli.Err(), newBuilderError(true, perr))
|
2022-01-27 13:30:06 -05:00
|
|
|
return args, osargs, nil
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
|
2022-01-27 13:30:06 -05:00
|
|
|
return fwargs, fwosargs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func forwardBuilder(alias string, args, osargs []string) ([]string, []string, bool) {
|
2022-02-03 04:37:55 -05:00
|
|
|
aliases := [][2][]string{
|
|
|
|
{
|
|
|
|
{"builder"},
|
2022-01-27 13:30:06 -05:00
|
|
|
{alias},
|
2022-02-03 04:37:55 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
{"build"},
|
2022-01-27 13:30:06 -05:00
|
|
|
{alias, "build"},
|
2022-02-03 04:37:55 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
{"image", "build"},
|
2022-01-27 13:30:06 -05:00
|
|
|
{alias, "build"},
|
2022-02-03 04:37:55 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, al := range aliases {
|
2022-01-27 13:30:06 -05:00
|
|
|
if fwargs, changed := command.StringSliceReplaceAt(args, al[0], al[1], 0); changed {
|
|
|
|
fwosargs, _ := command.StringSliceReplaceAt(osargs, al[0], al[1], -1)
|
|
|
|
return fwargs, fwosargs, true
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 13:30:06 -05:00
|
|
|
return args, osargs, false
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|