{{- if .Values.prometheus.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "kube2iam.fullname" . }}
  {{- if .Values.prometheus.serviceMonitor.namespace }}
  namespace: {{ .Values.prometheus.serviceMonitor.namespace }}
  {{- end }}
  labels:
    app.kubernetes.io/name: {{ template "kube2iam.name" . }}
    helm.sh/chart: {{ template "kube2iam.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    {{- if .Values.prometheus.serviceMonitor.labels }}
    {{- toYaml .Values.prometheus.serviceMonitor.labels | nindent 4 }}
    {{- end }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "kube2iam.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  endpoints:
    {{- if .Values.prometheus.metricsPort }}
    - port: metrics
    {{- else }}
    - port: http
    {{- end }}
      interval: {{ .Values.prometheus.serviceMonitor.interval }}
      path: /metrics
{{- end -}}
