Skip to content

Add Kubernetes exec support to devcontainer exec#1234

Open
joris-decombe wants to merge 1 commit into
devcontainers:mainfrom
joris-decombe:feature/kubernetes-exec
Open

Add Kubernetes exec support to devcontainer exec#1234
joris-decombe wants to merge 1 commit into
devcontainers:mainfrom
joris-decombe:feature/kubernetes-exec

Conversation

@joris-decombe
Copy link
Copy Markdown

@joris-decombe joris-decombe commented May 21, 2026

Summary

Adds Kubernetes exec support to the devcontainer exec command, enabling commands to run inside K8s pods via kubectl exec as 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, and workspaceFolder support from devcontainer.json.

New CLI flags

Flag Description
--kubectl-path kubectl CLI path (default: kubectl)
--k8s-context Kubernetes context from kubeconfig
--k8s-kubeconfig Path to kubeconfig file (for custom CAs or non-default configs)
--k8s-namespace Target pod namespace (required with --k8s-pod)
--k8s-pod Target pod name
--k8s-container Target container name (required with --k8s-pod)

Example usage

devcontainer exec \
  --k8s-context my-cluster \
  --k8s-namespace dev-envs \
  --k8s-pod sandbox-abc123 \
  --k8s-container dev \
  -- bash

Design decisions

  • Exec-only scope: This adds K8s support for exec only — no build/run lifecycle. The assumption is that pods already exist (created by an orchestrator, CI/CD, or platform tooling).
  • Shell wrapping for user/env/cwd: Since kubectl exec doesn't support Docker's -u, -e, or -w flags, 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).
  • User switching via su: Non-root remoteUser uses su -s /bin/sh <user> -c (no login shell, matching Docker's -u behaviour). User names are validated against a strict regex.
  • Runtime env probing: Pod spec env only contains static values — valueFrom refs (ConfigMaps, Secrets, Downward API) aren't visible. Environment probing uses probeRemoteEnv to capture the actual runtime env inside the container.
  • Conservative root escalation: remoteExecAsRoot is only available when the container already runs as root, avoiding failures in pods with runAsNonRoot or without privilege escalation tools.
  • Additive architecture: The K8s code path branches early in doExec() when K8s flags are present. No changes to the existing Docker/Podman code paths.

Related issues

Test plan

  • npm run compile — clean
  • npm run type-check — clean
  • Validation tests pass without K8s cluster (missing flag errors)
  • Integration tests skip cleanly when K8s env vars not set
  • Smoke tested against live K8s pods (echo, whoami, remote-env, pwd)
  • CI pipeline passes

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
@joris-decombe joris-decombe requested a review from a team as a code owner May 21, 2026 05:35
@joris-decombe
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Trade Me"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants