Skip to content

Instantly share code, notes, and snippets.

@akarca
Created April 14, 2021 08:29
Show Gist options
  • Save akarca/a4b84c559923d875603db047d388e226 to your computer and use it in GitHub Desktop.
Save akarca/a4b84c559923d875603db047d388e226 to your computer and use it in GitHub Desktop.
Kubernetes Resource Optimizer Tool
#
# Usage: krecommend namespace deployname
# Collect recommendations from vpa, patch cpu and memory resource of a deployment.
#
# usage: patchrequests namespace deploy 250m 500Mi
function patchlimits
set --local pdata (kubectl get deploy -n $argv[1] $argv[2] -o json | jq '.spec.template.spec.containers' | jq '.[0].resources.limits = {"cpu": "'$argv[3]'","memory":"'$argv[4]'"}')
set --local pdata2 (echo "{\"spec\":{\"template\":{\"spec\":{\"containers\":" $pdata "}}}}")
kubectl patch deploy -n $argv[1] $argv[2] --type=merge -p=$pdata2
end
# usage: patchrequests namespace deploy 50m 100Mi
function patchrequests
set --local pdata (kubectl get deploy -n $argv[1] $argv[2] -o json | jq '.spec.template.spec.containers' | jq '.[0].resources.requests = {"cpu": "'$argv[3]'","memory":"'$argv[4]'"}')
set --local pdata2 (echo "{\"spec\":{\"template\":{\"spec\":{\"containers\":" $pdata "}}}}")
kubectl patch deploy -n $argv[1] $argv[2] --type=merge -p=$pdata2
end
function vpacurrent
set --local lower (kubectl get deploy -n $argv[1] $argv[2] -o jsonpath='{.spec.template.spec.containers[].resources.requests}')
set --local kcpu (echo $lower | jq -r ".cpu")
set --local kmem (echo $lower | jq -r ".memory")
echo -e $kcpu "\t\t" $kmem
end
function vpacurrentlimit
set --local lower (kubectl get deploy -n $argv[1] $argv[2] -o jsonpath='{.spec.template.spec.containers[].resources.limits}')
set --local kcpu (echo $lower | jq -r ".cpu")
set --local kmem (echo $lower | jq -r ".memory")
echo -e $kcpu "\t\t" $kmem
end
function vpalower
set --local lower (kubectl get vpa -n $argv[1] $argv[2] -o jsonpath='{.status.recommendation.containerRecommendations[0].lowerBound}')
set --local kcpu (echo $lower | jq -r '.cpu')
set --local kmem (echo $lower | jq -r '.memory' | awk '{ mem = $1 / 1024 / 1024; print int(mem)"M" }')
set --local kmemi (echo $lower | jq -r '.memory' | awk '{ mem = $1 / 1024 / 1024; print int(mem)"Mi" }')
echo -e $kcpu "\t\t" $kmemi "\t\t" $kmem
end
function vpaupper
set --local lower (kubectl get vpa -n $argv[1] $argv[2] -o jsonpath='{.status.recommendation.containerRecommendations[0].upperBound}')
set --local kcpu (echo $lower | jq -r '.cpu')
set --local kmem (echo $lower | jq -r '.memory' | awk '{ mem = $1 / 1024 / 1024; print int(mem)"M" }')
set --local kmemi (echo $lower | jq -r '.memory' | awk '{ mem = $1 / 1000 / 1000; print int(mem)"Mi" }')
echo -e $kcpu "\t\t" $kmemi "\t\t" $kmem
end
function vpatarget
set --local lower (kubectl get vpa -n $argv[1] $argv[2] -o jsonpath='{.status.recommendation.containerRecommendations[0].target}')
set --local kcpu (echo $lower | jq -r '.cpu')
set --local kmem (echo $lower | jq -r '.memory' | awk '{ mem = $1 / 1024 / 1024; print int(mem)"M" }')
set --local kmemi (echo $lower | jq -r '.memory' | awk '{ mem = $1 / 1000 / 1000; print int(mem)"Mi" }')
echo -e $kcpu "\t\t" $kmemi "\t\t" $kmem
end
function vpatop
set --local podname (kubectl get pods -n $argv[1] | grep -e "^"$argv[2]"-" | grep Running | awk '{print $1}')
kubectl top pods $podname --use-protocol-buffers | grep -v NAME | awk '{print $2"\t\t "$3}'
end
# usage: krecommend namespace deploy
function krecommend
echo -e "\nNamespace:\t" $argv[1] "\nDeploy:\t\t" $argv[2]
echo -e "\nNAME\t\t CPU\t\t MiB\t\t Mem"
# lower bound
set --local lower (kubectl get vpa -n $argv[1] $argv[2] -o jsonpath='{.status.recommendation.containerRecommendations[0].lowerBound}')
set --local kcpu (echo $lower | jq -r '.cpu')
set --local kmem (echo $lower | jq -r '.memory' | awk '{ mem = $1 / 1024 / 1024; print int(mem)"M" }')
set --local kmemi (echo $lower | jq -r '.memory' | awk '{ mem = $1 / 1000 / 1000; print int(mem)"Mi" }')
# upper bound
set --local upper (kubectl get vpa -n $argv[1] $argv[2] -o jsonpath='{.status.recommendation.containerRecommendations[0].upperBound}')
set --local uppercpu (echo $upper | jq -r '.cpu')
set --local uppermem (echo $upper | jq -r '.memory' | awk '{ mem = $1 / 1024 / 1024; print int(mem)"M" }')
set --local uppermemi (echo $upper | jq -r '.memory' | awk '{ mem = $1 / 1000 / 1000; print int(mem)"Mi" }')
set --local target (vpatarget $argv[1] $argv[2])
set --local current (vpacurrent $argv[1] $argv[2])
set --local currentlimits (vpacurrentlimit $argv[1] $argv[2])
echo -e "Target\t\t" $target
echo -e "Lower\t\t" (echo -e $kcpu "\t\t" $kmemi "\t\t" $kmem)
echo -e "Upper\t\t" (echo -e $uppercpu "\t\t" $uppermemi "\t\t" $uppermem)
echo -e "Top\t\t" (vpatop $argv[1] $argv[2])
echo -e "\nCurrent Reqs\t" $current
echo -e "Current Limits\t" $currentlimits "\n"
set --local ucpu (read -P "Request CPU: " -c $kcpu)
set --local umem (read -P "Request MEM: " -c $kmemi)
set --local upcpu (read -P "Limit CPU: " -c $uppercpu)
set --local upmem (read -P "Limit MEM: " -c $uppermemi)
patchlimits $argv[1] $argv[2] $upcpu $upmem
patchrequests $argv[1] $argv[2] $ucpu $umem
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment