{{- if and (.Capabilities.APIVersions.Has "monitoring.coreos.com/v1") .Values.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "starrockscluster.fe.name" . }}
  namespace: {{ template "starrockscluster.namespace" . }}
  labels:
    cluster: {{ template "starrockscluster.name" . }}
    {{- if .Values.metrics.serviceMonitor.labels }}
    {{- toYaml .Values.metrics.serviceMonitor.labels | nindent 4 }}
    {{- end }}
spec:
  endpoints:
    - port: http
      {{- with .Values.metrics.serviceMonitor.interval }}
      interval: {{ . }}
      {{- end }}
      path: /metrics
      {{- if .Values.metrics.serviceMonitor.basicAuth.enabled }}
      basicAuth:
        username:
          name: {{ .Values.metrics.serviceMonitor.basicAuth.usernameSecretName }}
          key: {{ .Values.metrics.serviceMonitor.basicAuth.usernameSecretKey }}
        password:
          name: {{ .Values.metrics.serviceMonitor.basicAuth.passwordSecretName }}
          key: {{ .Values.metrics.serviceMonitor.basicAuth.passwordSecretKey }}
      {{- end }}
      {{- if .Values.metrics.serviceMonitor.endpointParam.enabled }}
      params:
        {{- toYaml .Values.metrics.serviceMonitor.endpointParam.params | nindent 8 }}
      {{- end }}
      relabelings:
        - sourceLabels: [__meta_kubernetes_endpoints_label_app_starrocks_ownerreference_name]
          targetLabel: app_starrocks_ownerreference_name
        - sourceLabels: [__meta_kubernetes_service_label_app_kubernetes_io_component]
          targetLabel: app_kubernetes_io_component
  namespaceSelector:
    matchNames:
      - {{ template "starrockscluster.namespace" . }}
  selector:
    matchLabels:
      app.starrocks.ownerreference/name: {{ template "starrockscluster.name" . }}
      app.kubernetes.io/component: fe

---

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "starrockscluster.be.name" . }}
  namespace: {{ template "starrockscluster.namespace" . }}
  labels:
    cluster: {{ template "starrockscluster.name" . }}
    {{- if .Values.metrics.serviceMonitor.labels }}
    {{- toYaml .Values.metrics.serviceMonitor.labels | nindent 4 }}
    {{- end }}
spec:
  endpoints:
    - port: webserver
      {{- with .Values.metrics.serviceMonitor.interval }}
      interval: {{ . }}
      {{- end }}
      path: /metrics
      relabelings:
        - sourceLabels: [__meta_kubernetes_endpoints_label_app_starrocks_ownerreference_name]
          targetLabel: app_starrocks_ownerreference_name
        - sourceLabels: [__meta_kubernetes_service_label_app_kubernetes_io_component]
          targetLabel: app_kubernetes_io_component
  namespaceSelector:
    matchNames:
      - {{ template "starrockscluster.namespace" . }}
  selector:
    matchLabels:
      app.starrocks.ownerreference/name: {{ template "starrockscluster.name" . }}
      app.kubernetes.io/component: be

---

{{- if .Values.starrocksCluster.enabledCn }}

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "starrockscluster.cn.name" . }}
  namespace: {{ template "starrockscluster.namespace" . }}
  labels:
    cluster: {{ template "starrockscluster.name" . }}
    {{- if .Values.metrics.serviceMonitor.labels }}
    {{- toYaml .Values.metrics.serviceMonitor.labels | nindent 4 }}
    {{- end }}
spec:
  endpoints:
    - port: webserver
      {{- with .Values.metrics.serviceMonitor.interval }}
      interval: {{ . }}
      {{- end }}
      path: /metrics
      relabelings:
        - sourceLabels: [__meta_kubernetes_endpoints_label_app_starrocks_ownerreference_name]
          targetLabel: app_starrocks_ownerreference_name
        - sourceLabels: [__meta_kubernetes_service_label_app_kubernetes_io_component]
          targetLabel: app_kubernetes_io_component
  namespaceSelector:
    matchNames:
      - {{ template "starrockscluster.namespace" . }}
  selector:
    matchLabels:
      app.starrocks.ownerreference/name: {{ template "starrockscluster.name" . }}
      app.kubernetes.io/component: cn

{{- end }}
{{- end }}