#######################################################################################
# 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: cluster-management-wft
  namespace: osm-workflows
spec:
  templates:

  # Create a PaaS cluster using CrossPlane (any cloud)
  - name: create-crossplane-cluster
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"
      - name: sw_catalogs_volume_name
      - name: sw_catalogs_mount_path
        value: "/sw-catalogs"

      # Specific parameters
      - name: cluster_kustomization_name
      - name: cluster_name
      ## As of today, one among `aks`, `eks` or `gke`:
      - name: cluster_type
      - name: providerconfig_name
      - name: vm_size
      - name: node_count
      - name: cluster_location
      - name: k8s_version
      - name: public_key_mgmt
      - name: public_key_new_cluster
      - name: secret_name_private_age_key_for_new_cluster
      - name: key_name_in_secret
        value: "agekey"
      - name: fleet_repo_url
      - name: sw_catalogs_repo_url
      - name: mgmt_project_name
        value: "osm_admin"
      ## Do we want to skip OSM's bootstrap?
      - name: skip_bootstrap
        value: "false"
      ## AKS only (otherwise, empty)
      - name: rg_name
        values: ""
      ## GKE only (otherwise, empty)
      - name: preemptible_nodes
        values: "false"
      # Other parameters - Recommended to keep defaults
      - name: mgmt_cluster_name
        value: "_management"
      - name: base_templates_path
        value: "cloud-resources"
      - name: cloned_fleet_folder_name
        value: "fleet-osm"
      - name: cloned_sw_catalogs_folder_name
        value: "sw-catalogs-osm"
      # Debug?
      - name: debug
        value: "false"

    container:
      image: ttl.sh/osm-krm-functions:24h
      # imagePullPolicy: Always
      env:
      - name: PRIVATE_KEY_NEW_CLUSTER
        valueFrom:
          secretKeyRef:
            name: "{{inputs.parameters.secret_name_private_age_key_for_new_cluster}}"
            key: "{{inputs.parameters.key_name_in_secret}}"
      - name: DEBUG
        value: "{{inputs.parameters.debug}}"
      command: ["/app/scripts/entrypoint.sh"]
      args:
      - create_crossplane_cluster
      - "{{inputs.parameters.cluster_kustomization_name}}"
      - "{{inputs.parameters.cluster_name}}"
      - "{{inputs.parameters.cluster_type}}"
      - "{{inputs.parameters.providerconfig_name}}"
      - "{{inputs.parameters.vm_size}}"
      - "{{inputs.parameters.node_count}}"
      - "{{inputs.parameters.cluster_location}}"
      - "{{inputs.parameters.k8s_version}}"
      - "{{inputs.parameters.public_key_mgmt}}"
      - "{{inputs.parameters.public_key_new_cluster}}"
      - ''
      - "{{inputs.parameters.rg_name}}"
      - "{{inputs.parameters.preemptible_nodes}}"
      - "{{inputs.parameters.fleet_mount_path}}/{{inputs.parameters.cloned_fleet_folder_name}}"
      - "{{inputs.parameters.fleet_repo_url}}"
      - "{{inputs.parameters.sw_catalogs_mount_path}}/{{inputs.parameters.cloned_sw_catalogs_folder_name}}"
      - "{{inputs.parameters.sw_catalogs_repo_url}}"
      - "{{inputs.parameters.skip_bootstrap}}"
      - "{{inputs.parameters.mgmt_project_name}}"
      - "{{inputs.parameters.mgmt_cluster_name}}"
      - "{{inputs.parameters.base_templates_path}}"

      volumeMounts:
      - name: fleet-repo-volume
        mountPath: '{{inputs.parameters.fleet_mount_path}}'
      - name: sw-catalogs-repo-volume
        mountPath: '{{inputs.parameters.sw_catalogs_mount_path}}'
    volumes:
      - name: fleet-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.fleet_volume_name}}'
      - name: sw-catalogs-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.sw_catalogs_volume_name}}'
    securityContext:
      runAsUser: 10000
    #   runAsGroup: 10000
    #   fsGroup: 10000


  # Update a PaaS cluster created using CrossPlane (any cloud)
  - name: update-crossplane-cluster
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"
      - name: sw_catalogs_volume_name
      - name: sw_catalogs_mount_path
        value: "/sw-catalogs"

      # Specific parameters
      - name: cluster_kustomization_name
      - name: cluster_name
      ## As of today, one among `aks`, `eks` or `gke`:
      - name: cluster_type
      - name: providerconfig_name
      - name: vm_size
      - name: node_count
      - name: cluster_location
      - name: k8s_version
      - name: public_key_mgmt
      - name: public_key_new_cluster
      - name: secret_name_private_age_key_for_new_cluster
      - name: key_name_in_secret
        value: "agekey"
      - name: fleet_repo_url
      - name: sw_catalogs_repo_url
      - name: mgmt_project_name
        value: "osm_admin"
      ## AKS only (otherwise, empty)
      - name: rg_name
        values: ""
      ## GKE only (otherwise, empty)
      - name: preemptible_nodes
        values: "false"
      # Other parameters - Recommended to keep defaults
      - name: mgmt_cluster_name
        value: "_management"
      - name: base_templates_path
        value: "cloud-resources"
      - name: cloned_fleet_folder_name
        value: "fleet-osm"
      - name: cloned_sw_catalogs_folder_name
        value: "sw-catalogs-osm"
      # Debug?
      - name: debug
        value: "false"

    container:
      image: ttl.sh/osm-krm-functions:24h
      # imagePullPolicy: Always
      env:
      - name: PRIVATE_KEY_NEW_CLUSTER
        valueFrom:
          secretKeyRef:
            name: "{{inputs.parameters.secret_name_private_age_key_for_new_cluster}}"
            key: "{{inputs.parameters.key_name_in_secret}}"
      - name: DEBUG
        value: "{{inputs.parameters.debug}}"
      command: ["/app/scripts/entrypoint.sh"]
      args:
      - update_crossplane_cluster
      - "{{inputs.parameters.cluster_kustomization_name}}"
      - "{{inputs.parameters.cluster_name}}"
      - "{{inputs.parameters.cluster_type}}"
      - "{{inputs.parameters.providerconfig_name}}"
      - "{{inputs.parameters.vm_size}}"
      - "{{inputs.parameters.node_count}}"
      - "{{inputs.parameters.cluster_location}}"
      - "{{inputs.parameters.k8s_version}}"
      - "{{inputs.parameters.public_key_mgmt}}"
      - "{{inputs.parameters.public_key_new_cluster}}"
      - ''
      - "{{inputs.parameters.rg_name}}"
      - "{{inputs.parameters.preemptible_nodes}}"
      - "{{inputs.parameters.fleet_mount_path}}/{{inputs.parameters.cloned_fleet_folder_name}}"
      - "{{inputs.parameters.fleet_repo_url}}"
      - "{{inputs.parameters.sw_catalogs_mount_path}}/{{inputs.parameters.cloned_sw_catalogs_folder_name}}"
      - "{{inputs.parameters.sw_catalogs_repo_url}}"
      ## Note that during upgrades, OSM's bootstrap is avoided:
      - "true"
      - "{{inputs.parameters.mgmt_project_name}}"
      - "{{inputs.parameters.mgmt_cluster_name}}"
      - "{{inputs.parameters.base_templates_path}}"

      volumeMounts:
      - name: fleet-repo-volume
        mountPath: '{{inputs.parameters.fleet_mount_path}}'
      - name: sw-catalogs-repo-volume
        mountPath: '{{inputs.parameters.sw_catalogs_mount_path}}'
    volumes:
      - name: fleet-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.fleet_volume_name}}'
      - name: sw-catalogs-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.sw_catalogs_volume_name}}'
    securityContext:
      runAsUser: 10000
    #   runAsGroup: 10000
    #   fsGroup: 10000


  # TODO: Deprecated - To be removed
  # Create a PaaS cluster in Azure
  - name: create-cluster-aks
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"
      - name: sw_catalogs_volume_name
      - name: sw_catalogs_mount_path
        value: "/sw-catalogs"

      # Specific parameters
      - name: cluster_name
      - name: vm_size
      - name: node_count
      - name: cluster_location
      - name: rg_name
      - name: k8s_version
      - name: providerconfig_name
      - name: cluster_kustomization_name
      - name: mgmt_project_name
        value: "osm_admin"

      # Other parameters - Recommended to keep defaults
      - name: fleet_repo_dir
        value: "/fleet/fleet-osm/"
      - name: sw_catalogs_repo_dir
        value: "/sw-catalogs/sw-catalogs-osm/"
      - name: target_folder
        value: "{{inputs.parameters.fleet_repo_dir}}/{{inputs.parameters.mgmt_project_name}}/managed-resources/_management"
      - name: manifest_filename
        value: "{{inputs.parameters.cluster_name}}.yaml"
      - name: templates
        value: "{{inputs.parameters.sw_catalogs_repo_dir}}/cloud-resources/aks/templates/"
      - name: template_manifest_filename
        value: "aks01.yaml"
    volumes:
      - name: fleet-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.fleet_volume_name}}'
      - name: sw-catalogs-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.sw_catalogs_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}}'
      - name: sw-catalogs-repo-volume
        mountPath: '{{inputs.parameters.sw_catalogs_mount_path}}'
      command: ["/app/scripts/entrypoint.sh"]
      args:
      - create_cluster_aks
      - "{{inputs.parameters.cluster_name}}"
      - "{{inputs.parameters.vm_size}}"
      - "{{inputs.parameters.node_count}}"
      - "{{inputs.parameters.cluster_location}}"
      - "{{inputs.parameters.rg_name}}"
      - "{{inputs.parameters.k8s_version}}"
      - "{{inputs.parameters.providerconfig_name}}"
      - "{{inputs.parameters.cluster_kustomization_name}}"
      - "{{inputs.parameters.target_folder}}"
      - "{{inputs.parameters.manifest_filename}}"
      - "{{inputs.parameters.templates}}"
      - "{{inputs.parameters.template_manifest_filename}}"


  # Bootstrap remote cluster running in **ANY cloud**
  - name: bootstrap-remote-cluster
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"
      - name: sw_catalogs_volume_name
      - name: sw_catalogs_mount_path
        value: "/sw-catalogs"

      # Specific parameters
      - name: cluster_name
      - name: cluster_kustomization_name
      - name: public_key_mgmt
      - name: public_key_new_cluster
      - name: secret_name_private_age_key_for_new_cluster
      - name: key_name_in_secret
        value: "agekey"
      - name: fleet_repo_url
      - name: sw_catalogs_repo_url

      # Other parameters - Recommended to keep defaults
      - name: mgmt_project_name
        value: "osm_admin"
      - name: imported_cluster
        value: "false"
      - name: fleet_repo_dir
        value: "/fleet/fleet-osm/"
      - name: sw_catalogs_repo_dir
        value: "/sw-catalogs/sw-catalogs-osm/"

      # Debug/dry run?
      - name: debug
        value: "false"

    container:
      image: ttl.sh/osm-krm-functions:24h
      # imagePullPolicy: Always
      env:
      - name: PRIVATE_KEY_NEW_CLUSTER
        valueFrom:
          secretKeyRef:
            name: "{{inputs.parameters.secret_name_private_age_key_for_new_cluster}}"
            key: "{{inputs.parameters.key_name_in_secret}}"
      - name: DEBUG
        value: "{{inputs.parameters.debug}}"
      command: ["/app/scripts/entrypoint.sh"]
      args:
      - create_bootstrap_for_remote_cluster
      - "{{inputs.parameters.cluster_name}}"
      - "{{inputs.parameters.cluster_kustomization_name}}"
      - "{{inputs.parameters.fleet_repo_dir}}"
      - "{{inputs.parameters.sw_catalogs_repo_dir}}"
      - "{{inputs.parameters.fleet_repo_url}}"
      - "{{inputs.parameters.sw_catalogs_repo_url}}"
      - "{{inputs.parameters.mgmt_project_name}}"
      - "{{inputs.parameters.public_key_mgmt}}"
      - "{{inputs.parameters.public_key_new_cluster}}"
      - ''
      - "{{inputs.parameters.imported_cluster}}"
      volumeMounts:
      - name: fleet-repo-volume
        mountPath: '{{inputs.parameters.fleet_mount_path}}'
      - name: sw-catalogs-repo-volume
        mountPath: '{{inputs.parameters.sw_catalogs_mount_path}}'
    volumes:
      - name: fleet-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.fleet_volume_name}}'
      - name: sw-catalogs-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.sw_catalogs_volume_name}}'
    securityContext:
      runAsUser: 10000
    #   runAsGroup: 10000
    #   fsGroup: 10000


  # Disconnect Flux in remote cluster running in **ANY cloud**
  - name: disconnect-flux-remote-cluster
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"

      # Specific parameters
      - name: cluster_kustomization_name
      - name: mgmt_project_name
        value: "osm_admin"

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

      # Debug/dry run?
      - name: debug
        value: "false"

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


  # Delete cluster from **ANY** cloud
  - name: delete-cluster
    inputs:
      parameters:
      # Volumes with cloned repos
      - name: fleet_volume_name
      - name: fleet_mount_path
        value: "/fleet"

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

      # Other parameters - Recommended to keep defaults
      - name: fleet_repo_dir
        value: "/fleet/fleet-osm/"
      - name: mgmt_resources_dir
        value: "{{inputs.parameters.fleet_repo_dir}}/{{inputs.parameters.project_name}}/managed-resources/_management"

    container:
      image: ttl.sh/osm-krm-functions:24h
      # imagePullPolicy: Always
      command: ["/app/scripts/entrypoint.sh"]
      args:
      - delete_remote_cluster
      - "{{inputs.parameters.cluster_kustomization_name}}"
      - "{{inputs.parameters.project_name}}"
      - "{{inputs.parameters.fleet_repo_dir}}"
      - "{{inputs.parameters.mgmt_resources_dir}}"

      volumeMounts:
      - name: fleet-repo-volume
        mountPath: '{{inputs.parameters.fleet_mount_path}}'
    volumes:
      - name: fleet-repo-volume
        persistentVolumeClaim:
          claimName: '{{inputs.parameters.fleet_volume_name}}'
    securityContext:
      runAsUser: 10000
    #   runAsGroup: 10000
    #   fsGroup: 10000
