#jinja2:lstrip_blocks: True
apiVersion: v1
kind: ConfigMap
metadata:
  name: '{{ansible_operator_meta.name}}-loki-conf'
  namespace: '{{ansible_operator_meta.namespace}}'
  labels:
    plugins.cloudminds.com/loki: loki-conf
    app.kubernetes.io/name: '{{ ansible_operator_meta.name }}-loki-conf'
data:
  local-config.yaml: >
    auth_enabled: {{multitenancy | lower }}

    server:
      http_listen_port: 3100
      http_listen_address: 0.0.0.0
      grpc_listen_port: 9095
      grpc_listen_address: 0.0.0.0
      grpc_server_max_recv_msg_size: 10000000
      grpc_server_max_send_msg_size: 10000000
      grpc_server_max_concurrent_streams: 0

    {% if service.mode == "cluster" %}
      {% if service.cluster.ring.type == "memberlist" %}
    memberlist:
      join_members: 
      {% for num in range(service.cluster.loki.replicas) %}
      - {{ansible_operator_meta.name}}-loki-system-{{num}}.{{ansible_operator_meta.name}}-loki-system-headless.{{ansible_operator_meta.namespace}}.svc.cluster.local
      {% endfor %}
      dead_node_reclaim_time: 30s
      gossip_to_dead_nodes_time: 15s
      left_ingesters_timeout: 30s
      bind_addr: ['0.0.0.0']
      bind_port: 7946
      {% endif %}
    {% endif %}

    querier:
      query_ingesters_within: 2h
    
    {% if service.mode == "cluster" %}
    frontend:
      compress_responses: true
      log_queries_longer_than: 5s
      downstream_url: http://{{ansible_operator_meta.name}}-loki-system:3100
    {% endif %}

    query_range:
      split_queries_by_interval: 24h
      align_queries_with_step: true
      max_retries: 5
      {% if cacheconfig is defined %}
      {% if cacheconfig.enabled %}
      results_cache:
        cache:
          {{cacheconfig.type}}:
            {% if cacheconfig.type == "redis" and cacheconfig.redis.external is defined %}
            endpoint: {{cacheconfig.redis.external.address}}:{{cacheconfig.redis.external.port}}
            {% elif cacheconfig.type == "redis" %}
            endpoint: {{ansible_operator_meta.name}}-redis-0.{{ansible_operator_meta.name}}-redis.{{ansible_operator_meta.namespace}}.svc.cluster.local:6379
            {% endif %}
            expiration: {{cacheconfig.expiration}}
          {% if cacheconfig.type == "memcached" %}
          memcached_client:
            {% if cacheconfig.memcached.external is defined %}
            host: {{cacheconfig.memcached.external.address}}
            service: memcached
            {% else %}
            host: {{ansible_operator_meta.name}}-memcached.{{ansible_operator_meta.namespace}}.svc.cluster.local
            service: memcached
            {% endif %}
          {% endif %}
      cache_results: true
      {% endif %}
      {% endif %}

    ingester:
      lifecycler:
        join_after: 60s
        observe_period: 5s
        ring:
          {% if service.mode == "cluster" %}
          replication_factor: {{service.cluster.replication_factor}}
          kvstore:
            store: {{service.cluster.ring.type}}
          {% else %}
          replication_factor: 1
          kvstore:
            store: inmemory
          {% endif %}
        final_sleep: 0s
      chunk_idle_period: 1h
      max_chunk_age: 1h
      chunk_retain_period: 1m
      max_transfer_retries: 1
      chunk_encoding: snappy
      chunk_target_size: 0
      chunk_block_size: 262144

    ingester_client:
      grpc_client_config:
          max_recv_msg_size: 20971520
      remote_timeout: 1s

    schema_config:
      configs:
      - from: 2021-04-25
        store: {{schemaconfig.index}}
        {% if schemaconfig.chunk == "s3" %}
        object_store: aws
        {% elif schemaconfig.chunk == "filesystem" %}
        object_store: filesystem
        {% endif %}
        schema: v11
        index:
          prefix: index_
          period: 24h
    
    {% if service.mode == "single" and schemaconfig.index == "boltdb-shipper" %}
    compactor:
      working_directory: /loki/compactor
      {% if storageconfig.boltdb_shipper.shared_store == "s3" %}
      shared_store: aws
      {% elif storageconfig.boltdb_shipper.shared_store == "filesystem" %}
      shared_store: filesystem
      {% endif %}
    {% endif %}

    storage_config:
      {% if storageconfig.boltdb_shipper is defined %}
      boltdb_shipper:
        {% if storageconfig.boltdb_shipper.shared_store == "s3" %}
        shared_store: aws
        {% elif storageconfig.boltdb_shipper.shared_store == "filesystem" %}
        shared_store: filesystem
        {% endif %}
        active_index_directory: /loki/index
        cache_location: /loki/boltdb-cache
      {% endif %}

      {% if storageconfig.boltdb_shipper.shared_store == "filesystem" or schemaconfig.chunk == "filesystem" %}
      filesystem:
        directory: {{storageconfig.filesystem.directory}}
      {% endif %}
    
      {% if storageconfig.s3 is defined %}
      aws:
        s3: s3://{{storageconfig.s3.access_key}}:{{storageconfig.s3.secret_key}}@{{storageconfig.s3.address}}/{{storageconfig.s3.bucket}}
        s3forcepathstyle: true
        insecure: true
      {% endif %}
      {% if cacheconfig is defined %}
      {% if cacheconfig.enabled %}
      index_queries_cache_config:
        {{cacheconfig.type}}:
          {% if cacheconfig.type == "redis" and cacheconfig.redis.external is defined %}
          endpoint: {{cacheconfig.redis.external.address}}:{{cacheconfig.redis.external.port}}
          {% elif cacheconfig.type == "redis" %}
          endpoint: {{ansible_operator_meta.name}}-redis-0.{{ansible_operator_meta.name}}-redis.{{ansible_operator_meta.namespace}}.svc.cluster.local:6379
          {% endif %}
          expiration: {{cacheconfig.expiration}}
        {% if cacheconfig.type == "memcached" %}
        memcached_client:
          {% if cacheconfig.memcached.external is defined %}
          host: {{cacheconfig.memcached.external.address}}
          service: memcached
          {% else %}
          host: {{ansible_operator_meta.name}}-memcached.{{ansible_operator_meta.namespace}}.svc.cluster.local
          service: memcached
          {% endif %}
        {% endif %}
      {% endif %}
      {% endif %}

    {% if cacheconfig is defined %}
    {% if cacheconfig.enabled %}
    chunk_store_config:
      chunk_cache_config:
        {{cacheconfig.type}}:
          {% if cacheconfig.type == "redis" and cacheconfig.redis.external is defined %}
          endpoint: {{cacheconfig.redis.external.address}}:{{cacheconfig.redis.external.port}}
          {% elif cacheconfig.type == "redis" %}
          endpoint: {{ansible_operator_meta.name}}-redis-0.{{ansible_operator_meta.name}}-redis.{{ansible_operator_meta.namespace}}.svc.cluster.local:6379
          {% endif %}
          expiration: {{cacheconfig.expiration}}
        {% if cacheconfig.type == "memcached" %}
        memcached_client:
          {% if cacheconfig.memcached.external is defined %}
          host: {{cacheconfig.memcached.external.address}}
          service: memcached
          {% else %}
          host: {{ansible_operator_meta.name}}-memcached.{{ansible_operator_meta.namespace}}.svc.cluster.local
          service: memcached
          {% endif %}
        {% endif %}
      max_look_back_period: 0    
      write_dedupe_cache_config:
        {{cacheconfig.type}}:
          {% if cacheconfig.type == "redis" and cacheconfig.redis.external is defined %}
          endpoint: {{cacheconfig.redis.external.address}}:{{cacheconfig.redis.external.port}}
          {% elif cacheconfig.type == "redis" %}
          endpoint: {{ansible_operator_meta.name}}-redis-0.{{ansible_operator_meta.name}}-redis.{{ansible_operator_meta.namespace}}.svc.cluster.local:6379
          {% endif %}
          expiration: {{cacheconfig.expiration}}
          {% if cacheconfig.type == "memcached" %}
        memcached_client:
          {% if cacheconfig.memcached.external is defined %}
          host: {{cacheconfig.memcached.external.address}}
          service: memcached
          {% else %}
          host: {{ansible_operator_meta.name}}-memcached.{{ansible_operator_meta.namespace}}.svc.cluster.local
          service: memcached
          {% endif %}
        {% endif %}
    {% endif %}
    {% endif %}
  
    {% if ruler is defined %}
    {% if ruler.enabled %}
    ruler:
      storage:
        type: {{ruler.storage}}
        {% if ruler.storage == "s3" %}
        s3:
          s3: s3://{{storageconfig.s3.access_key}}:{{storageconfig.s3.secret_key}}@{{storageconfig.s3.address}}/{{ruler.s3.bucket}}
          s3forcepathstyle: true
          insecure: true
          http_config:
            insecure_skip_verify: true
        {% endif %}
      enable_api: true
      enable_alertmanager_v2: true
      alertmanager_url: "{{ruler.alertmanager}}"
      ring:
        kvstore:
          {% if service.mode == "cluster" and ruler.ring is defined %}
          store: {{ruler.ring}}
          {% else %}
          store: inmemory
          {% endif %}
    {% endif %}
    {% endif %}
  
    limits_config:
      enforce_metric_name: false
      reject_old_samples: true
      reject_old_samples_max_age: 168h
      ingestion_rate_mb: 64
      ingestion_burst_size_mb: 128
      max_entries_limit_per_query: 50000
    
    table_manager:
      chunk_tables_provisioning:
        inactive_read_throughput: 0
        inactive_write_throughput: 0
        provisioned_read_throughput: 0
        provisioned_write_throughput: 0
      index_tables_provisioning:
        inactive_read_throughput: 0
        inactive_write_throughput: 0
        provisioned_read_throughput: 0
        provisioned_write_throughput: 0
      retention_deletes_enabled: false
      retention_period: 0