Last active
September 12, 2024 10:59
-
-
Save zcag/077ab5e2600b03057d167a55c53d83f8 to your computer and use it in GitHub Desktop.
kubehelpers
This file contains hidden or 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
#!/bin/zsh | |
# requires kubectl fzf | |
# Usage | |
# source kubehelpers.sh | |
# list kb pods belonging to current user | |
function kblist() { | |
kubectl get pod -l "user=$USER" | |
} | |
# usage: kbpick; # select pod you want to work on in fzf | |
function kbpick() { | |
selection=$(kubectl get pod -l "user=$USER" | fzf --header-lines=1 -1 -0) | |
[[ -n $selection ]] || { echo "Exiting without action.. USER=$USER"; return} | |
podname=$(echo $selection | cut -d' ' -f1) | |
echo "Setting \$CWPOD to $podname" | |
export CWPOD=$(echo $podname) | |
} | |
# usage: kbread /tmp/remote_file | |
function kbread() { | |
[[ -n $1 ]] || { echo "error: remote path missing"; return} | |
tmpfile=$(mktemp) | |
kubectl cp "$CWPOD:$1" $tmpfile | |
cat $tmpfile | grep -v 'tar: Removing leading' | |
} | |
# usage: echo "any stdout" | kbsend /tmp/remote_file | |
# usage: kbsend /tmp/remote_file ../local/file | |
function kbsend() { | |
[[ -n $1 ]] || { echo "error: remote path missing"; return} | |
echo "Working on pod/$CWPOD" | |
remote_path="$CWPOD:$1" | |
if [[ ! -t 0 ]]; then | |
tmpfile=$(mktemp) | |
cat - > $tmpfile | |
kubectl cp $tmpfile "$remote_path" | |
else | |
[[ -n $2 ]] || { echo "error: local file missing"; return} | |
kubectl cp "$2" "$remote_path" | |
fi | |
} | |
# usage: kbrb file | |
# then paste clipboard on remote rails to load | |
function kbrb() { | |
[[ -n $CWPOD ]] || { echo "error: do kbpick"; return} | |
echo "Working on pod/$CWPOD" | |
kubectl cp $1 "$CWPOD:/app/scripts/fromlocal.rb" | |
console_line="load Rails.root.join('scripts/fromlocal.rb')" | |
echo $console_line | clipcopy | |
echo "Use following to load the file: \n" | |
echo $console_line | |
} | |
# usage: echo "1\n2\n98798\n276" | kbids | |
# then paste clipboard on remote rails to load ids | |
function kbids() { | |
[[ -n $CWPOD ]] || { echo "error: do kbpick"; return} | |
[[ -t 0 ]] && { echo "error: you need to pipe ids in"; return} | |
echo "Working on pod/$CWPOD" | |
tmpfile=$(mktemp) | |
cat - > $tmpfile | |
grep -q '\D' $tmpfile && { echo "error: alpha chars from pipe"; return} | |
count=$(wc -l < $tmpfile | tr -d ' ') | |
kubectl cp $tmpfile "$CWPOD:/tmp/ids" | |
console_line='ids = File.read("/tmp/ids").split("\\n").map(&:to_i); ids.count' | |
console_line+="\nids.count == $count " | |
console_line+="&& 'Loaded $count ids..' || 'Something went wrong'" | |
echo $console_line | clipcopy | |
echo "Use following to load ids into rails console: \n" | |
echo $console_line | |
} | |
kblogs() { | |
PODNAME=$(kubectl get pods | fzf -q "$1" -1 -0 | cut -d' ' -f1) | |
[[ -n $PODNAME ]] || { echo 'Exiting without action..'; return } | |
kubectl logs $2 $PODNAME | |
} | |
kblogsf() { | |
PODNAME=$(kubectl get pods | fzf -q "$1" -1 -0 | cut -d' ' -f1) | |
[[ -n $PODNAME ]] || { echo 'Exiting without action..'; return } | |
kubectl logs -f $2 $PODNAME | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment