forked from jordanwilson230/kubectl-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubectl-ssh
executable file
·162 lines (139 loc) · 4.38 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
#!/usr/bin/env bash
set -eu
usage() {
cat <<EOF
Usage: kubectl ssh [OPTIONS] <pod name> [-- <commands...>]
Run a command in a running container
Options:
-h Show usage
-d Enable debug mode. Print a trace of each commands
-n If present, the namespace scope for this CLI request
-u Username or UID (format: <name|uid>[:<group|gid>])
-c Container name. If omitted, 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 "dh:p: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"
# Limits concurrent sessions (each session deploys a pod) to 2. It's not necessary, just a preference.
test "$(exec "${KUBECTL}" get po "$(whoami|tr -dc '[:alnum:]')-1" 2>/dev/null)" && container="$(whoami|tr -dc '[:alnum:]')-2" || container="$(whoami|tr -dc '[:alnum:]')-1"
# 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",
"spec": {
"containers": [
{
"image": "docker",
"name": "'$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",
"type": "File"
}
}
]
}
}
EOF
trap '$KUBECTL delete pod $container >/dev/null 2>&1 &' 0 1 2 3 15
eval "$KUBECTL" run -it --restart=Never --image=docker --overrides="'${OVERRIDES}'" "$container"