Skip to content

Commit 6a509ee

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 6a509ee

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
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: 7 additions & 11 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"
@@ -159,9 +158,11 @@ func NewBuildCommand(dockerCli command.Cli) *cobra.Command {
159158

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

163163
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>]])")
164164
flags.SetAnnotation("ssh", "version", []string{"1.39"})
165+
flags.SetAnnotation("ssh", "buildkit", nil)
165166
return cmd
166167
}
167168

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

184185
// nolint: gocyclo
185186
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 {
187+
buildkitEnabled, err := dockerCli.BuildKitEnabled()
188+
if err != nil {
189+
return err
190+
}
191+
if buildkitEnabled {
195192
return runBuildBuildKit(dockerCli, options)
196193
}
197194

198195
var (
199196
buildCtx io.ReadCloser
200197
dockerfileCtx io.ReadCloser
201-
err error
202198
contextDir string
203199
tempDir string
204200
relDockerfile string

cmd/docker/docker.go

Lines changed: 14 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,20 @@ 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")
247255
// hide flags not supported by the server
248256
if !isOSTypeSupported(f, osType) || !isVersionSupported(f, clientVersion) {
249257
f.Hidden = true
@@ -259,6 +267,7 @@ 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")
262271
// hide subcommands not supported by the server
263272
if subcmdVersion, ok := subcmd.Annotations["version"]; ok && versions.LessThan(clientVersion, subcmdVersion) {
264273
subcmd.Hidden = true
@@ -267,6 +276,7 @@ func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) {
267276
subcmd.Hidden = true
268277
}
269278
}
279+
return nil
270280
}
271281

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

0 commit comments

Comments
 (0)