Skip to content

Instantly share code, notes, and snippets.

@rkttu
Created January 18, 2020 16:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkttu/c5a59802884325503d36cd18a05c0915 to your computer and use it in GitHub Desktop.
Save rkttu/c5a59802884325503d36cd18a05c0915 to your computer and use it in GitHub Desktop.
PowerShell version of kubectx and kubens
function global:Select-KubeContext {
[CmdletBinding()]
[Alias('kubectx')]
param (
[parameter(Mandatory=$False,Position=0,ValueFromRemainingArguments=$True)]
[Object[]] $Arguments
)
begin {
if ($Arguments.Length -gt 0) {
$ctx = & kubectl.exe config get-contexts -o=name | fzf.exe -q @Arguments
} else {
$ctx = & kubectl.exe config get-contexts -o=name | fzf.exe
}
}
process {
if ($ctx -ne '') {
& kubectl.exe config use-context $ctx
}
}
}
# Example
# kubectx
# kubectx azure
function global:Select-KubeNamespace {
[CmdletBinding()]
[Alias('kubens')]
param (
[parameter(Mandatory=$False,Position=0,ValueFromRemainingArguments=$True)]
[Object[]] $Arguments
)
begin {
if ($Arguments.Length -gt 0) {
$ns = & kubectl.exe get namespace -o=name | fzf.exe -q @Arguments
} else {
$ns = & kubectl.exe get namespace -o=name | fzf.exe
}
}
process {
if ($ns -ne '') {
$ns = $ns -replace '^namespace/'
& kubectl.exe config set-context --current --namespace=$ns
}
}
}
# Example
# kubens
# kubens istio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment