Skip to content

Commit

Permalink
build: warning msg on deprecated or not implemented flags
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Oct 20, 2021
1 parent b145d42 commit cc0ecbb
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 84 deletions.
2 changes: 0 additions & 2 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,6 @@ func toSolveOpt(ctx context.Context, d driver.Driver, multiDriver bool, opt Opti
case "none":
so.FrontendAttrs["force-network-mode"] = opt.NetworkMode
case "", "default":
default:
return nil, nil, errors.Errorf("network mode %q not supported by buildkit", opt.NetworkMode)
}

// setup extrahosts
Expand Down
6 changes: 3 additions & 3 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (

type bakeOptions struct {
files []string
printOnly bool
overrides []string
printOnly bool
commonOptions
}

Expand Down Expand Up @@ -200,10 +200,10 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
flags := cmd.Flags()

flags.StringArrayVarP(&options.files, "file", "f", []string{}, "Build definition file")
flags.BoolVar(&options.exportLoad, "load", false, "Shorthand for `--set=*.output=type=docker`")
flags.BoolVar(&options.printOnly, "print", false, "Print the options without building")
flags.StringArrayVar(&options.overrides, "set", nil, "Override target value (e.g., `targetpattern.key=value`)")
flags.BoolVar(&options.exportPush, "push", false, "Shorthand for `--set=*.output=type=registry`")
flags.BoolVar(&options.exportLoad, "load", false, "Shorthand for `--set=*.output=type=docker`")
flags.StringArrayVar(&options.overrides, "set", nil, "Override target value (e.g., `targetpattern.key=value`)")

commonBuildFlags(&options.commonOptions, flags)

Expand Down
197 changes: 118 additions & 79 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,50 @@ import (
"github.com/docker/buildx/util/tracing"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/opts"
dockeropts "github.com/docker/cli/opts"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/go-units"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/util/appcontext"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

const defaultTargetName = "default"

type buildOptions struct {
commonOptions
allow []string
buildArgs []string
cacheFrom []string
cacheTo []string
contextPath string
dockerfileName string
tags []string
extraHosts []string
imageIDFile string
labels []string
buildArgs []string

cacheFrom []string
cacheTo []string
target string
platforms []string
secrets []string
ssh []string
outputs []string
imageIDFile string
extraHosts []string
networkMode string
quiet bool
shmSize opts.MemBytes
ulimits *opts.UlimitOpt

// unimplemented
squash bool

allow []string

// hidden
// untrusted bool
// memory opts.MemBytes
// memorySwap opts.MemSwapBytes
// shmSize opts.MemBytes
// cpuShares int64
// cpuPeriod int64
// cpuQuota int64
// cpuSetCpus string
// cpuSetMems string
// cgroupParent string
// isolation string
// compress bool
// securityOpt []string
networkMode string
outputs []string
platforms []string
quiet bool
secrets []string
shmSize dockeropts.MemBytes
ssh []string
tags []string
target string
ulimits *dockeropts.UlimitOpt
commonOptions
}

type commonOptions struct {
builder string
metadataFile string
noCache *bool
progress string
pull *bool
metadataFile string

// golangci-lint#826
// nolint:structcheck
exportPush bool
Expand All @@ -85,9 +67,6 @@ type commonOptions struct {
}

func runBuild(dockerCli command.Cli, in buildOptions) (err error) {
if in.squash {
return errors.Errorf("squash currently not implemented")
}
ctx := appcontext.Context()

ctx, end, err := tracing.TraceCurrentCommand(ctx, "build")
Expand Down Expand Up @@ -119,16 +98,16 @@ func runBuild(dockerCli command.Cli, in buildOptions) (err error) {
DockerfilePath: in.dockerfileName,
InStream: os.Stdin,
},
Tags: in.tags,
Labels: listToMap(in.labels, false),
BuildArgs: listToMap(in.buildArgs, true),
Pull: pull,
NoCache: noCache,
Target: in.target,
ImageIDFile: in.imageIDFile,
ExtraHosts: in.extraHosts,
ImageIDFile: in.imageIDFile,
Labels: listToMap(in.labels, false),
NetworkMode: in.networkMode,
NoCache: noCache,
Pull: pull,
ShmSize: in.shmSize,
Tags: in.tags,
Target: in.target,
Ulimits: in.ulimits,
}

Expand Down Expand Up @@ -268,7 +247,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu
func newBuildOptions() buildOptions {
ulimits := make(map[string]*units.Ulimit)
return buildOptions{
ulimits: opts.NewUlimitOpt(&ulimits),
ulimits: dockeropts.NewUlimitOpt(&ulimits),
}
}

Expand All @@ -283,91 +262,128 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
options.contextPath = args[0]
options.builder = rootOpts.builder
cmd.Flags().VisitAll(checkDeprecatedFlags)
return runBuild(dockerCli, options)
},
}

cmd.Flags()

var platformsDefault []string
if v := os.Getenv("DOCKER_DEFAULT_PLATFORM"); v != "" {
platformsDefault = []string{v}
}

flags := cmd.Flags()

flags.BoolVar(&options.exportPush, "push", false, "Shorthand for `--output=type=registry`")
flags.BoolVar(&options.exportLoad, "load", false, "Shorthand for `--output=type=docker`")
flags.StringSliceVar(&options.extraHosts, "add-host", []string{}, "Add a custom host-to-IP mapping (format: `host:ip`)")
flags.SetAnnotation("add-host", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#add-entries-to-container-hosts-file---add-host"})

flags.StringSliceVar(&options.allow, "allow", []string{}, "Allow extra privileged entitlement (e.g., `network.host`, `security.insecure`)")

flags.StringArrayVarP(&options.tags, "tag", "t", []string{}, "Name and optionally a tag (format: `name:tag`)")
flags.SetAnnotation("tag", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#tag-an-image--t"})
flags.StringArrayVar(&options.buildArgs, "build-arg", []string{}, "Set build-time variables")
flags.SetAnnotation("build-arg", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg"})

flags.StringArrayVar(&options.cacheFrom, "cache-from", []string{}, "External cache sources (e.g., `user/app:cache`, `type=local,src=path/to/dir`)")

flags.StringArrayVar(&options.cacheTo, "cache-to", []string{}, "Cache export destinations (e.g., `user/app:cache`, `type=local,dest=path/to/dir`)")

flags.StringVarP(&options.dockerfileName, "file", "f", "", "Name of the Dockerfile (default: `PATH/Dockerfile`)")
flags.SetAnnotation("file", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#specify-a-dockerfile--f"})

flags.StringVar(&options.imageIDFile, "iidfile", "", "Write the image ID to the file")

flags.StringArrayVar(&options.labels, "label", []string{}, "Set metadata for an image")

flags.StringArrayVar(&options.cacheFrom, "cache-from", []string{}, "External cache sources (e.g., `user/app:cache`, `type=local,src=path/to/dir`)")
flags.StringArrayVar(&options.cacheTo, "cache-to", []string{}, "Cache export destinations (e.g., `user/app:cache`, `type=local,dest=path/to/dir`)")
flags.BoolVar(&options.exportLoad, "load", false, "Shorthand for `--output=type=docker`")

flags.StringVar(&options.target, "target", "", "Set the target build stage to build.")
flags.SetAnnotation("target", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#specifying-target-build-stage---target"})
flags.StringVar(&options.networkMode, "network", "default", "Set the networking mode for the RUN instructions during build")
flags.SetAnnotation("network", "flag-notimplemented", []string{"network flag only supports <none|host|default> values. Custom network might be implemented in the future with a driver-opt."})

flags.StringSliceVar(&options.allow, "allow", []string{}, "Allow extra privileged entitlement (e.g., `network.host`, `security.insecure`)")
flags.StringArrayVarP(&options.outputs, "output", "o", []string{}, "Output destination (format: `type=local,dest=path`)")

flags.StringArrayVar(&options.platforms, "platform", platformsDefault, "Set target platform for build")

flags.BoolVar(&options.exportPush, "push", false, "Shorthand for `--output=type=registry`")

flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the build output and print image ID on success")
flags.StringVar(&options.networkMode, "network", "default", "Set the networking mode for the RUN instructions during build")
flags.StringSliceVar(&options.extraHosts, "add-host", []string{}, "Add a custom host-to-IP mapping (format: `host:ip`)")
flags.SetAnnotation("add-host", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#add-entries-to-container-hosts-file---add-host"})
flags.StringVar(&options.imageIDFile, "iidfile", "", "Write the image ID to the file")

flags.StringArrayVar(&options.secrets, "secret", []string{}, "Secret file to expose to the build (format: `id=mysecret,src=/local/secret`)")

flags.Var(&options.shmSize, "shm-size", "Size of `/dev/shm`")
flags.Var(options.ulimits, "ulimit", "Ulimit options")

// not implemented
flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer")
flags.MarkHidden("squash")
flags.StringArrayVar(&options.ssh, "ssh", []string{}, "SSH agent socket or keys to expose to the build (format: `default|<id>[=<socket>|<key>[,<key>]]`)")

flags.StringArrayVarP(&options.tags, "tag", "t", []string{}, "Name and optionally a tag (format: `name:tag`)")
flags.SetAnnotation("tag", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#tag-an-image--t"})

flags.StringVar(&options.target, "target", "", "Set the target build stage to build.")
flags.SetAnnotation("target", "docs.external.url", []string{"https://docs.docker.com/engine/reference/commandline/build/#specifying-target-build-stage---target"})

flags.Var(options.ulimits, "ulimit", "Ulimit options")

// hidden flags
var ignore string
var ignoreSlice []string
var ignoreBool bool
var ignoreInt int64
flags.BoolVar(&ignoreBool, "squash", false, "Squash newly built layers into a single new layer")
flags.MarkHidden("squash")
flags.SetAnnotation("squash", "flag-deprecated", []string{"squash flag was never taken out of experimental and is deprecated with BuildKit. You should squash layers with a multi-stage build for efficiency."})

flags.StringSliceVar(&ignoreSlice, "security-opt", []string{}, "Security options")
flags.MarkHidden("security-opt")
flags.SetAnnotation("security-opt", "flag-notimplemented", []string{"security-opt flag is only supported on a daemon running on Windows and not yet implemented with BuildKit."})

flags.BoolVar(&ignoreBool, "compress", false, "Compress the build context using gzip")
flags.MarkHidden("compress")
flags.SetAnnotation("compress", "flag-deprecated", []string{"compress flag is deprecated with BuildKit."})

flags.StringVarP(&ignore, "memory", "m", "", "Memory limit")
flags.MarkHidden("memory")
flags.SetAnnotation("memory", "flag-notimplemented", []string{"memory flag might be added in the future: https://github.com/moby/buildkit/issues/2108"})

flags.StringVar(&ignore, "memory-swap", "", "Swap limit equal to memory plus swap: `-1` to enable unlimited swap")
flags.MarkHidden("memory-swap")
flags.SetAnnotation("memory-swap", "flag-notimplemented", []string{"memory-swap flag might be added in the future: https://github.com/moby/buildkit/issues/2108"})

flags.Int64VarP(&ignoreInt, "cpu-shares", "c", 0, "CPU shares (relative weight)")
flags.MarkHidden("cpu-shares")
flags.SetAnnotation("cpu-shares", "flag-notimplemented", []string{"cpu-shares flag might be added in the future: https://github.com/moby/buildkit/issues/2108"})

flags.Int64Var(&ignoreInt, "cpu-period", 0, "Limit the CPU CFS (Completely Fair Scheduler) period")
flags.MarkHidden("cpu-period")
flags.SetAnnotation("cpu-period", "flag-notimplemented", []string{"cpu-period flag might be added in the future: https://github.com/moby/buildkit/issues/2108"})

flags.Int64Var(&ignoreInt, "cpu-quota", 0, "Limit the CPU CFS (Completely Fair Scheduler) quota")
flags.MarkHidden("cpu-quota")
flags.SetAnnotation("cpu-quota", "flag-notimplemented", []string{"cpu-quota flag might be added in the future: https://github.com/moby/buildkit/issues/2108"})

flags.StringVar(&ignore, "cpuset-cpus", "", "CPUs in which to allow execution (`0-3`, `0,1`)")
flags.MarkHidden("cpuset-cpus")
flags.SetAnnotation("cpuset-cpus", "flag-notimplemented", []string{"cpuset-cpus flag might be added in the future: https://github.com/moby/buildkit/issues/2108"})

flags.StringVar(&ignore, "cpuset-mems", "", "MEMs in which to allow execution (`0-3`, `0,1`)")
flags.MarkHidden("cpuset-mems")
flags.SetAnnotation("cpuset-mems", "flag-notimplemented", []string{"cpuset-mems flag might be added in the future: https://github.com/moby/buildkit/issues/2108"})

flags.StringVar(&ignore, "cgroup-parent", "", "Optional parent cgroup for the container")
flags.MarkHidden("cgroup-parent")
flags.SetAnnotation("cgroup-parent", "flag-deprecated", []string{"cgroup-parent is deprecated and now handled per builder with the docker-container driver."})

flags.StringVar(&ignore, "isolation", "", "Container isolation technology")
flags.MarkHidden("isolation")
flags.SetAnnotation("isolation", "flag-deprecated", []string{"isolation flag is deprecated with BuildKit."})

flags.BoolVar(&ignoreBool, "rm", true, "Remove intermediate containers after a successful build")
flags.MarkHidden("rm")
flags.SetAnnotation("rm", "flag-deprecated", []string{"rm flag is deprecated (BuildKit never leaks resources)."})

flags.BoolVar(&ignoreBool, "force-rm", false, "Always remove intermediate containers")
flags.MarkHidden("force-rm")

platformsDefault := []string{}
if v := os.Getenv("DOCKER_DEFAULT_PLATFORM"); v != "" {
platformsDefault = []string{v}
}
flags.StringArrayVar(&options.platforms, "platform", platformsDefault, "Set target platform for build")

flags.StringArrayVar(&options.secrets, "secret", []string{}, "Secret file to expose to the build (format: `id=mysecret,src=/local/secret`)")

flags.StringArrayVar(&options.ssh, "ssh", []string{}, "SSH agent socket or keys to expose to the build (format: `default|<id>[=<socket>|<key>[,<key>]]`)")

flags.StringArrayVarP(&options.outputs, "output", "o", []string{}, "Output destination (format: `type=local,dest=path`)")
flags.SetAnnotation("force-rm", "flag-deprecated", []string{"force-rm flag is deprecated (BuildKit never leaks resources)."})

commonBuildFlags(&options.commonOptions, flags)

return cmd
}

Expand All @@ -378,6 +394,29 @@ func commonBuildFlags(options *commonOptions, flags *pflag.FlagSet) {
flags.StringVar(&options.metadataFile, "metadata-file", "", "Write build result metadata to the file")
}

func checkDeprecatedFlags(f *pflag.Flag) {
if !f.Changed {
return
}
if f.Name == "network" {
for _, n := range []string{"none", "host", "default"} {
if f.Value.String() == n {
return
}
}
}
for t, m := range f.Annotations {
switch t {
case "flag-deprecated":
logrus.Warnf("DEPRECATED: %s", m[0])
break
case "flag-notimplemented":
logrus.Warnf("NOT IMPLEMENTED: %s", m[0])
break
}
}
}

func listToMap(values []string, defaultEnv bool) map[string]string {
result := make(map[string]string, len(values))
for _, value := range values {
Expand Down

0 comments on commit cc0ecbb

Please sign in to comment.