# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: gmp-system
  name: scrape-kubelet
  labels:
    type: scrape-config
data:
  config.yaml: |
    scrape_configs:

    # All kubelet metric endpoints export at least some metrics with a 'namespace' label. For these
    # we want them to override the target label 'namespace' for better usability.
    # Since only a subset of metrics is affected, generically dropping the target label does not work
    # though. Thus we need to set honor_labels=true for the intended semantics. Since there are no
    # other labels overlapping with our target labels, this is fine.

    # Metrics about the kubelet process itself.
    - job_name: kubelet/metrics
      scrape_interval: 30s
      scrape_timeout: 30s
      metrics_path: /metrics
      honor_labels: true
      scheme: https
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      tls_config:
        insecure_skip_verify: true
      kubernetes_sd_configs:
      - role: node
        selectors:
        - role: node
          field: metadata.name=$(NODE_NAME)
      relabel_configs:
      - action: replace
        target_label: job
        replacement: kubelet
      - source_labels: [__meta_kubernetes_node_name]
        target_label: instance
        replacement: ${1}:metrics
        action: replace

    # cAdvisor (per-container) metrics exposed through the kubelet.
    - job_name: kubelet/cadvisor
      scrape_interval: 30s
      scrape_timeout: 30s
      metrics_path: /metrics/cadvisor
      honor_labels: true
      scheme: https
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      tls_config:
        insecure_skip_verify: true
      kubernetes_sd_configs:
      - role: node
        selectors:
        - role: node
          field: metadata.name=$(NODE_NAME)
      relabel_configs:
      - action: replace
        target_label: job
        replacement: kubelet
      - source_labels: [__meta_kubernetes_node_name]
        target_label: instance
        replacement: ${1}:cadvisor
        action: replace

    # Metrics on probes executed by the kubelet.
    - job_name: kubelet/probes
      scrape_interval: 30s
      scrape_timeout: 30s
      metrics_path: /metrics/probes
      honor_labels: true
      scheme: https
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      tls_config:
        insecure_skip_verify: true
      kubernetes_sd_configs:
      - role: node
        selectors:
        - role: node
          field: metadata.name=$(NODE_NAME)
      relabel_configs:
      - action: replace
        target_label: job
        replacement: kubelet
      - source_labels: [__meta_kubernetes_node_name]
        target_label: instance
        replacement: ${1}:probes
        action: replace