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

Attach to kubernetes: Support alternate config files

Open efekete opened this issue 3 years ago • 4 comments

Attaching to Kubernetes container does not work if using different Kubernetes configuration other than default. Listing and describing containers work as expected

  • VSCode Version: 1.64.2 (Universal), Commit: f80445acd5a3dadef24aa209168452a3d97cc326
  • Local OS Version: MacOS Big Sur
  • Remote OS Version: Kubernetes 1.22, Alpine container
  • Remote Extension/Connection Type: Kubernetes
  • Logs: Error running command remote-containers.attachToK8sContainerFromViewlet: Command failed: kubectl config current-context. This is likely caused by the extension that contributes remote-containers.attachToK8sContainerFromViewlet.

Steps to Reproduce:

  1. mv ~/.kube/config ~/kube/config-staging
  2. Add config-staging to VSCode
  3. Chose any container and try to Attach Visual Studio Code
  4. Get the error above
  5. mv ~/kube/config-staging ~/.kube/config
  6. Attach to container successfully

efekete avatar Apr 03 '22 10:04 efekete

Speaking about the workaround at step 5/6: it doesn't work for me. I mention that I'm on Windows, I installed the Kubernetes missing dependencies and I added to PATH : ~/.vs-kubernetes/tools/kubectl. So if I open a plain terminal and I issue a kube command, e.g. kubectl config current-context: then I do get a correct output. I'm wondering if I'm doing something wrong, because I didn't see anyone complaining that attaching doesn't work (w/ kubectl installed on the system + having the config file in ~/.kube/config.

My other question: how can I debug/dig deeper? Activate some log, to see what commands are being executed, environment, etc? I would have expected to see the output in the Output/Kubernetes view. But I don't see anything there.

cristian-spiescu avatar Apr 19 '24 05:04 cristian-spiescu

Digging deeper, I discovered in exthost.log file this "computer language" message:

2024-04-19 08:19:01.252 [error] {"message":"Command failed: kubectl config current-context","stdout":{"type":"Buffer","data":[]},"stderr":{"type":"Buffer","data":[101,114,114,111,114,58,32,99,117,114,114,101,110,116,45,99,111,110,116,101,120,116,32,105,115,32,110,111,116,32,115,101,116,10]},"code":1} remote-containers.attachToK8sContainerFromViewlet {"value":"ms-vscode-remote.remote-containers","_lower":"ms-vscode-remote.remote-containers"}

Translated into human language this says: error: current-context is not set.

So when VS code executes the kubectl executable, kubectl behaves like it wouldn't have a config file; although I do have the config file in the default location (confirmed also by the fact the I can run the command from a terminal). So the config file should be retrieved cf. point 3 here.

However, during trials/error process: the setup did start to work as expected. Although I tried from the beginning to have a correct discipline regarding: make change => try to make sure that they are really taken into account/restart VS code, even restart PC.

cristian-spiescu avatar Apr 19 '24 06:04 cristian-spiescu

We need to pass the config file along. Currently only the context is remembered, maybe you could use that.

Note that as a workaround the KUBECONFIG environment variable could be used to configure kubectl to use a different config file. Though that might also cause confusion since it is not an obvious setting.

chrmarti avatar Apr 19 '24 07:04 chrmarti

We need to pass the config file along. Currently only the context is remembered, maybe you could use that.

Note that as a workaround the KUBECONFIG environment variable could be used to configure kubectl to use a different config file. Though that might also cause confusion since it is not an obvious setting.

facing the same issue, the workaround works for me, thanks

SummerXXXX avatar Jun 18 '24 08:06 SummerXXXX

We need to pass the config file along. Currently only the context is remembered, maybe you could use that.

Note that as a workaround the KUBECONFIG environment variable could be used to configure kubectl to use a different config file. Though that might also cause confusion since it is not an obvious setting.

Maybe I'm missing something, but since the Dev Containers extension adds the option to "Attach Visual Studio Code" while viewing pods using the official Kubernetes extension, wouldn't it be possible to retrieve the already set KUBECONFIG path?

settings.json

"vs-kubernetes": {
        "vs-kubernetes.knownKubeconfigs": [
            "/home/user/.kube/config-one",
            "/home/user/.kube/config-two"
        ],
        "vs-kubernetes.kubeconfig": "/home/user/.kube/config-two"
    }

I see that something similar has been mentioned a while ago here.

Here you can see the getActiveKubeconfig() function used by the kubernetes extension:

https://github.com/vscode-kubernetes-tools/vscode-kubernetes-tools/blob/master/src/components/config/config.ts#L101

iseppe avatar Feb 13 '25 16:02 iseppe

+1 to this. Copying to the default location ~/.kube/config worked for me (I'm not sure how setting KUBECONFIG as an env var in a shell would have any effect since the plugins don't create a shell to execute these commands).

It would be great to fix this and actually rely on one of the options in the settings.json. Having multiple config file options is very useful for isolation.

nathan-az avatar Jun 25 '25 13:06 nathan-az