apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-config
data:
  fluent.conf: |-
    ################################################################
    # This source gets all logs from local docker host
    @include pods-fluent.conf
    @include reporting-server.conf
    @include reporting-server-all-logs.conf
    @include keycloak-logs.conf
    # Use this configuration for production
    @include minio-fluent.conf
    # @include minio-fluent-dev.conf
  # This is for running fluentd in the cloud
  pods-fluent.conf: |-
    <source>
      @type tail
      read_from_head true
      tag rmf.all.*
      path /var/log/containers/*.log
      pos_file /var/log/fluent/fluentd-rmf-all.log.pos
      exclude_path ["/var/log/containers/fluent*"]
      format json
      time_format %Y-%m-%dT%H:%M:%S.%NZ
    </source>

    <filter rmf.all.**>
      @type kubernetes_metadata
    </filter>

    <source>
      @type tail
      read_from_head true
      tag rmf.server.*
      path /var/log/containers/rmf-server*.log
      pos_file /var/log/fluent/fluentd-rmf-server.log.pos
      exclude_path ["/var/log/containers/fluent*"]
      <parse>
        @type kubernetes
        @type "#{ENV['FLUENT_CONTAINER_TAIL_PARSER_TYPE'] || 'json'}"
        time_format %Y-%m-%dT%H:%M:%S.%NZ
      </parse>
    </source>

    <source>
      @type tail
      read_from_head true
      tag keycloak.*
      path /var/log/containers/keycloak*.log
      pos_file /var/log/fluent/fluentd-keycloak.log.pos
      exclude_path ["/var/log/containers/keycloak-db*"]
      <parse>
        @type kubernetes
        @type "#{ENV['FLUENT_CONTAINER_TAIL_PARSER_TYPE'] || 'json'}"
        time_format %Y-%m-%dT%H:%M:%S.%NZ
      </parse>
    </source>

    <source>
      @type tail
      read_from_head true
      tag minio.s3.*
      path /var/log/containers/*.log
      pos_file /var/log/fluent/fluentd-all-containers.log.pos
      exclude_path ["/var/log/containers/fluent*"]
      <parse>
        @type kubernetes
        @type "#{ENV['FLUENT_CONTAINER_TAIL_PARSER_TYPE'] || 'json'}"
        time_format %Y-%m-%dT%H:%M:%S.%NZ
      </parse>
    </source>

  minio-fluent-dev.conf: |-
    <match minio.s3.**>
      @type s3
      aws_key_id "#{ENV['MINIO_ACCESS_KEY']}" # The access key for Minio
      aws_sec_key "#{ENV['MINIO_SECRET_KEY']}" # The secret key for Minio
      s3_bucket test         # The bucket to store the log data
      s3_endpoint http://minio-service:9000/  # The endpoint URL
      # s3_region us-east-1           # See the region settings of your Minio server
      path logs/                    # This prefix is added to each file
      force_path_style true         # This prevents AWS SDK from breaking endpoint URL
      time_slice_format %Y%m%d%H%M  # This timestamp is added to each file name
      <buffer time>
        @type file
        path /var/log/fluent/s3
        timekey 1m                # Flush the accumulated chunks every 1 min
        timekey_wait 1s             # Wait for 1 seconds before flushing
        timekey_use_utc true        # Use this option if you prefer UTC timestamps
        chunk_limit_size 2k       # The maximum size of each chunk
      </buffer>
    </match>

  minio-fluent.conf: |-
    <match minio.s3.**>
      @type s3
      aws_key_id "#{ENV['MINIO_ACCESS_KEY']}" # The access key for Minio
      aws_sec_key "#{ENV['MINIO_SECRET_KEY']}" # The secret key for Minio
      s3_bucket test         # The bucket to store the log data
      s3_endpoint http://minio-service:9000/  # The endpoint URL
      # s3_region us-east-1           # See the region settings of your Minio server
      path logs/                    # This prefix is added to each file
      force_path_style true         # This prevents AWS SDK from breaking endpoint URL
      time_slice_format %Y%m%d%H%M  # This timestamp is added to each file name
      <buffer time>
        @type file
        path /var/log/fluent/s3
        timekey 60m                # Flush the accumulated chunks every hour
        timekey_wait 1m             # Wait for 60 seconds before flushing
        timekey_use_utc true        # Use this option if you prefer UTC timestamps
        chunk_limit_size 5mb       # The maximum size of each chunk
      </buffer>
    </match>

  reporting-server.conf: |-
    <match rmf.server.**>
      @type http
      endpoint http://reporting-server:8003/log/rmfserver/
      http_method post
      open_timeout 3
      # content_type
      <format>
        @type json
      </format>
      json_array true
      <buffer>
        flush_interval 10s
      </buffer>
      <auth>
        method basic
        username "#{ENV['FLUENTD_USER']}"
        password "#{ENV['FLUENTD_PASSWORD']}"
      </auth>
    </match>

  reporting-server-all-logs.conf: |-
    <match rmf.all.**>
      @type http
      endpoint http://reporting-server:8003/log/all/
      http_method post
      open_timeout 3
      <format>
        @type json
      </format>
      json_array true
      <buffer>
        flush_interval 10s
      </buffer>
    </match>

  keycloak-logs.conf: |-
    <match keycloak**>
      @type http
      endpoint http://reporting-server:8003/log/keycloak/
      http_method post
      open_timeout 3
      <format>
        @type json
      </format>
      json_array true
      <buffer>
        flush_interval 10s
      </buffer>
    </match>
    