#######################################################################################
# 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: full-create-nodegroup-wft
  namespace: osm-workflows

spec:
  arguments:
    parameters:

    # Fleet repo
    - name: git_fleet_url
    - name: fleet_destination_folder
    - name: git_fleet_cred_secret

    # SW-Catalogs repo
    - name: git_sw_catalogs_url
    - name: sw_catalogs_destination_folder
    - name: git_sw_catalogs_cred_secret

    # Specific parameters - Generic cluster creation
    - name: nodegroup_name
    - name: nodegroup_kustomization_name
    - name: cluster_name
    - name: cluster_type
    - name: providerconfig_name
    - name: vm_size
    - name: node_count
    - name: cluster_location
    - name: configmap_name
    - name: role

    # Specific parameters - Bootstrap and credentials
    - 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"
    # Using `git_fleet_url` instead:
    # - name: fleet_repo_url
    # Using `git_sw_catalogs_url` instead:
    # - name: sw_catalogs_repo_url
    - name: mgmt_project_name
      value: "osm_admin"

    # Specific parameters - AKS only
    - name: rg_name
      values: ""

    # Specific parameters - GKE only
    - name: preemptible_nodes
      values: "false"

    # Advanced parameters - Recommended to keep defaults
    - name: skip_bootstrap
      value: "false"
    - 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/dry run?
    - name: debug
      value: "false"
    - name: dry_run
      value: "false"


  # # Cleanup policy
  # ttlStrategy:
  #   secondsAfterCompletion: 100 # Time to live after workflow is completed, replaces ttlSecondsAfterFinished
  #   secondsAfterSuccess: 50     # Time to live after workflow is successful
  #   secondsAfterFailure: 50     # Time to live after workflow fails

  entrypoint: create-nodegroup

  templates:
  # Main template
  - name: create-nodegroup
    inputs:
      parameters:
      # Fleet repo
      - name: git_fleet_url
      - name: fleet_destination_folder
      - name: git_fleet_cred_secret
      # SW-Catalogs repo
      - name: git_sw_catalogs_url
      - name: sw_catalogs_destination_folder
      - name: git_sw_catalogs_cred_secret
      # Specific parameters
      - name: nodegroup_name
      - name: nodegroup_kustomization_name
      - name: cluster_name
      - name: cluster_type
      - name: providerconfig_name
      - name: vm_size
      - name: node_count
      - name: cluster_location
      - name: configmap_name
      - name: role
      - name: public_key_mgmt
      - name: public_key_new_cluster
      - name: secret_name_private_age_key_for_new_cluster
      - name: key_name_in_secret
      # - name: fleet_repo_url
      # - name: sw_catalogs_repo_url
      - name: mgmt_project_name
      - name: skip_bootstrap
      ## AKS only (otherwise, ignored)
      - name: rg_name
      ## GKE only (otherwise, ignored)
      - name: preemptible_nodes
      # Other parameters - Recommended to keep defaults
      - name: mgmt_cluster_name
      - name: base_templates_path
      - name: cloned_fleet_folder_name
      - name: cloned_sw_catalogs_folder_name
      # Debug/dry run?
      - name: debug
      - name: dry_run

    steps:
    # ------ Preparations for transaction
    - - name: generate-fleet-volume-repo
        templateRef:
          name: k8s-resources-wft
          template: generate-volume
        arguments:
          parameters:
            - name: pvc-size
              value: '100Mi'
      - name: generate-sw-catalogs-volume-repo
        templateRef:
          name: k8s-resources-wft
          template: generate-volume
        arguments:
          parameters:
            - name: pvc-size
              value: '100Mi'
    - - name: clone-fleet
        templateRef:
          name: git-wft
          template: git-clone
        arguments:
          parameters:
          - name: mount_path
            value: "/fleet"
          - name: repo_url
            value: "{{inputs.parameters.git_fleet_url}}"
          - name: destination_folder
            value: "{{inputs.parameters.fleet_destination_folder}}"
          - name: git_cred_secret
            value: "{{inputs.parameters.git_fleet_cred_secret}}"
          - name: git_volume_name
            value: '{{steps.generate-fleet-volume-repo.outputs.parameters.pvc-name}}'
      - name: clone-sw-catalogs
        templateRef:
          name: git-wft
          template: git-clone
        arguments:
          parameters:
          - name: mount_path
            value: "/sw-catalogs"
          - name: repo_url
            value: "{{inputs.parameters.git_sw_catalogs_url}}"
          - name: destination_folder
            value: "{{inputs.parameters.sw_catalogs_destination_folder}}"
          - name: git_cred_secret
            value: "{{inputs.parameters.git_sw_catalogs_cred_secret}}"
          - name: git_volume_name
            value: '{{steps.generate-sw-catalogs-volume-repo.outputs.parameters.pvc-name}}'
    # ------ end of preparations for transaction

    # ------ Transformations
    # Create cluster in target cloud
    - - name: create-nodegroup
        templateRef:
          name: nodegroup-management-wft
          template: create-nodegroup
        arguments:
          parameters:
          # Volumes with cloned repos
          - name: fleet_volume_name
            value: '{{steps.generate-fleet-volume-repo.outputs.parameters.pvc-name}}'
          - name: fleet_mount_path
            value: "/fleet"
          - name: sw_catalogs_volume_name
            value: '{{steps.generate-sw-catalogs-volume-repo.outputs.parameters.pvc-name}}'
          - name: sw_catalogs_mount_path
            value: "/sw-catalogs"
          # Specific parameters
          - name: nodegroup_name
            value: "{{inputs.parameters.nodegroup_name}}"
          - name: nodegroup_kustomization_name
            value: "{{inputs.parameters.nodegroup_kustomization_name}}"
          - name: cluster_name
            value: "{{inputs.parameters.cluster_name}}"
          - name: cluster_type
            value: "{{inputs.parameters.cluster_type}}"
          - name: providerconfig_name
            value: "{{inputs.parameters.providerconfig_name}}"
          - name: vm_size
            value: "{{inputs.parameters.vm_size}}"
          - name: node_count
            value: "{{inputs.parameters.node_count}}"
          - name: cluster_location
            value: "{{inputs.parameters.cluster_location}}"
          - name: configmap_name
            value: "{{inputs.parameters.configmap_name}}"
          - name: role
            value: "{{inputs.parameters.role}}"
          - name: public_key_mgmt
            value: "{{inputs.parameters.public_key_mgmt}}"
          - name: public_key_new_cluster
            value: "{{inputs.parameters.public_key_new_cluster}}"
          - name: secret_name_private_age_key_for_new_cluster
            value: "{{inputs.parameters.secret_name_private_age_key_for_new_cluster}}"
          - name: key_name_in_secret
            value: "{{inputs.parameters.key_name_in_secret}}"
          ## Fed with `git_fleet_url` to avoid duplicates
          - name: fleet_repo_url
            value: "{{inputs.parameters.git_fleet_url}}"
          ## Fed with `git_sw_catalogs_url` to avoid duplicates
          - name: sw_catalogs_repo_url
            value: "{{inputs.parameters.git_sw_catalogs_url}}"
          - name: mgmt_project_name
            value: "{{inputs.parameters.mgmt_project_name}}"
          - name: skip_bootstrap
            value: "{{inputs.parameters.skip_bootstrap}}"
          ## AKS only
          - name: rg_name
            value: "{{inputs.parameters.rg_name}}"
          ## GKE only
          - name: preemptible_nodes
            value: "{{inputs.parameters.preemptible_nodes}}"
          # Other parameters - Recommended to keep defaults
          - name: mgmt_cluster_name
            value: "{{inputs.parameters.mgmt_cluster_name}}"
          - name: base_templates_path
            value: "{{inputs.parameters.base_templates_path}}"
          - name: cloned_fleet_folder_name
            value: "{{inputs.parameters.cloned_fleet_folder_name}}"
          - name: cloned_sw_catalogs_folder_name
            value: "{{inputs.parameters.cloned_sw_catalogs_folder_name}}"
          # Debug?
          - name: debug
            value: "{{inputs.parameters.debug}}"
    # ------ end of transformations

    # ------ Commit transaction
    - - name: push-to-fleet
        templateRef:
          name: git-wft
          template: git-commit-merge-push
        arguments:
          parameters:
          - name: mount_path
            value: "/fleet"
          - name: repo_folder
            value: "{{inputs.parameters.fleet_destination_folder}}"
          - name: git_cred_secret
            value: "{{inputs.parameters.git_fleet_cred_secret}}"
          - name: git_volume_name
            value: '{{steps.generate-fleet-volume-repo.outputs.parameters.pvc-name}}'
          - name: commit_message
            value: "Create nodegroup {{inputs.parameters.nodegroup_kustomization_name}} at {{inputs.parameters.cluster_location}}"
          - name: main_branch
            value: main
          - name: contrib_branch
            value: osm_contrib
          - name: dry_run
            value: "{{inputs.parameters.dry_run}}"
# ------ end of commit transaction
