simplify signal handling for cobra context

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2024-01-19 16:44:30 -08:00
parent 650a7af0ae
commit 147c7135b0
No known key found for this signature in database
GPG Key ID: AFA9DE5F8AB7AF39
16 changed files with 33 additions and 95 deletions

View File

@ -15,7 +15,6 @@ import (
"github.com/docker/buildx/builder" "github.com/docker/buildx/builder"
"github.com/docker/buildx/localstate" "github.com/docker/buildx/localstate"
"github.com/docker/buildx/util/buildflags" "github.com/docker/buildx/util/buildflags"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/cobrautil/completion"
"github.com/docker/buildx/util/confutil" "github.com/docker/buildx/util/confutil"
"github.com/docker/buildx/util/desktop" "github.com/docker/buildx/util/desktop"
@ -262,7 +261,7 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
Use: "bake [OPTIONS] [TARGET...]", Use: "bake [OPTIONS] [TARGET...]",
Aliases: []string{"f"}, Aliases: []string{"f"},
Short: "Build from a file", Short: "Build from a file",
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
// reset to nil to avoid override is unset // reset to nil to avoid override is unset
if !cmd.Flags().Lookup("no-cache").Changed { if !cmd.Flags().Lookup("no-cache").Changed {
cFlags.noCache = nil cFlags.noCache = nil
@ -274,7 +273,7 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
options.metadataFile = cFlags.metadataFile options.metadataFile = cFlags.metadataFile
// Other common flags (noCache, pull and progress) are processed in runBake function. // Other common flags (noCache, pull and progress) are processed in runBake function.
return runBake(cmd.Context(), dockerCli, args, options, cFlags) return runBake(cmd.Context(), dockerCli, args, options, cFlags)
}), },
ValidArgsFunction: completion.BakeTargets(options.files), ValidArgsFunction: completion.BakeTargets(options.files),
} }

View File

@ -461,7 +461,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions, debugConfig *debug.D
Aliases: []string{"b"}, Aliases: []string{"b"},
Short: "Start a build", Short: "Start a build",
Args: cli.ExactArgs(1), Args: cli.ExactArgs(1),
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.contextPath = args[0] options.contextPath = args[0]
options.builder = rootOpts.builder options.builder = rootOpts.builder
options.metadataFile = cFlags.metadataFile options.metadataFile = cFlags.metadataFile
@ -485,7 +485,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions, debugConfig *debug.D
} }
return runBuild(cmd.Context(), dockerCli, *options) return runBuild(cmd.Context(), dockerCli, *options)
}), },
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveFilterDirs return nil, cobra.ShellCompDirectiveFilterDirs
}, },

View File

@ -95,9 +95,9 @@ func createCmd(dockerCli command.Cli) *cobra.Command {
Use: "create [OPTIONS] [CONTEXT|ENDPOINT]", Use: "create [OPTIONS] [CONTEXT|ENDPOINT]",
Short: "Create a new builder instance", Short: "Create a new builder instance",
Args: cli.RequiresMaxArgs(1), Args: cli.RequiresMaxArgs(1),
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runCreate(cmd.Context(), dockerCli, options, args) return runCreate(cmd.Context(), dockerCli, options, args)
}), },
ValidArgsFunction: completion.Disable, ValidArgsFunction: completion.Disable,
} }

View File

@ -10,7 +10,6 @@ import (
"time" "time"
"github.com/docker/buildx/builder" "github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/cobrautil/completion"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
@ -111,10 +110,10 @@ func duCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
Use: "du", Use: "du",
Short: "Disk usage", Short: "Disk usage",
Args: cli.NoArgs, Args: cli.NoArgs,
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.builder = rootOpts.builder options.builder = rootOpts.builder
return runDiskUsage(cmd.Context(), dockerCli, options) return runDiskUsage(cmd.Context(), dockerCli, options)
}), },
ValidArgsFunction: completion.Disable, ValidArgsFunction: completion.Disable,
} }

View File

