# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: batch/v1
kind: Job
metadata:
  name: data-cleanup-job
spec:
  completions: 1
  parallelism: 1
  template:
    spec:
      nodeSelector:
        cloud.google.com/compute-class: spot-capacity
      priorityClassName: higher-priority
      containers:
      - name: cleanup
        image: busybox
        command: ["sh", "-c"]
        env:
        - name: CLEANUP_DIR
          value: test_files
        - name: MOUNT_PATH
          value: /data
        args:
          - |
            echo "Data cleanup starting..."
            if [ -z "${CLEANUP_DIR}" ]; then
              echo "CLEANUP_DIR not set, skipping deletion."
            else
              echo "Deleting contents of: /data/${CLEANUP_DIR}"
              # Check if the directory exists before attempting to delete it
              if [ -d "/data/${CLEANUP_DIR}" ]; then
                rm -rf "/data/${CLEANUP_DIR}"
              else
                echo "Directory /data/${CLEANUP_DIR} does not exist, skipping deletion."
              fi
            fi
            # Check if the directory exists, and if it does, check if it is empty
            if [ -d "/data/${CLEANUP_DIR}" ]; then
              file_count=$(find "/data/${CLEANUP_DIR}" -mindepth 1 -maxdepth 1 -type f | wc -l)
              if [ "$file_count" -gt 0 ]; then
                echo "Error: Files remaining after cleanup. Count: $file_count"
                exit 1
              fi
            else
              echo "Directory /data/${CLEANUP_DIR} does not exist, skipping file count check."
            fi
            echo "Data cleanup complete."
        volumeMounts:
        - name: lustre-volume
          mountPath: /data
        resources:
          requests:
            memory: "16Gi"
            cpu: "4"
          limits:
            memory: "32Gi"
            cpu: "8"
      restartPolicy: Never
      volumes:
      - name: lustre-volume
        persistentVolumeClaim:
          claimName: lustre-pvc
  ttlSecondsAfterFinished: 120
