vscode-remote-release icon indicating copy to clipboard operation
vscode-remote-release copied to clipboard

Attach Visual Studio Code doesn't work if kubernetes setting is in WSL

Open mtsmfm opened this issue 3 years ago • 19 comments

Even if it shown on k8s sidebar, I get No Pod name found error.

image

image

[93 ms] Remote-Containers 0.205.2 in VS Code 1.62.1 (f4af3cbf5a99787542e2a30fe1fd37cd644cc31f).
[90 ms] Start: WSL env probe
[98 ms] Start: Run: wsl -d ubuntu-20.04
[1261 ms] Start: WSL env probe
[1262 ms] Start: Run: wsl -d ubuntu-20.04
[14345 ms] Start: WSL env probe
[14346 ms] Start: Run: wsl -d ubuntu-20.04
[14827 ms] No Pod name found. Pod object:
null

mtsmfm avatar Nov 10 '21 11:11 mtsmfm

After copying .kube/config from WSL to Windows, it works. ($ cp ~/.kube/config /mnt/c/Users/mtsmfm/.kube/config in WSL) It seems Attach Visual Studio Code doesn't consider such a case.

mtsmfm avatar Nov 11 '21 03:11 mtsmfm

You can easily reproduce this problem by Rancher Desktop

  1. Install Rancher Desktop https://github.com/rancher-sandbox/rancher-desktop/releases
  2. Run kubectl run foo --image=ubuntu --restart=Never sleep infinity in WSL
  3. Run code . in WSL
  4. Install k8s extension in WSL
  5. Attach Visual Studio Code image

mtsmfm avatar Nov 13 '21 12:11 mtsmfm

I have run into this exact issue in Visual Studio Code 1.62.3 and Kubernetes extension 1.3.4 with Rancher Desktop 0.6.1 on Windows 11 build 22000.348 with WSL 0.50.2.0.

My ~/.kube/config is symlinked from %USERPROFILE%/.kube/config as per the rancher desktop installation.

Right-clicking on the pod, and then selecting "terminal" does work (I get a shell on the container in the pod), but attach does not with the "No Pod name found" error.

I would be grateful to hear of any workarounds. @mtsmfm did you make any progress with this?

cpbotha avatar Nov 20 '21 13:11 cpbotha

@cpbotha I've found code command accepts URIs like the following:

code vscode-remote://k8s-container%2Bcontext%3Drancher-desktop%2Bpodname%3Dthings-5454d95b69-q9c6f%2Bnamespace%3Ddefault%2Bname%3D/workspace/things

I can't find full list of valid params though, it'll be like this:

https://github.com/mtsmfm/vscode-k8s-quick-attach/blob/fc26c521ad4cf66073fc804de8567394a64bfcc5/src/quickAttach.ts#L67-L73

I've released an extension to attach VSCode to pod quickly and easily.

https://marketplace.visualstudio.com/items?itemName=mtsmfm.vscode-k8s-quick-attach

Hopefully it helps.

mtsmfm avatar Nov 21 '21 09:11 mtsmfm

Since the Kubernetes extension runs in WSL and uses kubectl there, could you try enabling the 'Remote-Containers: Execute in WSL' user setting? image

chrmarti avatar Nov 22 '21 10:11 chrmarti

@chrmarti I got the same result.

image

mtsmfm avatar Nov 22 '21 14:11 mtsmfm

@chrmarti Just like @mtsmfm I can confirm that that setting does not help: It gives the same error.

As an additional datapoint, I did confirm earlier today that attaching to the pod with VSCode NOT connected to WSL (i.e. just open on Windows) succeeds.

cpbotha avatar Nov 22 '21 14:11 cpbotha

Got it, same cause as https://github.com/microsoft/vscode-remote-release/issues/4405.

chrmarti avatar Nov 22 '21 21:11 chrmarti

Still no fix to this? I'm still getting this same issue, and it prevents me from using Windows with WSL2 for my workstation.

CoopTang avatar Jan 31 '22 18:01 CoopTang

This depends on https://github.com/microsoft/vscode/issues/111238.

chrmarti avatar Feb 01 '22 16:02 chrmarti

You need to install kubectl on your Windows system. Also, kubectl must be able to be found via the $PATH variable. Additionally I installed the Kubernetes extension in VSCode in Windows (no idea if this is necessary)

If you then copy your .kube/config file from Linux to Windows, it should work.

Verified in WSL (Ubuntu 22.04) with docker and minikube.

marklechtermann avatar Aug 24 '22 07:08 marklechtermann

@marklechtermann I tried these steps but it doesn't work - still says "No pod name found". Only workaround is attaching to K8 pods from outside of WSL in non-WSL VSCode.

calclavia avatar Mar 06 '23 22:03 calclavia

Still no fix to this? I'm still getting this same issue.. Thanks in advance for any answers

raffaele-capasso avatar Jul 05 '23 09:07 raffaele-capasso

I went into this issue and tried to see if there is truly a workaround. The following is how I got it to work.

  1. Copy ~/.kube from WSL into %userprofile% (windows, outside of WSL) (It is possible use mklink to create a symlink in cmd as admin).
  2. Start vscode-insider (vscode should also work) in windows (not inside WSL)
  3. Add [path/to/%userprofile%]/.kube/config into kubeconfig via vscode image
  • you should see docker-desktop by then image
  1. The "Attach Visual Studio Code" option worked for me.
  • if docker-desktop doesn't show up under cluster, try to reload windows, repeat step 2-3. If it still doesn't work, sorry.

  • Note that this change will break kubernetes extension inside WSL and "Open dashboard" will not work.

    • To fix kubernetes extension inside WSL, change kubeconfig back to home/[username]/.kube/config.
    • For dashboard, go into bash of WSL and type kubectl proxy (I am not aware of any downside).

