Python3.11 can't open file [Errno 2] No such file or directory

55 Views Asked by At

I updated readOnlyRootFilesystem: true in my cronjob yaml but started getting the error

OSError: [Errno 30] Read-only file system: '/absolute/path/to/your/folder'

I further added an empty directory volumeMounts: - name: tmp-volume mountPath: absolute/path/to/your/folder volumes:

  • name: tmp-volume emptyDir: {}

After adding this I am starting to get

**python3.11: can't open file 'absolute/path/to/your/folder/text.py': [Errno 2] No such file or directory**

I have attached the cronjob yaml

           containers:
            - env:
              - name: ENV_VAR
                value: value
              - name: MAINTAIN_HA_POOL
                value: "true"
              - name: PROCESS_SCHEDULER_REQUESTS
                value: "true"
              - name: MAINTAIN_NODE_POOL
                value: "false"
              - name: ENABLE_LSV3
                value: "false"
              image: us.icr.io/nz-cloud/nzsaas-common-pool-cronjob:master-2.1.2.0-20240329-111245
              imagePullPolicy: Always
              name: common-machine-pool-cronjob-container
              resources: {}
              securityContext:
                privileged: true
                readOnlyRootFilesystem: true
              terminationMessagePath: /dev/termination-log
              terminationMessagePolicy: File
              volumeMounts:
              - mountPath: /opt/ibm/common-pool-cronjob/
                name: tmp-volume
            volumes:
            - emptyDir: {}
              name: tmp-volume  

FYI I have given CHMOD 777 to the particular folder

1

There are 1 best solutions below

0
DazWilkin On

Further to my comment, creating an emptyDir creates an empty folder.

You need to create text.py in the folder referenced by emptyDir before you can interact with it.

By way of example:

app.yaml:

kind: Deployment
apiVersion: apps/v1
metadata:
  name: app
  labels:
    app: test
spec:
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: docker.io/alpine:3.19
        args:
        - /bin/sh
        - -c
        - |
          # The emptyDir volume's mount path
          DIR="/absolute/path/to/your/folder"
          # Some file
          FILE="text.py"
          echo "Starting"
          if ! test -f ${DIR}/${FILE}
          then
            echo "${FILE} does **not** exist"
          fi
          echo "Creating ${DIR}/${FILE}"
          touch ${DIR}/${FILE}
          if test -f ${DIR}/${FILE}
          then
            echo "${FILE} exists"
          fi
          echo "Done" 
        volumeMounts:
        - name: tmp-volume
          mountPath: /absolute/path/to/your/folder
      volumes:
      - name: tmp-volume
        emptyDir:
          sizeLimit: 1Mi

And:

NAMESPACE="78244509"
kubectl create namespace ${NAMESPACE}

kubectl create \
--filename=./app.yaml \
--namespace=${NAMESPACE}

kubectl logs deployment/app \
--namespace=${NAMESPACE}

Yields:

Starting
Creating /absolute/path/to/your/folder/text.py
text.py exists
Done

and:

kubectl delete \
--filename=./app.yaml \
--namespacpe=${NAMESPACE}