2022-02-03 04:37:55 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-03 17:30:27 -04:00
|
|
|
"io"
|
2022-02-03 04:37:55 -05:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2022-11-03 17:30:27 -04:00
|
|
|
"strings"
|
2022-02-03 04:37:55 -05:00
|
|
|
|
|
|
|
pluginmanager "github.com/docker/cli/cli-plugins/manager"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
2022-11-03 17:30:27 -04:00
|
|
|
"github.com/spf13/pflag"
|
2022-02-03 04:37:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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:
|
2022-12-06 07:46:51 -05:00
|
|
|
https://docs.docker.com/go/buildx/`
|
|
|
|
|
|
|
|
buildkitDisabledWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
|
2022-12-19 07:03:28 -05:00
|
|
|
BuildKit is currently disabled; enable it by removing the DOCKER_BUILDKIT=0
|
2022-12-06 07:46:51 -05:00
|
|
|
environment-variable.`
|
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:
|
2022-12-06 07:46:51 -05:00
|
|
|
https://docs.docker.com/go/buildx/`
|
2022-01-27 12:42:05 -05:00
|
|
|
)
|
2022-02-03 04:37:55 -05:00
|
|
|
|
2022-12-06 07:46:51 -05:00
|
|
|
func newBuilderError(errorMsg string, pluginLoadErr error) error {
|
|
|
|
if pluginmanager.IsNotFound(pluginLoadErr) {
|
2022-01-27 12:42:05 -05:00
|
|
|
return errors.New(errorMsg)
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
2022-12-06 07:46:51 -05:00
|
|
|
if pluginLoadErr != nil {
|
|
|
|
return fmt.Errorf("%w\n\n%s", pluginLoadErr, errorMsg)
|
2022-02-03 13:11:05 -05:00
|
|
|
}
|
2022-12-06 07:46:51 -05:00
|
|
|
return errors.New(errorMsg)
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
|
2022-11-03 17:30:27 -04:00
|
|
|
//nolint:gocyclo
|
|
|
|
func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []string) ([]string, []string, []string, error) {
|
2022-12-06 07:46:51 -05:00
|
|
|
var buildKitDisabled, useBuilder, useAlias bool
|
2022-11-03 17:30:27 -04:00
|
|
|
var envs []string
|
2022-02-03 13:11:05 -05:00
|
|
|
|
2023-04-19 08:06:01 -04:00
|
|
|
// check DOCKER_BUILDKIT env var is not empty
|
|
|
|
// if it is assume we want to use the builder component
|
|
|
|
if v := os.Getenv("DOCKER_BUILDKIT"); v != "" {
|
2022-02-03 04:37:55 -05:00
|
|
|
enabled, err := strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
2022-11-03 17:30:27 -04:00
|
|
|
return args, osargs, nil, errors.Wrap(err, "DOCKER_BUILDKIT environment variable expects boolean value")
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
if !enabled {
|
2022-12-06 07:46:51 -05:00
|
|
|
buildKitDisabled = true
|
2022-02-03 13:11:05 -05:00
|
|
|
} 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-11-03 18:35:45 -04:00
|
|
|
useAlias = 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?
|
2024-02-13 16:40:55 -05:00
|
|
|
fwargs, fwosargs, alias, forwarded := forwardBuilder(builderAlias, args, osargs)
|
2022-02-03 13:11:05 -05:00
|
|
|
if !forwarded {
|
2022-11-03 17:30:27 -04:00
|
|
|
return args, osargs, nil, nil
|
2022-01-27 13:30:06 -05:00
|
|
|
}
|
|
|
|
|
2022-02-23 10:20:45 -05:00
|
|
|
// wcow build command must use the legacy builder
|
|
|
|
// if not opt-in through a builder component
|
|
|
|
if !useBuilder && dockerCli.ServerInfo().OSType == "windows" {
|
2022-11-03 17:30:27 -04:00
|
|
|
return args, osargs, nil, nil
|
2022-02-23 10:20:45 -05:00
|
|
|
}
|
|
|
|
|
2022-12-06 07:46:51 -05:00
|
|
|
if buildKitDisabled {
|
2022-02-03 13:11:05 -05:00
|
|
|
// display warning if not wcow and continue
|
|
|
|
if dockerCli.ServerInfo().OSType != "windows" {
|
2022-12-06 07:46:51 -05:00
|
|
|
_, _ = fmt.Fprintf(dockerCli.Err(), "%s\n\n", buildkitDisabledWarning)
|
2022-02-03 13:11:05 -05:00
|
|
|
}
|
2022-11-03 17:30:27 -04:00
|
|
|
return args, osargs, nil, nil
|
2022-01-27 13:30:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12-06 07:46:51 -05:00
|
|
|
// if builder is enforced with DOCKER_BUILDKIT=1, cmd must fail
|
|
|
|
// if the plugin is missing or broken.
|
2022-02-03 13:11:05 -05:00
|
|
|
if useBuilder {
|
2022-12-06 07:46:51 -05:00
|
|
|
return args, osargs, nil, newBuilderError(buildxMissingError, perr)
|
2022-01-27 13:30:06 -05:00
|
|
|
}
|
|
|
|
// otherwise, display warning and continue
|
2022-12-06 07:46:51 -05:00
|
|
|
_, _ = fmt.Fprintf(dockerCli.Err(), "%s\n\n", newBuilderError(buildxMissingWarning, perr))
|
2022-11-03 17:30:27 -04:00
|
|
|
return args, osargs, nil, nil
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
|
2022-11-03 17:30:27 -04:00
|
|
|
// If build subcommand is forwarded, user would expect "docker build" to
|
|
|
|
// always create a local docker image (default context builder). This is
|
|
|
|
// for better backward compatibility in case where a user could switch to
|
|
|
|
// a docker container builder with "docker buildx --use foo" which does
|
2022-11-03 18:35:45 -04:00
|
|
|
// not --load by default. Also makes sure that an arbitrary builder name
|
|
|
|
// is not being set in the command line or in the environment before
|
|
|
|
// setting the default context and keep "buildx install" behavior if being
|
|
|
|
// set (builder alias).
|
|
|
|
if forwarded && !useAlias && !hasBuilderName(args, os.Environ()) {
|
2022-11-03 17:30:27 -04:00
|
|
|
envs = append([]string{"BUILDX_BUILDER=" + dockerCli.CurrentContext()}, envs...)
|
|
|
|
}
|
|
|
|
|
2024-02-13 16:40:55 -05:00
|
|
|
// overwrite the command path for this plugin using the alias name.
|
|
|
|
cmd.Annotations[pluginmanager.CommandAnnotationPluginCommandPath] = fmt.Sprintf("%s %s", cmd.CommandPath(), alias)
|
|
|
|
|
2022-11-03 17:30:27 -04:00
|
|
|
return fwargs, fwosargs, envs, nil
|
2022-01-27 13:30:06 -05:00
|
|
|
}
|
|
|
|
|
2024-02-13 16:40:55 -05:00
|
|
|
func forwardBuilder(alias string, args, osargs []string) ([]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)
|
2024-02-13 16:40:55 -05:00
|
|
|
return fwargs, fwosargs, al[0][0], true
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
|
|
|
}
|
2024-02-13 16:40:55 -05:00
|
|
|
return args, osargs, "", false
|
2022-02-03 04:37:55 -05:00
|
|
|
}
|
2022-11-03 17:30:27 -04:00
|
|
|
|
|
|
|
// hasBuilderName checks if a builder name is defined in args or env vars
|
|
|
|
func hasBuilderName(args []string, envs []string) bool {
|
|
|
|
var builder string
|
|
|
|
flagset := pflag.NewFlagSet("buildx", pflag.ContinueOnError)
|
|
|
|
flagset.Usage = func() {}
|
|
|
|
flagset.SetOutput(io.Discard)
|
|
|
|
flagset.StringVar(&builder, "builder", "", "")
|
|
|
|
_ = flagset.Parse(args)
|
|
|
|
if builder != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, e := range envs {
|
|
|
|
if strings.HasPrefix(e, "BUILDX_BUILDER=") && e != "BUILDX_BUILDER=" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|