- 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
      prometheus:
        prometheusSpec:
          enableAdminAPI: true
  loop:
    - karpenter
    - cas
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item | upper)) }}"

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

- name: Add karpenter provider chart repo
  kubernetes.core.helm_repository:
    name: karpenter-provider-ibm
    repo_url: "https://pfeifferj.github.io/karpenter-provider-ibm-cloud"

- name: Deploy Cluster Autoscaler (IKS)
  kubernetes.core.helm:
    name: cas
    chart_ref: autoscaler_iks/cluster-autoscaler
    release_namespace: cas
    create_namespace: true
    set_values:
      - value: "'autoDiscovery.clusterName'=cas-iks"
        value_type: string
    values:
      workerpools:
        - default:
            enabled: true
            max: 15
            min: 3
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_CAS') }}"

- name: Deploy Karpenter Autoscaler (IKS)
  kubernetes.core.helm:
    name: karpenter
    chart_ref: karpenter-provider-ibm/karpenter
    chart_version: 0.35.0
    release_namespace: karpenter
    create_namespace: true
    values:
      image:
        repository: ghcr.io/pfeifferj/karpenter-provider-ibm-cloud/controller
        digest: "sha256:7f2c6252bd280fc473631240a43dbfa17c6c4eae08001092c7525c59806d7c86"
        pullPolicy: IfNotPresent
      credentials:
        ibmApiKey: "{{ IBM_CLOUD_TOKEN }}"
        vpcApiKey: "{{ IBM_VPC_API_KEY }}"
        globalCatalogApiKey: "{{ IBM_VPC_API_KEY }}"
        region: "us-south"
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_KARPENTER') }}"

- name: Create service account for Prometheus
  kubernetes.core.k8s:
    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:
      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.item | upper }}": "{{ item.resources[0].data.token | b64decode }}"
  loop: "{{ prometheus_sa_token.results }}"

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