apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-config
  namespace: kube-system
data:
  containers.input.conf: |-
    <source>
      @type tail
      path /var/log/containers/*.log
      exclude_path /var/log/containers/fluentd*
      pos_file /var/log/fluentd-containers.log.pos
      tag kubernetes.*
      <parse>
        @type multi_format
        <pattern>
          format json
          time_key time
          time_format %Y-%m-%dT%H:%M:%S.%NZ
        </pattern>
        <pattern>
          format none
          time_format %Y-%m-%dT%H:%M:%S.%N%:z
        </pattern>
      </parse>
    </source>

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

    <filter kubernetes.var.log.containers.**>
      @type parser
      <parse>
        @type json
        json_parser json
      </parse>
      replace_invalid_sequence true
      emit_invalid_record_to_error false
      key_name log
      reserve_data true
    </filter>

    <filter kubernetes.**>
      @type record_transformer
      enable_ruby true
      <record>
        @team_index logs-kind-${record['kubernetes']['labels']['team'] || 'default'}-${time.strftime("%Y.%m.%d")}
      </record>
    </filter>

    <match **>
        @type elasticsearch
        @log_level info
        templates { "kindflb": "/etc/fluent/es-template/template.json" }
        include_tag_key true
        host elasticsearch-logging
        port 9200
        scheme "#{ENV['FLUENT_ELASTICSEARCH_SCHEME'] || 'http'}"
        ssl_verify "#{ENV['FLUENT_ELASTICSEARCH_SSL_VERIFY'] || 'true'}"
        reload_connections "#{ENV['FLUENT_ELASTICSEARCH_RELOAD_CONNECTIONS'] || 'false'}"
        target_index_key @team_index
        logstash_format true
        type_name flb_type
        <buffer>
          flush_thread_count "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_FLUSH_THREAD_COUNT'] || '8'}"
          flush_interval "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_FLUSH_INTERVAL'] || '5s'}"
          chunk_limit_size "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_CHUNK_LIMIT_SIZE'] || '2M'}"
          queue_limit_length "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_QUEUE_LIMIT_LENGTH'] || '32'}"
          retry_max_interval "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_RETRY_MAX_INTERVAL'] || '30'}"
        retry_forever true
        </buffer>
      </match>

      <match **>
        @type stdout
      </match>

    <label @ERROR>
      <filter kubernetes.**>
        @type record_transformer
        enable_ruby true
        <record>
          @team_index logs-kind-failed-${record['kubernetes']['labels']['team'] || 'default'}-${time.strftime("%Y.%m.%d")}
        </record>
      </filter>

      <match **>
        @type elasticsearch
        @log_level info
        templates { "kindflb": "/etc/fluent/es-template/template.json" }
        flatten_hashes true
        flatten_hashes_separator _
        include_tag_key true
        host elasticsearch-logging
        port 9200
        scheme "#{ENV['FLUENT_ELASTICSEARCH_SCHEME'] || 'http'}"
        ssl_verify "#{ENV['FLUENT_ELASTICSEARCH_SSL_VERIFY'] || 'true'}"
        reload_connections "#{ENV['FLUENT_ELASTICSEARCH_RELOAD_CONNECTIONS'] || 'true'}"
        target_index_key @team_index
        logstash_format true
        type_name flb_type
        <buffer>
          flush_thread_count "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_FLUSH_THREAD_COUNT'] || '8'}"
          flush_interval "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_FLUSH_INTERVAL'] || '5s'}"
          chunk_limit_size "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_CHUNK_LIMIT_SIZE'] || '2M'}"
          queue_limit_length "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_QUEUE_LIMIT_LENGTH'] || '32'}"
          retry_max_interval "#{ENV['FLUENT_ELASTICSEARCH_BUFFER_RETRY_MAX_INTERVAL'] || '30'}"
        retry_forever false
        </buffer>
      </match>
    </label>

  monitoring.conf: |-
    # Prometheus Exporter Plugin
    # input plugin that exports metrics
    <source>
        @id prometheus
        @type prometheus
    </source>

    <source>
        @id monitor_agent
        @type monitor_agent
    </source>

    # input plugin that collects metrics from MonitorAgent
    <source>
        @id prometheus_monitor
        @type prometheus_monitor
        <labels>
            host ${hostname}
        </labels>
    </source>

    # input plugin that collects metrics for output plugin
    <source>
        @id prometheus_output_monitor
        @type prometheus_output_monitor
        <labels>
            host ${hostname}
        </labels>
    </source>

    # input plugin that collects metrics for in_tail plugin
    <source>
        @id prometheus_tail_monitor
        @type prometheus_tail_monitor
        <labels>
            host ${hostname}
        </labels>
    </source>
