Skip to content

Commit

Permalink
Merge pull request #6706 from inteon/release-1.14-cli-flags-bugfix
Browse files Browse the repository at this point in the history
[release-1.14] CLI flags parsing bugfix
  • Loading branch information
jetstack-bot committed Feb 2, 2024
2 parents f5bae17 + 241e64f commit c7b1e30
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
32 changes: 18 additions & 14 deletions cmd/ctl/pkg/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ import (
cmclient "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
)

var (
kubeConfigFlags = genericclioptions.NewConfigFlags(true)
factory = util.NewFactory(kubeConfigFlags)
)

// Factory provides a set of clients and configurations to authenticate and
// access a target Kubernetes cluster. Factory will ensure that its fields are
// populated and valid during command execution.
type Factory struct {
factory util.Factory

// Namespace is the namespace that the user has requested with the
// "--namespace" / "-n" flag. Defaults to "default" if the flag was not
// provided.
Expand Down Expand Up @@ -72,17 +69,24 @@ type Factory struct {
func New(ctx context.Context, 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))

// Setup a PreRun to populate the Factory. Catch the existing PreRun command
// Setup a PreRunE to populate the Factory. Catch the existing PreRunE command
// if one was defined, and execute it second.
existingPreRun := cmd.PreRun
cmd.PreRun = func(cmd *cobra.Command, args []string) {
util.CheckErr(f.complete())
if existingPreRun != nil {
existingPreRun(cmd, args)
existingPreRunE := cmd.PreRunE
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if err := f.complete(); err != nil {
return err
}

if existingPreRunE != nil {
return existingPreRunE(cmd, args)
}
return nil
}

return f
Expand All @@ -93,12 +97,12 @@ func New(ctx context.Context, cmd *cobra.Command) *Factory {
func (f *Factory) complete() error {
var err error

f.Namespace, f.EnforceNamespace, err = factory.ToRawKubeConfigLoader().Namespace()
f.Namespace, f.EnforceNamespace, err = f.factory.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

f.RESTConfig, err = factory.ToRESTConfig()
f.RESTConfig, err = f.factory.ToRESTConfig()
if err != nil {
return err
}
Expand All @@ -113,7 +117,7 @@ func (f *Factory) complete() error {
return err
}

f.RESTClientGetter = factory
f.RESTClientGetter = f.factory

return nil
}
23 changes: 11 additions & 12 deletions cmd/ctl/pkg/install/helm/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ func (n *NormalisedEnvSettings) Setup(ctx context.Context, cmd *cobra.Command) {
// Add a PreRunE hook to initialise the action configuration.
existingPreRunE := cmd.PreRunE
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if err := n.InitActionConfiguration(); err != nil {
return err
}

if existingPreRunE != nil {
return existingPreRunE(cmd, args)
if err := existingPreRunE(cmd, args); err != nil {
return err
}
}

return nil
return n.InitActionConfiguration()
}
}

Expand All @@ -95,25 +93,26 @@ func (n *NormalisedEnvSettings) setupEnvSettings(ctx context.Context, cmd *cobra
}

{
// Add a PreRun hook to set the debug value to true if the log level is
// Add a PreRunE hook to set the debug value to true if the log level is
// >= 3.
existingPreRun := cmd.PreRun
cmd.PreRun = func(cmd *cobra.Command, args []string) {
existingPreRunE := cmd.PreRunE
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if n.logger.V(debugLogLevel).Enabled() {
n.EnvSettings.Debug = true
}

if existingPreRun != nil {
existingPreRun(cmd, args)
if existingPreRunE != nil {
return existingPreRunE(cmd, args)
}
return nil
}
}
}

func (n *NormalisedEnvSettings) InitActionConfiguration() error {
return n.ActionConfiguration.Init(
n.Factory.RESTClientGetter,
n.EnvSettings.Namespace(),
n.Factory.Namespace,
os.Getenv("HELM_DRIVER"),
func(format string, v ...interface{}) {
n.logger.Info(fmt.Sprintf(format, v...))
Expand Down

0 comments on commit c7b1e30

Please sign in to comment.