Updated fzf usage and updated kube-ssh to use kubectl debug

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
This commit is contained in:
Marcus Noble 2023-01-04 23:38:25 +00:00
parent 2a59d2b677
commit c57251d4d5
Signed by: AverageMarcus
GPG Key ID: B8F2DB8A7AEBAF78
5 changed files with 32 additions and 20 deletions

View File

@ -41,3 +41,7 @@ else
echo $@
}
fi
_fzf() {
fzf --multi --ansi -i -1 --height=50% --reverse -0 --header-lines=1 --border --info=hidden
}

View File

@ -56,9 +56,9 @@ if [[ "$POD" == "" ]]; then
exit 1
)
pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
POD=$pod[1]
NAMESPACE=$pod[0]
pod=($(kubectl get pods --all-namespaces -o wide | _fzf | awk '{print $1, $2}'))
POD=${pod[1]}
NAMESPACE=${pod[0]}
fi
echo kubectl exec -it --namespace $NAMESPACE $POD $CMD

View File

@ -56,9 +56,9 @@ if [[ "$POD" == "" ]]; then
exit 1
)
pod=($(kubectl get pods --all-namespaces -owide | fzf | awk '{print $1, $2}'))
POD=$pod[1]
NAMESPACE=$pod[0]
pod=($(kubectl get pods --all-namespaces -o wide | _fzf | awk '{print $1, $2}'))
POD=${pod[1]}
NAMESPACE=${pod[0]}
fi
echo kubectl logs -f $ARGS --namespace $NAMESPACE $POD

View File

@ -50,9 +50,9 @@ if [[ "$DEPLOYMENT" == "" ]]; then
exit 1
)
deployment=($(kubectl get deployments --all-namespaces -owide | fzf | awk '{print $1, $2}'))
DEPLOYMENT=$deployment[1]
NAMESPACE=$pod[0]
deployment=($(kubectl get deployments --all-namespaces -o wide | _fzf | awk '{print $1, $2}'))
DEPLOYMENT=${deployment[1]}
NAMESPACE=${pod[0]}
fi
kubectl patch deployment -n ${NAMESPACE} ${DEPLOYMENT} -p '{"spec": { "template": { "spec": { "tolerations": [ { "operator": "Exists" } ] } } } }' 1>/dev/null

View File

@ -7,6 +7,7 @@ set -e
NAMESPACE=${NAMESPACE:-default}
POD="kube-ssh"
NODE=""
IMAGE="alpine"
print_usage() {
blue "kube-ssh - gain access to a Kubernetes host node (ssh-like for when a host doesn't have ssh)"
@ -17,8 +18,8 @@ print_usage() {
underline "Options:"
echo "-h, --help show this help text"
echo "-n, --namespace the namespace to launch the pod in"
echo "-p, --pod the name of the pod to launch (default: kube-ssh)"
echo "-N, --node the name of the node to access"
echo "-i, --image the image to launch for debugging (default: alpine)"
}
while test $# -gt 0; do
@ -28,16 +29,16 @@ while test $# -gt 0; do
NAMESPACE=$1
shift
;;
-p|--pod)
shift
POD=$1
shift
;;
-N|--node)
shift
NODE=$1
shift
;;
-i|--image)
shift
IMAGE=$1
shift
;;
-h|--help)
print_usage
exit 0
@ -49,9 +50,9 @@ while test $# -gt 0; do
done
if [[ "$NODE" == "" ]]; then
NODES=$(kubectl get nodes --no-headers -o custom-columns=name:.metadata.name)
if [ -z "$(which fzf)" ]; then
NODES=$(kubectl get nodes --no-headers -o custom-columns=name:.metadata.name)
i=0
while read -r node; do
echo "[$i] - $node"
@ -62,11 +63,18 @@ if [[ "$NODE" == "" ]]; then
IFS=$'\n' NODES=($NODES)
NODE=${NODES[$REPLY]}
else
NODE=$(echo "$NODES" | fzf)
NODES=$(kubectl get nodes)
NODE=$(echo "$NODES" | _fzf | awk '{print $1}')
fi
fi
NODE_NAME=$(kubectl get node $NODE -o template --template='{{index .metadata.labels "kubernetes.io/hostname"}}')
NODE_SELECTOR='"nodeSelector": { "kubernetes.io/hostname": "'${NODE_NAME}'" },'
SERVER_VERSION=$(kubectl version --client=false -o json 2>/dev/null | jq -r '.serverVersion.minor')
if [ ${SERVER_VERSION} -ge 22 ]; then
kubectl debug node/${NODE} -it --image ${IMAGE}
else
NODE_NAME=$(kubectl get node $NODE -o template --template='{{index .metadata.labels "kubernetes.io/hostname"}}')
NODE_SELECTOR='"nodeSelector": { "kubernetes.io/hostname": "'${NODE_NAME}'" },'
kubectl run --namespace ${NAMESPACE} $POD --rm -it --image ${IMAGE} --privileged --overrides '{"spec":{'"${NODE_SELECTOR}"'"hostPID": true}}' --command nsenter -- --mount=/proc/1/ns/mnt -- /bin/bash
fi
kubectl run --namespace ${NAMESPACE} $POD --rm -it --image alpine --privileged --overrides '{"spec":{'"${NODE_SELECTOR}"'"hostPID": true}}' --command nsenter -- --mount=/proc/1/ns/mnt -- /bin/bash