Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions apps/api/src/services/k8s-workload-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,14 +528,14 @@ export class K8sWorkloadManager {
}

// Pod-level security context for StatefulSets — ensures PVC mounts are
// writable by the agent user (UID/GID 1000). fsGroup sets the group owner
// of all files in mounted volumes. UID 1000 matches the `agent` user
// writable by the agent user (UID/GID 1001). fsGroup sets the group owner
// of all files in mounted volumes. UID 1001 matches the `agent` user
// created in images/base.Dockerfile and owns /workspace in the image.
if (restartPolicy === "Always") {
const podSecCtx = new V1PodSecurityContext();
podSecCtx.fsGroup = 1000;
podSecCtx.runAsUser = 1000;
podSecCtx.runAsGroup = 1000;
podSecCtx.fsGroup = 1001;
podSecCtx.runAsUser = 1001;
podSecCtx.runAsGroup = 1001;
podSpec.securityContext = podSecCtx;
}

Expand All @@ -550,7 +550,7 @@ export class K8sWorkloadManager {
const initContainers: V1Container[] = [];

// For StatefulSets, prepend an initContainer that chowns the home PVC
// mount to UID 1000 (the `agent` user in images/base.Dockerfile). This
// mount to UID 1001 (the `agent` user in images/base.Dockerfile). This
// is necessary because some storage classes (docker-desktop's hostpath,
// GKE default) don't honor the pod's fsGroup setting, leaving the mount
// root-owned and unwritable by the main container. Running chown as
Expand All @@ -561,7 +561,7 @@ export class K8sWorkloadManager {
permInit.name = "home-perm-fix";
permInit.image = spec.image;
permInit.imagePullPolicy = spec.imagePullPolicy ?? "IfNotPresent";
permInit.command = ["sh", "-c", "chown -R 1000:1000 /home/agent && chmod 755 /home/agent"];
permInit.command = ["sh", "-c", "chown -R 1001:1001 /home/agent && chmod 755 /home/agent"];
const initSec = new V1SecurityContext();
initSec.runAsUser = 0;
initSec.runAsGroup = 0;
Expand Down
8 changes: 3 additions & 5 deletions images/base.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ COPY scripts/optio-gh-wrapper /usr/local/bin/optio-gh-wrapper
COPY scripts/optio-glab-wrapper /usr/local/bin/optio-glab-wrapper
RUN chmod +x /usr/local/bin/optio-git-credential /usr/local/bin/optio-gh-wrapper /usr/local/bin/optio-glab-wrapper

# Non-root user (UID 1000 to match k8s securityContext)
# Ubuntu 24.04 ships with 'ubuntu' user at UID 1000 — remove it first
RUN userdel -r ubuntu 2>/dev/null || true \
&& groupadd -g 1000 agent \
&& useradd -m -s /bin/bash -u 1000 -g 1000 agent \
# Non-root user (UID 1001 to match k8s securityContext)
RUN groupadd -g 1001 agent \
&& useradd -m -s /bin/bash -u 1001 -g 1001 agent \
&& chown -R agent:agent /workspace
USER agent
WORKDIR /workspace
Expand Down
Loading