apiVersion: v1
kind: ConfigMap
metadata:
  name: zzz-fluent-bit-overrides
  namespace: kyma-installer
  labels:
    installer: overrides
    component: logging
    kyma-project.io/installation: ""
data:
  fluent-bit.config.filters.kubernetes.mergeLog: "On"
  fluent-bit.config.inputs.tail.tag: "kube.*"
  fluent-bit.config.outputs.loki.match: "kube.*"
  fluent-bit.config.outputs.additional: |
    [OUTPUT]
        Name forward
        Match *
        Host fluentd.{{ .Values.core_namespace }}.svc.cluster.local
        Port 24224
        Retry_Limit false
  fluent-bit.config.inputs.additional: |
    [INPUT]
        Name cpu
        Tag  metric
    [INPUT]
        Name mem
        Tag  metric
  fluent-bit.config.filters.kubernetes.match: "kube.*"
  fluent-bit.config.filters.additional: |
    [FILTER]
        Name record_modifier
        Match *
        Record cluster_identifier cloud
    [FILTER]
        Name record_modifier
        Match metric
        Remove_key Swap.total
        Remove_key Swap.used
        Remove_key Swap.free
        Record source_type metrics
        Record kubernetes.pod_name ${HOSTNAME}
        Record kubernetes.labels.app fluent-bit-metrics
        Record kubernetes.namespace_name kyma-system
    [FILTER]
        Name   lua
        Match  kube.*
        script script.lua
        call   kubernetes_map_keys
    [FILTER]
        Name   lua
        Match  *
        script script.lua
        call   add_source_type
    [FILTER]
        Name   lua
        Match  *
        script script.lua
        call   add_aligned_sent_received

  fluent-bit.config.script: |
    math.randomseed(os.time())
    local random = math.random
    local function uuid()
        local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
        return string.gsub(template, '[xy]', function(c)
            local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
            return string.format('%x', v)
        end)
    end
    function reformat(tag, timestamp, record)
        payload = record["log"]
        mpsTenant = "8a6949d9-9e0d-4711-8020-1a65f257d950"
        customerTenant = "e3899aed-21e6-4f38-a582-a2a47affe0ab"
        new_payload = "{\"id\": {\"externalTenant\": \"" .. customerTenant .. "\"},\"reason\":" .. record["log"] .. "}"
        new_record = {}
        new_record["type"] = "object"
        new_record["description"] = "Dex log record"
        new_record["time"] = payload:match("\"time\":\"(.-)\"")
        new_record["user"] = payload:match("email=\\\"(.-)\\\"") or "systemuser"
        new_record["tenant"] = mpsTenant
        new_record["uuid"] = uuid()
        new_record["data"] = new_payload
        return 1, 0, new_record
    end
    function kubernetes_map_keys(tag, timestamp, record)
      if record.kubernetes == nil then
          return 0
      end
      map_keys(record.kubernetes.annotations)
      map_keys(record.kubernetes.labels)
      return 1, timestamp, record
    end
    function map_keys(table)
        if table == nil then
            return
        end
        local new_table = {}
        local changed_keys = {}
        for key, val in pairs(table) do
            local mapped_key = string.gsub(key, "[%/%.]", "_")
            if mapped_key ~= key then
                new_table[mapped_key] = val
                changed_keys[key] = true
            end
        end
        for key in pairs(changed_keys) do
            table[key] = nil
        end
        for key, val in pairs(new_table) do
            table[key] = val
        end
    end
    function add_source_type(tag, timestamp, record)
        if record.kubernetes ~= nil then
            local namespace = record.kubernetes.namespace_name
            if record.kubernetes.container_name == "istio-proxy" and
                   record.protocol ~= nil then
                record.source_type = "istio-envoy-proxy-access-log"
            elseif string.find(namespace, "compass%-system") or
               string.find(namespace, "kube%-node%-lease") or
               string.find(namespace, "kube%-public") or
               string.find(namespace, "kube%-system") or
               string.find(namespace, "kyma%-installer") or
               string.find(namespace, "kyma%-integration") or
               string.find(namespace, "kyma%-system") or
               string.find(namespace, "istio%-system") then
                record.source_type = "kyma-system"
            else
                record.source_type = "application"
            end
        end
        return 1, timestamp, record
    end
    function add_aligned_sent_received(tag, timestamp, record)
      if record.source_type ~= "istio-envoy-proxy-access-log" then
          return 0
      end
      if record.upstream_cluster:find("outbound|", 1) == 1 then
          record.bytes_sent_aligned = record.bytes_received
          record.bytes_received_aligned = record.bytes_sent
      else
          record.bytes_sent_aligned = record.bytes_sent
          record.bytes_received_aligned = record.bytes_received
      end
      return 1, timestamp, record
    end
