- name: Download kube-burner archive
  become: yes
  ansible.builtin.get_url:
    url: "https://github.com/kube-burner/kube-burner/releases/download/v1.10.9/kube-burner-V1.10.9-linux-x86_64.tar.gz"
    dest: "/tmp/kube-burner-{{ kube_burner_version }}-{{ kube_burner_arch }}.tar.gz"
    mode: '0644'
    force: yes

- name: Unarchive kube-burner binary
  become: yes
  ansible.builtin.unarchive:
    src: "/tmp/kube-burner-{{ kube_burner_version }}-{{ kube_burner_arch }}.tar.gz"
    dest: /usr/local/bin/
    remote_src: yes
    mode: '0755'

- name: Prepare kube-burner configuration files
  ansible.builtin.copy:
    src: files/kube-burner-generic.yaml # change this to run different experiment
    dest: kube-burner-config.yaml

- name: Copy metrics profile file
  ansible.builtin.copy:
    src: "{{ playbook_dir }}/files/metrics-profiles/metrics-aggregated.yaml"
    dest: "metrics-aggregated.yaml"

- name: Template metrics-endpoints.yaml for IKS clusters
  ansible.builtin.template:
    src: templates/metrics-endpoints.yaml.j2
    dest: "metrics-endpoints-iks-{{ item }}.yaml"
  vars:
    prom_endpoint: "{{ lookup('vars', 'PROM_ENDPOINT_IKS_' ~ (item | upper)) }}"
    prom_metrics_profile_path: "metrics-aggregated.yaml"
    prom_token: "{{ lookup('vars', 'PROM_TOKEN_IKS_' ~ (item | upper)) }}"
    env: "iks"
    uuid: "{{ tag_uuid }}"
    cluster: "{{ item }}"
  loop: 
    - karpenter
    - cas
    
- name: deploy systemd service for port-forwarding
  become: yes
  ansible.builtin.template:
    src: templates/port-forward@.service.j2
    dest: /etc/systemd/system/port-forward-{{ item.cluster }}-iks.service
    mode: '0644'
  loop:
    - { cluster: 'karpenter', port: 9091 }
    - { cluster: 'cas', port: 9092 }
  vars:
    kubeconfig_path: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item.cluster | upper)) }}" 

- name: start port-forward service for each cluster
  become: yes
  ansible.builtin.systemd:
    name: "port-forward-{{ item }}-iks"
    state: started
    daemon_reload: true
    enabled: yes
  loop:
    - karpenter
    - cas

- name: wait 10 minutes
  ansible.builtin.wait_for:
    timeout: 1200

- name: Run kube-burner on all clusters
  become: yes
  ignore_errors: yes 
  ansible.builtin.shell: |
    /usr/local/bin/kube-burner init -c kube-burner-config.yaml -e metrics-endpoints-iks-{{ item }}.yaml --uuid {{ tag_uuid }}-iks-{{ item }} --timeout 1h
  register: benchmark_run_iks
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item | upper)) }}"
  loop: 
    - karpenter
    - cas
  async: 7200
  poll: 0

- name: create prometheus snapshot
  ansible.builtin.uri:
    url: "http://127.0.0.1:{{ item.port }}/api/v1/admin/tsdb/snapshot"
    method: POST
    return_content: yes
    status_code: 200
  register: snapshot_response
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item.cluster | upper)) }}"
  loop: 
      - { cluster: 'karpenter', port: 9091 }
      - { cluster: 'cas', port: 9092 }         

- name: extract snapshot name from response
  set_fact:
    snapshot_name: "{{ snapshot_response.results[0].json.data.name }}"

- name: copy snapshot to local directory
  command: >
    kubectl exec -n monitoring $(kubectl get po -n monitoring -l app.kubernetes.io/name=prometheus -o jsonpath='{.items[0].metadata.name}') -- tar cf - /prometheus/snapshots/{{ snapshot_name }} |
    tar xf - -C {{ playbook_dir }}/{{ snapshot_name }}-{{ item.cluster }}
  loop:
      - { cluster: 'karpenter' }
      - { cluster: 'cas' }
  environment:
    KUBECONFIG: "{{ lookup('vars', 'KUBECONFIG_IKS_' ~ (item.cluster | upper)) }}"

- name: stop and disable port-forward service for each cluster
  become: yes
  ansible.builtin.systemd:
    name: "port-forward-{{ item }}-iks"
    state: stopped
    enabled: no
  loop:
    - karpenter
    - cas
