From c57251d4d5df2a971d2f0e4a178285a1485247f9 Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Wed, 4 Jan 2023 23:38:25 +0000 Subject: [PATCH] Updated fzf usage and updated kube-ssh to use kubectl debug Signed-off-by: Marcus Noble --- home/.bin/.utils | 4 ++++ home/.bin/kube-exec | 6 +++--- home/.bin/kube-logs | 6 +++--- home/.bin/kube-schedule-anywhere | 6 +++--- home/.bin/kube-ssh | 30 +++++++++++++++++++----------- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/home/.bin/.utils b/home/.bin/.utils index bf19c57..a4942ec 100644 --- a/home/.bin/.utils +++ b/home/.bin/.utils @@ -41,3 +41,7 @@ else echo $@ } fi + +_fzf() { + fzf --multi --ansi -i -1 --height=50% --reverse -0 --header-lines=1 --border --info=hidden +} diff --git a/home/.bin/kube-exec b/home/.bin/kube-exec index d078695..d6233a8 100755 --- a/home/.bin/kube-exec +++ b/home/.bin/kube-exec @@ -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 diff --git a/home/.bin/kube-logs b/home/.bin/kube-logs index eccbceb..c7f088b 100755 --- a/home/.bin/kube-logs +++ b/home/.bin/kube-logs @@ -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 diff --git a/home/.bin/kube-schedule-anywhere b/home/.bin/kube-schedule-anywhere index 6c71b88..4da6a53 100755 --- a/home/.bin/kube-schedule-anywhere +++ b/home/.bin/kube-schedule-anywhere @@ -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 diff --git a/home/.bin/kube-ssh b/home/.bin/kube-ssh index 83fcead..3588365 100755 --- a/home/.bin/kube-ssh +++ b/home/.bin/kube-ssh @@ -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