Jan Matějka
Jan Matějka

Reputation: 391

How to enable autocomplete subcommnads on function that's call kubectl

Let's imagine you want to save your time writing all kubectl command: kubectl describe pods in shorter way: k d p.

So the solution is to add function to ~/.bashrc:

k() {
  cmd_kubectl="command kubectl"
  case ${1} in
    g)
      shift
      kubectl_get="${cmd_kubectl} get"
      case ${1} in
        p)
          shift
          ${kubectl_get} pods "$@"
          ;;
        d)
          shift
          ${kubectl_get} deploy "$@"
          ;;
        n)
          shift
          ${kubectl_get} ns "$@"
          ;;
        i)
          shift
          ${kubectl_get} ing "$@"
          ;;
        j)
          shift
          ${kubectl_get} job "$@"
          ;;
        *)
          ${kubectl_get} "$@"
          ;;
      esac
      ;;
    d)
      shift
      kubectl_desc="${cmd_kubectl} describe"
      case ${1} in
        p)
          shift
          ${kubectl_desc} pods "$@"
          ;;
        d)
          shift
          ${kubectl_desc} deploy "$@"
          ;;
        n)
          shift
          ${kubectl_desc} ns "$@"
          ;;
        i)
          shift
          ${kubectl_desc} ing "$@"
          ;;
        j)
          shift
          ${kubectl_desc} job "$@"
          ;;
        *)
          ${kubectl_desc} "$@"
          ;;
      esac
      ;;
    *)
      ${cmd_kubectl} "$@"
      ;;
  esac
}

But I'd like to save an effort and improve it more, so my question is:

How to enable auto-completion on this function to allow fill e.g. the name of the pod?

Usage:

k d p -> Tab -> k d p nginx (result)

I tried to think up thanks following references, but probably I'm not enough experienced Linux user/developer to compose the final solution.

Thanks in advance!

Upvotes: 2

Views: 1140

Answers (1)

matt_j
matt_j

Reputation: 4614

I also wanted to make it easier to write kubectl commands, but I solved it in a slightly different way.

I will describe this method below as I think you may find it useful.


I keep all the necessary files in the ~/.bash_completion.d directory but you can modify it depending on your needs.

$ mkdir ~/.bash_completion.d

First, I enabled kubectl autocompletion as described in the Kubernetes documentation:

kubectl completion bash > ~/.bash_completion.d/kubectl

Then I downloaded complete-alias - automagical shell alias completion:
NOTE: More information on complete-alias can be found here.

$ curl https://raw.githubusercontent.com/cykerway/complete-alias/master/complete_alias > ~/.bash_completion.d/complete_alias

Next I created the kubectl_aliases file with the aliases I want to use:

$ cat ~/.bash_completion.d/kubectl_aliases
alias kgp='kubectl get pods'
complete -F _complete_alias kgp
alias kgd='kubectl get deploy'
complete -F _complete_alias kgd
alias kgn='kubectl get ns'
complete -F _complete_alias kgn
alias kgi='kubectl get ing'
complete -F _complete_alias kgi
alias kgj='kubectl get job'
complete -F _complete_alias kgj
alias kg='kubectl get'
complete -F _complete_alias kg

Finally, we can execute commands from the files in the ~/.bash_completion.d directory and check if it works as expected:

$ source  ~/.bash_completion.d/kubectl
$ source  ~/.bash_completion.d/complete_alias 
$ source  ~/.bash_completion.d/kubectl_aliases 

After typing kgp and pressing Tab twice, we got a choice of Pods:

$ kgp 
app-1      nginx      webserver
$ kgp app-1 
NAME    READY   STATUS    RESTARTS   AGE
app-1   1/1     Running   0          5m28s

Upvotes: 5

Related Questions