EN

Automagic Kubectl

Don't waste any more time trying to remember those suffixes in your terminal; with Auto Kube, you can type your commands in a highly abbreviated way.

SRE/Kubernetes Admin

Mateus Caruccio

Tired of typing long and boring kubectl commands? Can't remember the aliases created by others? Tired of creating meaningless scripts, functions, and aliases?

Your troubles are over!

The amazing Auto Kubectl has arrived! - https://github.com/caruccio/autokube

With it, you type (practically) all kubectl parameters in a super abbreviated way.

Impress your family and friends by typing this:

kgpoowlnslnhw app=web default

Instead of this:

kubectl get pod -n default -o=wide -l=app=web --namespace=default --show-labels --no-headers --watch

Just start the command with the letter k and the rest is translated into the corresponding parameters.

For example:

`k`   -> Inicia um comando autokubectl
`g`   -> Verbo `get`
`po`  -> Recurso `pod`
`ow`  -> Opção `-o wide` (`owide` também funciona)
`l`   -> Opção `-L=$1`, onde `$1` é o primeiro parâmetro após o comando `k...`
`n`   -> Opção `--namespace=$2`, onde `$2` é o segundo parâmetro após o comando `k...`
`sl`  -> Opção `--show-label`
`w`   -> Opção `--watch`

Some abbreviations have parameters that are used in the final command. For example, n requires a parameter with the namespace, and l requires one or more labels.

But best of all, you don't need to learn the order. Each abbreviation is resolved from a pre-defined table that maps one or more characters to one or more kubectl parameters. The only rule is to follow the sequence verb -> resource -> option.

The order of the parameters after the command k... respects the order in which the abbreviations appear in it. Thus, the command kgponl default app=web becomes kubectl get pods --namespace=default -l=app=web, while the command kgpoln app=web default becomes kubectl get pods -l=app=web --namespace=default. Note that n and l swap places, therefore their parameters invert their order on the command line.

Prefixes and Suffixes

In addition to the kubectl parameters, you can use prefixes and suffixes in the final command.

For example, to execute kubectl with the time command in front, use:

kgpo- time

The final command will be:

time kubectl get pods

Prefixes are defined using the character - and an optional abbreviation. The following native options already exist:

`-`   -> [parametro 1] [ comando final]
`-t`  -> time [comando final]
`-w`  -> watch -n %s -- [comando final]

The prefix -w is treated in a special way, and allows a value for its flag -n to be defined alongside it, in the format -w[N]. If not defined, the default value of [N] is 2.

kg-wpo
kg-w5po

The final commands will be:

watch -n 2 -- kubectl get pods
watch -n 5 -- kubectl get pods

Suffixes can be used in the same way, but using the abbreviation +. The native suffixes are:

`+`   -> [ comando final] [parametro-1] 
`+gr` -> [ comando final] | grep [parametro-1]

Prefixes and suffixes can be used together:

kgno-+ 'echo DRY: --[' ']--'

With the final command being:

echo DRY: --[ kubectl get nodes ]

Installation

To install it is simple. Just download the git repository and source the autokubectl.sh.

It works as a hook that executes when a command is not found by the running shell. Therefore, if the command/alias/function kgpo already exists, then this one will be executed instead of autokubectl. It is up to you to decide which commands you want to keep and which ones you want to run via autokubectl.

git clone https://github.com/caruccio/autokube
cd autokube
make install-user

The command above clones the repository to the local directory (choose one of your preference) and installs the source in your ~/.bashrc, ~/.zshrc. If neither exists, the file used will be ~/.profile.

You need to start a new shell session, or to use it immediately run source ~/.bashrc (or source ~/.zshrc).

If you wish to install it on the operating system, run the command

sudo make install

The latter only copies the necessary scripts to /usr/local/bin and /etc/profile.d.

Configuration

To create or modify abbreviations, simply add them to the files /etc/autokubectl or ~/.autokubectl. These files are read every time autokubectl is executed and take effect immediately.

verb:
    wapo: wait --for=condition=Ready pod/%s
    evpo: alpha events --for pod/%s
    pug: deprecations   ## https://kubepug.xyz/
resource:
    po: pod -n default            # substitui `pod` por `pod -n default`
    hr: helmrelease
    hrepo: helmrepository
option:
    v5: -v=5
    v6: -v=6
prefix:
    dry: "echo DRY:"
suffix:
    wcl: "--no-headers | wc -l"

Now you can use:

kgpo
kgnsv6
kevpon web-api default

With the final commands being:

kubectl get pod -n default
kubectl get namespaces -v=6
kubectl alpha events --for pod/web-api --namespace=default


Banner

Conclusion

For more details on the importance of using our tools in a more agile way, check out Marcelo Melo's article at getup.io/blog/dicas-aliases.

As you can see, there are a thousand ways to run kubectl. Invent yours!

Newsletter Getup.

Atualizações sobre Kubernetes e Software Supply Chain Security todos os meses.

Operating Kubernetes in production for more than 13 years. With Quor, this experience extends to software supply chain security as well.