apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  labels:
    relatedNamespace: {{ .Values.namespace }}
  name: {{ .Values.postgresCluster.pgmonitor.podMonitorName }}-{{ .Values.namespace }}
  namespace: openshift-monitoring
spec:
  jobLabel: crunchy-postgres-exporter
  namespaceSelector:
    matchNames:
      - {{ .Values.namespace }}
  podMetricsEndpoints:
    - honorLabels: true
      interval: 30s
      path: /metrics
      relabelings:
        - action: drop
          regex: '5432'
          sourceLabels:
            - __meta_kubernetes_pod_container_port_number
        - action: drop
          regex: '10000'
          sourceLabels:
            - __meta_kubernetes_pod_container_port_number
        - action: drop
          regex: '8009'
          sourceLabels:
            - __meta_kubernetes_pod_container_port_number
        - action: drop
          regex: '2022'
          sourceLabels:
            - __meta_kubernetes_pod_container_port_number
        - action: drop
          regex: ^$
          sourceLabels:
            - __meta_kubernetes_pod_container_port_number
        - action: replace
          sourceLabels:
            - __meta_kubernetes_namespace
          targetLabel: kubernetes_namespace
        - sourceLabels:
            - __meta_kubernetes_pod_name
          targetLabel: pod
        - replacement: $1
          separator: ''
          sourceLabels:
            - __meta_kubernetes_pod_label_postgres_operator_crunchydata_com_cluster
          targetLabel: cluster
        - replacement: $1$2
          separator: ':'
          sourceLabels:
            - __meta_kubernetes_namespace
            - cluster
          targetLabel: pg_cluster
        - replacement: $1
          sourceLabels:
            - __meta_kubernetes_pod_ip
          targetLabel: ip
        - replacement: $1
          separator: ''
          sourceLabels:
            - __meta_kubernetes_pod_label_postgres_operator_crunchydata_com_instance
          targetLabel: deployment
        - replacement: $1
          separator: ''
          sourceLabels:
            - __meta_kubernetes_pod_label_postgres_operator_crunchydata_com_role
          targetLabel: role
        - replacement: $1
          sourceLabels:
            - dbname
          targetLabel: dbname
        - replacement: $1
          sourceLabels:
            - relname
          targetLabel: relname
        - replacement: $1
          sourceLabels:
            - schemaname
          targetLabel: schemaname
      scheme: http
      targetPort: 9187
  selector:
    matchLabels:
      postgres-operator.crunchydata.com/crunchy-postgres-exporter: 'true'
