{{- $metricsPort := int (include "eck-operator.metrics.port" .)}}
{{- if and .Values.config.metrics.secureMode.enabled (eq $metricsPort 0) }}
{{- fail "config.metrics.port must be greater than 0 when config.metrics.secureMode.enabled is true" }}
{{- end }}
{{- if and .Values.podMonitor.enabled (gt $metricsPort 0) }}
{{- if and .Values.podMonitor.enabled .Values.config.metrics.secureMode.enabled }}
{{- fail "podMonitor and config.metrics.secureMode are mutually exclusive" }}
{{- end }}
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: {{ include "eck-operator.fullname" . }}
  namespace: {{ ternary .Values.podMonitor.namespace .Release.Namespace (not (and (.Values.podMonitor) (empty .Values.podMonitor.namespace))) }}
  labels: {{- include "eck-operator.labels" . | nindent 4 }}
  {{- with .Values.podMonitor.labels }}
    {{- toYaml . | 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
      {{- with .Values.podMonitor.interval }}
      interval: {{ . }}
      {{- end }}
      {{- with .Values.podMonitor.scrapeTimeout }}
      scrapeTimeout: {{ . }}
      {{- end }}
      {{- with .Values.podMonitor.podMetricsEndpointConfig }}
        {{- toYaml . | nindent 6 }}
      {{- end }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  selector:
    matchLabels: {{- include "eck-operator.selectorLabels" . | nindent 6 }}
{{- end }}
