diff --git a/charts/kubeflow-trainer/templates/runtimes/data-cache/torch-distributed-with-cache.yaml b/charts/kubeflow-trainer/templates/runtimes/data-cache/torch-distributed-with-cache.yaml index 77146e1573..abe69aab0e 100644 --- a/charts/kubeflow-trainer/templates/runtimes/data-cache/torch-distributed-with-cache.yaml +++ b/charts/kubeflow-trainer/templates/runtimes/data-cache/torch-distributed-with-cache.yaml @@ -46,9 +46,23 @@ spec: template: spec: serviceAccountName: kubeflow-trainer-cache-initializer + securityContext: + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault containers: - name: dataset-initializer image: {{ printf "ghcr.io/kubeflow/trainer/dataset-initializer:%s" (include "trainer.defaultImageTag" .) }} + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault env: - name: CACHE_IMAGE value: {{ include "trainer.runtimeImage" (list .Values.dataCache.cacheImage .) | quote }} @@ -68,9 +82,24 @@ spec: spec: template: spec: + securityContext: + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault containers: - name: node image: pytorch/pytorch:2.10.0-cuda12.8-cudnn9-runtime + workingDir: /tmp + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault env: - name: TRAIN_JOB_NAME valueFrom: diff --git a/charts/kubeflow-trainer/templates/runtimes/torch-distributed.yaml b/charts/kubeflow-trainer/templates/runtimes/torch-distributed.yaml index bb5a3237f6..4d27d1d685 100644 --- a/charts/kubeflow-trainer/templates/runtimes/torch-distributed.yaml +++ b/charts/kubeflow-trainer/templates/runtimes/torch-distributed.yaml @@ -40,7 +40,22 @@ spec: spec: template: spec: + securityContext: + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault containers: - name: node image: pytorch/pytorch:2.10.0-cuda12.8-cudnn9-runtime + workingDir: /tmp + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault {{- end }} diff --git a/charts/kubeflow-trainer/tests/runtimes/torch_distributed_test.yaml b/charts/kubeflow-trainer/tests/runtimes/torch_distributed_test.yaml index 6b4462e385..df8e28a3f7 100644 --- a/charts/kubeflow-trainer/tests/runtimes/torch_distributed_test.yaml +++ b/charts/kubeflow-trainer/tests/runtimes/torch_distributed_test.yaml @@ -77,3 +77,31 @@ tests: - equal: path: spec.template.spec.replicatedJobs[0].template.spec.template.spec.containers[0].image value: pytorch/pytorch:2.10.0-cuda12.8-cudnn9-runtime + + - it: Should set restricted PSS compatible security contexts + set: + runtimes: + torchDistributed: + enabled: true + asserts: + - equal: + path: spec.template.spec.replicatedJobs[0].template.spec.template.spec.securityContext + value: + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault + - equal: + path: spec.template.spec.replicatedJobs[0].template.spec.template.spec.containers[0].workingDir + value: /tmp + - equal: + path: spec.template.spec.replicatedJobs[0].template.spec.template.spec.containers[0].securityContext + value: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault diff --git a/charts/kubeflow-trainer/tests/runtimes/torch_distributed_with_cache_test.yaml b/charts/kubeflow-trainer/tests/runtimes/torch_distributed_with_cache_test.yaml index 8b94b9e58f..b2e0864150 100644 --- a/charts/kubeflow-trainer/tests/runtimes/torch_distributed_with_cache_test.yaml +++ b/charts/kubeflow-trainer/tests/runtimes/torch_distributed_with_cache_test.yaml @@ -138,3 +138,57 @@ tests: - equal: path: spec.template.spec.replicatedJobs[1].dependsOn[0].status value: Complete + + - it: Should set restricted PSS compatible security contexts + set: + dataCache: + enabled: true + cacheImage: + registry: ghcr.io + repository: kubeflow/trainer/data-cache + tag: "v1.0.0" + runtimes: + torchDistributedWithCache: + enabled: true + asserts: + - equal: + path: spec.template.spec.replicatedJobs[0].template.spec.template.spec.securityContext + value: + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault + - notExists: + path: spec.template.spec.replicatedJobs[0].template.spec.template.spec.containers[0].workingDir + - equal: + path: spec.template.spec.replicatedJobs[0].template.spec.template.spec.containers[0].securityContext + value: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault + - equal: + path: spec.template.spec.replicatedJobs[1].template.spec.template.spec.securityContext + value: + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault + - equal: + path: spec.template.spec.replicatedJobs[1].template.spec.template.spec.containers[0].workingDir + value: /tmp + - equal: + path: spec.template.spec.replicatedJobs[1].template.spec.template.spec.containers[0].securityContext + value: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault diff --git a/manifests/base/runtimes/data-cache/torch_distributed_with_cache.yaml b/manifests/base/runtimes/data-cache/torch_distributed_with_cache.yaml index 0100479fc5..d9d6d84dc4 100644 --- a/manifests/base/runtimes/data-cache/torch_distributed_with_cache.yaml +++ b/manifests/base/runtimes/data-cache/torch_distributed_with_cache.yaml @@ -36,9 +36,23 @@ spec: template: spec: serviceAccountName: kubeflow-trainer-cache-initializer + securityContext: + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault containers: - name: dataset-initializer image: ghcr.io/kubeflow/trainer/dataset-initializer + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault env: - name: CACHE_IMAGE value: "ghcr.io/kubeflow/trainer/data-cache:latest" @@ -58,9 +72,24 @@ spec: spec: template: spec: + securityContext: + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault containers: - name: node image: pytorch/pytorch:2.10.0-cuda12.8-cudnn9-runtime + workingDir: /tmp + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault env: - name: TRAIN_JOB_NAME valueFrom: diff --git a/manifests/base/runtimes/torch_distributed.yaml b/manifests/base/runtimes/torch_distributed.yaml index 52e7a6b68b..39cab9e426 100644 --- a/manifests/base/runtimes/torch_distributed.yaml +++ b/manifests/base/runtimes/torch_distributed.yaml @@ -33,6 +33,21 @@ spec: spec: template: spec: + securityContext: + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault containers: - name: node image: pytorch/pytorch:2.10.0-cuda12.8-cudnn9-runtime + workingDir: /tmp + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsNonRoot: true + runAsUser: 1000 + seccompProfile: + type: RuntimeDefault