Run `find` command inside Kubernetes CronJob

Viewed 77

Unable to remove files older than one day from the mounted volume. My yaml:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cfs-cleanup
spec:
  concurrencyPolicy: Forbid
  schedule: '0 4 * * *'
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 10
  jobTemplate:
    spec:
      backoffLimit: 2
      activeDeadlineSeconds: 600
      template:
        metadata:
          annotations:
            sidecar.istio.io/inject: "false"
        spec:
          restartPolicy: Never
          containers:
            - name: cfs-cleanup
              image: alpine:3.13.2
              command: ["find", "/root/volumes/nginx-cache/cfs* -type f -mtime +1 -exec rm -f {} \;"]
              volumeMounts:
                - name: cache
                  mountPath: /root/volumes/nginx-cache

          volumes:
            - name: cache
              hostPath:
                path: /root/volumes/nginx-cache
                type: DirectoryOrCreate

the container fails without an error and does nothing. Could it be that command gets executed before volume is mounted?

1 Answers

I'm just guessing, but in your command

command: ["find", "/root/volumes/nginx-cache/cfs* -type f -mtime +1 -exec rm -f {} \;"]

path /root/volumes/nginx-cache/cfs* won't get expanded.

To verify it, just try command: ["ls", "/root/volumes/nginx-cache/cfs*"]

You can try to wrap your command in sh -c ... like

command: ["sh", "-c", "find /root/volumes/nginx-cache/cfs* -type f -mtime +1 -exec rm -f {} \;"]

Also, your command is possibly wrong altogether. You merged all arguments of find executable into single string (which shouldn't be problem when invoked via sh -c ....

Related