- name: loki
  namespace: observability
  create_namespace: true
  chart_ref: {{ .Modules.Observability.Logging.Loki.ChartRef }}
  chart_version: {{ .Modules.Observability.Logging.Loki.ChartVersion }}
  {{- if and .Modules.Observability.Enabled .Modules.Observability.Logging.Enabled }}
  release_state: "present"
  {{- else }}
  release_state: "absent"
  {{- end }}
  values:
    loki:
      image:
        registry: {{ .Modules.Observability.Logging.Loki.Registry }}
        repository: {{ .Modules.Observability.Logging.Loki.Image }}
        tag: {{ .Modules.Observability.Logging.Loki.Tag }}
      podAnnotations:
        app.kubernetes.io/component: "loki"
      auth_enabled: false
      commonConfig:
        replication_factor: 1
      storage:
        type: 'filesystem'

      frontend:
        max_outstanding_per_tenant: 10000

      limits_config:
        reject_old_samples: false
        split_queries_by_interval: 15m
        max_query_parallelism: 32
        max_query_series: 10000
        retention_period: {{ .Modules.Observability.Logging.Loki.Persistence.Retention }}

      compactor:
        compaction_interval: 10m
        retention_enabled: true
        retention_delete_delay: 2h

      querier:
        max_concurrent: 2048

      query_scheduler:
        max_outstanding_requests_per_tenant: 10000

      rulerConfig:
        storage:
          type: local
          local:
            directory: /var/loki/rules
        rule_path: /tmp/rules

        alertmanager_url: {{ .Modules.Observability.Logging.Loki.AlertManagerUrl }}


    singleBinary:
      replicas: 1

      extraVolumes:
        - name: loki-default-rules
          configMap:
            name: loki-default-alerting-rules

      extraVolumeMounts:
        - name: loki-default-rules
          mountPath: /var/loki/rules


    write:
      persistence:
        volumeClaimsEnabled: true
        storageClass: "{{ .Modules.Observability.Logging.Loki.Persistence.StorageClass }}"
        size: {{ .Modules.Observability.Logging.Loki.Persistence.StorageSize }}

    test:
      enabled: false

    gateway:
      enabled: false

    monitoring:
      selfMonitoring:
        enabled: false
        grafanaAgent:
          installOperator: false
      lokiCanary:
        enabled: false
      rules:
        enabled: true
        alerting: true
        additionalGroups: {}

    extraObjects:
      - apiVersion: v1
        kind: ConfigMap
        metadata:
          name: loki-default-alerting-rules
          labels:
            loki_rule: ""

        data:
          loki-default-alerting-rules.yaml: |-
            groups:
              {{- .Modules.Observability.Logging.Loki.AdditionalRulesGroups | toString | nindent 14 -}}
              - name: kube-events-alerts
                rules:
                  - alert: FailedEventsOccured
                    expr: |
                      count(rate({logs_type="kube-events"} | json reason="reason", event_type="event_type" | event_type = `Warning` | reason = `Failed` [1h])) > 0
                    for: 1m
                    labels:
                      severity: critical
                    annotations:
                      alertname: FailedEventsOccured
                      instance: kube-cluster
                      jobName: kube_events
                      summary: Failed events occured in cluster
                      addDefaultUrl: "true"

                  - alert: OOMKilledEventsOccured
                    expr: |
                      count(rate({logs_type="kube-events"} | json reason="reason", event_type="event_type" | event_type = `Warning` | reason = `OOMKilled` [1h])) > 0
                    for: 1m
                    labels:
                      severity: critical
                    annotations:
                      alertname: OOMKilledEventsOccured
                      instance: kube-cluster
                      jobName: kube_events
                      summary: OOMKilled events occured in cluster
                      addDefaultUrl: "true"

                  - alert: EvictedEventsOccured
                    expr: |
                      count(rate({logs_type="kube-events"} | json reason="reason", event_type="event_type" | event_type = `Warning` | reason = `Evicted` [1h])) > 0
                    for: 1m
                    labels:
                      severity: critical
                    annotations:
                      alertname: EvictedEventsOccured
                      instance: kube-cluster
                      jobName: kube_events
                      summary: Evicted events occured in cluster
                      addDefaultUrl: "true"

                  - alert: ImagePullBackOffEventsOccured
                    expr: |
                      count(rate({logs_type="kube-events"} | json reason="reason", event_type="event_type" | event_type = `Warning` | reason = `ImagePullBackOff` [1h])) > 0
                    for: 1m
                    labels:
                      severity: critical
                    annotations:
                      alertname: ImagePullBackOffEventsOccured
                      instance: kube-cluster
                      jobName: kube_events
                      summary: ImagePullBackOff events occured in cluster
                      addDefaultUrl: "true"

                  - alert: BackOffEventsOccured
                    expr: |
                      count(rate({logs_type="kube-events"} | json reason="reason", event_type="event_type" | event_type = `Warning` | reason = `BackOff` [1h])) > 0
                    for: 1m
                    labels:
                      severity: critical
                    annotations:
                      alertname: BackOffEventsOccured
                      instance: kube-cluster
                      jobName: kube_events
                      summary: BackOff events occured in cluster
                      addDefaultUrl: "true"
    sidecar:
      rules:
        enabled: true
        # -- Label that the configmaps/secrets with rules will be marked with.
        label: loki_rule
        # -- Label value that the configmaps/secrets with rules will be set to.
        labelValue: ""
        # -- Folder into which the rules will be placed.
        folder: /var/loki/rules
        # -- Comma separated list of namespaces. If specified, the sidecar will search for config-maps/secrets inside these namespaces.
        # Otherwise the namespace in which the sidecar is running will be used.
        # It's also possible to specify 'ALL' to search in all namespaces.
        searchNamespace: 'ALL'
        # -- Method to use to detect ConfigMap changes. With WATCH the sidecar will do a WATCH request, with SLEEP it will list all ConfigMaps, then sleep for 60 seconds.
        watchMethod: WATCH
        # -- Search in configmap, secret, or both.
        resource: both
        # -- Absolute path to the shell script to execute after a configmap or secret has been reloaded.
        script: null
        # -- WatchServerTimeout: request to the server, asking it to cleanly close the connection after that.
        # defaults to 60sec; much higher values like 3600 seconds (1h) are feasible for non-Azure K8S.
        watchServerTimeout: 60
        #
        # -- WatchClientTimeout: is a client-side timeout, configuring your local socket.
        # If you have a network outage dropping all packets with no RST/FIN,
        # this is how long your client waits before realizing & dropping the connection.
        # Defaults to 66sec.
        watchClientTimeout: 60
        # -- Log level of the sidecar container.
        logLevel: INFO
