-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUGFIX: re-enable install test and fix flags which were not parsed correctly #14
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -66,23 +63,32 @@ type Factory struct { | |
|
||
// New returns a new Factory. The supplied command will have flags registered | ||
// for interacting with the Kubernetes access options. Factory will be | ||
// populated when the command is executed using the cobra PreRun. If a PreRun | ||
// 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 { | ||
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) | ||
// WARNING: Do not set PreRun on the command as cobra will not execute PreRun when | ||
// PreRunE is set. | ||
Comment on lines
+80
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only figured this out after reading their source-code. |
||
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 | ||
|
@@ -93,12 +99,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 | ||
} | ||
|
@@ -113,7 +119,7 @@ func (f *Factory) complete() error { | |
return err | ||
} | ||
|
||
f.RESTClientGetter = factory | ||
f.RESTClientGetter = f.factory | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,6 @@ func NewNormalisedEnvSettings() *NormalisedEnvSettings { | |
ActionConfiguration: &action.Configuration{}, | ||
} | ||
} | ||
|
||
func (n *NormalisedEnvSettings) Namespace() string { | ||
return n.Factory.Namespace | ||
} | ||
|
@@ -97,23 +96,24 @@ 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 | ||
// >= 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The n.Factory.Namespace seems like a better source-of-truth to get the namespace. |
||
os.Getenv("HELM_DRIVER"), | ||
func(format string, v ...interface{}) { | ||
n.logger.Info(fmt.Sprintf(format, v...)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made these global variables into local variables, so the test can parse flags multiple times without issues.