apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: grafana
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: grafana
  {{- if .Values.updateStrategy }}
  strategy: {{ toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: grafana
      annotations:
        {{- if ne .Values.cloudProvider "AWS" }}
        backup.velero.io/backup-volumes: data
        {{- end }}
        {{- if (include "grafana.createAdminSecret" .) }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if (include "grafana.createSMTPSecret" .) }}
        checksum/smtp-secret: {{ include (print $.Template.BasePath "/smtp-secret.yaml") . | sha256sum }}
        {{- end }}
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/dashboard-provider: {{ include (print $.Template.BasePath "/dashboard-provider.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
        {{- toYaml .Values.podAnnotations | nindent 8 }}
        {{- end }}
    spec:
      {{- include "grafana.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "grafana.serviceAccountName" . }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "component" "grafana" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "grafana" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        runAsUser: {{ .Values.securityContext.runAsUser }}
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsNonRoot: {{ .Values.securityContext.runAsNonRoot }}
      {{- end }}
      containers:
        - name: grafana
          image: {{ template "grafana.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          envFrom:
            - configMapRef:
                name: {{ include "common.names.fullname" . }}-envvars
          env:
            - name: GF_SECURITY_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "grafana.adminSecretName" . }}
                  key: {{ include "grafana.adminSecretPasswordKey" . }}
            {{- if .Values.smtp.enabled }}
            - name: GF_SMTP_ENABLED
              value: "true"
            {{- if .Values.smtp.host }}
            - name: GF_SMTP_HOST
              value: {{ .Values.smtp.host }}
            {{- end }}
            - name: GF_SMTP_USER
              valueFrom:
                secretKeyRef:
                  name: {{ include "grafana.smtpSecretName" . }}
                  key: {{ include "grafana.smtpSecretUserKey" . }}
            - name: GF_SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "grafana.smtpSecretName" . }}
                  key: {{ include "grafana.smtpSecretPasswordKey" . }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- toYaml .Values.extraEnvVars | nindent 12 }}
            {{- end }}
          volumeMounts:
            {{- if .Values.config.useGrafanaIniFile }}
            - name: grafana-ini
              mountPath: /opt/bitnami/grafana/conf/grafana.ini
              subPath: grafana.ini
            {{- end }}
            {{- if .Values.persistence.enabled }}
            - name: data
              mountPath: /opt/bitnami/grafana/data
            {{- end }}
            {{- if .Values.dashboardsProvider.enabled }}
            - name: dashboards-provider
              mountPath: /opt/bitnami/grafana/conf/provisioning/dashboards
            {{- end }}
            {{- range .Values.dashboardsConfigMaps }}
            - name: {{ .configMapName }}
              mountPath: /opt/bitnami/grafana/dashboards/{{ .fileName }}
              subPath: {{ .fileName }}
            {{- end }}
            {{- if .Values.datasources.secretName }}
            - name: datasources
              mountPath: /opt/bitnami/grafana/conf/provisioning/datasources
            {{- end }}
            {{- if and .Values.ldap.enabled .Values.ldap.configMapName }}
            - name: ldap
              mountPath: /opt/bitnami/grafana/conf/ldap.toml
              subPath: ldap.toml
            {{- end }}
            {{- range .Values.extraConfigmaps }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath | default "" }}
              readOnly: {{ .readOnly }}
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- toYaml .Values.extraVolumeMounts | nindent 12 }}
            {{- end }}
          ports:
            - name: dashboard
              containerPort: 3000
              protocol: TCP
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /api/health
              port: dashboard
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /api/health
              port: dashboard
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: secret-grafana
          secret:
            secretName: grafana
            defaultMode: 420
        {{- if .Values.persistence.enabled }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "common.names.fullname" .) }}
        {{- end }}
        {{- if and .Values.ldap.enabled .Values.ldap.configMapName }}
        - name: ldap
          configMap:
            name: {{ .Values.ldap.configMapName }}
        {{- end }}
        {{- if .Values.dashboardsProvider.enabled }}
        - name: dashboards-provider
          configMap:
            {{- if .Values.dashboardsProvider.configMapName }}
            name: {{ include "common.tplvalues.render" ( dict "value" .Values.dashboardsProvider.configMapName "context" $) }}
            {{- else }}
            name: {{ include "common.names.fullname" . }}-provider
            {{- end }}
        {{- end }}
        {{- range .Values.dashboardsConfigMaps }}
        - name: {{ .configMapName }}
          configMap:
            name: {{ .configMapName }}
        {{- end }}
        {{- if .Values.datasources.secretName }}
        - name: datasources
          secret:
            secretName: {{ .Values.datasources.secretName }}
        {{- end }}
        {{- if .Values.config.useGrafanaIniFile }}
        - name: grafana-ini
          {{- if .Values.config.grafanaIniConfigMap }}
          configMap:
            name: {{ .Values.config.grafanaIniConfigMap }}
          {{- else if .Values.config.grafanaIniSecret }}
          secret:
            secretName: {{ .Values.config.grafanaIniSecret }}
          {{- end }}
        {{- end }}
        {{- range .Values.extraConfigmaps }}
        - name: {{ .name }}
          configMap:
            name: {{ .name }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- toYaml .Values.extraVolumes | nindent 8 }}
        {{- end }}
