-
Notifications
You must be signed in to change notification settings - Fork 63
/
kubectl-ssh
executable file
·167 lines (145 loc) · 4.62 KB
/
kubectl-ssh
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env bash
set -eu
usage() {
cat <<EOF
Usage: kubectl ssh [OPTIONAL: -n <namespace>] [OPTIONAL: -u <user>] [OPTIONAL: -c <Container Name>] [ Pod Name ] -- [command]
Example: kubectl ssh -n default -u root -c prometheus prometheus-282sd0s2 -- bash
Run a command in a running container
Options:
-h Show usage
-d Enable debug mode. Print a trace of each commands
-n The namespace to use for this request. If not provided, defaults to current namespace.
-u Username or UID (format: <name|uid>[:<group|gid>]). If not provided, defaults to root.
-c Container name. If not provided, the first container in the pod will be chosen
EOF
exit 0
}
to_json() {
text="$*"
p=""
for i in $text; do
[[ -n "$p" ]] && p+=", "
p+="\"$i"\"
done
echo -en "$p"
}
[ $# -eq 0 ] && usage
KUBECTL="$(type -p kubectl)"
COMMAND="/bin/sh"
USERNAME="root"
CONTAINER="NONE"
NAMESPACE="NONE"
while getopts "hdp:n:u:c:" arg; do
case $arg in
p) # Specify pod name.
POD=${OPTARG}
;;
n) # Specify namespace
NAMESPACE=${OPTARG}
KUBECTL+=" --namespace=${OPTARG}"
;;
u) # Specify user
USERNAME=${OPTARG}
;;
c) # Specify container
CONTAINER=${OPTARG}
;;
d) # Enable debug mode
set -x
;;
h) # Display help.
usage
;;
\?)
usage
;;
esac
done
shift $((OPTIND-1))
if [[ -z ${1+x} ]]; then
echo "Error: You have to specify the pod name" >&2
usage
fi
POD="$1"
shift
if [[ $# -gt 0 ]]; then
if [[ $1 == "--" ]]; then
shift
COMMAND="$*"
else
echo "Error: Invalid option: $0" >&2
usage
fi
fi
echo -e "\nConnecting...\nPod: ${POD}\nNamespace: ${NAMESPACE}\nUser: ${USERNAME}\nContainer: ${CONTAINER}\nCommand: $COMMAND\n"
# $temp_container is the name of our temporary container which we use to connect behind the scenes. Will be cleaned up automatically.
temp_container="ssh-pod-${RANDOM}"
# We want to mount the docker socket on the node of the pod we're exec'ing into.
NODENAME=$( ${KUBECTL} get pod "${POD}" -o go-template='{{.spec.nodeName}}' )
NODESELECTOR='"nodeSelector": {"kubernetes.io/hostname": "'$NODENAME'"},'
# Adds toleration if the target container runs on a tainted node. Assumes no more than one taint. Change if yours have more than one or are configured differently.
TOLERATION_VALUE=$($KUBECTL get pod "${POD}" -ojsonpath='{.spec.tolerations[].value}') >/dev/null 2>&1
if [[ "$TOLERATION_VALUE" ]]; then
TOLERATION_KEY=$($KUBECTL get pod "${POD}" -ojsonpath='{.spec.tolerations[].key}')
TOLERATION_OPERATOR=$($KUBECTL get pod "${POD}" -ojsonpath='{.spec.tolerations[].operator}')
TOLERATION_EFFECT=$($KUBECTL get pod "${POD}" -ojsonpath='{.spec.tolerations[].effect}')
TOLERATIONS='"tolerations": [{"effect": "'$TOLERATION_EFFECT'","key": "'$TOLERATION_KEY'","operator": "'$TOLERATION_OPERATOR'","value": "'$TOLERATION_VALUE'"}],'
else
TOLERATIONS=''
fi
if [[ ${CONTAINER} != "NONE" ]]; then
DOCKER_CONTAINERID=$( eval "$KUBECTL" get pod "${POD}" -o go-template="'{{ range .status.containerStatuses }}{{ if eq .name \"${CONTAINER}\" }}{{ .containerID }}{{ end }}{{ end }}'" )
else
DOCKER_CONTAINERID=$( $KUBECTL get pod "${POD}" -o go-template='{{ (index .status.containerStatuses 0).containerID }}' )
fi
CONTAINERID=${DOCKER_CONTAINERID#*//}
read -r -d '' OVERRIDES <<EOF || :
{
"apiVersion": "v1",
"metadata": {
"annotations": {
"sidecar.istio.io/inject" : "false"
}
},
"spec": {
"securityContext": { "runAsUser": 0 },
"containers": [
{
"image": "docker",
"name": "'$temp_container'",
"stdin": true,
"stdinOnce": true,
"tty": true,
"restartPolicy": "Never",
"args": [
"exec",
"--privileged",
"-it",
"-u",
"${USERNAME}",
"${CONTAINERID}",
$(to_json "${COMMAND}")
],
"volumeMounts": [
{
"mountPath": "/var/run/docker.sock",
"name": "docker"
}
]
}
],
$NODESELECTOR
$TOLERATIONS
"volumes": [
{
"name": "docker",
"hostPath": {
"path": "/var/run/docker.sock"
}
}
]
}
}
EOF
trap '$KUBECTL delete pod $temp_container >/dev/null 2>&1 &' 0 1 2 3 15
eval "$KUBECTL" run -it --restart=Never --image=docker --overrides="'${OVERRIDES}'" "$temp_container"