Add Kubernetes exec support to devcontainer exec#1234
Open
joris-decombe wants to merge 1 commit into
Open
Conversation
Enable `devcontainer exec` to run commands inside Kubernetes pods via `kubectl exec`, as an alternative to the existing Docker exec path. New CLI flags for the `exec` command: --kubectl-path kubectl CLI path (default: kubectl) --k8s-context Kubernetes context (from kubeconfig) --k8s-kubeconfig Path to kubeconfig file (for custom CAs) --k8s-namespace Target pod namespace (required with --k8s-pod) --k8s-pod Target pod name --k8s-container Target container name (required with --k8s-pod) Since kubectl exec doesn't support Docker's -u (user), -e (env), or -w (cwd) flags natively, these are handled by wrapping commands in shell invocations with proper POSIX quoting. A fast path avoids shell wrapping when none of these are needed (e.g., for the shell server). User switching for non-root remoteUser uses `su -s /bin/sh <user> -c` (no login shell, matching Docker's -u behaviour). The user parameter is validated against a strict regex to prevent shell injection. Environment probing uses probeRemoteEnv to capture the actual runtime environment inside the container, correctly resolving K8s valueFrom env vars (ConfigMaps, Secrets, Downward API) that aren't visible in the static pod spec. Relates to: devcontainers/spec#672, microsoft/vscode-remote-release#6413
Author
|
@microsoft-github-policy-service agree company="Trade Me" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds Kubernetes exec support to the
devcontainer execcommand, enabling commands to run inside K8s pods viakubectl execas an alternative to Docker exec.This addresses a gap where users with Kubernetes-based dev environments (e.g., cloud-hosted sandboxes) cannot use the devcontainer CLI to exec into their pods with proper
remoteUser,remoteEnv, andworkspaceFoldersupport fromdevcontainer.json.New CLI flags
--kubectl-pathkubectl)--k8s-context--k8s-kubeconfig--k8s-namespace--k8s-pod)--k8s-pod--k8s-container--k8s-pod)Example usage
devcontainer exec \ --k8s-context my-cluster \ --k8s-namespace dev-envs \ --k8s-pod sandbox-abc123 \ --k8s-container dev \ -- bashDesign decisions
execonly — no build/run lifecycle. The assumption is that pods already exist (created by an orchestrator, CI/CD, or platform tooling).kubectl execdoesn't support Docker's-u,-e, or-wflags, these are handled by wrapping commands in POSIX shell invocations with proper quoting. A fast path avoids wrapping when none of these are needed (critical for the shell server).su: Non-rootremoteUserusessu -s /bin/sh <user> -c(no login shell, matching Docker's-ubehaviour). User names are validated against a strict regex.valueFromrefs (ConfigMaps, Secrets, Downward API) aren't visible. Environment probing usesprobeRemoteEnvto capture the actual runtime env inside the container.remoteExecAsRootis only available when the container already runs as root, avoiding failures in pods withrunAsNonRootor without privilege escalation tools.doExec()when K8s flags are present. No changes to the existing Docker/Podman code paths.Related issues
Test plan
npm run compile— cleannpm run type-check— clean