# 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: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - cleanup-disk.yaml
  - configmap.yaml
  - ../../base

images:
- name: cleanup
  newName: busybox
  newTag: latest

configMapGenerator:
- literals:
  - JOB_NAME=fio-cleanup
  - PROJECT_ID=fsi-research-1
  - LOCATION=us-central1-a
  - INSTANCE_NAME=lustre-us-central1-a
  - STORAGE_SIZE=16Ti
  - INSTANCE_IP=172.16.1.3
  - FILESYSTEM=testfs
  - CLEANUP_DIR=test_files/large_files
  - MOUNT_PATH=/data
  name: lustre-vars

replacements:
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.JOB_NAME
  targets:
  - select:
      kind: Job
    fieldPaths:
    - metadata.name
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.STORAGE_SIZE
  targets:
  - select:
      kind: PersistentVolume
    fieldPaths:
    - spec.capacity.storage
  - select:
      kind: PersistentVolumeClaim
    fieldPaths:
    - spec.resources.requests.storage
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.LOCATION
  targets:
  - select:
      kind: PersistentVolumeClaim
    fieldPaths:
    - spec.volumeName
    options:
      delimiter: '-'
      index: 2
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.CLEANUP_DIR
  targets:
  - select:
      kind: Job
    fieldPaths:
    - spec.template.spec.containers.[name=cleanup].env.[name=CLEANUP_DIR].value
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.MOUNT_PATH
  targets:
  - select:
      kind: Job
    fieldPaths:
    - spec.template.spec.containers.[name=cleanup].env.[name=MOUNT_PATH].value
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.MOUNT_PATH
  targets:
  - select:
      kind: Job
    fieldPaths:
    - spec.template.spec.containers.[name=cleanup].volumeMounts.[name=lustre-volume].mountPath
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.INSTANCE_IP
  targets:
  - select:
      kind: PersistentVolume
    fieldPaths:
    - spec.csi.volumeAttributes.ip
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.FILESYSTEM
  targets:
  - select:
      kind: PersistentVolume
    fieldPaths:
    - spec.csi.volumeAttributes.filesystem

# Volume handle specific replacement
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.PROJECT_ID
  targets:
  - select:
      kind: PersistentVolume
    fieldPaths:
    - spec.csi.volumeHandle
    options:
      delimiter: '/'
      index: 0
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.LOCATION
  targets:
  - select:
      kind: PersistentVolume
    fieldPaths:
    - spec.csi.volumeHandle
    options:
      delimiter: '/'
      index: 1
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.INSTANCE_NAME
  targets:
  - select:
      kind: PersistentVolume
    fieldPaths:
    - spec.csi.volumeHandle
    options:
      delimiter: '/'
      index: 2
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.LOCATION
  targets:
  - select:
      kind: PersistentVolume
    fieldPaths:
    - metadata.name
    options:
      delimiter: '-'
      index: 2
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.LOCATION
  targets:
  - select:
      kind: PersistentVolumeClaim
    fieldPaths:
    - metadata.name
    options:
      delimiter: '-'
      index: 2

- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.LOCATION
  targets:
  - select:
      kind: Job
    fieldPaths:
    - spec.template.spec.volumes.[name=lustre-volume].persistentVolumeClaim.claimName
    options:
      delimiter: '-'
      index: 2
