Last active
November 17, 2023 11:00
-
-
Save reschex/5701e6041b99079372e4ec3a7cd56406 to your computer and use it in GitHub Desktop.
powershell profile functions for working with kubernetes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for all of this to work, you need FZF! https://github.com/junegunn/fzf | |
New-Alias -Name k -Value kubectl -force | |
kubectl completion powershell | Out-Null | |
# enable colours | |
$ESC=[char]27 | |
$RED="$ESC[31m" | |
$GREEN="$ESC[32m" | |
$YELLOW="$ESC[33m" | |
$BLUE="$ESC[34m" | |
$MAGENTA="$ESC[35m" | |
$CYAN="$ESC[36m" | |
$END="$ESC[0m" | |
# enable special text formating | |
$BOLD="$ESC[1m" | |
$LOW="$ESC[2m" | |
$UNDERLINE="$ESC[4m" | |
$INVERT="$ESC[7m" | |
$PASSWORD="$ESC[8m" | |
$BLINK="$ESC[5m" | |
$NORMAL="$ESC[0m" | |
function base64 { | |
param ( | |
[Parameter(Mandatory = $false)] | |
[switch]$d, | |
[Parameter(Mandatory = $false)] | |
[string]$value, | |
[Parameter(ValueFromPipeline=$true)] | |
[string[]]$in | |
) | |
$value = $in ? $in : $value | |
if (Test-Path -Path $value -PathType Leaf) { | |
$value = Get-Content $value | |
} | |
switch ($d) { | |
$true { | |
return [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($value)) | |
} | |
Default { | |
return [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($value)) | |
} | |
} | |
} | |
function Get-KubePS1 { | |
if ($env:KUBEPS1 -eq 1) { | |
if (!$env:KUBECONTEXT -or !$env:KUBENS) { | |
$env:KUBECONTEXT = kubectl config current-context | |
$env:KUBENS = kubectl config view --minify -o jsonpath='{..namespace}' | |
} | |
return '(' + $BLUE + [char]0x2388 + $RED + " $env:KUBECONTEXT" + $END + ':' + $CYAN + "$env:KUBENS" + $END + ') ' | |
} else { | |
return '' | |
} | |
} | |
# turn kubeps1 prompt on or off | |
function kps { | |
if ($env:KUBEPS1 -eq 1) { | |
$env:KUBEPS1 = 0 | |
} else { | |
$env:KUBEPS1 = 1 | |
} | |
} | |
$env:KUBEPS1 = 1 | |
# load all kubeconfigs into KUBECONFIG env var, so I don't have to maintain one big kubeconfig file | |
function Get-kubeconfigs { | |
# you need to create this dir and put your kubeconfig files into it | |
$kubeConfigDir = "$HOME/.kube/configs" | |
$env:KUBECONFIG = "" | |
foreach ($config in (Get-ChildItem $kubeConfigDir)) { | |
$env:KUBECONFIG += ";$config" | |
} | |
} | |
Get-kubeconfigs | |
# this is my short version of the official ktx | |
# the official ktx doesn't support multiple kubeconfigs | |
# in the KUBECONFIG env var - hence I've created my own | |
function ktx { | |
$context = kubectl config get-contexts -o name | fzf | |
kubectl config use-context $context | |
$env:KUBECONTEXT = kubectl config current-context | |
$env:KUBENS = kubectl config view --minify -o jsonpath='{..namespace}' | |
} | |
# as above, my short, hacky kubens alternative | |
function ksn { | |
param ( | |
[Parameter(Mandatory = $false)] | |
[string]$ns | |
) | |
if (!$ns) { | |
$ns = kubectl get namespace -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}' | fzf | |
} | |
kubectl config set-context --current --namespace=$ns | |
$env:KUBENS = $ns | |
} | |
# helper function | |
# to enable selecting and pre-filtering of resource names | |
function Get-K8sResource { | |
param ( | |
[Parameter(Mandatory = $false)] | |
$preFilter, | |
[Parameter(Mandatory = $true)] | |
$type | |
) | |
if ($preFilter) { | |
return ([string[]](kubectl get $type --no-headers -o custom-columns=:.metadata.name | Select-String $preFilter ) | fzf -1 --header="Select $($type):" ) | |
} else { | |
return ((kubectl get $type --no-headers -o wide | fzf --header="Select $($type):") -split "\s+")[0] | |
} | |
} | |
# helper function | |
# to execute certain commands against a container inside a pod | |
# returns straight away if only one container exists | |
function Get-ContainerName { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$pod | |
) | |
return (kubectl get pod $pod -o jsonpath='{range .spec.containers[*]}{@.name}{"\n"}{end}' | fzf -1 --header="Select container:") | |
} | |
# quick shell into a running pod/container | |
function kssh { | |
param ( | |
[Parameter(Mandatory = $false)] | |
[string]$preFilter | |
) | |
$pod = Get-K8sResource $preFilter -type "pod" | |
$container = Get-ContainerName $pod | |
Write-Host "kubectl exec -it $pod -c $container -- /bin/bash || /bin/sh || /bin/ash/" | |
kubectl exec -it $pod -c $container -- /bin/bash || /bin/sh || /bin/ash/ | |
} | |
# quick logs from a running pod/container | |
function kl { | |
param ( | |
[Parameter(Mandatory = $false)] | |
[string]$preFilter | |
) | |
$pod = Get-K8sResource $preFilter -type "pod" | |
$container = Get-ContainerName $pod | |
Write-Host "$RED>>$YELLOW $pod $RED>$GREEN $container$END" | |
kubectl logs $pod -c $container | |
} | |
# quickly get secrets and their decoded values | |
function ks { | |
param ( | |
[Parameter(Mandatory = $false)] | |
[string]$preFilter, | |
[Parameter(Mandatory = $false)] | |
[switch]$all | |
) | |
$secret = Get-K8sResource $preFilter -type "secret" | |
Write-Host $MAGENTA"> "$secret | |
$secret_obj = (kubectl get secret $secret -o json | ConvertFrom-Json) | |
if ($all) { | |
foreach ($item in $secret_obj.data.PSObject.Properties) { | |
Write-Host $YELLOW">> "$($item.Name)": "$RED$(base64 -d $item.Value)$END | |
} | |
} else { | |
$data = ($secret_obj.data.PSObject.Properties.Name | fzf -1 --header="Select Data to decode:") | |
Write-Host $YELLOW">> "$($data)": "$RED$(base64 -d $secret_obj.data.$data) | |
} | |
} | |
# quickly describe a k8s resource | |
function kd { | |
param ( | |
[Parameter(Mandatory = $false)] | |
[string]$type, | |
[Parameter(Mandatory = $false)] | |
[string]$preFilter | |
) | |
if ($type) { | |
$resourceType = $type | |
} else { | |
$resourceType = (kubectl api-resources -o name | fzf --header="Select resource type:") | |
} | |
$resource = Get-K8sResource -preFilter $preFilter -type $resourceType | |
kubectl describe $resourceType $resource | |
} | |
# quick method to drain nodes, use carefully! | |
function kdn { | |
$node = Get-K8sResource -type "node" | |
k drain $node --delete-emptydir-data --ignore-daemonsets --force | |
} | |
# number of pods on a node | |
function knp { | |
$node = Get-K8sResource -type "node" | |
Write-Host $node | |
k describe node $node | findstr "Non-terminated Pods:" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment