-
Notifications
You must be signed in to change notification settings - Fork 1
/
kube-debug-pod.sh
executable file
·70 lines (57 loc) · 2.23 KB
/
kube-debug-pod.sh
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
debug_image=javipolo/ubuntu-debug:latest
[ "$1" == "-n" ] && { dryrun=true; shift 1; }
[ "$1" == "-d" ] && { image=${debug_image}; shift 1; }
[ "$1" == "-e" ] && { safe_to_evict=false; shift 1; }
[ "$1" == "-p" ] && { type=pod; shift 1; }
[ "$1" == "-i" ] && { image=$2; shift 2; }
deploy_name=$1
pod_new_name=$USER-$1
pod_base='{ "apiVersion": "v1", "kind": "Pod" }'
pod_name='{ "name": "'$pod_new_name'" }'
pod_labels='{ "name": "'$pod_new_name'", "debug": "'$USER'" }'
pod_command='{ "command": ["tail", "-f", "/dev/null"] }'
[ "$image" ] && pod_image='{ "image": "'${image}'"}' || pod_image=null
[ "$safe_to_evict" == "false" ] && pod_no_evict='{ "cluster-autoscaler.kubernetes.io/safe-to-evict": "false" }' || pod_no_evict=null
apply_command="kubectl apply -f -"
[ "$dryrun" == "true" ] && apply_command=$(command -v json2yaml > /dev/null && echo json2yaml || echo cat)
usage(){
cat << EOF
Creates a debugging pod using a deployment as base
Usage: $0 [-n] [-d] [-i image:name] <deploy-name>
-n dry run - Do nothing, just print the generated json/yaml
-d debug image - Use defaut debug image in all the containers
-e - Set the safe-to-evict to false for this pod
-p - Use a pod instead of a deployment as template
-i image custom image - Use specific image in all the containers
default debug image: $debug_image
EOF
exit 1
}
deploy_to_pod(){
jq "$pod_base + .spec.template"
}
pod_cleanup(){
jq 'del(.metadata.creationTimestamp) |
del(.metadata.labels) |
del(.spec.containers[].livenessProbe) |
del(.spec.containers[].readinessProbe) |
del(.spec.resources) |
del(.spec.containers[].command)
'
}
pod_amend(){
jq ". |
.metadata += $pod_name |
.metadata.labels += $pod_labels |
.metadata.annotations += $pod_no_evict |
.spec.containers[] += $pod_image |
.spec.containers[] += $pod_command
"
}
[ "$deploy_name" ] || usage
if [ "$type" == "pod" ]; then
kubectl get pod $1 -o json | pod_cleanup | pod_amend | $apply_command
else
kubectl get deploy $1 -o json | deploy_to_pod | pod_cleanup | pod_amend | $apply_command
fi