---
apiVersion: v1
kind: ConfigMap
metadata:
  name: promtail
  labels:
    app: promtail
data:
  kubernetes-config.yaml: |-
    server:
      http_listen_port: 3101
      grpc_listen_port: 0

    positions:
      filename: /tmp/positions.yaml

    clients:
      - url: http://loki:3100/loki/api/v1/push

    target_config:
      sync_period: 10s

    scrape_configs:
      - job_name: journal
        journal:
          max_age: 12h
          labels:
            job: systemd-journal
        relabel_configs:
          - source_labels: ['__journal__systemd_unit']
            target_label: 'unit'

      - job_name: system
        static_configs:
        - targets:
            - localhost
          labels:
            job: varlogs
            __path__: /var/log/*log

      - job_name: kubernetes-pods-name
        pipeline_stages:
          - docker: {}
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels:
          - __meta_kubernetes_pod_label_name
          target_label: __service__
        - source_labels:
          - __meta_kubernetes_pod_node_name
          target_label: __host__
        - action: drop
          regex: ''
          source_labels:
          - __service__
        - action: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - action: replace
          replacement: $1
          separator: /
          source_labels:
          - __meta_kubernetes_namespace
          - __service__
          target_label: job
        - action: replace
          source_labels:
          - __meta_kubernetes_namespace
          target_label: namespace
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_name
          target_label: instance
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_container_name
          target_label: container_name
        - replacement: /var/log/pods/*$1/*.log
          separator: /
          source_labels:
          - __meta_kubernetes_pod_uid
          - __meta_kubernetes_pod_container_name
          target_label: __path__
      - job_name: kubernetes-pods-app
        pipeline_stages:
          - docker: {}
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - action: drop
          regex: .+
          source_labels:
          - __meta_kubernetes_pod_label_name
        - source_labels:
          - __meta_kubernetes_pod_label_app
          target_label: __service__
        - source_labels:
          - __meta_kubernetes_pod_node_name
          target_label: __host__
        - action: drop
          regex: ''
          source_labels:
          - __service__
        - action: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - action: replace
          replacement: $1
          separator: /
          source_labels:
          - __meta_kubernetes_namespace
          - __service__
          target_label: job
        - action: replace
          source_labels:
          - __meta_kubernetes_namespace
          target_label: namespace
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_name
          target_label: instance
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_container_name
          target_label: container_name
        - replacement: /var/log/pods/*$1/*.log
          separator: /
          source_labels:
          - __meta_kubernetes_pod_uid
          - __meta_kubernetes_pod_container_name
          target_label: __path__
      - job_name: kubernetes-pods-direct-controllers
        pipeline_stages:
          - docker: {}
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - action: drop
          regex: .+
          separator: ''
          source_labels:
          - __meta_kubernetes_pod_label_name
          - __meta_kubernetes_pod_label_app
        - action: drop
          regex: '[0-9a-z-.]+-[0-9a-f]{8,10}'
          source_labels:
          - __meta_kubernetes_pod_controller_name
        - source_labels:
          - __meta_kubernetes_pod_controller_name
          target_label: __service__
        - source_labels:
          - __meta_kubernetes_pod_node_name
          target_label: __host__
        - action: drop
          regex: ''
          source_labels:
          - __service__
        - action: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - action: replace
          replacement: $1
          separator: /
          source_labels:
          - __meta_kubernetes_namespace
          - __service__
          target_label: job
        - action: replace
          source_labels:
          - __meta_kubernetes_namespace
          target_label: namespace
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_name
          target_label: instance
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_container_name
          target_label: container_name
        - replacement: /var/log/pods/*$1/*.log
          separator: /
          source_labels:
          - __meta_kubernetes_pod_uid
          - __meta_kubernetes_pod_container_name
          target_label: __path__
      - job_name: kubernetes-pods-indirect-controller
        pipeline_stages:
          - docker: {}
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - action: drop
          regex: .+
          separator: ''
          source_labels:
          - __meta_kubernetes_pod_label_name
          - __meta_kubernetes_pod_label_app
        - action: keep
          regex: '[0-9a-z-.]+-[0-9a-f]{8,10}'
          source_labels:
          - __meta_kubernetes_pod_controller_name
        - action: replace
          regex: '([0-9a-z-.]+)-[0-9a-f]{8,10}'
          source_labels:
          - __meta_kubernetes_pod_controller_name
          target_label: __service__
        - source_labels:
          - __meta_kubernetes_pod_node_name
          target_label: __host__
        - action: drop
          regex: ''
          source_labels:
          - __service__
        - action: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - action: replace
          replacement: $1
          separator: /
          source_labels:
          - __meta_kubernetes_namespace
          - __service__
          target_label: job
        - action: replace
          source_labels:
          - __meta_kubernetes_namespace
          target_label: namespace
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_name
          target_label: instance
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_container_name
          target_label: container_name
        - replacement: /var/log/pods/*$1/*.log
          separator: /
          source_labels:
          - __meta_kubernetes_pod_uid
          - __meta_kubernetes_pod_container_name
          target_label: __path__
      - job_name: kubernetes-pods-static
        pipeline_stages:
          - docker: {}
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - action: drop
          regex: ''
          source_labels:
          - __meta_kubernetes_pod_annotation_kubernetes_io_config_mirror
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_label_component
          target_label: __service__
        - source_labels:
          - __meta_kubernetes_pod_node_name
          target_label: __host__
        - action: drop
          regex: ''
          source_labels:
          - __service__
        - action: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - action: replace
          replacement: $1
          separator: /
          source_labels:
          - __meta_kubernetes_namespace
          - __service__
          target_label: job
        - action: replace
          source_labels:
          - __meta_kubernetes_namespace
          target_label: namespace
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_name
          target_label: instance
        - action: replace
          source_labels:
          - __meta_kubernetes_pod_container_name
          target_label: container_name
        - replacement: /var/log/pods/*$1/*.log
          separator: /
          source_labels:
          - __meta_kubernetes_pod_annotation_kubernetes_io_config_mirror
          - __meta_kubernetes_pod_container_name
          target_label: __path__