Skip to content

Commit b98f5d5

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 b98f5d5

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

cli/command/cli.go

Lines changed: 15 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"
@@ -133,6 +134,20 @@ func (cli *DockerCli) ContentTrustEnabled() bool {
133134
return cli.contentTrust
134135
}
135136

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

cli/command/image/build.go

Lines changed: 13 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"
@@ -137,6 +136,8 @@ func NewBuildCommand(dockerCli command.Cli) *cobra.Command {
137136
flags.BoolVar(&options.pull, "pull", false, "Always attempt to pull a newer version of the image")
138137
flags.StringSliceVar(&options.cacheFrom, "cache-from", []string{}, "Images to consider as cache sources")
139138
flags.BoolVar(&options.compress, "compress", false, "Compress the build context using gzip")
139+
flags.SetAnnotation("compress", "no-buildkit", nil)
140+
140141
flags.StringSliceVar(&options.securityOpt, "security-opt", []string{}, "Security options")
141142
flags.StringVar(&options.networkMode, "network", "default", "Set the networking mode for the RUN instructions during build")
142143
flags.SetAnnotation("network", "version", []string{"1.25"})
@@ -150,18 +151,23 @@ func NewBuildCommand(dockerCli command.Cli) *cobra.Command {
150151
flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer")
151152
flags.SetAnnotation("squash", "experimental", nil)
152153
flags.SetAnnotation("squash", "version", []string{"1.25"})
154+
flags.SetAnnotation("squash", "no-buildkit", nil)
153155

154156
flags.BoolVar(&options.stream, "stream", false, "Stream attaches to server to negotiate build context")
155157
flags.SetAnnotation("stream", "experimental", nil)
156158
flags.SetAnnotation("stream", "version", []string{"1.31"})
159+
flags.SetAnnotation("stream", "no-buildkit", nil)
157160

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")
161+
flags.StringVar(&options.progress, "progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
162+
flags.SetAnnotation("progress", "buildkit", nil)
159163

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

163168
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>]])")
164169
flags.SetAnnotation("ssh", "version", []string{"1.39"})
170+
flags.SetAnnotation("ssh", "buildkit", nil)
165171
return cmd
166172
}
167173

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

184190
// nolint: gocyclo
185191
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 {
192+
buildkitEnabled, err := command.BuildKitEnabled(dockerCli.ServerInfo())
193+
if err != nil {
194+
return err
195+
}
196+
if buildkitEnabled {
195197
return runBuildBuildKit(dockerCli, options)
196198
}
197199

198200
var (
199201
buildCtx io.ReadCloser
200202
dockerfileCtx io.ReadCloser
201-
err error
202203
contextDir string
203204
tempDir string
204205
relDockerfile string

cmd/docker/docker.go

Lines changed: 15 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
}
@@ -235,15 +237,21 @@ func hideFeatureSubCommand(subcmd *cobra.Command, hasFeature bool, annotation st
235237
}
236238
}
237239

238-
func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
240+
func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) error {
239241
clientVersion := details.Client().ClientVersion()
240242
osType := details.ServerInfo().OSType
241243
hasExperimental := details.ServerInfo().HasExperimental
242244
hasExperimentalCLI := details.ClientInfo().HasExperimental
245+
hasBuildKit, err := command.BuildKitEnabled(details.ServerInfo())
246+
if err != nil {
247+
return err
248+
}
243249

244250
cmd.Flags().VisitAll(func(f *pflag.Flag) {
245251
hideFeatureFlag(f, hasExperimental, "experimental")
246252
hideFeatureFlag(f, hasExperimentalCLI, "experimentalCLI")
253+
hideFeatureFlag(f, hasBuildKit, "buildkit")
254+
hideFeatureFlag(f, !hasBuildKit, "no-buildkit")
247255
// hide flags not supported by the server
248256
if !isOSTypeSupported(f, osType) || !isVersionSupported(f, clientVersion) {
249257
f.Hidden = true
@@ -259,6 +267,8 @@ func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
259267
for _, subcmd := range cmd.Commands() {
260268
hideFeatureSubCommand(subcmd, hasExperimental, "experimental")
261269
hideFeatureSubCommand(subcmd, hasExperimentalCLI, "experimentalCLI")
270+
hideFeatureSubCommand(subcmd, hasBuildKit, "buildkit")
271+
hideFeatureSubCommand(subcmd, !hasBuildKit, "no-buildkit")
262272
// hide subcommands not supported by the server
263273
if subcmdVersion, ok := subcmd.Annotations["version"]; ok && versions.LessThan(clientVersion, subcmdVersion) {
264274
subcmd.Hidden = true
@@ -267,6 +277,7 @@ func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
267277
subcmd.Hidden = true
268278
}
269279
}
280+
return nil
270281
}
271282

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

0 commit comments

Comments
 (0)