Skip to content

Instantly share code, notes, and snippets.

@odytrice
Last active March 24, 2023 07:24
Show Gist options
  • Save odytrice/9688c8f4b02c32fc02f4551695ea0bee to your computer and use it in GitHub Desktop.
Save odytrice/9688c8f4b02c32fc02f4551695ea0bee to your computer and use it in GitHub Desktop.
Kubectx, Kubens for Windows (Powershell)

Kubectx and Kubens

NOTE: THIS TOOL IS NOW OBSOLETE AS KUBECTX AND KUBENS ARE NOW WRITTEN IN GO

SEE THE OFFICIAL REPO FOR MORE INFO

This is a fork of KubensWin (https://github.com/thomasliddledba/kubenswin) and KubectxWin (https://github.com/thomasliddledba/kubectxwin) The only major tweak is that it drops the "win" suffix

Installation

Simply Download the following files and then add them to a folder that's been added to your Path Environment Variable.

Note: These scripts only work in Powershell

Param(
[Parameter(Mandatory=$false)]
[string]$action,
[Parameter(Mandatory=$false)]
[string]$name,
[Parameter(Mandatory=$false)]
[string]$newname
)
function usage() {
Write-Output "
Usage
kubectx : current context
kubectx ls : list the contexts
kubectx set <name> : switch to context <name>
kubectx rn <oldname> <newname> : rename context <oldname> <newname>
kubectx rm <name> : delete context <name>
kubectx help : display usage"
}
function current_context() {
kubectl config view -o=jsonpath='{.current-context}'
}
function delete_context() {
kubectl config delete-context $name
}
function rename_context() {
kubectl config rename-context $name $newname
}
function get_contexts() {
kubectl config get-contexts -o=name | Sort-Object
}
function list_contexts() {
$gctx = (get_contexts)
$cur = (current_context)
ForEach ($i in $gctx) {
if ($i -eq $cur) {
Write-Host "*" $i -ForegroundColor Red
} else {
Write-Host $i
}
}
}
function switch_context($context) {
kubectl config use-context $context
}
function main() {
switch ($action) {
set { switch_context $name }
Default { current_context }
ls { list_contexts }
rn { rename_context }
rm { delete_context }
help { usage }
}
}
main
Param(
[Parameter(Mandatory=$false)]
[string]$action,
[Parameter(Mandatory=$false)]
[string]$name
)
function usage() {
Write-Output "
Usage
kubens : current namespace
kubens ls : list the namespaces in the current context
kubens set <name> : switch to another namespace in the current context
kubens help : display usage"
}
function current_context() {
kubectl config view -o=jsonpath='{.current-context}'
}
function print_current_namespace() {
$cur = (current_context)
$nsp = kubectl config view -o=jsonpath="{.contexts[?(@.name=='$cur')].context.namespace}"
if (!$nsp) {
Write-Host 'default'
}else{
Write-Host $nsp
}
}
function set_namespace() {
$cur = (current_context)
kubectl config set-context $cur --namespace=$name
Write-Host "Active namespace is $name in context $cur"
}
function current_namespace() {
$cur = (current_context)
$nsp = kubectl config view -o=jsonpath="{.contexts[?(@.name=='$cur')].context.namespace}"
if (!$nsp) {
return 'default'
}else{
return $nsp
}
}
function list_namespaces() {
$cur = (current_namespace)
$gnsp = (get_namespaces)
ForEach ($i in $gnsp) {
if ($i -eq $cur) {
Write-Host "*" $i -ForegroundColor Red
} else {
Write-Host $i
}
}
}
function get_namespaces() {
kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{''\n''}{end}'
}
function main() {
switch ($action) {
Default {print_current_namespace}
ls {list_namespaces}
set {set_namespace}
help {usage}
}
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment