# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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
#
# http://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.
# Tri-state for PodMonitor creation (dynamo.metrics.podMonitors.enabled):
#   null  = auto-detect via .Capabilities (works with helm install/upgrade)
#   true  = always create (use with helm template / GitOps)
#   false = never create (opt-out even if prometheus-operator is installed)
{{- $podMonitorsEnabled := false }}
{{- if kindIs "invalid" .Values.dynamo.metrics.podMonitors.enabled }}
  {{- $podMonitorsEnabled = (.Capabilities.APIVersions.Has "monitoring.coreos.com/v1") }}
{{- else }}
  {{- $podMonitorsEnabled = .Values.dynamo.metrics.podMonitors.enabled }}
{{- end }}
{{- if $podMonitorsEnabled }}
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: dynamo-frontend
spec:
  {{- if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  {{- else }}
  namespaceSelector:
    any: true
  {{- end }}
  podMetricsEndpoints:
  - interval: 5s
    path: /metrics
    port: http
    # honorLabels ensures dynamo_namespace and other labels set by the dynamo
    # runtime (including router metrics embedded in the frontend) are preserved.
    honorLabels: true
    relabelings:
    - action: replace
      sourceLabels:
      - __meta_kubernetes_pod_label_nvidia_com_dynamo_namespace
      targetLabel: dynamo_namespace
  selector:
    matchLabels:
      nvidia.com/dynamo-component-type: frontend
      nvidia.com/metrics-enabled: "true"
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: dynamo-worker
spec:
  {{- if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  {{- else }}
  namespaceSelector:
    any: true
  {{- end }}
  podMetricsEndpoints:
  - interval: 5s
    path: /metrics
    port: system
  - interval: 5s
    path: /metrics
    port: nixl
  selector:
    matchLabels:
      nvidia.com/dynamo-component-type: worker
      nvidia.com/metrics-enabled: "true"
---
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: dynamo-planner
spec:
  {{- if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  {{- else }}
  namespaceSelector:
    any: true
  {{- end }}
  podMetricsEndpoints:
  - interval: 5s
    path: /metrics
    port: metrics
  selector:
    matchLabels:
      nvidia.com/dynamo-component-type: planner
      nvidia.com/metrics-enabled: "true"
---
# TODO: Once a first-class 'router' componentType is added to the dynamo operator,
#       replace this PodMonitor with one that selects on
#       nvidia.com/dynamo-component-type: router instead of 'default'.
#       That would also eliminate the port-override relabeling below, since router
#       pods could declare containerPort: 9090 (DYN_SYSTEM_PORT) explicitly and
#       the standard targetPort selector would work without the address rewrite hack.
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: dynamo-router
spec:
  {{- if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  {{- else }}
  namespaceSelector:
    any: true
  {{- end }}
  podMetricsEndpoints:
  - interval: 5s
    path: /metrics
    honorLabels: true
    relabelings:
    # Force port 9090 (DYN_SYSTEM_PORT) without requiring containerPort in the pod spec.
    # targetPort: 9090 would generate a keep rule on __meta_kubernetes_pod_container_port_number
    # that drops pods without an explicit containerPort: 9090 declaration.
    - action: replace
      sourceLabels:
      - __address__
      regex: '([^:]+)(?::\d+)?'
      replacement: '${1}:9090'
      targetLabel: __address__
    - action: replace
      sourceLabels:
      - __meta_kubernetes_pod_label_nvidia_com_dynamo_namespace
      targetLabel: dynamo_namespace
  selector:
    matchLabels:
      nvidia.com/dynamo-component-type: default
      nvidia.com/metrics-enabled: "true"
{{- end }}