0
 kind: Job
metadata:
  name: test
spec:
  ttlSecondsAfterFinished: 3600
  backoffLimit: 0
  template:
    metadata:
      annotations:
        version: "1.0"
      labels:
        component: test
      name: test
    spec:
      securityContext:
          runAsUser: 1000
          runAsGroup: 1000
          fsGroup: 5000
      restartPolicy: Never
      initContainers:
        - name: test-init
          image:  sampleimage:latest
          volumeMounts:
          - name: testvol
            mountPath: /u01/subpath
          command: ['sh', '-c', "whoami && cd /u01/subpath && echo 1 && mkdir -p -m 0777 folder1 && echo 2 && mkdir -p -m 0777 folder2 && echo 4 && echo done"]
      containers:
        - image:  sampleimage:latest
          imagePullPolicy: Always
          name: testcontainer
          resources:
            requests:
              cpu: 1
              memory: 4G
            limits:
              cpu: 1
              memory: 4G

          volumeMounts:
            - name: testvol
              mountPath: /u01/subpath/folder1
              subPath: folder1
            - name: testvol
              mountPath: /u01/subpath/folder2
              subPath: folder2
          command: ['sh', '-c','ls -lrt /u01 ']
      volumes:
      - name: testvol
        persistentVolumeClaim:
          claimName: testpvc```

I am trying to create a job with the above specs. the default user of folder1 and folder 2 is coming as root. How can the user of the folder be changed when a persistent volume claim is mounted to a folder with sub-paths as shown? I tried to change the permission in init-containers as chmod 777 -R /u01/subpath but it throws an error saying cannot change the owner or permission of the folder.

David Maze
  • 130,717
  • 29
  • 175
  • 215
Sourabh Ninawe
  • 389
  • 1
  • 6
  • 17
  • Have you tried to use an `initContainer` to run the `chmod` like in this answer: https://stackoverflow.com/a/51195446/12257134 ? – Dawid Kruk Apr 19 '21 at 17:45
  • I have tried @DawidKruk but the user when it tries to run init-container is also not a root user so chmod chown everything fails – Sourabh Ninawe Apr 21 '21 at 06:15
  • I saw that your securityContext is defined on the whole resource and it's possible to assign it per `container` and `initContainer` like here: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container . Please check if that worked for you. – Dawid Kruk Apr 27 '21 at 14:58

1 Answers1

0

try chown uid example:

name: testcontainer
command: ["sh", "-c", "chmod 777 /u01/ && chown 1000:1000 /u01/"]