forked from thecasualcoder/kube-fzf
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ktdump
executable file
·54 lines (41 loc) · 1.66 KB
/
ktdump
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env bash
set -e
set -o pipefail
[[ -z "${KUBE_FZF_PATH}" ]] && source kube-fzf.sh || source $KUBE_FZF_PATH
getrs() {
local namespace_query pod_query result namespace rs_name pid path
local rs=$1
if [[ $rs == *"-"* ]] || [ -z "$rs" ]; then
rs='pods'
if [ -z "$rs" ]; then
exit
fi
_kube_fzf_handler "kdesc" "$@"
else
_kube_fzf_handler "kdesc" "$2" "$3" "$4" "$5" "$6"
fi
namespace_query=$(echo $args | awk -F '|' '{ print $1 }')
pod_query=$(echo $args | awk -F '|' '{ print $2 }')
context_selector=$(echo $args | awk -F '|' '{ print $5 }')
pid=$(echo $args | awk -F '|' '{ print $6 }')
path=$(echo $args | awk -F '|' '{ print $7 }')
if [ -z "$path" ]; then
path="/dumps"
fi
result=$(_kube_fzf_search "$namespace_query" "$pod_query" "$context_selector" "$rs")
IFS=$'|' read -r namespace rs_name <<< "$result"
if [ -z "$pid" ]; then
pid=$(kubectl exec --namespace=$namespace $rs_name --context $context_selector -- sh -c "top -n 1 | grep java" | fzf | awk '{print $1}')
if [ -z "$pid" ]; then
exit
fi
fi
local opath=$(date +%s)'_'$rs_name'.tdump'
_kube_fzf_echo "kubectl exec --namespace='$namespace' $rs_name --context $context_selector -- jcmd $pid Thread.print > $path/$opath"
kubectl exec --namespace=$namespace $rs_name --context $context_selector -- sh -c "jcmd $pid Thread.print > $path/$opath"
_kube_fzf_echo "Thread dump Complete > $path/$opath"
_kube_fzf_echo "Thread dump Download from Pod to Host"
kubectl cp --namespace=$namespace $rs_name:${path#/}/$opath --context $context_selector ./$opath
_kube_fzf_echo "Thread dump Download Complete"
}
getrs "$@"