{{- 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: true
        include_file_name: true
        operators:
          # 简单解析JSON，不改变结构
          - type: json_parser
          - type: remove
            field: body
    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
        pod_association:
          - sources:
              - from: resource_attribute
                name: k8s.pod.ip
          - sources:
              - from: resource_attribute
                name: k8s.pod.uid
          - sources:
              - from: connection
      resource:
        attributes:
          - key: service.name
            value: "{{ .Release.Name }}"
            action: upsert
          - key: service.namespace
            value: "{{ .Release.Namespace }}"
            action: upsert
          - key: loki.format
            action: insert
            value: json
      batch:
        send_batch_size: 1000
        timeout: 10s  
    exporters:
      loki:
        endpoint: {{ .Values.logIngest.lokiEndpoint }}
        tls:
          insecure: true
    service:
      telemetry:
        logs:
          level: "info"
      pipelines:
        logs:
          receivers: [filelog]
          processors: [k8sattributes, resource, batch]
          exporters: [loki]
{{- end }}