Search code examples
kubectlk3sbash-completioncloudnative-pg

How to activate bash-completion for cnpg's kubectl plugin when running on a local k3s node?


cnpg completion is designed to work when running like this:

. <( kubectl cnpg completion bash )

however, when running on a k3s node, the proper way to run kubectl is either:

k3s kubectl ...

or, make the /etc/rancher/k3s/k3s.yaml readable (or copy to the user ~/.kube/config ), and export KUBECONFIG with the path.

When I try either, followed by one of the following options, it runs, but doesn't do anything when hitting tabtab.

Nov 15th, 2023 Update

Following coment from Grisha Levit (thanks!)

An old issue, no longer relevant in v1.21.1

I noticed there is a kubectl-cnpg command on the path, but running the following end up with an error:

$ . <( kubectl-cnpg completion bash )
Error: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused

Still relevant in v1.21.1

After loading cnpg-completion with:

. <( kubectl cnpg completion bash )
# or
. <( kubectl-cnpg completoin bash )

Only kubectl-cnpg tabtab works, (only with commands and params, not with cluster names - although I don't know if it ever worked that way).

It does not work with kubectl cnpg tabtab

Any idea if this the extent of the completion?


Solution

  • The auto-completion of clusters name it's not something implemented on the plugin yet, if you like to have that feature please open a feature request on the GitHub Issue here https://github.com/cloudnative-pg/cloudnative-pg/issues

    Ok the autocomplete is there and has always been there, the only thing that you need is to add the following script named kubectl_complete-cnpg:

    #!/usr/bin/env sh
    
    # Call the __complete command passing it all arguments
    kubectl cnpg __complete "$@"
    

    In your PATH and make it executable, that was added here https://github.com/kubernetes/kubernetes/pull/105867

    So that's the first part of the right answer, the second part will be answered by this PR https://github.com/cloudnative-pg/cloudnative-pg/pull/3875

    Hopefully that will help you