- name: Generate a UUID for tagging
  ansible.builtin.command: uuidgen
  register: generated_uuid

- name: Set UUID fact
  ansible.builtin.set_fact:
    tag_uuid: "{{ generated_uuid.stdout }}"

# 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.30.4"
      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 }}"
      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 }}"

- name: Get kubeconfig for clusters
  command: terraform output -raw kubeconfig_{{ item }}
  register: kubeconfig_output
  loop:
    - karpenter
    - cas

- name: Set kubeconfig for further tasks
  set_fact:
    "KUBECONFIG_{{ item | upper }}": "{{ kubeconfig_output.stdout }}"
  loop:
    - karpenter
    - cas

- 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_' + cluster_name | upper) }}"

- name: Create service account token for Prometheus
  kubernetes.core.k8s:
    api_version: v1
    kind: ServiceAccount
    namespace: monitoring
    name: prometheus-sa
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_' + cluster_name | upper) }}"
  loop:
    - karpenter
    - cas

- name: Retrieve the token for Prometheus SA
  kubernetes.core.k8s_info:
    api_version: v1
    kind: Secret
    namespace: monitoring
    label_selectors:
      - "kubernetes.io/service-account.name=prometheus-sa"
  register: prometheus_sa_token
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_' + cluster_name | upper) }}"
  loop:
    - karpenter
    - cas

- name: Set Prometheus token fact
  set_fact:
    "PROM_TOKEN_{{ item | upper }}": "{{ item.data.token | b64decode }}"
  with_items: "{{ prometheus_sa_token.resources }}"
  loop_control:
    loop_var: cluster_name

- name: Deploy Prometheus for monitoring
  kubernetes.core.helm:
    name: prometheus
    chart_ref: stable/prometheus
    release_namespace: monitoring
    create_namespace: true
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_' + cluster_name | upper) }}"
  loop:
    - karpenter
    - cas

- name: Deploy baseline configuration for monitoring
  kubernetes.core.helm:
    name: baseline
    chart_ref: '../../helm'
    values:
      cluster_name: "{{ item }}"
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_' + cluster_name | upper) }}"
  loop:
    - karpenter
    - cas
