@@ -47,14 +47,18 @@ func newSSHCmd(opts *Options) *cobra.Command {
4747 if err := ensureSessionOwnership (opts , k , ns , sn , true ); err != nil {
4848 return err
4949 }
50+ sshMode , modeErr := detectSessionSSHMode (opts , ns , sn )
51+ if modeErr != nil {
52+ return fmt .Errorf ("detect ssh mode: %w" , modeErr )
53+ }
5054 stopMaintenance := startSessionMaintenance (opts , cfg , ns , sn , cmd .OutOrStdout (), true , true )
5155 defer stopMaintenance ()
5256
5357 if user == "" {
5458 user = cfg .Spec .SSH .User
5559 }
5660 if remotePort == 0 {
57- remotePort = cfg . Spec . SSH . RemotePort
61+ remotePort = sshRemotePortForMode ( cfg , sshMode )
5862 }
5963 if localPort == 0 {
6064 localPort = 2222
@@ -67,11 +71,11 @@ func newSSHCmd(opts *Options) *cobra.Command {
6771 }
6872
6973 if setupKey {
70- if err := ensureSSHKeyOnPod (opts , cfg , ns , podName (sn ), keyPath ); err != nil {
74+ if err := ensureSSHKeyOnPod (opts , cfg , ns , podName (sn ), keyPath , sshMode ); err != nil {
7175 return err
7276 }
7377 }
74- if err := waitForSSHDReady (opts , cfg , ns , podName (sn ), 20 * time .Second ); err != nil {
78+ if err := waitForSSHDReady (opts , cfg , ns , podName (sn ), sshMode , 20 * time .Second ); err != nil {
7579 fmt .Fprintf (cmd .ErrOrStderr (), "warning: sshd not ready yet: %v\n " , err )
7680 }
7781
@@ -248,7 +252,7 @@ func newSSHCmd(opts *Options) *cobra.Command {
248252 return cmd
249253}
250254
251- func ensureSSHKeyOnPod (opts * Options , cfg * config.DevEnvironment , namespace , pod , keyPath string ) error {
255+ func ensureSSHKeyOnPod (opts * Options , cfg * config.DevEnvironment , namespace , pod , keyPath , sshMode string ) error {
252256 if err := ensureCommand ("ssh-keygen" ); err != nil {
253257 return err
254258 }
@@ -275,6 +279,7 @@ func ensureSSHKeyOnPod(opts *Options, cfg *config.DevEnvironment, namespace, pod
275279 k := newKubeClient (opts )
276280 container := sshTargetContainer (cfg )
277281 var lastErr error
282+ installed := false
278283 for i := 0 ; i < 3 ; i ++ {
279284 ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
280285 if container == "" {
@@ -284,25 +289,58 @@ func ensureSSHKeyOnPod(opts *Options, cfg *config.DevEnvironment, namespace, pod
284289 }
285290 cancel ()
286291 if err == nil {
287- return nil
292+ installed = true
293+ break
288294 }
289295 lastErr = err
290296 time .Sleep (time .Duration (i + 1 ) * 500 * time .Millisecond )
291297 }
292- return fmt .Errorf ("install ssh key in pod: %w" , lastErr )
298+ if ! installed {
299+ return fmt .Errorf ("install ssh key in pod: %w" , lastErr )
300+ }
301+
302+ if normalizeSSHMode (sshMode ) == sshModeEmbedded {
303+ copyScript := `set -eu
304+ DEV_PID=""
305+ for pid in $(ls /proc 2>/dev/null | grep -E '^[0-9]+$' | sort -n); do
306+ [ "$pid" = "1" ] && continue
307+ [ "$pid" = "$$" ] && continue
308+ [ -r "/proc/$pid/root" ] 2>/dev/null || continue
309+ if ! [ "/proc/$pid/root" -ef "/proc/self/root" ] 2>/dev/null; then
310+ if [ -d "/proc/$pid" ]; then
311+ DEV_PID="$pid"
312+ break
313+ fi
314+ fi
315+ done
316+ [ -n "$DEV_PID" ]
317+ nsenter --target "$DEV_PID" --mount -- mkdir -p /var/okdev
318+ cat /root/.ssh/authorized_keys | nsenter --target "$DEV_PID" --mount -- sh -c "cat > /var/okdev/authorized_keys && chmod 600 /var/okdev/authorized_keys"`
319+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
320+ _ , err := k .ExecShInContainer (ctx , namespace , pod , "okdev-sidecar" , copyScript )
321+ cancel ()
322+ if err != nil {
323+ return fmt .Errorf ("sync embedded ssh authorized_keys: %w" , err )
324+ }
325+ }
326+ return nil
293327}
294328
295- func waitForSSHDReady (opts * Options , cfg * config.DevEnvironment , namespace , pod string , timeout time.Duration ) error {
329+ func waitForSSHDReady (opts * Options , cfg * config.DevEnvironment , namespace , pod , sshMode string , timeout time.Duration ) error {
296330 k := newKubeClient (opts )
297331 container := sshTargetContainer (cfg )
298332 if container == "" {
299333 return nil
300334 }
335+ probeCmd := "ps | grep '[s]shd' >/dev/null 2>&1"
336+ if normalizeSSHMode (sshMode ) == sshModeEmbedded {
337+ probeCmd = "ps | grep '[o]kdev-sshd' >/dev/null 2>&1"
338+ }
301339 deadline := time .Now ().Add (timeout )
302340 var lastErr error
303341 for time .Now ().Before (deadline ) {
304342 ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
305- _ , err := k .ExecShInContainer (ctx , namespace , pod , container , "ps | grep '[s]shd' >/dev/null 2>&1" )
343+ _ , err := k .ExecShInContainer (ctx , namespace , pod , container , probeCmd )
306344 cancel ()
307345 if err == nil {
308346 return nil
0 commit comments