{{- if .Values.logIngest.enabled }}
apiVersion: opentelemetry.io/v1beta1
kind: OpenTelemetryCollector
metadata:
  name: {{ .Release.Name }}-opentelemetry-collector
  namespace: {{ .Release.Namespace }}
spec:
  mode: sidecar
  image: ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-contrib:latest
  serviceAccount: "{{ .Release.Name }}-otel-collector"
  volumeMounts:
    - name: app-logs
      mountPath: {{ .Values.logIngest.logPath }}
  securityContext:
    allowPrivilegeEscalation: true
    privileged: true
    runAsUser: 0
    runAsGroup: 0
  config:
    receivers:
      filelog:
        include:
          - {{ .Values.logIngest.logPathPattern }}
        start_at: end
        include_file_path: false
        include_file_name: false
        operators:
          - type: json_parser
            parse_from: body
            parse_to: attributes
    processors:
      k8sattributes:
        auth_type: serviceAccount
        wait_for_metadata: true
        wait_for_metadata_timeout: 10s
        passthrough: false
        extract:
          metadata:
            - k8s.pod.name
            - k8s.pod.ip
            - k8s.pod.uid
            - k8s.deployment.name
            - k8s.deployment.uid
            - k8s.namespace.name
            - k8s.node.name
      attributes:
        actions:
          - action: insert
            key: kubernetes.pod_name
            from_attribute: k8s.pod.name
          - action: insert
            key: kubernetes.pod_ip
            from_attribute: k8s.pod.ip
          - action: insert
            key: kubernetes.pod_uid
            from_attribute: k8s.pod.uid
          - action: insert
            key: kubernetes.deployment_name
            from_attribute: k8s.deployment.name
          - action: insert
            key: kubernetes.deployment_uid
            from_attribute: k8s.deployment.uid
          - action: insert
            key: kubernetes.namespace_name
            from_attribute: k8s.namespace.name
          - action: insert
            key: kubernetes.node_name
            from_attribute: k8s.node.name
          - action: insert
            key: app
            from_attribute: context.app
          - action: insert
            key: environment
            from_attribute: context.env
      transform:
        log_statements:
          - context: log
            statements:
              - set(log.body["kubernetes"], log.attributes["kubernetes"])
      batch:
        send_batch_size: 5
        timeout: 10s  
    exporters:
      otlphttp/logs:
        endpoint: {{ .Values.logIngest.lokiEndpoint }}/otlp
        tls:
          insecure: true
      debug:
        verbosity: detailed
    service:
      pipelines:
        logs:
          receivers: [filelog]
          processors: [k8sattributes, attributes, transform, batch]
          exporters: [otlphttp/logs, debug]
{{- end }}