---
# NOTE: the split filter cuts the image path (quay.io:443/infrawatch/container_image:tag_name) on the colon. Field :-1 (everything but the final field) is the image path, field -1 (final field) is the image tag
# --- Smart Gateway Operator ---
- name: Generate Smart Gateway Operator CSV
  shell:
    chdir: working/smart-gateway-operator/build
    cmd: |
      WORKING_DIR="{{ playbook_dir }}/working/smart-gateway-operator-bundle" \
      RELATED_IMAGE_CORE_SMARTGATEWAY={{ sg_core_image_path | parse_image | quote }} \
      RELATED_IMAGE_BRIDGE_SMARTGATEWAY={{ sg_bridge_image_path | parse_image | quote }} \
      RELATED_IMAGE_CORE_SMARTGATEWAY_TAG={{ sg_core_image_path | parse_tag | quote }} \
      RELATED_IMAGE_BRIDGE_SMARTGATEWAY_TAG={{ sg_bridge_image_path | parse_tag | quote }} \
      OPERATOR_IMAGE={{ sgo_image_path | parse_image | quote }} \
      OPERATOR_TAG={{ sgo_image_path | parse_tag | quote }} \
      ./generate_bundle.sh
  register: generate_bundle_sgo

- name: Results of bundle generation
  debug:
    var: generate_bundle_sgo.stdout_lines

- name: Load Smart Gateway Operator RBAC
  command: oc apply -f working/smart-gateway-operator/deploy/{{ item }} -n "{{ namespace }}"
  loop:
    - service_account.yaml
    - role.yaml
    - role_binding.yaml
    - olm-catalog/smart-gateway-operator/manifests/smartgateway.infra.watch_smartgateways_crd.yaml

- name: Replace namespace in SGO CSV
  replace:
    path: "{{ playbook_dir }}/working/smart-gateway-operator-bundle/manifests/smart-gateway-operator.clusterserviceversion.yaml"
    regexp: 'placeholder'
    replace: '{{ namespace }}'

- name: Load Smart Gateway Operator CSV
  shell: oc apply -f working/smart-gateway-operator-bundle/manifests/smart-gateway-operator.clusterserviceversion.yaml -n "{{ namespace }}"

# --- Service Telemetry Operator ---
- name: Generate Service Telemetry Operator CSV
  shell:
    chdir: "{{ playbook_dir }}"
    cmd: |
      WORKING_DIR="{{ playbook_dir }}/working/service-telemetry-operator-bundle" \
      RELATED_IMAGE_PROMETHEUS_WEBHOOK_SNMP={{ prometheus_webhook_snmp_image_path | parse_image | quote }} \
      RELATED_IMAGE_PROMETHEUS_WEBHOOK_SNMP_TAG={{ prometheus_webhook_snmp_image_path | parse_tag | quote }} \
      OPERATOR_IMAGE={{ sto_image_path | parse_image | quote }} \
      OPERATOR_TAG={{ sto_image_path | parse_tag | quote }} \
      ./generate_bundle.sh

- name: Replace namespace in STO role binding
  replace:
    path: "{{ playbook_dir }}/../deploy/role_binding.yaml"
    regexp: 'placeholder'
    replace: '{{ namespace }}'

- name: Load Service Telemetry Operator RBAC
  command: oc apply -f ../deploy/{{ item }} -n "{{ namespace }}"
  loop:
    - service_account.yaml
    - role.yaml
    - role_binding.yaml
    - olm-catalog/service-telemetry-operator/manifests/infra.watch_servicetelemetrys_crd.yaml

- name: Replace namespace in STO CSV
  replace:
    path: "{{ playbook_dir }}/working/service-telemetry-operator-bundle/manifests/service-telemetry-operator.clusterserviceversion.yaml"
    regexp: 'placeholder'
    replace: '{{ namespace }}'

- name: Load Service Telemetry Operator CSV
  shell: oc apply -f working/service-telemetry-operator-bundle/manifests/service-telemetry-operator.clusterserviceversion.yaml -n "{{ namespace }}"

