{{- if and ( .Capabilities.APIVersions.Has "monitoring.coreos.com/v1" ) .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "kong.fullname" . }}
  {{- if .Values.serviceMonitor.namespace }}
  namespace: {{ .Values.serviceMonitor.namespace }}
  {{- end }}
  labels:
    {{- include "kong.metaLabels" . | nindent 4 }}
  {{- if .Values.serviceMonitor.labels }}
    {{ toYaml .Values.serviceMonitor.labels | nindent 4 }}
  {{- end }}
spec:
  endpoints:
  - targetPort: status
    scheme: http
    {{- if .Values.serviceMonitor.interval }}
    interval: {{ .Values.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.serviceMonitor.honorLabels }}
    honorLabels: true
    {{- end }}
    {{- if .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings: {{ toYaml .Values.serviceMonitor.metricRelabelings | nindent 6 }}
    {{- end }}
  {{ if (semverCompare ">= 2.0.0" (include "kong.effectiveVersion" .Values.ingressController.image)) -}}
  - targetPort: cmetrics
    scheme: http
    {{- if .Values.serviceMonitor.interval }}
    interval: {{ .Values.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.serviceMonitor.honorLabels }}
    honorLabels: true
    {{- end }}
    {{- if .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings: {{ toYaml .Values.serviceMonitor.metricRelabelings | nindent 6 }}
    {{- end }}
  {{- end }}
  jobLabel: {{ .Release.Name }}
  namespaceSelector:
    matchNames:
    - {{ template "kong.namespace" . }}
  selector:
    matchLabels:
      enable-metrics: "true"
      {{- include "kong.metaLabels" . | nindent 6 }}
  {{- if .Values.serviceMonitor.targetLabels }}
  targetLabels: {{ toYaml .Values.serviceMonitor.targetLabels | nindent 4 }}
  {{- end }}
{{- end }}