hin-fan-alt avatar Jul 20 '23 18:07 hin-fan-alt

@cpbotha I've found code command accepts URIs like the following:

code vscode-remote://k8s-container%2Bcontext%3Drancher-desktop%2Bpodname%3Dthings-5454d95b69-q9c6f%2Bnamespace%3Ddefault%2Bname%3D/workspace/things

I can't find full list of valid params though, it'll be like this:

https://github.com/mtsmfm/vscode-k8s-quick-attach/blob/fc26c521ad4cf66073fc804de8567394a64bfcc5/src/quickAttach.ts#L67-L73

I've released an extension to attach VSCode to pod quickly and easily.

https://marketplace.visualstudio.com/items?itemName=mtsmfm.vscode-k8s-quick-attach

Hopefully it helps.

@mtsmfm I am actually trying your extension and getting error Command failed with exit code 1: <linux_path_to_internal_file> k8s create-token --profile=default The system cannot find the path specified. even though the same line successfully executes in wsl. Do you know what could go wrong?

also, just copying the config file to the Windows location still produces No Pod name found for some reason

fedorzh avatar Nov 21 '23 15:11 fedorzh

@fedorzh Make sure if you can open the editor with vs-code-remote://k8s-container URL

mtsmfm avatar Nov 27 '23 11:11 mtsmfm

In my minikube in WSL environment I have same problem.

@mtsmfm Simply copying kube config file as you said: cp ~/.kube/config /mnt/c/Users/mtsmfm/.kube/config

does not work for me, since windows kubectl can not access client-certificate & client-key files. So i prefix \\wsl$\Ubuntu to the path of cert files, and now windows kubectl can find my pods.

But your extension 'k8s quick attach' raises errors:

UNC host 'wsl$' access is not allowed

Still, I can use 'Terminal' comand like @cpbotha .

Following is the log when i run 'Attach Visual Studio Code' command from the menu, and VS Code raises No Pod name found

[42698 ms] Dev Containers 0.327.0 in VS Code 1.85.2 (8b3775030ed1a69b13e4f4c628c612102e30a681).
[1529412 ms] Start: Run: wsl -d Ubuntu -e /bin/sh -c cd '/home/haje01/works/wai-da' && /bin/sh
[1529420 ms] Start: Run in host: id -un
[1529540 ms] haje01
[1529541 ms] 
[1529541 ms] Start: Run in host:  (command -v getent >/dev/null 2>&1 && getent passwd 'haje01' || grep -E '^haje01|^[^:]*:[^:]*:haje01:' /etc/passwd || true)
[1529543 ms] Start: Run in host: echo ~
[1529544 ms] /home/haje01
[1529544 ms] 
[1529544 ms] Start: Run in host: test -x '/home/haje01/.vscode-remote-containers/bin/8b3775030ed1a69b13e4f4c628c612102e30a681/node'
[1529545 ms] 
[1529545 ms] 
[1529545 ms] Exit code 1
[1529546 ms] Start: Run in host: test -x '/home/haje01/.vscode-server/bin/8b3775030ed1a69b13e4f4c628c612102e30a681/node'
[1529547 ms] 
[1529547 ms] 
[1529547 ms] Start: Run in host: test -f '/home/haje01/.vscode-server/bin/8b3775030ed1a69b13e4f4c628c612102e30a681/node_modules/node-pty/package.json'
[1529548 ms] 
[1529548 ms] 
[1529548 ms] Start: Run in host: test -f '/home/haje01/.vscode-remote-containers/dist/vscode-remote-containers-server-0.327.0.js'
[1529549 ms] 
[1529550 ms] 
[1529551 ms] userEnvProbe: loginInteractiveShell (default)
[1529551 ms] userEnvProbe: not found in cache
[1529551 ms] userEnvProbe shell: /bin/bash
[1531007 ms] userEnvProbe PATHs:
Probe:     '/home/haje01/.pyenv/shims:/home/haje01/.local/bin:/home/haje01/.sdkman/candidates/gradle/current/bin:/home/haje01/.npm-global/bin:/usr/local/cuda-12.2/bin:~/.local/apache-maven-3.9.3/bin:/home/haje01/.local/bin:/home/haje01/.bun/bin:/home/haje01/.krew/bin:/home/haje01/.pyenv/plugins/pyenv-virtualenv/shims:/home/haje01/.pyenv/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.2/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.2/libnvvp:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/Bandizip/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/dotnet/:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/Program Files/NVIDIA Corporation/Nsight Compute 2023.2.2/:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/Git/cmd:/mnt/c/Users/haje01/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/haje01/AppData/Local/Programs/Microsoft VS Code/bin:/snap/bin:/home/haje01/.fzf/bin'
Container: None
[1531060 ms] No Pod name found. Pod object:
null
[1651396 ms] No Pod name found. Pod object:
null

Interestingly enough, when i run 'Copy Name' command from the menu, It successfuly copy the right pod name.

haje01 avatar Jan 19 '24 09:01 haje01

This is a pretty common workflow now that people want to avoid the Docker Desktop mess. It would be nice to see this get supported.

xgalaxy avatar Jan 19 '24 23:01 xgalaxy

I tried as you instructed, but I still get the 'No Pod name found. Pod object: null' error in WSL. Instead, it works fine when I open it with k8s extensions in Windows (PowerShell) instead of WSL (Ubuntu 20.04). image image

kbae1230 avatar May 02 '24 12:05 kbae1230