From 1404ca14f88524a994a19491f3a1027f1c66cd4e Mon Sep 17 00:00:00 2001 From: Tim Ramlot <42113979+inteon@users.noreply.github.com> Date: Thu, 7 Mar 2024 10:08:58 +0100 Subject: [PATCH] pass context through cmd and use RunE instead of Run Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com> --- cmd/cmd.go | 1 + go.mod | 1 + internal/util/context.go | 36 ------------------- internal/util/signal.go | 22 ++++++------ main.go | 8 +++-- pkg/approve/approve.go | 15 ++++---- pkg/check/api/api.go | 13 +++---- pkg/check/check.go | 4 +-- pkg/completion/bash.go | 5 ++- pkg/completion/completion.go | 2 +- pkg/completion/fish.go | 5 ++- pkg/completion/powershell.go | 5 ++- pkg/completion/zsh.go | 5 ++- pkg/convert/convert.go | 10 +++--- .../certificaterequest/certificaterequest.go | 15 ++++---- .../certificatesigningrequest.go | 15 ++++---- pkg/create/create.go | 4 +-- pkg/deny/deny.go | 15 ++++---- pkg/experimental/experimental.go | 8 ++--- pkg/factory/factory.go | 6 ++-- pkg/factory/validargs.go | 32 ++++++++--------- pkg/inspect/inspect.go | 4 +-- pkg/inspect/secret/secret.go | 15 ++++---- pkg/install/helm/settings.go | 6 ++-- pkg/install/install.go | 8 ++--- pkg/renew/renew.go | 14 ++++---- pkg/status/certificate/certificate.go | 15 ++++---- pkg/status/status.go | 4 +-- pkg/uninstall/uninstall.go | 8 ++--- pkg/upgrade/migrateapiversion/command.go | 18 ++++++---- pkg/upgrade/upgrade.go | 4 +-- pkg/version/version.go | 18 ++++++---- 32 files changed, 159 insertions(+), 182 deletions(-) delete mode 100644 internal/util/context.go diff --git a/cmd/cmd.go b/cmd/cmd.go index a6c2203..7b2881b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -47,6 +47,7 @@ func NewCertManagerCtlCommand(ctx context.Context, in io.Reader, out, err io.Wri return logf.ValidateAndApply(logOptions) }, SilenceErrors: true, // Errors are already logged when calling cmd.Execute() + SilenceUsage: true, // Don't print usage when an error occurs } cmds.SetUsageTemplate(usageTemplate()) diff --git a/go.mod b/go.mod index ad407cf..07c16f4 100644 --- a/go.mod +++ b/go.mod @@ -65,6 +65,7 @@ require ( github.com/fatih/camelcase v1.0.0 // indirect github.com/fatih/color v1.15.0 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect github.com/go-errors/errors v1.4.2 // indirect github.com/go-gorp/gorp/v3 v3.1.0 // indirect diff --git a/internal/util/context.go b/internal/util/context.go deleted file mode 100644 index 6f3c57f..0000000 --- a/internal/util/context.go +++ /dev/null @@ -1,36 +0,0 @@ -/* -Copyright 2021 The cert-manager Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -import ( - "context" -) - -// ContextWithStopCh will wrap a context with a stop channel. -// When the provided stopCh closes, the cancel() will be called on the context. -// This provides a convenient way to represent a stop channel as a context. -func ContextWithStopCh(ctx context.Context, stopCh <-chan struct{}) context.Context { - ctx, cancel := context.WithCancel(ctx) - go func() { - defer cancel() - select { - case <-ctx.Done(): - case <-stopCh: - } - }() - return ctx -} diff --git a/internal/util/signal.go b/internal/util/signal.go index 0017450..b7a4673 100644 --- a/internal/util/signal.go +++ b/internal/util/signal.go @@ -17,6 +17,8 @@ limitations under the License. package util import ( + "context" + "fmt" "os" "os/signal" "syscall" @@ -40,35 +42,35 @@ const ( ) // SetupExitHandler: -// A stop channel is returned which is closed on receiving a shutdown signal (SIGTERM +// A context is returned which is canceled on receiving a shutdown signal (SIGTERM // or SIGINT). If a second signal is caught, the program is terminated directly with // exit code 130. // SetupExitHandler also returns an exit function, this exit function calls os.Exit(...) // if there is a exit code in the errorExitCodeChannel. // The errorExitCodeChannel receives exit codes when SetExitCode is called or when // a shutdown signal is received (only if exitBehavior is AlwaysErrCode). -func SetupExitHandler(exitBehavior ExitBehavior) (<-chan struct{}, func()) { +func SetupExitHandler(parentCtx context.Context, exitBehavior ExitBehavior) (context.Context, func()) { close(onlyOneSignalHandler) // panics when called twice - stop := make(chan struct{}) + ctx, cancel := context.WithCancelCause(parentCtx) c := make(chan os.Signal, 2) signal.Notify(c, shutdownSignals...) go func() { - // first signal. Close stop chan and pass exit code to exitCodeChannel. - exitCode := 128 + int((<-c).(syscall.Signal)) + // first signal. Cancel context and pass exit code to errorExitCodeChannel. + signalInt := int((<-c).(syscall.Signal)) if exitBehavior == AlwaysErrCode { - errorExitCodeChannel <- exitCode + errorExitCodeChannel <- signalInt } - close(stop) + cancel(fmt.Errorf("received signal %d", signalInt)) // second signal. Exit directly. <-c os.Exit(130) }() - return stop, func() { + return ctx, func() { select { - case signal := <-errorExitCodeChannel: - os.Exit(signal) + case signalInt := <-errorExitCodeChannel: + os.Exit(128 + signalInt) default: // Do not exit, there are no exit codes in the channel, // so just continue and let the main function go out of diff --git a/main.go b/main.go index 32c9452..e47a162 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ import ( "strings" cmdutil "k8s.io/kubectl/pkg/cmd/util" + ctrl "sigs.k8s.io/controller-runtime" logf "github.com/cert-manager/cert-manager/pkg/logs" ctlcmd "github.com/cert-manager/cmctl/v2/cmd" @@ -31,11 +32,13 @@ import ( ) func main() { - stopCh, exit := util.SetupExitHandler(util.AlwaysErrCode) + ctx, exit := util.SetupExitHandler(context.Background(), util.AlwaysErrCode) defer exit() // This function might call os.Exit, so defer last logf.InitLogs() defer logf.FlushLogs() + ctrl.SetLogger(logf.Log) + ctx = logf.NewContext(ctx, logf.Log, "cmctl") // In cmctl, we are using cmdutil.CheckErr, a kubectl utility function that creates human readable // error messages from errors. By default, this function will call os.Exit(1) if it receives an error. @@ -56,10 +59,9 @@ func main() { runtime.Goexit() // Do soft exit (handle all defers, that should set correct exit code) }) - ctx := util.ContextWithStopCh(context.Background(), stopCh) cmd := ctlcmd.NewCertManagerCtlCommand(ctx, os.Stdin, os.Stdout, os.Stderr) - if err := cmd.Execute(); err != nil { + if err := cmd.ExecuteContext(ctx); err != nil { cmdutil.CheckErr(err) } } diff --git a/pkg/approve/approve.go b/pkg/approve/approve.go index 696b25b..afa97b0 100644 --- a/pkg/approve/approve.go +++ b/pkg/approve/approve.go @@ -24,7 +24,6 @@ import ( "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/genericclioptions" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" @@ -68,7 +67,7 @@ func newOptions(ioStreams genericclioptions.IOStreams) *Options { } } -func NewCmdApprove(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdApprove(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := newOptions(ioStreams) cmd := &cobra.Command{ @@ -76,10 +75,12 @@ func NewCmdApprove(ctx context.Context, ioStreams genericclioptions.IOStreams) * Short: "Approve a CertificateRequest", Long: `Mark a CertificateRequest as Approved, so it may be signed by a configured Issuer.`, Example: example, - ValidArgsFunction: factory.ValidArgsListCertificateRequests(ctx, &o.Factory), - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate(args)) - cmdutil.CheckErr(o.Run(ctx, args)) + ValidArgsFunction: factory.ValidArgsListCertificateRequests(&o.Factory), + PreRunE: func(cmd *cobra.Command, args []string) error { + return o.Validate(args) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context(), args) }, } @@ -88,7 +89,7 @@ func NewCmdApprove(ctx context.Context, ioStreams genericclioptions.IOStreams) * cmd.Flags().StringVar(&o.Message, "message", fmt.Sprintf("manually approved by %q", build.Name()), "The message to give as to why this CertificateRequest was approved.") - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd } diff --git a/pkg/check/api/api.go b/pkg/check/api/api.go index a073d31..755aadc 100644 --- a/pkg/check/api/api.go +++ b/pkg/check/api/api.go @@ -26,7 +26,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/cli-runtime/pkg/genericclioptions" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" @@ -83,22 +82,24 @@ func (o *Options) Complete() error { } // NewCmdCheckApi returns a cobra command for checking creating cert-manager resources against the K8S API server -func NewCmdCheckApi(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdCheckApi(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ Use: "api", Short: "Check if the cert-manager API is ready", Long: checkApiDesc, - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Complete()) - cmdutil.CheckErr(o.Run(ctx)) + PreRunE: func(cmd *cobra.Command, args []string) error { + return o.Complete() + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context()) }, } cmd.Flags().DurationVar(&o.Wait, "wait", 0, "Wait until the cert-manager API is ready (default 0s = poll once)") cmd.Flags().DurationVar(&o.Interval, "interval", 5*time.Second, "Time between checks when waiting, must include unit, e.g. 1m or 10m") - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd } diff --git a/pkg/check/check.go b/pkg/check/check.go index 259a546..47570fe 100644 --- a/pkg/check/check.go +++ b/pkg/check/check.go @@ -26,9 +26,9 @@ import ( ) // NewCmdCheck returns a cobra command for checking cert-manager components. -func NewCmdCheck(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdCheck(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { cmds := NewCmdCreateBare() - cmds.AddCommand(api.NewCmdCheckApi(ctx, ioStreams)) + cmds.AddCommand(api.NewCmdCheckApi(setupCtx, ioStreams)) return cmds } diff --git a/pkg/completion/bash.go b/pkg/completion/bash.go index 3a5f25b..c7f703d 100644 --- a/pkg/completion/bash.go +++ b/pkg/completion/bash.go @@ -19,7 +19,6 @@ package completion import ( "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" - "k8s.io/kubectl/pkg/cmd/util" "github.com/cert-manager/cmctl/v2/pkg/build" ) @@ -39,8 +38,8 @@ Bash: $ {{.BuildName}} completion bash > /usr/local/etc/bash_completion.d/{{.BuildName}} `), DisableFlagsInUseLine: true, - Run: func(cmd *cobra.Command, args []string) { - util.CheckErr(cmd.Root().GenBashCompletion(ioStreams.Out)) + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Root().GenBashCompletion(ioStreams.Out) }, } } diff --git a/pkg/completion/completion.go b/pkg/completion/completion.go index 6ba46a6..5798460 100644 --- a/pkg/completion/completion.go +++ b/pkg/completion/completion.go @@ -23,7 +23,7 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" ) -func NewCmdCompletion(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdCompletion(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { cmds := &cobra.Command{ Use: "completion", Short: "Generate completion scripts for the cert-manager CLI", diff --git a/pkg/completion/fish.go b/pkg/completion/fish.go index 629769b..d999baa 100644 --- a/pkg/completion/fish.go +++ b/pkg/completion/fish.go @@ -19,7 +19,6 @@ package completion import ( "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" - "k8s.io/kubectl/pkg/cmd/util" "github.com/cert-manager/cmctl/v2/pkg/build" ) @@ -35,8 +34,8 @@ func newCmdCompletionFish(ioStreams genericclioptions.IOStreams) *cobra.Command $ {{.BuildName}} completion fish > ~/.config/fish/completions/{{.BuildName}}.fish `), DisableFlagsInUseLine: true, - Run: func(cmd *cobra.Command, args []string) { - util.CheckErr(cmd.Root().GenFishCompletion(ioStreams.Out, true)) + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Root().GenFishCompletion(ioStreams.Out, true) }, } } diff --git a/pkg/completion/powershell.go b/pkg/completion/powershell.go index 34fc985..8ec0931 100644 --- a/pkg/completion/powershell.go +++ b/pkg/completion/powershell.go @@ -19,7 +19,6 @@ package completion import ( "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" - "k8s.io/kubectl/pkg/cmd/util" "github.com/cert-manager/cmctl/v2/pkg/build" ) @@ -36,8 +35,8 @@ func newCmdCompletionPowerShell(ioStreams genericclioptions.IOStreams) *cobra.Co # and source this file from your PowerShell profile. `), DisableFlagsInUseLine: true, - Run: func(cmd *cobra.Command, args []string) { - util.CheckErr(cmd.Root().GenPowerShellCompletion(ioStreams.Out)) + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Root().GenPowerShellCompletion(ioStreams.Out) }, } } diff --git a/pkg/completion/zsh.go b/pkg/completion/zsh.go index 31977be..b917aca 100644 --- a/pkg/completion/zsh.go +++ b/pkg/completion/zsh.go @@ -19,7 +19,6 @@ package completion import ( "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" - "k8s.io/kubectl/pkg/cmd/util" "github.com/cert-manager/cmctl/v2/pkg/build" ) @@ -38,8 +37,8 @@ func newCmdCompletionZSH(ioStreams genericclioptions.IOStreams) *cobra.Command { # You will need to start a new shell for this setup to take effect. `), DisableFlagsInUseLine: true, - Run: func(cmd *cobra.Command, args []string) { - util.CheckErr(cmd.Root().GenZshCompletion(ioStreams.Out)) + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Root().GenZshCompletion(ioStreams.Out) }, } } diff --git a/pkg/convert/convert.go b/pkg/convert/convert.go index 5a7d45f..0bc8ca9 100644 --- a/pkg/convert/convert.go +++ b/pkg/convert/convert.go @@ -85,7 +85,7 @@ func NewOptions(ioStreams genericclioptions.IOStreams) *Options { } // NewCmdConvert returns a cobra command for converting cert-manager resources -func NewCmdConvert(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdConvert(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ @@ -94,9 +94,11 @@ func NewCmdConvert(ctx context.Context, ioStreams genericclioptions.IOStreams) * Long: longDesc, Example: example, DisableFlagsInUseLine: true, - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Complete()) - cmdutil.CheckErr(o.Run()) + PreRunE: func(cmd *cobra.Command, args []string) error { + return o.Complete() + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run() }, } diff --git a/pkg/create/certificaterequest/certificaterequest.go b/pkg/create/certificaterequest/certificaterequest.go index 5506f05..da6e612 100644 --- a/pkg/create/certificaterequest/certificaterequest.go +++ b/pkg/create/certificaterequest/certificaterequest.go @@ -31,7 +31,6 @@ import ( "k8s.io/apimachinery/pkg/util/wait" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/resource" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" @@ -103,7 +102,7 @@ func NewOptions(ioStreams genericclioptions.IOStreams) *Options { } // NewCmdCreateCR returns a cobra command for create CertificateRequest -func NewCmdCreateCR(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdCreateCR(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ @@ -112,10 +111,12 @@ func NewCmdCreateCR(ctx context.Context, ioStreams genericclioptions.IOStreams) Short: "Create a cert-manager CertificateRequest resource, using a Certificate resource as a template", Long: long, Example: example, - ValidArgsFunction: factory.ValidArgsListCertificateRequests(ctx, &o.Factory), - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate(args)) - cmdutil.CheckErr(o.Run(ctx, args)) + ValidArgsFunction: factory.ValidArgsListCertificateRequests(&o.Factory), + PreRunE: func(cmd *cobra.Command, args []string) error { + return o.Validate(args) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context(), args) }, } cmd.Flags().StringVar(&o.InputFilename, "from-certificate-file", o.InputFilename, @@ -129,7 +130,7 @@ func NewCmdCreateCR(ctx context.Context, ioStreams genericclioptions.IOStreams) cmd.Flags().DurationVar(&o.Timeout, "timeout", 5*time.Minute, "Time before timeout when waiting for CertificateRequest to be signed, must include unit, e.g. 10m or 1h") - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd } diff --git a/pkg/create/certificatesigningrequest/certificatesigningrequest.go b/pkg/create/certificatesigningrequest/certificatesigningrequest.go index b0558ba..cfe8813 100644 --- a/pkg/create/certificatesigningrequest/certificatesigningrequest.go +++ b/pkg/create/certificatesigningrequest/certificatesigningrequest.go @@ -35,7 +35,6 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/resource" "k8s.io/client-go/discovery" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" @@ -115,7 +114,7 @@ func NewOptions(ioStreams genericclioptions.IOStreams) *Options { } // NewCmdCreateCSR returns a cobra command for create CertificateSigningRequest -func NewCmdCreateCSR(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdCreateCSR(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ @@ -124,10 +123,12 @@ func NewCmdCreateCSR(ctx context.Context, ioStreams genericclioptions.IOStreams) Short: "Create a Kubernetes CertificateSigningRequest resource, using a Certificate resource as a template", Long: long, Example: example, - ValidArgsFunction: factory.ValidArgsListCertificateSigningRequests(ctx, &o.Factory), - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate(args)) - cmdutil.CheckErr(o.Run(ctx, args)) + ValidArgsFunction: factory.ValidArgsListCertificateSigningRequests(&o.Factory), + PreRunE: func(cmd *cobra.Command, args []string) error { + return o.Validate(args) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context(), args) }, } cmd.Flags().StringVarP(&o.InputFilename, "from-certificate-file", "f", o.InputFilename, @@ -141,7 +142,7 @@ func NewCmdCreateCSR(ctx context.Context, ioStreams genericclioptions.IOStreams) cmd.Flags().DurationVar(&o.Timeout, "timeout", 5*time.Minute, "Time before timeout when waiting for CertificateSigningRequest to be signed, must include unit, e.g. 10m or 1h") - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd } diff --git a/pkg/create/create.go b/pkg/create/create.go index 59b8a13..28dc983 100644 --- a/pkg/create/create.go +++ b/pkg/create/create.go @@ -25,9 +25,9 @@ import ( "github.com/cert-manager/cmctl/v2/pkg/create/certificaterequest" ) -func NewCmdCreate(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdCreate(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { cmds := NewCmdCreateBare() - cmds.AddCommand(certificaterequest.NewCmdCreateCR(ctx, ioStreams)) + cmds.AddCommand(certificaterequest.NewCmdCreateCR(setupCtx, ioStreams)) return cmds } diff --git a/pkg/deny/deny.go b/pkg/deny/deny.go index c935038..3b8ece2 100644 --- a/pkg/deny/deny.go +++ b/pkg/deny/deny.go @@ -24,7 +24,6 @@ import ( "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/genericclioptions" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" @@ -68,7 +67,7 @@ func NewOptions(ioStreams genericclioptions.IOStreams) *Options { } } -func NewCmdDeny(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdDeny(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ @@ -76,10 +75,12 @@ func NewCmdDeny(ctx context.Context, ioStreams genericclioptions.IOStreams) *cob Short: "Deny a CertificateRequest", Long: `Mark a CertificateRequest as Denied, so it may never be signed by a configured Issuer.`, Example: example, - ValidArgsFunction: factory.ValidArgsListCertificateRequests(ctx, &o.Factory), - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate(args)) - cmdutil.CheckErr(o.Run(ctx, args)) + ValidArgsFunction: factory.ValidArgsListCertificateRequests(&o.Factory), + PreRunE: func(cmd *cobra.Command, args []string) error { + return o.Validate(args) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context(), args) }, } @@ -88,7 +89,7 @@ func NewCmdDeny(ctx context.Context, ioStreams genericclioptions.IOStreams) *cob cmd.Flags().StringVar(&o.Message, "message", fmt.Sprintf("manually denied by %q", build.Name()), "The message to give as to why this CertificateRequest was denied.") - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd } diff --git a/pkg/experimental/experimental.go b/pkg/experimental/experimental.go index 0c4144e..a9aebdf 100644 --- a/pkg/experimental/experimental.go +++ b/pkg/experimental/experimental.go @@ -28,7 +28,7 @@ import ( "github.com/cert-manager/cmctl/v2/pkg/uninstall" ) -func NewCmdExperimental(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdExperimental(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { cmds := &cobra.Command{ Use: "experimental", Aliases: []string{"x"}, @@ -37,10 +37,10 @@ func NewCmdExperimental(ctx context.Context, ioStreams genericclioptions.IOStrea } create := create.NewCmdCreateBare() - create.AddCommand(certificatesigningrequest.NewCmdCreateCSR(ctx, ioStreams)) + create.AddCommand(certificatesigningrequest.NewCmdCreateCSR(setupCtx, ioStreams)) cmds.AddCommand(create) - cmds.AddCommand(install.NewCmdInstall(ctx, ioStreams)) - cmds.AddCommand(uninstall.NewCmd(ctx, ioStreams)) + cmds.AddCommand(install.NewCmdInstall(setupCtx, ioStreams)) + cmds.AddCommand(uninstall.NewCmd(setupCtx, ioStreams)) return cmds } diff --git a/pkg/factory/factory.go b/pkg/factory/factory.go index c9b32fb..beba3d9 100644 --- a/pkg/factory/factory.go +++ b/pkg/factory/factory.go @@ -17,8 +17,6 @@ limitations under the License. package factory import ( - "context" - "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/kubernetes" @@ -66,14 +64,14 @@ type Factory struct { // populated when the command is executed using the cobra PreRunE. If a PreRunE // is already defined, it will be executed _after_ Factory has been populated, // making it available. -func New(ctx context.Context, cmd *cobra.Command) *Factory { +func New(cmd *cobra.Command) *Factory { f := new(Factory) kubeConfigFlags := genericclioptions.NewConfigFlags(true) f.factory = util.NewFactory(kubeConfigFlags) kubeConfigFlags.AddFlags(cmd.Flags()) - cmd.RegisterFlagCompletionFunc("namespace", validArgsListNamespaces(ctx, f)) + cmd.RegisterFlagCompletionFunc("namespace", validArgsListNamespaces(f)) // Setup a PreRunE to populate the Factory. Catch the existing PreRunE command // if one was defined, and execute it second. diff --git a/pkg/factory/validargs.go b/pkg/factory/validargs.go index 86f282c..9adc578 100644 --- a/pkg/factory/validargs.go +++ b/pkg/factory/validargs.go @@ -17,15 +17,13 @@ limitations under the License. package factory import ( - "context" - "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // ValidArgsListCertificates returns a cobra ValidArgsFunction for listing Certificates. -func ValidArgsListCertificates(ctx context.Context, factory **Factory) func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { - return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { +func ValidArgsListCertificates(factory **Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { + return func(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { if len(args) > 0 { return nil, cobra.ShellCompDirectiveNoFileComp } @@ -35,7 +33,7 @@ func ValidArgsListCertificates(ctx context.Context, factory **Factory) func(_ *c return nil, cobra.ShellCompDirectiveNoFileComp } - certList, err := f.CMClient.CertmanagerV1().Certificates(f.Namespace).List(ctx, metav1.ListOptions{}) + certList, err := f.CMClient.CertmanagerV1().Certificates(f.Namespace).List(cmd.Context(), metav1.ListOptions{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -50,8 +48,8 @@ func ValidArgsListCertificates(ctx context.Context, factory **Factory) func(_ *c } // ValidArgsListSecrets returns a cobra ValidArgsFunction for listing Secrets. -func ValidArgsListSecrets(ctx context.Context, factory **Factory) func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { - return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { +func ValidArgsListSecrets(factory **Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { + return func(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { if len(args) > 0 { return nil, cobra.ShellCompDirectiveNoFileComp } @@ -61,7 +59,7 @@ func ValidArgsListSecrets(ctx context.Context, factory **Factory) func(_ *cobra. return nil, cobra.ShellCompDirectiveNoFileComp } - secretsList, err := f.KubeClient.CoreV1().Secrets(f.Namespace).List(ctx, metav1.ListOptions{}) + secretsList, err := f.KubeClient.CoreV1().Secrets(f.Namespace).List(cmd.Context(), metav1.ListOptions{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -77,8 +75,8 @@ func ValidArgsListSecrets(ctx context.Context, factory **Factory) func(_ *cobra. // ValidArgsListCertificateSigningRequests returns a cobra ValidArgsFunction for // listing CertificateSigningRequests. -func ValidArgsListCertificateSigningRequests(ctx context.Context, factory **Factory) func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { - return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { +func ValidArgsListCertificateSigningRequests(factory **Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { + return func(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { if len(args) > 0 { return nil, cobra.ShellCompDirectiveNoFileComp } @@ -88,7 +86,7 @@ func ValidArgsListCertificateSigningRequests(ctx context.Context, factory **Fact return nil, cobra.ShellCompDirectiveNoFileComp } - csrList, err := f.KubeClient.CertificatesV1().CertificateSigningRequests().List(ctx, metav1.ListOptions{}) + csrList, err := f.KubeClient.CertificatesV1().CertificateSigningRequests().List(cmd.Context(), metav1.ListOptions{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -104,8 +102,8 @@ func ValidArgsListCertificateSigningRequests(ctx context.Context, factory **Fact // ValidArgsListCertificateRequests returns a cobra ValidArgsFunction for listing // CertificateRequests. -func ValidArgsListCertificateRequests(ctx context.Context, factory **Factory) func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { - return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { +func ValidArgsListCertificateRequests(factory **Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { + return func(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { if len(args) > 0 { return nil, cobra.ShellCompDirectiveNoFileComp } @@ -113,7 +111,7 @@ func ValidArgsListCertificateRequests(ctx context.Context, factory **Factory) fu if err := f.complete(); err != nil { return nil, cobra.ShellCompDirectiveNoFileComp } - crList, err := f.CMClient.CertmanagerV1().CertificateRequests(f.Namespace).List(ctx, metav1.ListOptions{}) + crList, err := f.CMClient.CertmanagerV1().CertificateRequests(f.Namespace).List(cmd.Context(), metav1.ListOptions{}) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -127,8 +125,8 @@ func ValidArgsListCertificateRequests(ctx context.Context, factory **Factory) fu // validArgsListNamespaces returns a cobra ValidArgsFunction for listing // namespaces. -func validArgsListNamespaces(ctx context.Context, factory *Factory) func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { - return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { +func validArgsListNamespaces(factory *Factory) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { + return func(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { if len(args) > 0 { return nil, cobra.ShellCompDirectiveNoFileComp } @@ -137,7 +135,7 @@ func validArgsListNamespaces(ctx context.Context, factory *Factory) func(_ *cobr return nil, cobra.ShellCompDirectiveNoFileComp } - namespaceList, err := factory.KubeClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) + namespaceList, err := factory.KubeClient.CoreV1().Namespaces().List(cmd.Context(), metav1.ListOptions{}) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/pkg/inspect/inspect.go b/pkg/inspect/inspect.go index f2d68a8..d327fb3 100644 --- a/pkg/inspect/inspect.go +++ b/pkg/inspect/inspect.go @@ -25,14 +25,14 @@ import ( "github.com/cert-manager/cmctl/v2/pkg/inspect/secret" ) -func NewCmdInspect(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdInspect(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { cmds := &cobra.Command{ Use: "inspect", Short: "Get details on certificate related resources", Long: `Get details on certificate related resources, e.g. secrets`, } - cmds.AddCommand(secret.NewCmdInspectSecret(ctx, ioStreams)) + cmds.AddCommand(secret.NewCmdInspectSecret(setupCtx, ioStreams)) return cmds } diff --git a/pkg/inspect/secret/secret.go b/pkg/inspect/secret/secret.go index f4d3134..fae05de 100644 --- a/pkg/inspect/secret/secret.go +++ b/pkg/inspect/secret/secret.go @@ -31,7 +31,6 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/genericclioptions" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" k8sclock "k8s.io/utils/clock" @@ -105,7 +104,7 @@ func NewOptions(ioStreams genericclioptions.IOStreams) *Options { } // NewCmdInspectSecret returns a cobra command for status certificate -func NewCmdInspectSecret(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdInspectSecret(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ @@ -113,14 +112,16 @@ func NewCmdInspectSecret(ctx context.Context, ioStreams genericclioptions.IOStre Short: "Get details about a kubernetes.io/tls typed secret", Long: long, Example: example, - ValidArgsFunction: factory.ValidArgsListSecrets(ctx, &o.Factory), - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate(args)) - cmdutil.CheckErr(o.Run(ctx, args)) + ValidArgsFunction: factory.ValidArgsListSecrets(&o.Factory), + PreRunE: func(cmd *cobra.Command, args []string) error { + return o.Validate(args) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context(), args) }, } - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd } diff --git a/pkg/install/helm/settings.go b/pkg/install/helm/settings.go index 9f65c51..d7af14e 100644 --- a/pkg/install/helm/settings.go +++ b/pkg/install/helm/settings.go @@ -54,8 +54,8 @@ func (n *NormalisedEnvSettings) Setup(ctx context.Context, cmd *cobra.Command) { log := logf.FromContext(ctx) n.logger = log - n.Factory = factory.New(ctx, cmd) - n.setupEnvSettings(ctx, cmd) + n.Factory = factory.New(cmd) + n.setupEnvSettings(cmd) { // Add a PreRunE hook to initialise the action configuration. @@ -76,7 +76,7 @@ func (n *NormalisedEnvSettings) Setup(ctx context.Context, cmd *cobra.Command) { cmd.Flag("namespace").Value.Set(defaultCertManagerNamespace) } -func (n *NormalisedEnvSettings) setupEnvSettings(ctx context.Context, cmd *cobra.Command) { +func (n *NormalisedEnvSettings) setupEnvSettings(cmd *cobra.Command) { { // Create a tempoary flag set to add the EnvSettings flags to, this // can then be iterated over to copy the flags we want to the command diff --git a/pkg/install/install.go b/pkg/install/install.go index d34a2ad..d3588c1 100644 --- a/pkg/install/install.go +++ b/pkg/install/install.go @@ -75,7 +75,7 @@ pass in a file or use the '--set' flag and pass configuration from the command l `) } -func NewCmdInstall(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdInstall(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { settings := helm.NewNormalisedEnvSettings() options := &InstallOptions{ @@ -93,7 +93,7 @@ func NewCmdInstall(ctx context.Context, ioStreams genericclioptions.IOStreams) * RunE: func(cmd *cobra.Command, args []string) error { options.client.Namespace = settings.Namespace() - rel, err := options.runInstall(ctx) + rel, err := options.runInstall(cmd.Context()) if err != nil { return err } @@ -106,11 +106,9 @@ func NewCmdInstall(ctx context.Context, ioStreams genericclioptions.IOStreams) * printReleaseSummary(ioStreams.Out, rel) return nil }, - SilenceUsage: true, - SilenceErrors: true, } - settings.Setup(ctx, cmd) + settings.Setup(setupCtx, cmd) helm.AddInstallUninstallFlags(cmd.Flags(), &options.client.Timeout, &options.Wait) diff --git a/pkg/renew/renew.go b/pkg/renew/renew.go index 5f7e26f..3ca7744 100644 --- a/pkg/renew/renew.go +++ b/pkg/renew/renew.go @@ -72,17 +72,19 @@ func NewOptions(ioStreams genericclioptions.IOStreams) *Options { } // NewCmdRenew returns a cobra command for renewing Certificates -func NewCmdRenew(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdRenew(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ Use: "renew", Short: "Mark a Certificate for manual renewal", Long: long, Example: example, - ValidArgsFunction: factory.ValidArgsListCertificates(ctx, &o.Factory), - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate(cmd, args)) - cmdutil.CheckErr(o.Run(ctx, args)) + ValidArgsFunction: factory.ValidArgsListCertificates(&o.Factory), + PreRunE: func(cmd *cobra.Command, args []string) error { + return o.Validate(cmd, args) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context(), args) }, } @@ -90,7 +92,7 @@ func NewCmdRenew(ctx context.Context, ioStreams genericclioptions.IOStreams) *co cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespaces", "A", o.AllNamespaces, "If present, mark Certificates across namespaces for manual renewal. Namespace in current context is ignored even if specified with --namespace.") cmd.Flags().BoolVar(&o.All, "all", o.All, "Renew all Certificates in the given Namespace, or all namespaces with --all-namespaces enabled.") - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd } diff --git a/pkg/status/certificate/certificate.go b/pkg/status/certificate/certificate.go index 04693b4..d6912ae 100644 --- a/pkg/status/certificate/certificate.go +++ b/pkg/status/certificate/certificate.go @@ -28,7 +28,6 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/reference" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" @@ -85,7 +84,7 @@ func NewOptions(ioStreams genericclioptions.IOStreams) *Options { } // NewCmdStatusCert returns a cobra command for status certificate -func NewCmdStatusCert(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdStatusCert(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ @@ -93,14 +92,16 @@ func NewCmdStatusCert(ctx context.Context, ioStreams genericclioptions.IOStreams Short: "Get details about the current status of a cert-manager Certificate resource", Long: long, Example: example, - ValidArgsFunction: factory.ValidArgsListCertificates(ctx, &o.Factory), - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate(args)) - cmdutil.CheckErr(o.Run(ctx, args)) + ValidArgsFunction: factory.ValidArgsListCertificates(&o.Factory), + PreRunE: func(cmd *cobra.Command, args []string) error { + return o.Validate(args) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context(), args) }, } - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd } diff --git a/pkg/status/status.go b/pkg/status/status.go index a188b21..eae8de1 100644 --- a/pkg/status/status.go +++ b/pkg/status/status.go @@ -25,14 +25,14 @@ import ( "github.com/cert-manager/cmctl/v2/pkg/status/certificate" ) -func NewCmdStatus(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdStatus(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { cmds := &cobra.Command{ Use: "status", Short: "Get details on current status of cert-manager resources", Long: `Get details on current status of cert-manager resources, e.g. Certificate`, } - cmds.AddCommand(certificate.NewCmdStatusCert(ctx, ioStreams)) + cmds.AddCommand(certificate.NewCmdStatusCert(setupCtx, ioStreams)) return cmds } diff --git a/pkg/uninstall/uninstall.go b/pkg/uninstall/uninstall.go index 9dca18c..aaddcd5 100644 --- a/pkg/uninstall/uninstall.go +++ b/pkg/uninstall/uninstall.go @@ -73,7 +73,7 @@ or `) } -func NewCmd(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmd(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { settings := helm.NewNormalisedEnvSettings() options := options{ @@ -88,7 +88,7 @@ func NewCmd(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.C Short: "Uninstall cert-manager", Long: description(), RunE: func(cmd *cobra.Command, args []string) error { - res, err := run(ctx, options) + res, err := run(cmd.Context(), options) if err != nil { return err } @@ -105,11 +105,9 @@ func NewCmd(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.C fmt.Fprintf(ioStreams.Out, "release \"%s\" uninstalled\n", options.releaseName) return nil }, - SilenceUsage: true, - SilenceErrors: true, } - settings.Setup(ctx, cmd) + settings.Setup(setupCtx, cmd) helm.AddInstallUninstallFlags(cmd.Flags(), &options.client.Timeout, &options.wait) diff --git a/pkg/upgrade/migrateapiversion/command.go b/pkg/upgrade/migrateapiversion/command.go index d17c072..f2a4121 100644 --- a/pkg/upgrade/migrateapiversion/command.go +++ b/pkg/upgrade/migrateapiversion/command.go @@ -23,7 +23,6 @@ import ( apiextinstall "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/install" "k8s.io/apimachinery/pkg/runtime" "k8s.io/cli-runtime/pkg/genericclioptions" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "k8s.io/kubectl/pkg/util/i18n" "k8s.io/kubectl/pkg/util/templates" "sigs.k8s.io/controller-runtime/pkg/client" @@ -73,17 +72,22 @@ func NewOptions(ioStreams genericclioptions.IOStreams) *Options { // NewCmdMigrate returns a cobra command for updating resources in an apiserver // to force a new storage version to be used. -func NewCmdMigrate(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdMigrate(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ Use: "migrate-api-version", Short: "Migrate all existing persisted cert-manager resources to the v1 API version", Long: long, Example: example, - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate(args)) - cmdutil.CheckErr(o.Complete()) - cmdutil.CheckErr(o.Run(ctx, args)) + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := o.Validate(args); err != nil { + return err + } + + return o.Complete() + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context(), args) }, } @@ -92,7 +96,7 @@ func NewCmdMigrate(ctx context.Context, ioStreams genericclioptions.IOStreams) * "Use this mode if you have previously manually modified the 'status.storedVersions' field on CRD resources.") cmd.Flags().Float32Var(&o.qps, "qps", 5, "Indicates the maximum QPS to the apiserver from the client.") cmd.Flags().IntVar(&o.burst, "burst", 10, "Maximum burst value for queries set to the apiserver from the client.") - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd } diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index ab1be16..79ad059 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -25,14 +25,14 @@ import ( "github.com/cert-manager/cmctl/v2/pkg/upgrade/migrateapiversion" ) -func NewCmdUpgrade(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdUpgrade(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { cmds := &cobra.Command{ Use: "upgrade", Short: "Tools that assist in upgrading cert-manager", Long: `Note: this command does NOT actually upgrade cert-manager installations`, } - cmds.AddCommand(migrateapiversion.NewCmdMigrate(ctx, ioStreams)) + cmds.AddCommand(migrateapiversion.NewCmdMigrate(setupCtx, ioStreams)) return cmds } diff --git a/pkg/version/version.go b/pkg/version/version.go index 4f647d4..290d0ee 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -25,7 +25,6 @@ import ( "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/runtime" "k8s.io/cli-runtime/pkg/genericclioptions" - cmdutil "k8s.io/kubectl/pkg/cmd/util" "sigs.k8s.io/yaml" "github.com/cert-manager/cert-manager/pkg/util" @@ -92,17 +91,22 @@ or } // NewCmdVersion returns a cobra command for fetching versions -func NewCmdVersion(ctx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { +func NewCmdVersion(setupCtx context.Context, ioStreams genericclioptions.IOStreams) *cobra.Command { o := NewOptions(ioStreams) cmd := &cobra.Command{ Use: "version", Short: "Print the cert-manager CLI version and the deployed cert-manager version", Long: versionLong(), - Run: func(cmd *cobra.Command, args []string) { - cmdutil.CheckErr(o.Validate()) - cmdutil.CheckErr(o.Complete()) - cmdutil.CheckErr(o.Run(ctx)) + PreRunE: func(cmd *cobra.Command, args []string) error { + if err := o.Validate(); err != nil { + return err + } + + return o.Complete() + }, + RunE: func(cmd *cobra.Command, args []string) error { + return o.Run(cmd.Context()) }, } @@ -110,7 +114,7 @@ func NewCmdVersion(ctx context.Context, ioStreams genericclioptions.IOStreams) * cmd.Flags().BoolVar(&o.Short, "short", o.Short, "If true, print just the version number.") cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.") - o.Factory = factory.New(ctx, cmd) + o.Factory = factory.New(cmd) return cmd }