---
# tasks file for stf-run-ci
- name: Setup default values
  set_fact:
    branch: "{{ working_branch | default('master') }}"
    namespace: "{{ working_namespace | default('service-telemetry') }}"

- name: Set default image paths for local builds
  set_fact:
    sgo_image_path: "{{ __internal_registry_path }}/{{ namespace }}/smart-gateway-operator:{{ sgo_image_tag }}"
    sto_image_path: "{{ __internal_registry_path }}/{{ namespace }}/service-telemetry-operator:{{ sto_image_tag }}"
    sg_core_image_path: "{{ __internal_registry_path }}/{{ namespace }}/sg-core:{{ sg_core_image_tag }}"
    sg_bridge_image_path: "{{ __internal_registry_path }}/{{ namespace }}/sg-bridge:{{ sg_bridge_image_tag }}"
    prometheus_webhook_snmp_image_path: "{{ __internal_registry_path }}/{{ namespace }}/prometheus-webhook-snmp:{{ prometheus_webhook_snmp_image_tag }}"

- name: Clear out existing CRDs so we don't conflict or fail merge
  k8s:
    state: absent
    api_version: apiextensions.k8s.io/v1
    kind: CustomResourceDefinition
    name: "{{ item }}"
  loop:
    - smartgateways.smartgateway.infra.watch
    - servicetelemetrys.infra.watch

- block:
  - name: Setup supporting repositories
    include_tasks: clone_repos.yml
    tags:
      - clone

  - name: Setup supporting Operator subscriptions
    include_tasks: setup_base.yml
    tags:
      - deploy

  - name: Create builds and artifacts
    include_tasks: create_builds.yml
    loop:
      - { name: service-telemetry-operator, dockerfile_path: build/Dockerfile, image_reference_name: sto_image_path, working_build_dir: ../ }
      - { name: smart-gateway-operator, dockerfile_path: build/Dockerfile, image_reference_name: sgo_image_path, working_build_dir: ./working/smart-gateway-operator }
      - { name: sg-core, dockerfile_path: build/Dockerfile, image_reference_name: sg_core_image_path, working_build_dir: ./working/sg-core }
      - { name: sg-bridge, dockerfile_path: build/Dockerfile, image_reference_name: sg_bridge_image_path, working_build_dir: ./working/sg-bridge }
      - { name: prometheus-webhook-snmp, dockerfile_path: Dockerfile, image_reference_name: prometheus_webhook_snmp_image_path, working_build_dir: ./working/prometheus-webhook-snmp }
    loop_control:
      loop_var: artifact
    tags:
      - build

  - name: Setup STF using local artifacts
    include_tasks: setup_stf_local_build.yml
    tags:
      - deploy

  when: __local_build_enabled | bool

- block:
  - name: Setup supporting Operator subscriptions
    include_tasks: setup_base.yml

  - name: Setup Service Telemetry Framework from application registry
    include_tasks: setup_stf.yml

  when: not __local_build_enabled | bool

- name: Pre-flight checks
  include_tasks: preflight_checks.yml

- block:
  - name: Deploy an instance of STF
    include_tasks: deploy_stf.yml

  - name: Validate system is operational
    shell: ./validate_deployment.sh
    args:
      executable: /bin/bash
    register: validate_deployment

  - debug:
      var: validate_deployment.stdout_lines

  when: __deploy_stf | bool
