# 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-kube-state-metrics
  labels:
    type: scrape-config
    app.kubernetes.io/name: kube-state-metrics
data:
  config.yaml: |
    # The resource state metrics typically have a namespace associated, which we want to use
    # rather than the typical target label 'namespace', which merely contains the namespace of the
    # KSM pod.
    # To ensure the namespace is left empty, we use this hard-coded configuration.
    
    # Scraping of KSM's process metrics is configured via PodMonitoring.

    scrape_configs:
    - job_name: kube-state-metrics/metrics
      scrape_interval: 30s
      scrape_timeout: 30s
      metrics_path: /metrics
      honor_labels: true
      kubernetes_sd_configs:
      - role: pod
        selectors:
        - role: pod
          field: spec.nodeName=$(NODE_NAME)
      relabel_configs:
      - source_labels: [__meta_kubernetes_pod_node_name]
        regex: $(NODE_NAME)
        action: keep
      - source_labels: [__meta_kubernetes_namespace]
        regex: gmp-system
        action: keep
      - source_labels: [__meta_kubernetes_pod_label_app_kubernetes_io_name]
        regex: kube-state-metrics
        action: keep
      - target_label: job
        replacement: kube-state-metrics
        action: replace
      - source_labels: [__meta_kubernetes_pod_name, __meta_kubernetes_pod_container_port_name]
        regex: (.+);(.+)
        target_label: instance
        replacement: $1:$2
        action: replace
      - source_labels: [__meta_kubernetes_pod_container_port_name]
        regex: metrics
        action: keep