Skip to content

Commit 7da7e86

Browse files
authored
Merge pull request #23 from partcyborg/ns-and-ctx
Support providing namespace and context
2 parents 10f9b71 + 542d2a3 commit 7da7e86

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,18 @@ Options:
125125
dest_node must start from the following letters:
126126
ASCII letters 'a' through 'z' or 'A' through 'Z',
127127
the digits '0' through '9', or hyphen ('-').
128-
NOTE: Setting dest_node as 'jumphost' allows to
129-
ssh into SSH jump Pod as 'root' user
128+
NOTE: Setting dest_node as 'jumphost' allows to
129+
ssh into SSH jump Pod as 'root' user
130130
-u, --user <sshuser> SSH User name
131131
-i, --identity <identity_file> Identity key file, or PEM(Privacy Enhanced Mail)
132132
-p, --pubkey <pub_key_file> Public key file
133133
-P, --port <port> SSH port for target node SSH server
134134
Defaults to 22
135135
-a, --args <args> Args to exec in ssh session
136+
-n, --namespace <ns> Namespace for jump pod
137+
--context <context> Kubernetes context
136138
--pod-template <file> Path to custom sshjump pod definition
137-
--skip-agent Skip automatically starting SSH agent and adding
139+
--skip-agent Skip automatically starting SSH agent and adding
138140
SSH Identity key into the agent before SSH login
139141
(=> You need to manage SSH agent by yourself)
140142
--cleanup-agent Clearning up SSH agent at the end
@@ -174,10 +176,11 @@ In addtion, add `--skip-agent` option if you want to skip automatic starting `ss
174176

175177
### Customize SSH jump pod
176178

177-
You can customize the sshjump pod created by `kubectl ssh-jump` by setting the `$SSH_JUMP_POD_TEMPLATE` environment variable to the path to a pod template on disk.
178-
179+
You can customize the sshjump pod created by `kubectl ssh-jump` by setting the `--pod-template` flag to the path to a pod template on disk.
179180
However, customized sshjump pods must be named `sshjump` and run in the current namespace or `kubectl ssh-jump` won't be able to find them.
180181

182+
You can also specify the namespace and context used by `kubectl ssh-jump` by setting the `--namespace` and `--context` flags respectively.
183+
181184
### Examples
182185

183186
Show all node list. Simply executing `kubectl ssh-jump` gives you the list of destination nodes as well as command usage

kubectl-ssh-jump

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Options:
3333
-P, --port <port> SSH port for target node SSH server
3434
Defaults to 22
3535
-a, --args <args> Args to exec in ssh session
36+
-n, --namespace <ns> Namespace for jump pod
37+
--context <context> Kubernetes context
3638
--pod-template <file> Path to custom sshjump pod definition
3739
--skip-agent Skip automatically starting SSH agent and adding
3840
SSH Identity key into the agent before SSH login
@@ -78,7 +80,7 @@ EOF
7880

7981
get_node_list(){
8082
echo "List of destination node..."
81-
kubectl get no -o custom-columns=Hostname:.metadata.name,Internal-IP:'{.status.addresses[?(@.type=="InternalIP")].address}'
83+
kubectl "${k_args[@]}" get no -o custom-columns=Hostname:.metadata.name,Internal-IP:'{.status.addresses[?(@.type=="InternalIP")].address}'
8284
echo ""
8385
}
8486

@@ -88,7 +90,7 @@ get_openssh_verion_number() {
8890

8991
cleanup_sshjump_pod(){
9092
echo "Clearning up SSH Jump host (Pod)..."
91-
kubectl delete pod sshjump
93+
kubectl "${k_args[@]}" delete pod sshjump
9294
}
9395

9496
check_and_start_agent(){
@@ -150,7 +152,7 @@ EOF
150152
)
151153
fi
152154
echo "Creating SSH jump host (Pod)..."
153-
echo "${pod_template}" | kubectl apply -f -
155+
echo "${pod_template}" | kubectl "${k_args[@]}" apply -f -
154156
}
155157

156158
run_ssh_node(){
@@ -162,15 +164,15 @@ run_ssh_node(){
162164
local sshargs="$6"
163165

164166
# Install an SSH Server if not yet installed
165-
r=$(kubectl get pod sshjump 2>/dev/null | tail -1 | awk '{print $1}') #
167+
r=$(kubectl "${k_args[@]}" get pod sshjump 2>/dev/null | tail -1 | awk '{print $1}') #
166168
if [ "${r}" != "sshjump" ];then
167169
create_jump_pod
168170

169171
# Wait until sshjump gets ready
170172
c=1
171173
while [[ ${c} -le ${MAX_POD_CREATION_TIME} ]];
172174
do
173-
pod_status=$(kubectl get pod sshjump 2>/dev/null | tail -1 | awk '{print $3}')
175+
pod_status=$(kubectl "${k_args[@]}" get pod sshjump 2>/dev/null | tail -1 | awk '{print $3}')
174176
if [ "${pod_status}" = "Running" ]; then
175177
break
176178
fi
@@ -192,15 +194,15 @@ run_ssh_node(){
192194
fi
193195

194196
# Setup portforward
195-
kubectl port-forward sshjump 2222:22 2>/dev/null &
197+
kubectl "${k_args[@]}" port-forward sshjump 2222:22 2>/dev/null &
196198
pid_port_forward=$!
197199

198200
# Wait a bit for the port forwarding to get ready for connection handling for 2222
199201
sleep 2
200202

201203
# Inject public SSH key to sshjump
202204
cat ${pubkey_sshjump} | \
203-
kubectl exec -i sshjump -- /bin/bash -c "cat > /root/.ssh/authorized_keys"
205+
kubectl "${k_args[@]}" exec -i sshjump -- /bin/bash -c "cat > /root/.ssh/authorized_keys"
204206

205207
# Add default ssh option
206208
sshargs="${sshargs} -o StrictHostKeyChecking=no"
@@ -227,6 +229,7 @@ plugin_main() {
227229
cleanup_jump=no
228230
cleanup_agent=no
229231
sshargs=""
232+
k_args=()
230233
while [ $# -gt 0 ] ; do
231234
nSkip=1
232235
case $1 in
@@ -266,6 +269,10 @@ plugin_main() {
266269
sshargs="${sshargs} $2"
267270
nSkip=2
268271
;;
272+
"-n" | "--namespace" | "--context")
273+
k_args+=("$1" "$2")
274+
nSkip=2
275+
;;
269276
"--pod-template")
270277
jump_pod_template="$2"
271278
nSkip=2

0 commit comments

Comments
 (0)