{{- if .Values.enabled | default false }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "grafana.fullname" . }}
  labels:
    {{- include "grafana.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  strategy:
    type: Recreate
  {{- end }}
  selector:
    matchLabels:
      {{- include "grafana.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
      labels:
        {{- include "grafana.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "grafana.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - env:
            - name: GF_AUTH_ANONYMOUS_ENABLED
              value: {{ quote .Values.env.gfAuthAnonymousEnable }}
            - name: GF_SECURITY_ALLOW_EMBEDDING
              value: {{ quote .Values.env.gfSecurityAllowEmbedding }}
            - name: GF_PATHS_PROVISIONING
              value: /etc/grafana/provisioning
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          name: {{ .Chart.Name }}
          envFrom:
            - secretRef:
                name: grafana-secrets
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
#          livenessProbe:
#            tcpSocket:
#              port: {{ .Values.service.port }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: grafana-datasources
              mountPath: /etc/grafana/provisioning/datasources/datasources.yaml
              subPath: datasources.yaml
            - name: grafana-default
              mountPath: /etc/grafana/provisioning/dashboards/default.yaml
              subPath: default.yaml
            - name: kubernetes-dashboard
              mountPath: /var/lib/grafana/dashboards/kubernetes-dashboard.json
              subPath: kubernetes-dashboard.json
            - name: grafana-loki
              mountPath: /var/lib/grafana/dashboards/Loki-Logs.json
              subPath: loki-logs.json
            {{- if .Values.persistence.enable | default false }}
            - name: grafana-claim0
              mountPath: /var/lib/grafana
            {{- end }}
      volumes:
      - name: grafana-datasources
        configMap:
          name: datasources
          items:
          - key: "datasources.yaml"
            path: "datasources.yaml"
      - name: grafana-default
        configMap:
          name: default
          items:
          - key: "default.yaml"
            path: "default.yaml"
      - name: kubernetes-dashboard
        configMap:
          name: kubernetes-dashboard
          items:
          - key: "kubernetes-dashboard.json"
            path: "kubernetes-dashboard.json"
      - name: grafana-loki
        configMap:
          name: loki-logs
          items:
          - key: "loki-logs.json"
            path: "loki-logs.json"
      {{- if .Values.persistence.enable | default false }}
      - name: grafana-claim0
        persistentVolumeClaim:
          claimName: grafana-claim0
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}