Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bring back and expose BuildKitEnabled func #3435

Merged
merged 2 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions cli/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -58,6 +59,7 @@ type Cli interface {
ManifestStore() manifeststore.Store
RegistryClient(bool) registryclient.RegistryClient
ContentTrustEnabled() bool
BuildKitEnabled() (bool, error)
ContextStore() store.Store
CurrentContext() string
DockerEndpoint() docker.Endpoint
Expand Down Expand Up @@ -167,6 +169,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably for a follow-up, but I just realise we don't take older daemons into account. (Should we default to "false" on, daemons that do not yet have BuildKit, or before BuildKit left experimental (BuildKit no longer required experimental since 18.09.0; https://docs.docker.com/engine/release-notes/18.09/#new-features)

}

// ManifestStore returns a store for local manifests
func (cli *DockerCli) ManifestStore() manifeststore.Store {
// TODO: support override default location from config file
Expand Down
9 changes: 7 additions & 2 deletions cmd/docker/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
var useLegacy bool
var useBuilder bool
var useLegacy, useBuilder bool

// check DOCKER_BUILDKIT env var is present and
// if not assume we want to use the builder component
Expand Down Expand Up @@ -73,6 +72,12 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
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" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly concerned that there's now 2 implementations to do the BuildKit check (one in BuildKitEnabled()); I don't want to necessarily block this PR on that, but wondering if we can use the same code for both (so that we're sure the logic won't diverge)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was thinking of moving some logic to BuildKitEnabled but in processBuilder we need to know if DOCKER_BUILDKIT is being used/enforced.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we extract the common bits to a non-exported function perhaps? (haven't checked), so something like;

func (cli *DockerCli) BuildKitEnabled() (bool, error) {
    foo, _, err := doTheBuildKitCheck(cli)
    return foo, err
}

Copy link
Member Author

@crazy-max crazy-max Feb 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum yeah we could do that, let me see.

return args, osargs, nil
}
crazy-max marked this conversation as resolved.
Show resolved Hide resolved

if useLegacy {
// display warning if not wcow and continue
if dockerCli.ServerInfo().OSType != "windows" {
Expand Down
5 changes: 5 additions & 0 deletions internal/test/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,8 @@ func (c *FakeCli) ContentTrustEnabled() bool {
func EnableContentTrust(c *FakeCli) {
c.contentTrust = true
}

// BuildKitEnabled on the fake cli
func (c *FakeCli) BuildKitEnabled() (bool, error) {
return true, nil
}