devcontainers / templates

Repository for Dev Container Templates that are managed by Dev Container spec maintainers. See https://github.com/devcontainers/template-starter to create your own!
https://containers.dev/templates
MIT License
958 stars 246 forks source link

Writing a VSCode extension, want to enable "Attach Visual Studio Code" to kubectl pod #199

Closed benjiebob closed 1 year ago

benjiebob commented 1 year ago

Hi there!

I'm writing a VSCode extension and want to be able to add an "Attach Visual Studio Code" to a Kubectl pod button to my UI. I want to build an experience that closely matches this Kubernetes demo (but with my own custom list of pods). However, after a few days looking through this repo, I'm still struggling to find how to implement this.

Screenshot from Kubernetes demo is here: k8s-attach

I'd be really grateful if you could either point me towards a tutorial for setting this up, or if you could explain the steps that would be great.

Thanks! Ben

bamurtaugh commented 1 year ago

Thanks for opening! If you install the VS Code Dev Containers extension and right click, I believe this attach option is added when exploring containers from the Docker extension. Does installing the Dev Containers extension help?

benjiebob commented 1 year ago

Yes, sorry if I wasn't clear enough. I am trying to add a "Attach Visual Studio Code" to my own VSCode extension which I'm building so I need to implement the code behind the feature I referenced. The use-case is similar - I want my users to be able to Attach VSCode to a Kubernetes pod I show them in my UI. I'm struggling to find docs to help me with this - I'd be grateful if you could share/link me to the steps.

benjiebob commented 1 year ago

I've been doing some digging and found a seemingly related question that proposes the following terminal command to open a Remote Visual Studio window attached to a docker container:

code --folder-uri "vscode-remote://attached-container+<SOMETHING>"

I suspect that the "Attach Visual Studio Code" menu option I screenshotted above is launching a terminal command like this. I've tried lots of variants of hex values etc. to try and launch a VSCode window connected to my kubectl pod but haven't yet had any success.

I'd be really grateful if you could share the terminal command (or process) that mirrors the steps described in Attach to a container in a Kubernetes cluster.

Thanks!

benjiebob commented 1 year ago

Aha, I found the answer. This fantastic contributor solved it.

cmd = f"code --folder-uri "vscode-remote://k8s-container%2Bcontext%3D{context}+podname={podname}+namespace={namespace}+name={container_name}{some_path}"

Be careful to escape any slashes with %2F in the URI.