-
Notifications
You must be signed in to change notification settings - Fork 40.1k
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
Support passing boolean flags to kubectl plugins #58768
Conversation
/ok-to-test cc @fabianofranz are you a good person to review? Maybe someone else from sig/cli? |
cc @kubernetes/sig-cli-pr-reviews |
pkg/kubectl/cmd/plugin.go
Outdated
cmd.Flags().StringP(flag.Name, flag.Shorthand, flag.DefValue, flag.Desc) | ||
switch flag.Type { | ||
// When a flag type is not defined, treat it as a string flag (original behavior before type was introduced) | ||
case "", plugins.StringFlagType: |
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.
IMO, better make it as default.
pkg/kubectl/plugins/plugins.go
Outdated
return ErrInvalidFlagType | ||
} | ||
} | ||
if f.Type == BoolFlagType && f.DefValue != "" { |
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.
What if DefValue
is empty for boolean type?
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.
You're right, I'll update the patch so that it defaults to false
when not specified for boolean flags.
pkg/kubectl/plugins/plugins.go
Outdated
) | ||
|
||
// flagTypes defines the set of supported flag types. | ||
var flagTypes = map[FlagType]struct{}{BoolFlagType: {}, StringFlagType: {}} |
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 prefer to move this into function Validate
. No need to expose here.
pkg/kubectl/plugins/plugins.go
Outdated
@@ -105,6 +127,16 @@ func (f Flag) Validate() error { | |||
if strings.Contains(f.Name, " ") { | |||
return ErrInvalidFlagName | |||
} | |||
if f.Type != "" { |
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.
We should make this backwards compatible. If the flag type is missing or empty string, we should take it as string flag by default.
pkg/kubectl/plugins/plugins.go
Outdated
Desc string `json:"desc"` | ||
DefValue string `json:"defValue,omitempty"` | ||
Name string `json:"name"` | ||
Type FlagType `json:"type"` |
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.
To make it backwards compatible, this new-added field should be optional.
Should be json:"type,omitempty"
here.
Thanks for the feedback, @dixudx! I've updated the PR to incorporate your suggestions. |
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.
Only a small nit. LGTM mostly.
pkg/kubectl/plugins/plugins.go
Outdated
@@ -33,6 +34,10 @@ var ( | |||
ErrInvalidFlagName = fmt.Errorf("flag name can't contain spaces") | |||
// ErrInvalidFlagShorthand indicates flag shorthand is invalid. | |||
ErrInvalidFlagShorthand = fmt.Errorf("flag shorthand must be only one letter") | |||
// ErrInvalidFlagType indicates flag type is invalid. | |||
ErrInvalidFlagType = fmt.Errorf("invalid flag type, must be one of the following: string, bool") |
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.
Adding default to bool?
How about invalid flag type, must be one of the following: string, bool(default)
?
@carolynvs Please also squash your commits. Then lgtm. Thanks. |
Boolean flags are set to true when the flag is present and no value is specified. --flag is equivalent to --flag=true and is useful for commands such as a --version flag. To preserve backwards compatibiity with existing plugins, all flags by default are string flags, but now a new field has been introduced to the flag definition, type, which allows specifying the type of pflag to use. Currently only string and bool are supported, but this allows for other types to be added in the future. Bool flags support a default value, and when defValue is set, kubectl validates that it can be parsed as a boolean when validating the plugin. Acceptable values can be found at https://golang.org/pkg/strconv/#ParseBool.
be8883b
to
4aa04d8
Compare
@dixudx I've updated the error message to indicate that strings are the default type and squashed my commits. |
/lgtm |
/assign @deads2k |
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.
/lgtm
test failure looks unrelated /test pull-kubernetes-bazel-test |
Ping @kubernetes/sig-cli-maintainers for approval. Thanks. |
/test all Tests are more than 96 hours old. Re-running tests. |
/retest |
None of the failing tests are related to my PR. I'm not quite sure what I should do next? 🤷♀️ |
/test pull-kubernetes-e2e-gce |
I just re-start the failed test case, lets see what's the result. |
This overall looks good to me, I would like to get this merged asap. Then I will open a new issue to put things which we need to do before we can move kubectl plugin to beta. |
/assign @adohe |
Seems milestone freeze, could be get merged after the 1.10 release. |
The current plugin design led to the necessity for this pull. Before going further down the road of requiring plugins to describe all their flags to kubectl, it’s worth discussing whether that is the correct approach at all. That approach stunts plugins by requiring all their flags to be parseable by kubectl. I’d be strongly in favor of just passing through all envvars, flags and input/output streams as is and letting plugins run as if you had just invoked them directly. |
@carolynvs: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
@liggitt I could not agree more that having kubectl parse the Service Catalog's plugin flags makes things much more difficult for us, and it's not clear what is being gained. Our plugin is a binary that uses cobra, so for us it's a pointless exercise to generate the plugin yaml, use viper to rebind the envvars back to the original flags, etc. If there is a conversation about this going on elsewhere, I'd love to provide feedback! 😀 |
We're mostly busy closing down 1.10, but I think that we should reconsider how plugins for |
@adohe @carolynvs let's discuss the idea @deads2k voiced out during our next sig-cli call. That's basically the direction I'd like us take before we promote this to beta. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: carolynvs, dixudx Assign the PR to them by writing The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
c.f. sig-cli meeting info (https://github.com/kubernetes/community/tree/master/sig-cli) and agenda (https://docs.google.com/document/d/1r0YElcXt6G5mOWxwZiXgGu_X6he3F--wKwg-9UBc29I/edit#) |
Now that 1.10 is out, is this something that can be merged to help make the current plugin experience better? |
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
/remove-lifecycle stale |
Can anyone from SIG CLI comment on if the plan is to not make any further fixes to the plugin infrastructure because something else is coming down the pipeline? I'm unsure if I should just close this PR, or keep trying to get this merged. |
@carolynvs have a look at kubernetes/community#2437 and #66876 which should address all your problems. This new mechanism should unblock all the problems you were struggling with before. Ping me if you have any questions/problems. |
@soltysh This is great, thanks! Looking forward to the next release of k8s. I'm working with SIG Service Catalog to get svcat updated to work with the new model. 👍 |
What this PR does / why we need it:
This allows kubectl plugins to define boolean flags. Boolean flags are set to true when the flag is present and no value is specified. --flag is equivalent to --flag=true and is useful for commands such as
kubectl plugin svcat --version
.To preserve backwards compatibility with existing plugins, all flags by default are string flags, but now a new field has been introduced to the flag definition, type, which allows specifying the type of pflag to use. Currently only string and bool are supported, but this allows for other types to be added in the future.
Bool flags support a default value, and when defValue is set, kubectl validates that it can be parsed as a boolean when validating the plugin. Acceptable values can be found at https://golang.org/pkg/strconv/#ParseBool.
Example
Which issue(s) this PR fixes
Fixes #53640
Special notes for your reviewer:
None
Release note:
/sig cli