Kubernetes Context & Namespace Switcher For PowerShell, Works Like A Charm
Published in
2 min readMar 9, 2020
If you are stick with Kubernetes every day, you may know or consider shorthand expression of the kubectl.
I’m using the alias K
, kubectx
and kubens
every day. But these scripts are not designed for PowerShell. So I tried to make the own version of each shorthand convention. Naturally, you can use below snippets to your other environments, including Windows.
Requirements
These modules require the PSFzf module and fzf binary for your operating system.
- You can install the fzf binary. Please refer https://github.com/junegunn/fzf#installation
- Then you can install PSfzf module with
Install-Module -Scope CurrentUser PSFzf
command. - Also, the kubectl binary is required. Please refer https://kubernetes.io/docs/tasks/tools/install-kubectl/
The “K” Alias
Set-Alias k kubectl
The “Select-KubeContext” (a.k.a. kubectx)
function global:Select-KubeContext {
[CmdletBinding()]
[Alias('kubectx')]
param (
[parameter(Mandatory=$False,Position=0,ValueFromRemainingArguments=$True)]
[Object[]] $Arguments
)
begin {
if ($Arguments.Length -gt 0) {…