{{- if .Values.monitor.create }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "cluster.name" . }}-monitor
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "chart.name" . }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: monitor
    helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+"  "_" }}
spec:
  replicas: 1
  strategy:
    type: Recreate
    rollingUpdate: null
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "chart.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/component: monitor
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ template "chart.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/component: monitor
    spec:
  {{- if .Values.monitor.serviceAccount }}
      serviceAccount: {{ .Values.monitor.serviceAccount }}
  {{- else }}
    {{- if .Values.rbac.create }}
      serviceAccount: {{ template "cluster.name" . }}-monitor
    {{- end }}
  {{- end }}
    {{- if .Values.monitor.nodeSelector }}
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
            {{- range $key, $val := .Values.monitor.nodeSelector }}
              {{- $valList := splitList "," $val }}
              - key: {{ $key }}
                operator: In
                values:
              {{- range $kl, $vl := $valList }}
                - {{ $vl }}
              {{- end }}
            {{- end }}
    {{- end }}
      initContainers:
      - name: monitor-initializer
        image: {{ .Values.monitor.initializer.image }}
        imagePullPolicy: {{ .Values.monitor.initializer.imagePullPolicy | default "IfNotPresent" }}
        env:
        - name: GF_PROVISIONING_PATH
          value: /grafana-dashboard-definitions/tidb
        - name: GF_DATASOURCE_PATH
          value: /etc/grafana/provisioning/datasources
        - name: TIDB_CLUSTER_NAME
          value: {{ template "cluster.name" . }}
        - name: TIDB_ENABLE_BINLOG
          value: {{ .Values.binlog.pump.create | default false | quote }}
        - name:  PROM_CONFIG_PATH
          value: /prometheus-rules
        - name: PROM_PERSISTENT_DIR
          value: /data
        - name: TIDB_VERSION
      {{- if .Values.monitor.prometheus.alertManagerRulesVersion }}
          value: tidb:{{ .Values.monitor.prometheus.alertManagerRulesVersion }}
      {{- else }}
          value: {{ .Values.tidb.image }}
      {{- end }}
        - name: GF_K8S_PROMETHEUS_URL
          value: {{ .Values.monitor.initializer.config.K8S_PROMETHEUS_URL }}
        - name: GF_TIDB_PROMETHEUS_URL
          value: http://127.0.0.1:9090
        - name: TIDB_CLUSTER_NAMESPACE
          value: {{ .Release.Namespace }}
      {{- if and (ne .Values.timezone "UTC") (ne .Values.timezone "") }}
        - name: TZ
          value: {{ .Values.timezone | default "UTC" }}
      {{- end }}
        command:
        - /bin/sh
        - -c
        - |
          mkdir -p /data/prometheus {{- if .Values.monitor.grafana.create }} /data/grafana {{- end }}
          chmod 777 /data/prometheus {{- if .Values.monitor.grafana.create }} /data/grafana {{- end }}
          /usr/bin/init.sh
        securityContext:
          runAsUser: 0
        volumeMounts:
        - mountPath: /grafana-dashboard-definitions/tidb
          name: grafana-dashboard
          readOnly: false
        - name: prometheus-rules
          mountPath: /prometheus-rules
          readOnly: false
        - name: monitor-data
          mountPath: /data
        - name: datasource
          mountPath: /etc/grafana/provisioning/datasources
          readOnly: false
        resources:
{{ toYaml .Values.monitor.initializer.resources | indent 10 }}
      containers:
      - name: prometheus
        image: {{ .Values.monitor.prometheus.image }}
        imagePullPolicy: {{ .Values.monitor.prometheus.imagePullPolicy | default "IfNotPresent" }}
        {{- if .Values.monitor.prometheus.resources }}
        resources:
{{ toYaml .Values.monitor.prometheus.resources | indent 12 }}
        {{- end }}
        command:
        - /bin/prometheus
        - --web.enable-admin-api
        - --web.enable-lifecycle
        - --log.level={{ .Values.monitor.prometheus.logLevel }}
        - --config.file=/etc/prometheus/prometheus.yml
        - --storage.tsdb.path=/data/prometheus
        - --storage.tsdb.retention.time={{ .Values.monitor.prometheus.reserveDays }}d
        ports:
        - name: prometheus
          containerPort: 9090
          protocol: TCP
        # `TZ` is unused in Prometheus Docker image, we set it here just to keep consistency
        env:
        - name: TZ
          value: {{ .Values.timezone | default "UTC" }}
        volumeMounts:
          - name: prometheus-config
            mountPath: /etc/prometheus
            readOnly: true
          - name: monitor-data
            mountPath: /data
          - name: prometheus-rules
            mountPath: /prometheus-rules
            readOnly: false
          {{- if and .Values.tlsCluster .Values.tlsCluster.enabled }}
          - name: cluster-client-tls
            mountPath: /var/lib/cluster-client-tls
            readOnly: true
          {{- end }}
      {{- if .Values.monitor.grafana.create }}
      - name: reloader
        image: {{ .Values.monitor.reloader.image }}
        imagePullPolicy: {{ .Values.monitor.reloader.imagePullPolicy | default "IfNotPresent" }}
        command:
        - /bin/reload
        - --root-store-path=/data
        - --sub-store-path={{ .Values.tidb.image }}
        - --watch-path=/prometheus-rules/rules
        - --prometheus-url=http://127.0.0.1:9090
        ports:
        - name: reloader
          containerPort: 9089
          protocol: TCP
        volumeMounts:
          - name: prometheus-rules
            mountPath: /prometheus-rules
            readOnly: false
          - name: monitor-data
            mountPath: /data
        resources:
{{ toYaml .Values.monitor.reloader.resources | indent 10 }}
      {{- if and (ne .Values.timezone "UTC") (ne .Values.timezone "") }}
        env:
        - name: TZ
          value: {{ .Values.timezone | default "UTC" }}
      {{- end }}
      {{- end }}
      {{- if .Values.monitor.grafana.create }}
      - name: grafana
        image: {{ .Values.monitor.grafana.image }}
        imagePullPolicy: {{ .Values.monitor.grafana.imagePullPolicy | default "IfNotPresent" }}
        {{- if .Values.monitor.grafana.resources }}
        resources:
{{ toYaml .Values.monitor.grafana.resources | indent 12 }}
        {{- end }}
        ports:
        - name: grafana
          containerPort: 3000
          protocol: TCP
        env:
        - name: GF_PATHS_DATA
          value: /data/grafana
        - name: GF_SECURITY_ADMIN_USER
          valueFrom:
            secretKeyRef:
              name: {{ template "cluster.name" . }}-monitor
              key: username
        - name: GF_SECURITY_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "cluster.name" . }}-monitor
              key: password
        {{- if .Values.monitor.grafana.config }}
        {{- range $key, $value := .Values.monitor.grafana.config }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}
        {{- end }}
        - name: TZ
          value: {{ .Values.timezone | default "UTC" }}
        volumeMounts:
          - name: monitor-data
            mountPath: /data
          - mountPath: /etc/grafana/provisioning/datasources
            name: datasource
            readOnly: false
          - mountPath: /etc/grafana/provisioning/dashboards
            name: dashboards-provisioning
            readOnly: false
          - mountPath: /grafana-dashboard-definitions/tidb
            name: grafana-dashboard
            readOnly: false
      {{- end }}
      volumes:
      - name: monitor-data
        {{- if .Values.monitor.persistent }}
        persistentVolumeClaim:
          claimName: {{ template "cluster.name" . }}-monitor
        {{- else }}
        emptyDir: {}
        {{- end }}
      - name: prometheus-config
        configMap:
          name: {{ template "cluster.name" . }}-monitor
          items:
          - key: prometheus-config
            path: prometheus.yml
      {{- if .Values.monitor.grafana.create }}
      - emptyDir: {}
        name: datasource
      - configMap:
          name: {{ template "cluster.name" . }}-monitor
          items:
          - key: dashboard-config
            path: dashboards.yaml
        name: dashboards-provisioning
      {{- end }}
      - emptyDir: {}
        name: prometheus-rules
      - emptyDir: {}
        name: grafana-dashboard
      {{- if and .Values.tlsCluster .Values.tlsCluster.enabled }}
      - name: cluster-client-tls
        secret:
          defaultMode: 420
          secretName: {{ .Release.Name }}-cluster-client-secret
      {{- end }}
    {{- if .Values.monitor.tolerations }}
      tolerations:
{{ toYaml .Values.monitor.tolerations | indent 6 }}
    {{- end }}
{{- end }}
