apiVersion: apps/v1beta1
kind: Deployment
metadata:
  labels:
    app: {{ template "grafana.fullname" . }}
    chart: "{{.Chart.Name}}-{{.Chart.Version}}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
  name: {{ template "grafana.server.fullname" . }}
spec:
  replicas: 1
  template:
    metadata:
      annotations:
      {{- range $key, $value := .Values.annotations }}
        {{ $key }}: {{ $value }}
      {{- end }}
      labels:
        app: {{ template "grafana.fullname" . }}
        release: "{{ .Release.Name }}"
    spec:
      containers:
      - name: grafana
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        env:
        - name: GF_AUTH_BASIC_ENABLED
          value: "true"
        - name: GF_AUTH_ANONYMOUS_ENABLED
          value: "{{ .Values.auth.anonymous.enabled }}"
        - name: GF_SECURITY_ADMIN_USER
          valueFrom:
            secretKeyRef:
              name: {{ template "grafana.server.fullname" . }}
              key: user
        - name: GF_SECURITY_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "grafana.server.fullname" . }}
              key: password
{{- if .Values.extraVars }}
{{ toYaml .Values.extraVars | indent 8 }}
{{- end }}
        volumeMounts:
        - name: grafana-storage
          mountPath: /var/lib/grafana
      {{- if .Values.mountGrafanaConfig }}
        - name: grafana-config
          mountPath: /etc/grafana
      {{- end }}
        ports:
        - name: web
          containerPort: 3000
        readinessProbe:
          httpGet:
            path: /api/health
            port: 3000
          periodSeconds: 1
          timeoutSeconds: 1
          successThreshold: 1
          failureThreshold: 10
        resources:
          requests:
            memory: 100Mi
            cpu: 100m
          limits:
            memory: 200Mi
            cpu: 200m
      - name: grafana-watcher
        image: {{ .Values.grafanaWatcher.repository }}:{{ .Values.grafanaWatcher.tag }}
        args:
          - '--watch-dir=/var/grafana-dashboards'
          - '--watch-dir=/var/grafana-dashboards/k8s'
          - '--watch-dir=/var/grafana-dashboards/k8s-resources'
          - '--grafana-url=http://127.0.0.1:3000'
          {{- range .Values.serverDashboardConfigmaps }}
          - '--watch-dir=/var/additional-dashboards/{{ . }}'
          {{- end }}
        env:
        - name: GRAFANA_USER
          valueFrom:
            secretKeyRef:
              name: {{ template "grafana.server.fullname" . }}
              key: user
        - name: GRAFANA_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "grafana.server.fullname" . }}
              key: password
        resources:
          requests:
            memory: "16Mi"
            cpu: "50m"
          limits:
            memory: "32Mi"
            cpu: "100m"
        volumeMounts:
          - name: grafana-dashboards-k8s
            mountPath: /var/grafana-dashboards/k8s
          - name: grafana-dashboards-resources
            mountPath: /var/grafana-dashboards/k8s-resources
    {{- range .Values.serverDashboardConfigmaps }}
          - name: {{ . }}
            mountPath: /var/additional-dashboards/{{ . }}
    {{- end }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
    {{ toYaml .Values.nodeSelector | indent 4 }}
    {{- end }}
    {{- if .Values.global.rbacEnable }}
      serviceAccountName: {{ template "grafana.server.fullname" . }}
    {{- end }}
    {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
    {{- end }}
      volumes:
        - name: grafana-storage
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ template "grafana.server.fullname" . }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        - name: grafana-dashboards-k8s
          configMap:
            name: {{ template "grafana.server.fullname" . }}
        - name: grafana-dashboards-resources
          configMap:
            name: {{ template "grafana.server.fullname" . }}-resources
      {{- if .Values.mountGrafanaConfig }}
        - name: grafana-config
          hostPath:
            path: /etc/grafana
            type: Directory
      {{- end }}
      {{- range .Values.serverDashboardConfigmaps }}
        - name: {{ . }}
          configMap:
            name: {{ . }}
      {{- end }}
