apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus.config
  namespace: default
data:
  config.yaml: |-
    global:
      scrape_interval: 15s
      evaluation_interval: 15s
    #rule_files:
    #- "/etc/rules/*.rule"
    scrape_configs:
    
    # Self prometheus
    - job_name: 'prometheus'
      scrape_interval: 5s
      static_configs:
        - targets: ['localhost:9090']
    
    # Kubernetes Nodes
    - job_name: 'kubernetes-nodes'
      scheme: https
      kubernetes_sd_configs:
      - role: node
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        insecure_skip_verify: true
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      relabel_configs:
      - action: labelmap
        regex: __meta_kubernetes_node_label_(.+)
      - action: replace
        source_labels: ['__meta_kubernetes_node_name']
        target_label: node_name
    
    # Kubernetes API Server
    - job_name: 'kubernetes-apiserver'
      scheme: https
      kubernetes_sd_configs:
      - role: endpoints
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        insecure_skip_verify: true
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      relabel_configs:
      - source_labels: [ __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
        action: keep
        regex: kubernetes;https

    # Kubernetes service endpoints
    - job_name: 'kubernetes-service-endpoints'
      scheme: http
      kubernetes_sd_configs:
      - role: endpoints
      relabel_configs:
      # Look for the Prometheus annotations and scrape based on those
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
        action: keep
        regex: true
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
        action: replace
        target_label: __scheme__
        regex: (https?)
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
        action: replace
        target_label: __address__
        regex: (.+)(?::\d+);(\d+)
        replacement: $1:$2
      # Add pod information
      - action: replace
        source_labels: ['__meta_kubernetes_pod_ip']
        target_label: pod_ip
      - action: replace
        source_labels: ['__meta_kubernetes_pod_label_app']
        target_label: app
      - action: replace
        source_labels: ['__meta_kubernetes_pod_name']
        target_label: pod_name
      - action: replace
        source_labels: ['__meta_kubernetes_pod_node_name']
        target_label: node_name
