Skip to content

Commit efeec06

Browse files
author
Tibor Vass
committed
build: only show buildkit-specific flags if buildkit is enabled
Signed-off-by: Tibor Vass <tibor@docker.com>
1 parent ab50c2f commit efeec06

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

cli/command/cli.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010
"runtime"
11+
"strconv"
1112
"time"
1213

1314
"github.com/docker/cli/cli"
@@ -56,6 +57,7 @@ type Cli interface {
5657
RegistryClient(bool) registryclient.RegistryClient
5758
ContentTrustEnabled() bool
5859
NewContainerizedEngineClient(sockPath string) (clitypes.ContainerizedClient, error)
60+
BuildKitEnabled() (bool, error)
5961
}
6062

6163
// DockerCli is an instance the docker command line client.
@@ -69,6 +71,7 @@ type DockerCli struct {
6971
serverInfo ServerInfo
7072
clientInfo ClientInfo
7173
contentTrust bool
74+
buildkitEnabled *bool
7275
newContainerizeClient func(string) (clitypes.ContainerizedClient, error)
7376
}
7477

@@ -133,6 +136,23 @@ func (cli *DockerCli) ContentTrustEnabled() bool {
133136
return cli.contentTrust
134137
}
135138

139+
// BuildKitEnabled returns whether buildkit is enabled either through a daemon setting
140+
// or otherwise the client-side DOCKER_BUILDKIT environment variable
141+
func (cli *DockerCli) BuildKitEnabled() (bool, error) {
142+
if cli.buildkitEnabled == nil {
143+
bke := cli.ServerInfo().BuildkitVersion == types.BuilderBuildKit
144+
if buildkitEnv := os.Getenv("DOCKER_BUILDKIT"); buildkitEnv != "" {
145+
enableBuildkit, err := strconv.ParseBool(buildkitEnv)
146+
if err != nil {
147+
return false, errors.Wrap(err, "DOCKER_BUILDKIT environment variable expects boolean value")
148+
}
149+
bke = enableBuildkit
150+
}
151+
cli.buildkitEnabled = &bke
152+
}
153+
return *cli.buildkitEnabled, nil
154+
}
155+
136156
// ManifestStore returns a store for local manifests
137157
func (cli *DockerCli) ManifestStore() manifeststore.Store {
138158
// TODO: support override default location from config file

cli/command/image/build.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"path/filepath"
1414
"regexp"
1515
"runtime"
16-
"strconv"
1716
"strings"
1817

1918
"github.com/docker/cli/cli"
@@ -154,14 +153,18 @@ func NewBuildCommand(dockerCli command.Cli) *cobra.Command {
154153
flags.BoolVar(&options.stream, "stream", false, "Stream attaches to server to negotiate build context")
155154
flags.SetAnnotation("stream", "experimental", nil)
156155
flags.SetAnnotation("stream", "version", []string{"1.31"})
156+
flags.SetAnnotation("stream", "no-buildkit", nil)
157157

158-
flags.StringVar(&options.progress, "progress", "auto", "Set type of progress output (only if BuildKit enabled) (auto, plain, tty). Use plain to show container output")
158+
flags.StringVar(&options.progress, "progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
159+
flags.SetAnnotation("progress", "buildkit", nil)
159160

160161
flags.StringArrayVar(&options.secrets, "secret", []string{}, "Secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret")
161162
flags.SetAnnotation("secret", "version", []string{"1.39"})
163+
flags.SetAnnotation("secret", "buildkit", nil)
162164

163165
flags.StringArrayVar(&options.ssh, "ssh", []string{}, "SSH agent socket or keys to expose to the build (only if BuildKit enabled) (format: default|<id>[=<socket>|<key>[,<key>]])")
164166
flags.SetAnnotation("ssh", "version", []string{"1.39"})
167+
flags.SetAnnotation("ssh", "buildkit", nil)
165168
return cmd
166169
}
167170

@@ -183,22 +186,17 @@ func (out *lastProgressOutput) WriteProgress(prog progress.Progress) error {
183186

184187
// nolint: gocyclo
185188
func runBuild(dockerCli command.Cli, options buildOptions) error {
186-
if buildkitEnv := os.Getenv("DOCKER_BUILDKIT"); buildkitEnv != "" {
187-
enableBuildkit, err := strconv.ParseBool(buildkitEnv)
188-
if err != nil {
189-
return errors.Wrap(err, "DOCKER_BUILDKIT environment variable expects boolean value")
190-
}
191-
if enableBuildkit {
192-
return runBuildBuildKit(dockerCli, options)
193-
}
194-
} else if dockerCli.ServerInfo().BuildkitVersion == types.BuilderBuildKit {
189+
buildkitEnabled, err := dockerCli.BuildKitEnabled()
190+
if err != nil {
191+
return err
192+
}
193+
if buildkitEnabled {
195194
return runBuildBuildKit(dockerCli, options)
196195
}
197196

198197
var (
199198
buildCtx io.ReadCloser
200199
dockerfileCtx io.ReadCloser
201-
err error
202200
contextDir string
203201
tempDir string
204202
relDockerfile string

cmd/docker/docker.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ func setHelpFunc(dockerCli *command.DockerCli, cmd *cobra.Command, flags *pflag.
100100
ccmd.Println(err)
101101
return
102102
}
103-
104-
hideUnsupportedFeatures(ccmd, dockerCli)
103+
if err := hideUnsupportedFeatures(ccmd, dockerCli); err != nil {
104+
ccmd.Println(err)
105+
return
106+
}
105107
defaultHelpFunc(ccmd, args)
106108
})
107109
}
@@ -215,6 +217,7 @@ type versionDetails interface {
215217
Client() client.APIClient
216218
ClientInfo() command.ClientInfo
217219
ServerInfo() command.ServerInfo
220+
BuildKitEnabled() (bool, error)
218221
}
219222

220223
func hideFeatureFlag(f *pflag.Flag, hasFeature bool, annotation string) {
@@ -235,15 +238,21 @@ func hideFeatureSubCommand(subcmd *cobra.Command, hasFeature bool, annotation st
235238
}
236239
}
237240

238-
func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
241+
func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) error {
239242
clientVersion := details.Client().ClientVersion()
240243
osType := details.ServerInfo().OSType
241244
hasExperimental := details.ServerInfo().HasExperimental
242245
hasExperimentalCLI := details.ClientInfo().HasExperimental
246+
hasBuildKit, err := details.BuildKitEnabled()
247+
if err != nil {
248+
return err
249+
}
243250

244251
cmd.Flags().VisitAll(func(f *pflag.Flag) {
245252
hideFeatureFlag(f, hasExperimental, "experimental")
246253
hideFeatureFlag(f, hasExperimentalCLI, "experimentalCLI")
254+
hideFeatureFlag(f, hasBuildKit, "buildkit")
255+
hideFeatureFlag(f, !hasBuildKit, "no-buildkit")
247256
// hide flags not supported by the server
248257
if !isOSTypeSupported(f, osType) || !isVersionSupported(f, clientVersion) {
249258
f.Hidden = true
@@ -259,6 +268,8 @@ func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
259268
for _, subcmd := range cmd.Commands() {
260269
hideFeatureSubCommand(subcmd, hasExperimental, "experimental")
261270
hideFeatureSubCommand(subcmd, hasExperimentalCLI, "experimentalCLI")
271+
hideFeatureSubCommand(subcmd, hasBuildKit, "buildkit")
272+
hideFeatureSubCommand(subcmd, !hasBuildKit, "no-buildkit")
262273
// hide subcommands not supported by the server
263274
if subcmdVersion, ok := subcmd.Annotations["version"]; ok && versions.LessThan(clientVersion, subcmdVersion) {
264275
subcmd.Hidden = true
@@ -267,6 +278,7 @@ func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
267278
subcmd.Hidden = true
268279
}
269280
}
281+
return nil
270282
}
271283

272284
// Checks if a command or one of its ancestors is in the list
@@ -313,6 +325,7 @@ func areFlagsSupported(cmd *cobra.Command, details versionDetails) error {
313325
if _, ok := f.Annotations["experimentalCLI"]; ok && !hasExperimentalCLI {
314326
errs = append(errs, fmt.Sprintf("\"--%s\" is on a Docker cli with experimental cli features enabled", f.Name))
315327
}
328+
// buildkit-specific flags are noop when buildkit is not enabled, so we do not add an error in that case
316329
}
317330
})
318331
if len(errs) > 0 {

0 commit comments

Comments
 (0)