/kubectl-alias

The missing alias command for kubectl.

Primary LanguageShellMIT LicenseMIT

kubectl-alias

GitHub release version License Github Workflow status

The missing alias command for kubectl.

Examples

# "v" for version
kubectl alias v version

kubectl v --client
# "cd" to switch namespace
kubectl alias cd 'config set-context $(kubectl config current-context) --namespace'

kubectl cd my-namespace
# Get Pod's YAML spec and open it with less
kubectl alias --no-args gpyl 'get pod -o yaml $1 | less'

kubectl gpyl my-pod
# Make kubectl awesome!
kubectl alias avada-kedavra 'delete'
kubectl alias alohomora 'exec -it $1 -- bash'

Installation

Homebrew

  1. Install Homebrew.

  2. brew install predatorray/brew/kubectl-alias

  3. Add this line to your rc file (e.g.: ~/.bashrc, ~/.zshrc).

export PATH="$PATH:$(brew --prefix kubectl-alias)/alias"

Krew

  1. Install Krew or upgrade to its latest version using kubectl krew upgrade.

  2. kubectl krew index add predatorray https://github.com/predatorray/krew-index.git

  3. kubectl krew install predatorray/alias

  4. Add this line to your rc file (e.g.: ~/.bashrc, ~/.zshrc).

export PATH="$PATH:$(kubectl alias --prefix)"

Manually

  1. Download the latest release.

  2. Unpack the kubectl-alias-*.tar.gz file and copy all the files to a directory, /usr/local/kubectl-alias for instance.

  3. Add both the bin/ and the alias/ directories to the PATH. For example, add this line to your rc file:

export PATH="$PATH:/usr/local/kubectl-alias/bin:/usr/local/kubectl-alias/alias"
  1. If it is not running on GNU-Linux, install the GNU getopt. After that, add this line export GNU_GETOPT_PREFIX="path/to/gnu-getopt" to your rc file.

Usage

Create an alias

kubectl alias ALIAS COMMAND

kubectl alias -N ALIAS COMMAND
kubectl alias --no-args ALIAS COMMAND

The -N, --no-args flag is used when the arguments should not be passed to the end of the commands. It is useful when the offset parameter is explicitly declared in the alias command. For example,

kubectl alias --no-args gpyl 'get pod -o yaml $1 | less'

If the flag is absent, by executing kubectl gpyl my-pod, the my-pod argument will also be passed to the less commnd.

# WRONG
kubectl alias gpyl 'get pod -o yaml $1 | less'
kubectl get pod -o yaml my-pod | less my-pod 

Delete an alias

kubectl alias -d ALIAS
kubectl alias --delete ALIAS

List all the alias

kubectl alias -l
kubectl alias --list

FAQ

error: unknown command "ALIAS NAME" for "kubectl"

This means that the alias/ directory is not correctly added to the PATH environment variable.

Add this line to your rc file.

export PATH="$PATH:$(brew --prefix kubectl-alias)/alias"

# Or, if installed manually.
export PATH="$PATH:${PREFIX}/alias"

After that, run kubectl plugin list to check if the aliases have been loaded successfully. If the alias is named v, the output of the plugin list will be:

The following compatible plugins are available:

/usr/local/bin/kubectl-alias
/usr/local/opt/kubectl-alias/alias/kubectl-v

Support

Please feel free to open an issue if you find any bug or have any suggestion.