@ -9,7 +9,6 @@ import (
"github.com/distribution/reference" "github.com/distribution/reference"
"github.com/docker/buildx/builder" "github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/cobrautil/completion"
"github.com/docker/buildx/util/imagetools" "github.com/docker/buildx/util/imagetools"
"github.com/docker/buildx/util/progress" "github.com/docker/buildx/util/progress"
@ -270,10 +269,10 @@ func createCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "create [OPTIONS] [SOURCE] [SOURCE...]", Use: "create [OPTIONS] [SOURCE] [SOURCE...]",
Short: "Create a new image based on source images", Short: "Create a new image based on source images",
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.builder = *opts.Builder options.builder = *opts.Builder
return runCreate(cmd.Context(), dockerCli, options, args) return runCreate(cmd.Context(), dockerCli, options, args)
}), },
ValidArgsFunction: completion.Disable, ValidArgsFunction: completion.Disable,
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"github.com/docker/buildx/builder" "github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/cobrautil/completion"
"github.com/docker/buildx/util/imagetools" "github.com/docker/buildx/util/imagetools"
"github.com/docker/cli-docs-tool/annotation" "github.com/docker/cli-docs-tool/annotation"
@ -49,10 +48,10 @@ func inspectCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
Use: "inspect [OPTIONS] NAME", Use: "inspect [OPTIONS] NAME",
Short: "Show details of an image in the registry", Short: "Show details of an image in the registry",
Args: cli.ExactArgs(1), Args: cli.ExactArgs(1),
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.builder = *rootOpts.Builder options.builder = *rootOpts.Builder
return runInspect(cmd.Context(), dockerCli, options, args[0]) return runInspect(cmd.Context(), dockerCli, options, args[0])
}), },
ValidArgsFunction: completion.Disable, ValidArgsFunction: completion.Disable,
} }

View File

@ -11,7 +11,6 @@ import (
"github.com/docker/buildx/builder" "github.com/docker/buildx/builder"
"github.com/docker/buildx/driver" "github.com/docker/buildx/driver"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/cobrautil/completion"
"github.com/docker/buildx/util/platformutil" "github.com/docker/buildx/util/platformutil"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
@ -143,13 +142,13 @@ func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
Use: "inspect [NAME]", Use: "inspect [NAME]",
Short: "Inspect current builder instance", Short: "Inspect current builder instance",
Args: cli.RequiresMaxArgs(1), Args: cli.RequiresMaxArgs(1),
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.builder = rootOpts.builder options.builder = rootOpts.builder
if len(args) > 0 { if len(args) > 0 {
options.builder = args[0] options.builder = args[0]
} }
return runInspect(cmd.Context(), dockerCli, options) return runInspect(cmd.Context(), dockerCli, options)
}), },
ValidArgsFunction: completion.BuilderNames(dockerCli), ValidArgsFunction: completion.BuilderNames(dockerCli),
} }

View File

@ -99,9 +99,9 @@ func lsCmd(dockerCli command.Cli) *cobra.Command {
Use: "ls", Use: "ls",
Short: "List builder instances", Short: "List builder instances",
Args: cli.ExactArgs(0), Args: cli.ExactArgs(0),
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runLs(cmd.Context(), dockerCli, options) return runLs(cmd.Context(), dockerCli, options)
}), },
ValidArgsFunction: completion.Disable, ValidArgsFunction: completion.Disable,
} }

View File

@ -9,7 +9,6 @@ import (
"time" "time"
"github.com/docker/buildx/builder" "github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/cobrautil/completion"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
@ -135,10 +134,10 @@ func pruneCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
Use: "prune", Use: "prune",
Short: "Remove build cache", Short: "Remove build cache",
Args: cli.NoArgs, Args: cli.NoArgs,
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.builder = rootOpts.builder options.builder = rootOpts.builder
return runPrune(cmd.Context(), dockerCli, options) return runPrune(cmd.Context(), dockerCli, options)
}), },
ValidArgsFunction: completion.Disable, ValidArgsFunction: completion.Disable,
} }

View File

@ -8,7 +8,6 @@ import (
"github.com/docker/buildx/builder" "github.com/docker/buildx/builder"
"github.com/docker/buildx/store" "github.com/docker/buildx/store"
"github.com/docker/buildx/store/storeutil" "github.com/docker/buildx/store/storeutil"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/cobrautil/completion"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -98,7 +97,7 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "rm [OPTIONS] [NAME] [NAME...]", Use: "rm [OPTIONS] [NAME] [NAME...]",
Short: "Remove one or more builder instances", Short: "Remove one or more builder instances",
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.builders = []string{rootOpts.builder} options.builders = []string{rootOpts.builder}
if len(args) > 0 { if len(args) > 0 {
if options.allInactive { if options.allInactive {
@ -107,7 +106,7 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
options.builders = args options.builders = args
} }
return runRm(cmd.Context(), dockerCli, options) return runRm(cmd.Context(), dockerCli, options)
}), },
ValidArgsFunction: completion.BuilderNames(dockerCli), ValidArgsFunction: completion.BuilderNames(dockerCli),
} }

