#######################################################################################
# Copyright ETSI Contributors and Others.
#
# 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
#
#    http://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: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: profile-management-wft
  namespace: osm-workflows
spec:
  templates:

  # Create a profile
  - name: create-profile
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"

      # Specific parameters
      - name: profile_name
      - name: profile_type
      - name: project_name
        value: "osm_admin"
      - name: fleet_repo_url

      # Other parameters - Recommended to keep defaults
      - name: fleet_repo_dir
        value: "/fleet/fleet-osm/"

    volumes:
      - name: fleet-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.fleet_volume_name}}'
    container:
      image: ttl.sh/osm-krm-functions:24h
      # imagePullPolicy: Always
      # securityContext:
      #   runAsUser: 10000
      #   runAsGroup: 10000
      #   fsGroup: 10000
      volumeMounts:
      - name: fleet-repo-volume
        mountPath: '{{inputs.parameters.fleet_mount_path}}'
      command: ["/app/scripts/entrypoint.sh"]
      args:
      - create_profile
      - "{{inputs.parameters.profile_name}}"
      - "{{inputs.parameters.profile_type}}"
      - "{{inputs.parameters.project_name}}"
      - "{{inputs.parameters.fleet_repo_url}}"
      - "{{inputs.parameters.fleet_repo_dir}}"


  # Delete a profile
  - name: delete-profile
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"

      # Specific parameters
      - name: profile_name
      - name: profile_type
      - name: project_name
        value: "osm_admin"

      # Other parameters - Recommended to keep defaults
      - name: fleet_repo_dir
        value: "/fleet/fleet-osm/"

    volumes:
      - name: fleet-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.fleet_volume_name}}'
    container:
      image: ttl.sh/osm-krm-functions:24h
      # imagePullPolicy: Always
      # securityContext:
      #   runAsUser: 10000
      #   runAsGroup: 10000
      #   fsGroup: 10000
      volumeMounts:
      - name: fleet-repo-volume
        mountPath: '{{inputs.parameters.fleet_mount_path}}'
      command: ["/app/scripts/entrypoint.sh"]
      args:
      - delete_profile
      - "{{inputs.parameters.profile_name}}"
      - "{{inputs.parameters.profile_type}}"
      - "{{inputs.parameters.project_name}}"
      - "{{inputs.parameters.fleet_repo_dir}}"


  # Attach a profile to a cluster
  - name: attach-profile-to-cluster
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"

      # Specific parameters
      - name: profile_name
      - name: profile_type
      - name: project_name
        value: "osm_admin"
      - name: cluster_kustomization_name

      # Other parameters - Recommended to keep defaults
      - name: fleet_repo_dir
        value: "/fleet/fleet-osm/"
    volumes:
      - name: fleet-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.fleet_volume_name}}'
    container:
      image: ttl.sh/osm-krm-functions:24h
      # imagePullPolicy: Always
      # securityContext:
      #   runAsUser: 10000
      #   runAsGroup: 10000
      #   fsGroup: 10000
      volumeMounts:
      - name: fleet-repo-volume
        mountPath: '{{inputs.parameters.fleet_mount_path}}'
      command: ["/app/scripts/entrypoint.sh"]
      args:
      - attach_profile_to_cluster
      - "{{inputs.parameters.profile_name}}"
      - "{{inputs.parameters.profile_type}}"
      - "{{inputs.parameters.project_name}}"
      - "{{inputs.parameters.cluster_kustomization_name}}"
      - "{{inputs.parameters.fleet_repo_dir}}"


  # Detach profile from cluster
  - name: detach-profile-from-cluster
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"

      # Specific parameters
      - name: profile_name
      - name: profile_type
      - name: project_name
        value: "osm_admin"
      - name: cluster_kustomization_name

      # Other parameters - Recommended to keep defaults
      - name: fleet_repo_dir
        value: "/fleet/fleet-osm/"
    volumes:
      - name: fleet-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.fleet_volume_name}}'
    container:
      image: ttl.sh/osm-krm-functions:24h
      # imagePullPolicy: Always
      # securityContext:
      #   runAsUser: 10000
      #   runAsGroup: 10000
      #   fsGroup: 10000
      volumeMounts:
      - name: fleet-repo-volume
        mountPath: '{{inputs.parameters.fleet_mount_path}}'
      command: ["/app/scripts/entrypoint.sh"]
      args:
      - detach_profile_from_cluster
      - "{{inputs.parameters.profile_name}}"
      - "{{inputs.parameters.profile_type}}"
      - "{{inputs.parameters.project_name}}"
      - "{{inputs.parameters.cluster_kustomization_name}}"
      - "{{inputs.parameters.fleet_repo_dir}}"
