2025-03-18 19:21:17 +01:00

54 lines
1.4 KiB
Fish
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function kex
set -l context (kubectl config current-context)
set -l namespace apps
set -l pod ""
set -l command ""
set -l debug false
getopts $argv | while read -l key value
switch $key
case c
set context $value
case n
set namespace $value
case d
set debug $value
case _
if test -z "$pod"
set pod $value
continue
end
if test -n "$command"
set command "$command "
end
set command "$command$value"
end
end
if $debug == true
echo "Context : $context"
echo "Namespace : $namespace"
echo "Pod basename: $pod"
echo "Command : '$command'"
end
set podname (kubectl --context="$context" --namespace="$namespace" get pod -l app="$pod",instance=app -o name | shuf | head -1 | sed 's/^pod\///')
if test -z "$podname"
echo "Pod $pod not found in cluster $contexts $namespace namespace" >/dev/stderr
return 1
end
echo "Pod found : $podname"
if test -z "$command"
kubectl --context="$context" --namespace="$namespace" exec -ti "$podname" -- bash
else
kubectl --context="$context" --namespace="$namespace" exec -ti "$podname" -- bash -c "$command"
end
end