{{- if .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "opensearch.uname" . }}-service-monitor
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "opensearch.labels" . | nindent 4 }}
    {{- with .Values.serviceMonitor.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  selector:
    matchLabels:
      {{- include "opensearch.selectorLabels" . | nindent 6 }}
  endpoints:
  - port: {{ .Values.service.httpPortName | default "http" }}
    interval: {{ .Values.serviceMonitor.interval }}
    path: {{ .Values.serviceMonitor.path }}
    scheme: {{ .Values.serviceMonitor.scheme }}
    {{- with .Values.serviceMonitor.tlsConfig }}
    tlsConfig:
      {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- if .Values.serviceMonitor.basicAuth.enabled }}
    basicAuth:
      username:
        {{- if .Values.serviceMonitor.basicAuth.existingSecret }}
        name: {{ .Values.serviceMonitor.basicAuth.existingSecret }}
        {{- else }}
        name: {{ template "opensearch.uname" . }}-service-monitor-credentials
        {{- end }}
        key: username
      password:
        {{- if .Values.serviceMonitor.basicAuth.existingSecret }}
        name: {{ .Values.serviceMonitor.basicAuth.existingSecret }}
        {{- else }}
        name: {{ template "opensearch.uname" . }}-service-monitor-credentials
        {{- end }}
        key: password
    {{- end }}
{{- end }}
