apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "grafana.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "grafana.labels" . | nindent 4 }}
    app.kubernetes.io/component: grafana
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels: {{- include "grafana.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: grafana
  {{- if .Values.updateStrategy }}
  strategy: {{ toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "grafana.labels" . | nindent 8 }}
        app.kubernetes.io/component: grafana
      annotations:
        {{- 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 }}
      serviceAccountName: {{ template "grafana.serviceAccountName" . }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "grafana.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "grafana.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "grafana.tplValue" (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 "grafana.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_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: /
              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: /
              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 "grafana.tplValue" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.persistence.enabled }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "grafana.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 "grafana.tplValue" ( dict "value" .Values.dashboardsProvider.configMapName "context" $) }}
            {{- else }}
            name: {{ include "grafana.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 }}
