From 02bd9e1fe4b6c97f5488b1ed3d4cd9c7f54f39a0 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 23 Feb 2022 16:20:21 +0100 Subject: [PATCH] bring back and expose BuildKitEnabled func Signed-off-by: CrazyMax --- cli/command/cli.go | 22 ++++++++++++++++++++++ cmd/docker/builder.go | 18 ++++++------------ internal/test/cli.go | 5 +++++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index 97d72993332e..ff9fcc8f527a 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "runtime" + "strconv" "strings" "time" @@ -58,6 +59,7 @@ type Cli interface { ManifestStore() manifeststore.Store RegistryClient(bool) registryclient.RegistryClient ContentTrustEnabled() bool + BuildKitEnabled() (bool, error) ContextStore() store.Store CurrentContext() string StackOrchestrator(flagValue string) (Orchestrator, error) @@ -168,6 +170,26 @@ func (cli *DockerCli) ContentTrustEnabled() bool { return cli.contentTrust } +// BuildKitEnabled returns buildkit is enabled or not. +func (cli *DockerCli) BuildKitEnabled() (bool, error) { + // use DOCKER_BUILDKIT env var value if set + if v, ok := os.LookupEnv("DOCKER_BUILDKIT"); ok { + enabled, err := strconv.ParseBool(v) + if err != nil { + return false, errors.Wrap(err, "DOCKER_BUILDKIT environment variable expects boolean value") + } + return enabled, nil + } + // if a builder alias is defined, we are using BuildKit + aliasMap := cli.ConfigFile().Aliases + if _, ok := aliasMap["builder"]; ok { + return true, nil + } + // otherwise, assume BuildKit is enabled but + // not if wcow reported from server side + return cli.ServerInfo().OSType != "windows", nil +} + // ManifestStore returns a store for local manifests func (cli *DockerCli) ManifestStore() manifeststore.Store { // TODO: support override default location from config file diff --git a/cmd/docker/builder.go b/cmd/docker/builder.go index 03d6055b8e57..bc3db2066cf3 100644 --- a/cmd/docker/builder.go +++ b/cmd/docker/builder.go @@ -2,8 +2,6 @@ package main import ( "fmt" - "os" - "strconv" pluginmanager "github.com/docker/cli/cli-plugins/manager" "github.com/docker/cli/cli/command" @@ -44,17 +42,13 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st var useLegacy bool var useBuilder bool - // check DOCKER_BUILDKIT env var is present and - // if not assume we want to use the builder component - if v, ok := os.LookupEnv("DOCKER_BUILDKIT"); ok { - enabled, err := strconv.ParseBool(v) - if err != nil { - return args, osargs, errors.Wrap(err, "DOCKER_BUILDKIT environment variable expects boolean value") - } - if !enabled { - useLegacy = true - } else { + if bkExplicit, err := command.DockerBuildKitEnabled(); err != nil { + return nil, nil, err + } else if bkExplicit != nil { + if *bkExplicit { useBuilder = true + } else { + useLegacy = true } } diff --git a/internal/test/cli.go b/internal/test/cli.go index f3844142f793..8d9f92e7641b 100644 --- a/internal/test/cli.go +++ b/internal/test/cli.go @@ -215,6 +215,11 @@ func EnableContentTrust(c *FakeCli) { c.contentTrust = true } +// BuildKitEnabled on the fake cli +func (c *FakeCli) BuildKitEnabled() (bool, error) { + return true, nil +} + // StackOrchestrator return the selected stack orchestrator func (c *FakeCli) StackOrchestrator(flagValue string) (command.Orchestrator, error) { configOrchestrator := ""