Skip to content
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

Closed

Conversation

carolynvs
Copy link

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

name: "svcat"
shortDesc: "Service Catalog CLI"
command: "./svcat"
flags:
  - name: version
    desc: Display the version of the plugin
    type: bool

Which issue(s) this PR fixes
Fixes #53640

Special notes for your reviewer:
None

Release note:

Support passing boolean flags to kubectl plugins

/sig cli

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jan 24, 2018
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jan 24, 2018
@ericchiang
Copy link
Contributor

/ok-to-test

cc @fabianofranz are you a good person to review? Maybe someone else from sig/cli?

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jan 24, 2018
@ericchiang
Copy link
Contributor

cc @kubernetes/sig-cli-pr-reviews

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:
Copy link
Member

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.

return ErrInvalidFlagType
}
}
if f.Type == BoolFlagType && f.DefValue != "" {
Copy link
Member

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?

Copy link
Author

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.

)

// flagTypes defines the set of supported flag types.
var flagTypes = map[FlagType]struct{}{BoolFlagType: {}, StringFlagType: {}}
Copy link
Member

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.

@@ -105,6 +127,16 @@ func (f Flag) Validate() error {
if strings.Contains(f.Name, " ") {
return ErrInvalidFlagName
}
if f.Type != "" {
Copy link
Member

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.

Desc string `json:"desc"`
DefValue string `json:"defValue,omitempty"`
Name string `json:"name"`
Type FlagType `json:"type"`
Copy link
Member

@dixudx dixudx Jan 30, 2018

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.

@carolynvs
Copy link
Author

Thanks for the feedback, @dixudx! I've updated the PR to incorporate your suggestions.

Copy link
Member

@dixudx dixudx left a 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.

@@ -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")
Copy link
Member

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)?

@dixudx
Copy link
Member

dixudx commented Feb 6, 2018

@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.
@carolynvs carolynvs force-pushed the kubectl-plugin-bool-flag branch from be8883b to 4aa04d8 Compare February 6, 2018 15:36
@carolynvs
Copy link
Author

carolynvs commented Feb 6, 2018

@dixudx I've updated the error message to indicate that strings are the default type and squashed my commits.

@dixudx
Copy link
Member

dixudx commented Feb 6, 2018

/lgtm
@carolynvs Good job! Thanks for your work.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 6, 2018
@carolynvs
Copy link
Author

/assign @deads2k

Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@ericchiang
Copy link
Contributor

test failure looks unrelated

/test pull-kubernetes-bazel-test

@dixudx
Copy link
Member

dixudx commented Feb 13, 2018

Ping @kubernetes/sig-cli-maintainers for approval. Thanks.

@k8s-github-robot
Copy link

/test all

Tests are more than 96 hours old. Re-running tests.

@carolynvs
Copy link
Author

/retest

@thockin
Copy link
Member

thockin commented Mar 5, 2018

This change is Reviewable

@carolynvs
Copy link
Author

None of the failing tests are related to my PR. I'm not quite sure what I should do next? 🤷‍♀️

@adohe-zz
Copy link

/test pull-kubernetes-e2e-gce

@adohe-zz
Copy link

None of the failing tests are related to my PR. I'm not quite sure what I should do next?

I just re-start the failed test case, lets see what's the result.

@adohe-zz
Copy link

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.

@adohe-zz
Copy link

/assign @adohe

@adohe-zz
Copy link

Seems milestone freeze, could be get merged after the 1.10 release.

@liggitt
Copy link
Member

liggitt commented Mar 10, 2018

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.

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.

@k8s-ci-robot
Copy link
Contributor

@carolynvs: The following test failed, say /retest to rerun them all:

Test name Commit Details Rerun command
pull-kubernetes-e2e-gce 4aa04d8 link /test pull-kubernetes-e2e-gce

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.

@carolynvs
Copy link
Author

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.

@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! 😀

@deads2k
Copy link
Contributor

deads2k commented Mar 12, 2018

@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 kubectl work. Straight pass-through with execve avoids unnecessary meta, allows easier testing, and makes the plugged in binaries make sense both with and without kubectl.

@soltysh

@soltysh
Copy link
Contributor

soltysh commented Mar 12, 2018

@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.
/lgtm cancel

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 12, 2018
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: carolynvs, dixudx
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: adohe

Assign the PR to them by writing /assign @adohe in a comment when ready.

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@liggitt
Copy link
Member

liggitt commented Mar 12, 2018

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#)

@carolynvs
Copy link
Author

Now that 1.10 is out, is this something that can be merged to help make the current plugin experience better?

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 26, 2018
@carolynvs
Copy link
Author

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 26, 2018
@carolynvs
Copy link
Author

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.

@soltysh
Copy link
Contributor

soltysh commented Aug 24, 2018

@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.
/close

@carolynvs carolynvs deleted the kubectl-plugin-bool-flag branch August 24, 2018 15:59
@carolynvs
Copy link
Author

@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. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Not complete support for flags of Boolean type in kubectl plugins