# NOTE: be aware that if the API version changes for the ServiceTelemetry
# object that it'll need to be updated here
- name: Create default ServiceTelemetry manifest with observabilityStrategy use_community
  set_fact:
    service_telemetry_manifest: |
      apiVersion: infra.watch/v1beta1
      kind: ServiceTelemetry
      metadata:
        name: default
        namespace: "{{ namespace }}"
      spec:
        observabilityStrategy: "use_community"
        alerting:
          alertmanager:
            storage:
              strategy: {{ "ephemeral" if __service_telemetry_storage_ephemeral_enabled else "persistent" }}
      {% if __service_telemetry_storage_persistent_storage_class is defined %}
              persistent:
                storageClass: {{ __service_telemetry_storage_persistent_storage_class }}
      {% endif %}
            receivers:
              snmpTraps:
                enabled: {{ __service_telemetry_snmptraps_enabled }}
        backends:
          events:
            elasticsearch:
              enabled: {{ __service_telemetry_events_enabled }}
              storage:
                strategy: {{ "ephemeral" if __service_telemetry_storage_ephemeral_enabled else "persistent" }}
      {% if __service_telemetry_storage_persistent_storage_class is defined %}
                persistent:
                  storageClass: {{ __service_telemetry_storage_persistent_storage_class }}
      {% endif %}
          metrics:
            prometheus:
              enabled: {{ __service_telemetry_metrics_enabled }}
              storage:
                strategy: {{ "ephemeral" if __service_telemetry_storage_ephemeral_enabled else "persistent" }}
      {% if __service_telemetry_storage_persistent_storage_class is defined %}
                persistent:
                  storageClass: {{ __service_telemetry_storage_persistent_storage_class }}
      {% endif %}
          logs:
            loki:
              enabled: {{ __service_telemetry_logs_enabled }}
              replicationFactor: 1
              flavor: 1x.extra-small
              storage:
                objectStorageSecret: test
      {% if __service_telemetry_storage_persistent_storage_class is defined %}
                storageClass: {{ __service_telemetry_storage_persistent_storage_class }}
      {% endif %}
        highAvailability:
          enabled: {{ __service_telemetry_high_availability_enabled }}
  when:
    - service_telemetry_manifest is not defined
    - __service_telemetry_observability_strategy == "use_community"

- name: Create default ServiceTelemetry manifest with observabilityStrategy none
  set_fact:
    service_telemetry_manifest: |
      apiVersion: infra.watch/v1beta1
      kind: ServiceTelemetry
      metadata:
        name: default
        namespace: "{{ namespace }}"
      spec:
        observabilityStrategy: none
  when:
    - service_telemetry_manifest is not defined
    - __service_telemetry_observability_strategy == "none"

- name: Show ServiceTelemetry manifest
  debug:
    var: service_telemetry_manifest | from_yaml

- name: Create ServiceTelemetry instance
  k8s:
    definition:
      '{{ service_telemetry_manifest }}'
