diff --git a/.changes/unreleased/Bugfix-20261119-000000.yaml b/.changes/unreleased/Bugfix-20261119-000000.yaml new file mode 100644 index 0000000..4da6074 --- /dev/null +++ b/.changes/unreleased/Bugfix-20261119-000000.yaml @@ -0,0 +1,3 @@ +kind: Bugfix +body: In agent mode, merge the configured pod `securityContext` instead of replacing it; `runAsUser` and `fsGroup` default to `0` only when unset +time: 2026-11-19T00:00:00.000000Z diff --git a/src/pkg/k8s.go b/src/pkg/k8s.go index 68325b6..2105d06 100644 --- a/src/pkg/k8s.go +++ b/src/pkg/k8s.go @@ -191,14 +191,18 @@ func executable() *int32 { func (s *JobRunner) getPodObject(identifier string, labels map[string]string, job opslevel.RunnerJob) *corev1.Pod { // TODO: Allow configuration of Pod Command - podSecurityContext := s.podConfig.SecurityContext + podSecurityContext := *s.podConfig.SecurityContext.DeepCopy() if s.podConfig.AgentMode { - // Agent mode jobs need root user for Docker daemon - runAsUser := int64(0) - fsGroup := int64(0) - podSecurityContext = corev1.PodSecurityContext{ - RunAsUser: &runAsUser, - FSGroup: &fsGroup, + // Agent mode jobs need root for the Docker daemon. Default RunAsUser and + // FSGroup to 0 only when the pod config leaves them unset, so configured + // values (e.g. fsGroup for volume ownership) are kept. + if podSecurityContext.RunAsUser == nil { + runAsUser := int64(0) + podSecurityContext.RunAsUser = &runAsUser + } + if podSecurityContext.FSGroup == nil { + fsGroup := int64(0) + podSecurityContext.FSGroup = &fsGroup } } diff --git a/src/pkg/k8s_test.go b/src/pkg/k8s_test.go index 0d8dbf3..e35d0e3 100644 --- a/src/pkg/k8s_test.go +++ b/src/pkg/k8s_test.go @@ -371,3 +371,78 @@ func mountIsRW(mounts []corev1.VolumeMount, name string) bool { // Suppress unused import warning for policyv1 var _ = policyv1.PodDisruptionBudget{} + +func TestGetPodObject_AgentModeSecurityContextMerge(t *testing.T) { + int64Ptr := func(v int64) *int64 { return &v } + boolPtr := func(v bool) *bool { return &v } + + cases := []struct { + name string + agentMode bool + config corev1.PodSecurityContext + expected corev1.PodSecurityContext + }{ + { + name: "agent mode, no config defaults to root", + agentMode: true, + config: corev1.PodSecurityContext{}, + expected: corev1.PodSecurityContext{RunAsUser: int64Ptr(0), FSGroup: int64Ptr(0)}, + }, + { + name: "agent mode, fsGroup only keeps fsGroup", + agentMode: true, + config: corev1.PodSecurityContext{FSGroup: int64Ptr(1000)}, + expected: corev1.PodSecurityContext{RunAsUser: int64Ptr(0), FSGroup: int64Ptr(1000)}, + }, + { + name: "agent mode, every field set keeps config", + agentMode: true, + config: corev1.PodSecurityContext{ + RunAsUser: int64Ptr(1001), + RunAsGroup: int64Ptr(1002), + FSGroup: int64Ptr(1003), + SupplementalGroups: []int64{2000}, + RunAsNonRoot: boolPtr(true), + }, + expected: corev1.PodSecurityContext{ + RunAsUser: int64Ptr(1001), + RunAsGroup: int64Ptr(1002), + FSGroup: int64Ptr(1003), + SupplementalGroups: []int64{2000}, + RunAsNonRoot: boolPtr(true), + }, + }, + { + name: "non-agent mode, no config is unchanged", + agentMode: false, + config: corev1.PodSecurityContext{}, + expected: corev1.PodSecurityContext{}, + }, + { + name: "non-agent mode, config is unchanged", + agentMode: false, + config: corev1.PodSecurityContext{FSGroup: int64Ptr(1000)}, + expected: corev1.PodSecurityContext{FSGroup: int64Ptr(1000)}, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + runner := &JobRunner{ + logger: zerolog.Nop(), + podConfig: &K8SPodConfig{ + Namespace: "test", + SecurityContext: tc.config, + TerminationGracePeriodSeconds: 30, + AgentMode: tc.agentMode, + }, + } + + pod := runner.getPodObject("test-pod", map[string]string{"app": "test"}, opslevel.RunnerJob{Image: "alpine:latest"}) + + autopilot.Equals(t, tc.expected, *pod.Spec.SecurityContext) + // The shared pod config must not be mutated by defaulting. + autopilot.Equals(t, tc.config, runner.podConfig.SecurityContext) + }) + } +}