# create two clusters: one for karpenter, one for cas
- name: Create clusters with Terraform
  community.general.terraform:
    project_path: "{{ playbook_dir }}/../terraform-iks"
    state: present
    force_init: true
    complex_vars: true
    variables:
      flavour: "bx2.2x8" 
      ibmcloud_api_key: "{{ IBM_CLOUD_TOKEN }}"
      kube_version: "1.29.9"
      resource_group: "{{ RESOURCE_GROUP_ID }}"
      region: "us-south"
      vpc_name: "kscale-workload-vpc"
      vpc_id: "{{ VPC_ID }}"
      worker_count: 1
      worker_pool_count: 1
      tag_uuid: "{{ tag_uuid }}"
      kube_config_path: "{{ playbook_dir }}"
      zones:
        - name: us-south-1
          subnet_id: "{{ SUBNET_ID.US_SOUTH_1 }}"
        - name: us-south-2
          subnet_id: "{{ SUBNET_ID.US_SOUTH_2 }}"
        - name: us-south-3
          subnet_id: "{{ SUBNET_ID.US_SOUTH_3 }}"
  ignore_errors: yes 
  async: 7200  
  poll: 0      

- name: wait for terraform cluster creation to finish
  ansible.builtin.async_status:
    jid: "{{ terraform_create_cluster.ansible_job_id }}"
  register: terraform_cluster_result
  until: terraform_cluster_result.finished
  retries: 240   # retries * delay = 240 * 30 = 7200 seconds (2 hours)
  delay: 30  

- name: Find kubeconfig paths
  find:
    paths: "{{ playbook_dir }}/files/kubeconfig-iks-{{ item }}"
    recurse: no
    file_type: directory
  loop:
    - karpenter
    - cas
  register: kubeconfig_dirs

- name: Set kubeconfig for further tasks
  set_fact:
    "KUBECONFIG_IKS_{{ item.item | upper }}": "{{ item.files[0].path }}/config.yml"
  loop: "{{ kubeconfig_dirs.results }}"
  when: item.matched > 0
  
- name: Wait for cluster nodes to be ready
  shell: |
    kubectl get nodes --no-headers | awk '{print $2}' | grep -v 'Ready' | wc -l
  register: nodes_not_ready
  until: nodes_not_ready.stdout == "0"
  retries: 20
  delay: 30
  loop:
    - karpenter
    - cas
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item | upper)) }}"
