# vmnodescrape cadvisor
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMNodeScrape
metadata:
  name: kubelet-cadvisor
spec:
  scheme: https
  path: /metrics/cadvisor
  tlsConfig:
    insecureSkipVerify: true
    caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
  bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
  
  # Override job label
  relabelConfigs:
    - targetLabel: job
      replacement: "kubernetes-nodes-cadvisor"
    - action: labelmap
      regex: __meta_kubernetes_node_label_(.+)
  
  # relabel metrics to work with Grafana dashboard
  metricRelabelConfigs:
    - action: replace
      sourceLabels: [pod]
      regex: "(.+)"
      targetLabel: pod_name
      replacement: "$1"
    - action: replace
      sourceLabels: [container]
      regex: "(.+)"
      targetLabel: container_name
      replacement: "$1"
    - action: replace
      targetLabel: name
      replacement: "k8s_stub"
    - action: replace
      sourceLabels: [id]
      regex: "^/system\\.slice/(.+)\\.service$"
      targetLabel: systemd_service_name
      replacement: "$1"

---

# vmnodescrape kubelet
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMNodeScrape
metadata:
  name: kubelet-metrics
spec:
  scheme: https
  tlsConfig:
    insecureSkipVerify: true
    caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
  bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
  
  # relabel metrics to work with Grafana dashboard
  relabelConfigs:
    - targetLabel: job
      replacement: "kubernetes-nodes"
    - action: labelmap
      regex: __meta_kubernetes_node_label_(.+)

---

# vmscrapeconfig apiservers
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMScrapeConfig
metadata:
  name: kubernetes-apiservers
spec:
  kubernetesSDConfigs:
    - role: endpoints
  
  scheme: https
  tlsConfig:
    caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    insecureSkipVerify: true
  bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
  
  relabelConfigs:
    # Keep only the kubernetes service on https port
    - sourceLabels:
        - __meta_kubernetes_namespace
        - __meta_kubernetes_service_name
        - __meta_kubernetes_endpoint_port_name
      action: keep
      regex: "default;kubernetes;https"
    
    # relabel metrics to work with Grafana dashboard
    - targetLabel: job
      replacement: "kubernetes-apiservers"

---

# vmscrapeconfig pods
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMScrapeConfig
metadata:
  name: kubernetes-pods
spec:
  kubernetesSDConfigs:
    - role: pod
  
  relabelConfigs:
    # Skip init containers
    - action: drop
      sourceLabels: [__meta_kubernetes_pod_container_init]
      regex: "true"
    
    # Ensure port annotation matches container port
    - action: keep_if_equal
      sourceLabels:
        - __meta_kubernetes_pod_annotation_prometheus_io_port
        - __meta_kubernetes_pod_container_port_number
    
    # Only pods with prometheus.io/scrape="true"
    - sourceLabels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
      action: keep
      regex: "true"
    
    # Exclude pods marked as "slow"
    - sourceLabels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape_slow]
      action: drop
      regex: "true"
    
    # Scheme override
    - sourceLabels: [__meta_kubernetes_pod_annotation_prometheus_io_scheme]
      action: replace
      targetLabel: __scheme__
      regex: "(https?)"
    
    # Path override
    - sourceLabels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
      action: replace
      targetLabel: __metrics_path__
      regex: "(.+)"
    
    # Address override using prometheus.io/port
    - sourceLabels:
        - __address__
        - __meta_kubernetes_pod_annotation_prometheus_io_port
      action: replace
      targetLabel: __address__
      regex: "([^:]+)(?::\\d+)?;(\\d+)"
      replacement: "$1:$2"
    
    # Copy pod labels
    - action: labelmap
      regex: __meta_kubernetes_pod_label_(.+)
    
    # Use kubernetes_namespace, kubernetes_pod_name, kubernetes_node labels
    - sourceLabels: [__meta_kubernetes_namespace]
      action: replace
      targetLabel: kubernetes_namespace
    - sourceLabels: [__meta_kubernetes_pod_name]
      action: replace
      targetLabel: kubernetes_pod_name
    - sourceLabels: [__meta_kubernetes_pod_node_name]
      action: replace
      targetLabel: kubernetes_node
    
    # Drop non-running pods
    - sourceLabels: [__meta_kubernetes_pod_phase]
      action: drop
      regex: "Pending|Succeeded|Failed|Completed"
    
    # Override job label
    - targetLabel: job
      replacement: "kubernetes-pods"

---

# vmscrapeconfig service endpoints
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMScrapeConfig
metadata:
  name: kubernetes-service-endpoints
spec:
  kubernetesSDConfigs:
    - role: endpoints
  
  relabelConfigs:
    # Skip init containers
    - action: drop
      sourceLabels: [__meta_kubernetes_pod_container_init]
      regex: "true"
    
    # Ensure port annotation matches container port
    - action: keep_if_equal
      sourceLabels:
        - __meta_kubernetes_pod_annotation_prometheus_io_port
        - __meta_kubernetes_pod_container_port_number
    
    # Only services with prometheus.io/scrape="true"
    - sourceLabels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
      action: keep
      regex: "true"
    
    # Exclude "slow" services
    - sourceLabels: [__meta_kubernetes_service_annotation_prometheus_io_scrape_slow]
      action: drop
      regex: "true"
    
    # Scheme override
    - sourceLabels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
      action: replace
      targetLabel: __scheme__
      regex: "(https?)"
    
    # Path override
    - sourceLabels: [__meta_kubernetes_service_annotation_prometheus_io_path]
      action: replace
      targetLabel: __metrics_path__
      regex: "(.+)"
    
    # Address override using prometheus.io/port
    - sourceLabels:
        - __address__
        - __meta_kubernetes_service_annotation_prometheus_io_port
      action: replace
      targetLabel: __address__
      regex: "([^:]+)(?::\\d+)?;(\\d+)"
      replacement: "$1:$2"
    
    # Copy service labels
    - action: labelmap
      regex: __meta_kubernetes_service_label_(.+)
    
    # relabel metrics to work with Grafana dashboard
    - sourceLabels: [__meta_kubernetes_namespace]
      action: replace
      targetLabel: kubernetes_namespace
    - sourceLabels: [__meta_kubernetes_service_name]
      action: replace
      targetLabel: kubernetes_name
    - sourceLabels: [__meta_kubernetes_pod_node_name]
      action: replace
      targetLabel: kubernetes_node
    
    # Override job label
    - targetLabel: job
      replacement: "kubernetes-service-endpoints"

