{{- with .Values.dynamicConfig.logging }}
{{- if and .enabled $.Values.dynamicConfig.enabled }}

---
apiVersion: logging.banzaicloud.io/v1beta1
kind: Flow
metadata:
  name: {{ include "collabora-online.fullname" $ }}-dynconfig
spec:
  match:
    - select:
        labels:
          type: dynconfig
          {{- include "collabora-online.selectorLogLabels" $ | nindent 10 }}
        container_names:
          - "{{ $.Chart.Name }}-dynconfig"

  filters:
    - parser:
        hash_value_field: "nginx"
        reserve_data: true
        reserve_time: true
        remove_key_name_field: true
        parse:
          type: "multi_format"
          patterns:
            - format: "regexp"
              # for
              expression: '^(?<remote>[^ ]*) -?(?<host>.*) -?(?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S*) ?(?<path>[^\"]*) (?<httpversion>HTTP\/[0-9\.]+)?" (?<code>[^ ]*) (?<size>[^ ]*) "-?(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?:(?<upstream_address_list>[^\"-]*)|-)"?$'
              types: 'code:integer,size:integer,upstream_address_list:array'
              time_key: "time"
              time_format: "%d/%b/%Y:%H:%M:%S %z"
            - format: "none"
    {{- if .ecs }}
    - record_transformer:
        enable_ruby: true
        records:
          - event.module: "nginx"
            destination.domain: '${ !(record["nginx"]["referer"].nil?) ? URI(record["nginx"]["referer"]).host : record["nginx"]["host"] }'
            url.original: '${ record["nginx"]["referer"] }${ record["nginx"]["path"] }'
            url.domain: '${ !(record["nginx"]["referer"].nil?) ? URI(record["nginx"]["referer"]).host : record["nginx"]["host"] }'
            url.path: '${ record["nginx"]["path"] }'
            http.version: '${ record["nginx"]["httpversion"] }'
            source.ip: '${ record["nginx"]["remote"] }'
            related.ip: '${ record["nginx"]["remote"] }'
            http.request.method: '${ record["nginx"]["method"] }'
            http.request.referrer: '${ record["nginx"]["referer"] }'
            user.name: '${ record["nginx"]["user"] }'
            related.user: '${ record["nginx"]["user"] }'
            http.request.useragent: '${ record["nginx"]["agent"] }'
            user_agent.original: '${ record["nginx"]["agent"] }'
            http.response.status_code: '${ record["nginx"]["code"] }'
            nginx.ingress_controller.upstream_address_list: '${ record["nginx"]["upstream_address_list"] }'
            # TODO split ip and port
            nginx.ingress_controller.upstream.address.merged: '${ [record["upstream_address_list"]].flatten&.last }'
            http.response.body.bytes: '${ record["nginx"]["size"] }'
            event.category: "web"
            event.kind: "event"
            event.type: "access"
            event.outcome: '${ record["nginx"]["code"].to_i < 400 ? "success" : "failure" }'
            # for dashboard
            fileset.name: '${[ "ingress_controller", "access" ]}'
            # on second regex -> error handling
            message: '${ record["nginx"]["message"] }'
        remove_keys: "$['nginx']['remote'],$['nginx']['host'],$['nginx']['user'],$['nginx']['method'],$['nginx']['path'],$['nginx']['httpversion'],$['nginx']['code'],$['nginx']['size'],$['nginx']['referer'],$['nginx']['agent'],$['nginx']['upstream_address_list'],$['nginx']['message']"
    {{- end }}
    {{- with .additionalFilters }}
    {{- toYaml . | nindent 4 }}
    {{- end }}

  {{- with .globalOutputRefs }}
  globalOutputRefs:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .localOutputRefs }}
  localOutputRefs:
    {{- toYaml . | nindent 4 }}
  {{- end }}
{{- end }}

{{- end }}