# --- Loki Operator ---
- block:
  - name: Prevent Loki Operator from building operator-sdk
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/.bingo/Variables.mk"
      regexp: '^.*modfile=operator-sdk.mod.*$'
      replace: ''

  - name: Prevent Loki Operator from replacing GOBIN
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/.bingo/Variables.mk"
      regexp: '^GOBIN.*$'
      replace: 'GOBIN  ?= $(shell go env GOBIN)'

  - name: Prevent Loki Operator from using system golang
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/.bingo/Variables.mk"
      regexp: '^GO .*$'
      replace: 'GO     ?= $(GOBIN)"/go"'

  - name: Prevent Loki Operator from putting authentication on /metrics
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/config/overlays/openshift/kustomization.yaml"
      regexp: '{{ item }}'
      replace: ''
    loop:
      - "patchesStrategicMerge:"
      - "- manager_auth_proxy_patch.yaml"
      - "- manager_related_image_patch.yaml"
      - "- manager_run_flags_patch.yaml"
      - "- prometheus_service_monitor_patch.yaml"

  - name: Generate Loki Operator CSV
    make:
      chdir: "{{ playbook_dir }}/working/loki-operator"
      target: bundle
      params:
          REGISTRY_ORG: infrawatch
          OPERATOR_SDK: "{{ playbook_dir }}/working/operator-sdk-{{ new_operator_sdk_version }}"
          GOROOT: "{{ playbook_dir }}/working/go"
          GOTOOLDIR: "{{ playbook_dir }}/working/go/pkg/tool/linux_amd64"
          GOBIN: "{{ playbook_dir }}/working/go/bin"

  - name: Replace namespace in loki-operator CSV
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/bundle/manifests/loki-operator.clusterserviceversion.yaml"
      regexp: 'placeholder'
      replace: '{{ namespace }}'

  - name: Replace image path in loki-operator CSV
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/bundle/manifests/loki-operator.clusterserviceversion.yaml"
      regexp: '{{ item }}'
      replace: '{{ loki_operator_image_path }}'
    loop:
      - quay.io/infrawatch/loki-operator:v0.0.1
      - quay.io/openshift-logging/loki-operator:v0.0.1

  - name: Replace namespace in loki-operator
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/config/overlays/development/kustomization.yaml"
      regexp: 'default'
      replace: '{{ namespace }}'

  - name: Remove additional manager deployment
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/config/overlays/development/kustomization.yaml"
      regexp: '^.*manager'
      replace: ''

  - name: Setup PVs for Loki in crc
    shell:
      cmd: ./create_standard_pvs.sh 3
    when:
    - is_crc | bool
    - __service_telemetry_logs_enabled | bool

  - name: Replace namespace in S3 secret
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/config/overlays/development/minio/secret.yaml"
      regexp: 'default'
      replace: '{{ namespace }}'
  when:
    - __service_telemetry_observability_strategy == "use_community"

- block:
  - name: Remove minio deployment
    replace:
      path: "{{ playbook_dir }}/working/loki-operator/config/overlays/development/kustomization.yaml"
      regexp: '^.*minio'
      replace: ''
  when:
    - not __deploy_minio_enabled | bool
    - __service_telemetry_observability_strategy == "use_community"

- block:
  - name: Deploy Loki Operator
    make:
      chdir: "{{ playbook_dir }}/working/loki-operator"
      target: deploy
      params:
          REGISTRY_ORG: infrawatch
          OPERATOR_SDK: "{{ playbook_dir }}/working/operator-sdk-{{ new_operator_sdk_version }}"
          GOROOT: "{{ playbook_dir }}/working/go"
          GOTOOLDIR: "{{ playbook_dir }}/working/go/pkg/tool/linux_amd64"
          GOBIN: "{{ playbook_dir }}/working/go/bin"

  - name: Load Loki Operator bundle manifests
    command: oc apply -f working/loki-operator/bundle/manifests/{{ item }} -n "{{ namespace }}"
    loop:
    - loki.openshift.io_lokistacks.yaml
    - loki-operator-controller-manager-metrics-service_v1_service.yaml
    - loki-operator-manager-config_v1_configmap.yaml
    - loki-operator-metrics-reader_rbac.authorization.k8s.io_v1_clusterrole.yaml
    - loki-operator-prometheus_rbac.authorization.k8s.io_v1_rolebinding.yaml
    - loki-operator-prometheus_rbac.authorization.k8s.io_v1_role.yaml
    - loki-operator.clusterserviceversion.yaml
  when:
    - __service_telemetry_observability_strategy == "use_community"
