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

Adds the PowerShell completion generation #103758

Merged
merged 5 commits into from Aug 26, 2021

Conversation

zikhan
Copy link
Contributor

@zikhan zikhan commented Jul 19, 2021

What type of PR is this?

/kind feature

What this PR does / why we need it:

This PR adds PowerShell completion generation via the Cobra package. This is a quality-of-life improvement for users of kubectl in PowerShell.

Which issue(s) this PR fixes:

Fixes kubernetes/kubectl#613

Special notes for your reviewer:

#92989 modifies the same file. If that PR merges first, I'll rebase my changes.

Does this PR introduce a user-facing change?

Added PowerShell completion generation by running `kubectl completion powershell`

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

No Docs yet, however, I will open a separate PR on the kubernetes/website repo to update the Install and Set Up kubectl on Windows docs to reflect the new PowerShell completion feature.

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jul 19, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @zikhan!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 19, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @zikhan. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. label Jul 19, 2021
Copy link
Contributor

@yangjunmyfm192085 yangjunmyfm192085 left a comment

Choose a reason for hiding this comment

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

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 19, 2021
@zikhan
Copy link
Contributor Author

zikhan commented Jul 19, 2021

The integration failure looks to be a flaky test. I'm thinking it's related to #103512. Someone should correct me if I'm wrong. Just gonna rerun and see if it magically doesn't timeout.

/retest

@zikhan
Copy link
Contributor Author

zikhan commented Jul 22, 2021

/retest

return err
}

return kubectl.GenPowerShellCompletion(out)
Copy link
Member

Choose a reason for hiding this comment

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

If you want to have descriptions for the completion choices, likes zsh has, you can instead use GenPowerShellCompletionWithDesc().

See https://github.com/spf13/cobra/blob/master/shell_completions.md#powershell-completions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comment. I don't typically use the "Menu Complete" completion to see those descriptions, but I'm sure other people will appreciate it. I went ahead and updated the method.

@zikhan
Copy link
Contributor Author

zikhan commented Jul 25, 2021

/retest

Copy link
Member

@knight42 knight42 left a comment

Choose a reason for hiding this comment

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

/lgtm

Thanks!

kubectl completion zsh > "${fpath[1]}/_kubectl"

# Load the kubectl completion code for powershell into the current shell
kubectl completion powershell | Out-String | Invoke-Expression`))
Copy link
Member

Choose a reason for hiding this comment

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

nit: Could you make the indentation depth consistent with the above text?

Copy link
Member

Choose a reason for hiding this comment

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

just out of curiosity, could we save the completion code to a dedicated file so that powershell could load the code on startup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This just came from cobra's example here: https://github.com/spf13/cobra/blob/master/shell_completions.md#creating-your-own-completion-command.
You can save the completion string to a .ps1 file, but you still need to run it from the $PROFILE. I've typically just shoved the command into my $PROFILE to make the mobilty a bit easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Corrected the indentation.
I also added some more options for auto-loading the powershell completion code on startup.

Copy link
Member

Choose a reason for hiding this comment

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

Great!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 10, 2021
@knight42
Copy link
Member

/priority important-longterm
/triage accepted
/assign @eddiezane

@k8s-ci-robot k8s-ci-robot added priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. triage/accepted Indicates an issue or PR is ready to be actively worked on. labels Aug 10, 2021
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 25, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: eddiezane, knight42, zikhan

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

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 25, 2021
@eddiezane
Copy link
Member

/label tide/merge-method-squash

@k8s-ci-robot k8s-ci-robot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Aug 25, 2021
@zikhan
Copy link
Contributor Author

zikhan commented Aug 25, 2021

/retest

@knight42
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 26, 2021
@knight42
Copy link
Member

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 26, 2021
@k8s-ci-robot k8s-ci-robot merged commit dec8528 into kubernetes:master Aug 26, 2021
SIG CLI automation moved this from Needs Approver to Done Aug 26, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Aug 26, 2021
ibabou pushed a commit to ibabou/kubernetes that referenced this pull request Sep 28, 2021
* Adds the powershell completion generation

* Fixes formatting based on verification script

* Changes generation to include descriptions

* Adjusts formatting and Adds startup information

* Fix build
@micahmo
Copy link

micahmo commented Jan 5, 2022

Hi all,

Maybe this would be better served as a new issue, but I was wondering how this can be used with PowerShell aliases.

I've downloaded kubectl v1.23 and run kubectl completion powershell | Out-String | Invoke-Expression. Autocomplete works great for kubectl but does not work for any aliases. For example, I am setting the following alias in my PowerShell profile.

sal k kubectl

I see on this documentation page that you can add autocomplete aliases in Bash with complete -F __start_kubectl k, but that is a Bash-specific command.

P.S. This third-party PowerShell module does handle aliases correctly: https://github.com/mziyabo/PSKubectlCompletion

Thanks for any help!

@zikhan
Copy link
Contributor Author

zikhan commented Jan 7, 2022

@micahmo Looks like the cobra library is not registering the alias's for the command in the generated script, that can be found here: https://github.com/mziyabo/PSKubectlCompletion/blob/f6f28153a616e14789a3a7fbcb493ff7f1f1ae85/PSKubectlCompletion.psm1#L16-L20

    $aliases = (get-alias).Where( { $_.Definition -in $rootCommands }).Name;
    if ($aliases) {
        $rootCommands += $aliases
    }
    Register-ArgumentCompleter -CommandName $rootCommands -ScriptBlock $ArgumentCompleter -Native

There is a bit of a workaround that I tried. If you take the output from the kubectl completion powershell and change the CommandName input to include the alias. It may worth creating an issue/PR on https://github.com/spf13/cobra to include it.

I was able to get the workaround to a single line command that you can use.

(kubectl completion powershell | Out-String) -replace "CommandName 'kubectl'","CommandName 'kubectl','k'" | iex

@micahmo
Copy link

micahmo commented Jan 7, 2022

Thanks for the response @zikhan! Your workaround works perfectly for me.

Do you think it would be worth adding it to the documentation? There's already precedent for documenting additional alias-related commands, and this isn't that much more hacky than in Bash. :-)

@marckhouzam
Copy link
Member

@zikhan Should supporting aliases for powershell take a similar approach to what kubectl documents for bash aliasing and have the user run the following command?

Register-ArgumentCompleter -CommandName <myalias> -ScriptBlock $CobraScriptBlock

If that is the right solution for powershell, then Cobra needs to put the script block in a variable, like you have done in your scripts. I can take care of that if that is the proper solution (I'm not familiar with powershell much).

I like how you automated detecting aliases. However I worry it may not always work and that it would be simpler to teach the user what to do. For example, sal k /tmp/kubectl is not detected by using .Definition on get-alias.

@zikhan
Copy link
Contributor Author

zikhan commented Jan 12, 2022

I haven't had much time to play around get-alias to clean up the scripts, but I did notice it allowed for wildcard ala * in the -Description parameter. I'm also not sure about PowerShell version compatibility with wildcard description search. I was hoping to produce a less specific script for updating the docs so it could be a bit more usable for people who may have multiple aliases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. 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/M Denotes a PR that changes 30-99 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Development

Successfully merging this pull request may close these issues.

kubectl powershell completion
7 participants