Home Make PowerShell with k8s great again
Post
Cancel

Make PowerShell with k8s great again

So, like me, you’re using Windows. WSL isn’t always reliable (e.g. VPN problems), yet you’re working with Kubernetes and require autocomplete tools. I’m the same way :)

Kubectl autocomplete

With bash or zsh, having autocomplete is straightforward. Almost only one command is required to complete the task:

1
source <(kubectl completion bash)

However, this will obviously not work in PowerShell😅. Especially when you look what and how many lines the kubectl completion bash command produces and how many lines it generates:

1
kubectl completion bash | wc -l

14143 is the correct answer! To implement autocomplete, more than 14k lines of code were written! How to use it on Windows? Is it possible to move it? Yes, and thankfully, someone else did the legwork for you: https://github.com/mziyabo/PSKubectlCompletion/blob/master/PSKubectlCompletion.psm1

To use autocomplete we will need the PSKubectlCompletion module. Install it with Install-Module PSKubectlCompletion and include with:

1
2
3
Import-Module PSKubectlCompletion
Set-Alias k -Value kubectl
Register-KubectlCompletion

At this point the basics are working quite well. But how about other tools? Like …

kubectx and kubens

If you never use the original ones these are two simple commands to switch your current Kubernetes context (kubectx) and namespace (kubens). In PowerShell world they should be named rather like Select-KubeContext and Select-KubeNamespace, but I will you aliases instead 😜

And for above someone else did the work for you once more. It’s me this time: https://github.com/ptrstpp950/PSKubeContext How it works? Just let me present a small YouTube:

Great, isn’t it? I’m so glad it works that I made and published a module called PSKubeContext

To utilize it, install it with Install-Module PSKubeContext, and simply add the following to your profile:

1
2
3
4
Import-Module PSKubeContext
Set-Alias kubens -Value Select-KubeNamespace
Set-Alias kubectx -Value Select-KubeContext
Register-PSKubeContextComplete

What’s inside:

  • Select-KubeContext or if you prefer alias kubectx
  • Select-KubeNamespace or kubens
  • 100% autocomplete using 🙈🙉🙊
  • A menu in command line to select the context/namespace if something goes wrong 😅

And that’s it. Your PowerShell is great again and works well with Kubernetes CLI.

This post is licensed under CC BY 4.0 by the author.