View File

@ -13,6 +13,7 @@ import (
"github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli-plugins/plugin"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/debug" "github.com/docker/cli/cli/debug"
"github.com/moby/buildkit/util/appcontext"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -29,12 +30,15 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
CompletionOptions: cobra.CompletionOptions{ CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true, HiddenDefaultCmd: true,
}, },
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.SetContext(appcontext.Context())
if !isPlugin {
return nil
} }
if isPlugin {
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
return plugin.PersistentPreRunE(cmd, args) return plugin.PersistentPreRunE(cmd, args)
},
} }
} else { if !isPlugin {
// match plugin behavior for standalone mode // match plugin behavior for standalone mode
// https://github.com/docker/cli/blob/6c9eb708fa6d17765d71965f90e1c59cea686ee9/cli-plugins/plugin/plugin.go#L117-L127 // https://github.com/docker/cli/blob/6c9eb708fa6d17765d71965f90e1c59cea686ee9/cli-plugins/plugin/plugin.go#L117-L127
cmd.SilenceUsage = true cmd.SilenceUsage = true

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"github.com/docker/buildx/builder" "github.com/docker/buildx/builder"
"github.com/docker/buildx/util/cobrautil"
"github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/cobrautil/completion"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
@ -38,13 +37,13 @@ func stopCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
Use: "stop [NAME]", Use: "stop [NAME]",
Short: "Stop builder instance", Short: "Stop builder instance",
Args: cli.RequiresMaxArgs(1), Args: cli.RequiresMaxArgs(1),
RunE: cobrautil.ConfigureContext(func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.builder = rootOpts.builder options.builder = rootOpts.builder
if len(args) > 0 { if len(args) > 0 {
options.builder = args[0] options.builder = args[0]
} }
return runStop(cmd.Context(), dockerCli, options) return runStop(cmd.Context(), dockerCli, options)
}), },
ValidArgsFunction: completion.BuilderNames(dockerCli), ValidArgsFunction: completion.BuilderNames(dockerCli),
} }

View File

@ -1,13 +1,6 @@
package cobrautil package cobrautil
import ( import (
"context"
"os"
"os/signal"
"github.com/moby/buildkit/util/bklog"
detect "github.com/moby/buildkit/util/tracing/env"
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -58,35 +51,3 @@ func MarkCommandExperimental(c *cobra.Command) {
c.Annotations[annotationExperimentalCLI] = "" c.Annotations[annotationExperimentalCLI] = ""
c.Short += " (EXPERIMENTAL)" c.Short += " (EXPERIMENTAL)"
} }
// ConfigureContext sets up signal handling and hooks into the command's
// context so that it will be cancelled when signalled, as well as implementing
// the "hard exit after 3 signals" logic. It also configures OTEL tracing
// for the relevant context.
func ConfigureContext(fn func(*cobra.Command, []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
ctx := detect.InitContext(cmd.Context())
cancellableCtx, cancel := context.WithCancelCause(ctx)
ctx = cancellableCtx
signalLimit := 3
s := make(chan os.Signal, signalLimit)
signal.Notify(s, interruptSignals...)
go func() {
retries := 0
for {
<-s
retries++
err := errors.Errorf("got %d SIGTERM/SIGINTs, forcing shutdown", retries)
cancel(err)
if retries >= signalLimit {
bklog.G(ctx).Errorf(err.Error())
os.Exit(1)
}
}
}()
cmd.SetContext(ctx)
return fn(cmd, args)
}
}

View File

@ -1,10 +0,0 @@
//go:build !windows
package cobrautil
import (
"golang.org/x/sys/unix"
"os"
)
var interruptSignals = []os.Signal{unix.SIGTERM, unix.SIGINT}

View File

@ -1,9 +0,0 @@
//go:build windows
package cobrautil
import (
"os"
)
var interruptSignals = []os.Signal{os.Interrupt}

View File

@ -14,10 +14,10 @@ const (
) )
func init() { func init() {
appcontext.Register(InitContext) appcontext.Register(initContext)
} }
func InitContext(ctx context.Context) context.Context { func initContext(ctx context.Context) context.Context {
// open-telemetry/opentelemetry-specification#740 // open-telemetry/opentelemetry-specification#740
parent := os.Getenv("TRACEPARENT") parent := os.Getenv("TRACEPARENT")
state := os.Getenv("TRACESTATE") state := os.Getenv("TRACESTATE")