{{- $metricsPort := int .Values.config.metricsPort -}}
{{- if and .Values.podMonitor.enabled (gt $metricsPort 0) }}
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: {{ include "eck-operator.fullname" . }}
  namespace: {{ ternary .Values.podMonitor.namespace .Release.Namespace (not (empty .Values.podMonitor.namespace)) }}
  labels: {{- include "eck-operator.labels" . | nindent 4 }}
  {{- if .Values.podMonitor.labels }}
    {{- toYaml .Values.podMonitor.labels | nindent 4 }}
  {{- end }}
  {{- with .Values.podMonitor.annotations }}
  annotations: {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- with .Values.podMonitor.podTargetLabels }}
  podTargetLabels: {{- toYaml . | nindent 4 }}
  {{- end }}
  podMetricsEndpoints:
    - port: metrics
      path: /metrics
      {{- if .Values.podMonitor.interval }}
      interval: {{ .Values.podMonitor.interval }}
      {{- end }}
      {{- if .Values.podMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.podMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.podMonitor.podMetricsEndpointConfig }}
        {{- toYaml .Values.podMonitor.podMetricsEndpointConfig | nindent 6 }}
      {{- end }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  selector:
    matchLabels: {{- include "eck-operator.selectorLabels" . | nindent 6 }}
{{- end }}