---
# 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 }}"
    loki_operator_image_path: "{{ __internal_registry_path }}/{{ namespace }}/loki-operator:{{ loki_operator_image_tag }}"
    loki_operator_bundle_image_path: "{{ __internal_registry_path }}/{{ namespace }}/loki-operator-bundle:{{ loki_operator_image_tag }}"

- name: Get the node hostnames
  set_fact:
    nodes: "{{ lookup('k8s', kind='nodes') }}"

- name: Find out if we are using crc
  block:
    - name: Try finding out for cluster with more than 1 node
      set_fact:
        is_crc: "{{ True if 'crc' in nodes[0].metadata.labels[\"kubernetes.io/hostname\"] else False }}"
  rescue:
    - name: Try finding out for cluster with only 1 node
      set_fact:
        is_crc: "{{ True if 'crc' in nodes.metadata.labels[\"kubernetes.io/hostname\"] else False }}"

- 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
    - lokistacks.loki.openshift.io

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

  - name: Get new operator sdk
    command: ./get_new_operator_sdk.sh "{{ new_operator_sdk_version }}"

  - name: Get new go
    command: ./get_go.sh "{{ new_go_version }}"

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

    # TLS verification support doesn't seem to be implemented in the operator yet
  - block:
    - name: Prepare for skip Loki TLS patch
      replace:
        path: "{{ playbook_dir }}/working/loki-operator/internal/manifests/internal/config/loki-config.yaml"
        regexp: "\ \ \ \ insecure: false\n
\ \ \ http_config:\n
\ \ \ \ \ insecure_skip_verify: true"
        replace: ""

    - name: Skip Loki TLS verification
      replace:
        path: "{{ playbook_dir }}/working/loki-operator/internal/manifests/internal/config/loki-config.yaml"
        regexp: "\ \ \ \ s3forcepathstyle: true"
        replace: "\ \ \ \ s3forcepathstyle: true\n
\ \ \ insecure: false\n
\ \ \ http_config:\n
\ \ \ \ \ insecure_skip_verify: true"
    when:
      - __loki_skip_tls_verify | bool
      - __service_telemetry_observability_strategy == "use_community"

  - block:
    - name: Replace loki-operator golang base image
      replace:
        path: "{{ playbook_dir }}/working/loki-operator/Dockerfile"
        regexp: "golang:1.16"
        replace: "{{ __golang_image_path }}"

    - name: Replace Loki image
      replace:
        path: "{{ playbook_dir }}/working/loki-operator/internal/manifests/var.go"
        regexp: "docker.io/grafana/loki:\\d\\.\\d\\.\\d"
        replace: "{{ __loki_image_path }}"
    when:
      - __service_telemetry_observability_strategy == "use_community"

  - name: Create base build list
    set_fact:
      build_list:
        - { 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 }

  - name: Create Loki build list
    set_fact:
      loki_build_list:
        - { name: loki-operator-bundle, dockerfile_path: bundle.Dockerfile, image_reference_name: loki_operator_bundle_image_path, working_build_dir: ./working/loki-operator }
        - { name: loki-operator, dockerfile_path: Dockerfile, image_reference_name: loki_operator_image_path, working_build_dir: ./working/loki-operator }

  - name: Combine lists when community operators are enabled
    set_fact:
      build_list: "{{ build_list + loki_build_list }}"
    when:
      - __service_telemetry_observability_strategy == "use_community"

  - debug:
     var: build_list

  - name: Create builds and artifacts
    include_tasks: create_builds.yml
    loop: "{{ build_list }}"
    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: |
      OCP_PROJECT="{{ namespace }}" VALIDATION_SCOPE="{{ __service_telemetry_observability_strategy }}" ./validate_deployment.sh
    args:
      executable: /bin/bash
    register: validate_deployment

  - debug:
      var: validate_deployment.stdout_lines

  when: __deploy_stf | bool
