# 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: OUTPUT_DIR
          value: test_files
        args:
          - |
            echo "Data cleanup starting..."
            if [ -z "${OUTPUT_DIR}" ]; then
              echo "OUTPUT_DIR not set, skipping deletion."
            else
              echo "Deleting contents of: /data/${OUTPUT_DIR}"
              rm -rf /data/${OUTPUT_DIR}
            fi
            file_count=$(find /data/${OUTPUT_DIR} -mindepth 1 -print0 | wc -l --files0-from=-)
            if [ "$file_count" -gt 0 ]; then
              echo "Error: Files remaining after cleanup. Count: $file_count"
              exit 1
            fi
            echo "Data cleanup complete."
        volumeMounts:
        - name: parallelstore-volume
          mountPath: /data
      restartPolicy: Never
      volumes:
      - name: parallelstore-volume
        persistentVolumeClaim:
          claimName: parallelstore-pvc
  ttlSecondsAfterFinished: 120
