# 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:
  - generate-data.yaml
  - configmap.yaml
  - ../../base

images:
- name: fio
  newName: us-docker.pkg.dev/fsi-research-1/research-images/fio
  newTag: latest

configMapGenerator:
- literals:
  - JOB_NAME=generate-data-lustre-small-files
  - COMPLETIONS=1
  - PARALLELISM=1
  - 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
  # - OUTPUT_DIR=/data/test_files/large_files
  - OUTPUT_DIR=/data/test_files/small_files
  - MAX_RETRIES=5
  - SLEEP_TIME=10
  - NUM_FILES=1000
  - MIN_FILE_SIZE_KB=50
  - MAX_FILE_SIZE_KB=5000
  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.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
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.COMPLETIONS
  targets:
  - select:
      kind: Job
    fieldPaths:
    - spec.completions
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.PARALLELISM
  targets:
  - select:
      kind: Job
    fieldPaths:
    - spec.parallelism
- source:
    kind: ConfigMap
    name: lustre-vars
    fieldPath: data.OUTPUT_DIR
  targets:
  - select:
      kind: Job
    fieldPaths:
    - spec.template.spec.containers.[name=file-generator].env.[name=OUTPUT_DIR].value
