- name: Add prometheus-community helm chart repo
  kubernetes.core.helm_repository:
    name: prometheus-community
    repo_url: "https://prometheus-community.github.io/helm-charts"

- name: Install Prometheus Operator (Kube Prometheus Stack)
  kubernetes.core.helm:
    name: prometheus-operator
    chart_ref: prometheus-community/kube-prometheus-stack
    release_namespace: monitoring
    create_namespace: true
    values: 
      grafana:
        enabled: false
  loop:
    - karpenter
    - cas
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item | upper)) }}"

- name: Add cluster autoscaler chart repo
  kubernetes.core.helm_repository:
    name: autoscaler
    repo_url: "https://icr.io/helm/iks-charts"

- name: Deploy Cluster Autoscaler (IKS)
  kubernetes.core.helm:
    name: cas
    chart_ref: autoscaler/cluster-autoscaler
    release_namespace: cas
    set_values:
      - value: "'autoDiscovery.clusterName'=cas-iks"
        value_type: string
    environment:
      KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_CAS') }}"

# - name: Deploy Karpenter Autoscaler (IKS)
#   kubernetes.core.helm:
#     name: karpenter
#     chart_ref: pfeifferj/ibm-cloud-karpenter
#     release_namespace: karpenter
#   environment:
#     KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_KARPENTER' }}"

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

- name: Create service account token secret for Prometheus
  kubernetes.core.k8s:
    resource_definition:
      api_version: v1
      kind: Secret
      metadata:
        name: prometheus-sa-token
        namespace: monitoring
        annotations:
          kubernetes.io/service-account.name: prometheus-sa
      type: kubernetes.io/service-account-token
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item | upper)) }}"
  loop:
    - karpenter
    - cas

- name: Retrieve the token for Prometheus SA
  kubernetes.core.k8s_info:
    api_version: v1
    kind: Secret
    namespace: monitoring
    name: prometheus-sa-token
  register: prometheus_sa_token
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item | upper)) }}" 
  loop:
    - karpenter
    - cas

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

- name: Start port-forwarding for Prometheus
  ansible.builtin.command:
    cmd: "kubectl port-forward svc/prometheus-operator-kube-p-prometheus -n monitoring {{ item.port }}:9090"
    environment:
      KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item.cluster | upper)) }}"
    async: 3600
    poll: 0
  loop:
    - { cluster: 'karpenter', port: 9091 }
    - { cluster: 'cas', port: 9092 }

- name: Set Prometheus endpoint facts for port-forwarded services
  set_fact:
    "PROM_ENDPOINT_IKS_{{ item.cluster | upper }}": "http://localhost:{{ item.port }}"
  loop:
    - { cluster: 'karpenter', port: 9091 }
    - { cluster: 'cas', port: 9092 }
    