apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-logging
  namespace: kube-system
  labels:
    app: fluentd-logging
data:
  fluent.conf: |
    <source>
      @type tail
      @id in_tail_container_logs
      path /var/log/containers/*{{cookiecutter.project_slug}}-*.log,/var/log/containers/ingress-nginx-controller-*.log
      pos_file /var/log/fluentd-containers.log.pos
      tag kubernetes.*
      read_from_head true
      <parse>
        @type regexp
        expression /^(?<time>.+) (?<stream>stdout|stderr)( (?<logtag>.))? (?<log>.*)$/
      </parse>
    </source>

    <filter kubernetes.**>
      @type kubernetes_metadata
      @id filter_kube_metadata
    </filter>

    <filter kubernetes.var.log.containers.**>
      @type record_transformer
      enable_ruby
      remove_keys kubernetes, docker

      <record>
        app ${ record.dig("kubernetes", "labels", "app") }
        job ${ record.dig("kubernetes", "labels", "app") }
        namespace ${ record.dig("kubernetes", "namespace_name") }
        pod ${ record.dig("kubernetes", "pod_name") }
        container ${ record.dig("kubernetes", "container_name") }
      </record>
    </filter>

    <match kubernetes.var.log.containers.**>
      @type copy

      <store>
        @type loki
        url "#{ENV['LOKI_URL']}"
        username "#{ENV['LOKI_USERNAME']}"
        password "#{ENV['LOKI_PASSWORD']}"
        # extra_labels {"env":"dev"}
        label_keys "app,job,namespace,pod"
        flush_interval 10s
        flush_at_shutdown true
        buffer_chunk_limit 1m
      </store>

      <store>
        @type stdout
      </store>
    </match>
