Merge pull request #3435 from crazy-max/buildkit-enabled-check

bring back and expose BuildKitEnabled func
This commit is contained in:
Sebastiaan van Stijn 2022-02-25 11:14:27 +01:00 committed by GitHub
commit ec8a3169ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"time" "time"
@ -58,6 +59,7 @@ type Cli interface {
ManifestStore() manifeststore.Store ManifestStore() manifeststore.Store
RegistryClient(bool) registryclient.RegistryClient RegistryClient(bool) registryclient.RegistryClient
ContentTrustEnabled() bool ContentTrustEnabled() bool
BuildKitEnabled() (bool, error)
ContextStore() store.Store ContextStore() store.Store
CurrentContext() string CurrentContext() string
DockerEndpoint() docker.Endpoint DockerEndpoint() docker.Endpoint
@ -167,6 +169,26 @@ func (cli *DockerCli) ContentTrustEnabled() bool {
return cli.contentTrust 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 // ManifestStore returns a store for local manifests
func (cli *DockerCli) ManifestStore() manifeststore.Store { func (cli *DockerCli) ManifestStore() manifeststore.Store {
// TODO: support override default location from config file // TODO: support override default location from config file

View File

@ -41,8 +41,7 @@ func newBuilderError(warn bool, err error) error {
} }
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) {
var useLegacy bool var useLegacy, useBuilder bool
var useBuilder bool
// check DOCKER_BUILDKIT env var is present and // check DOCKER_BUILDKIT env var is present and
// if not assume we want to use the builder component // if not assume we want to use the builder component
@ -73,6 +72,12 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
return args, osargs, nil return args, osargs, nil
} }
// wcow build command must use the legacy builder
// if not opt-in through a builder component
if !useBuilder && dockerCli.ServerInfo().OSType == "windows" {
return args, osargs, nil
}
if useLegacy { if useLegacy {
// display warning if not wcow and continue // display warning if not wcow and continue
if dockerCli.ServerInfo().OSType != "windows" { if dockerCli.ServerInfo().OSType != "windows" {

View File

@ -215,3 +215,8 @@ func (c *FakeCli) ContentTrustEnabled() bool {
func EnableContentTrust(c *FakeCli) { func EnableContentTrust(c *FakeCli) {
c.contentTrust = true c.contentTrust = true
} }
// BuildKitEnabled on the fake cli
func (c *FakeCli) BuildKitEnabled() (bool, error) {
return true, nil
}