apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: grafana
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ template "grafana.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: grafana
      annotations:
        sidecar.istio.io/inject: "false"
        scheduler.alpha.kubernetes.io/critical-pod: ""
    spec:
{{- if .Values.global.priorityClassName }}
      priorityClassName: "{{ .Values.global.priorityClassName }}"
{{- end }}
      containers:
        - name: {{ .Chart.Name }}
{{- if contains "/" .Values.image }}
          image: "{{ .Values.image }}"
{{- else }}
          image: "{{ .Values.global.hub }}/{{ .Values.image }}:{{ .Values.global.tag }}"
{{- end }}
          imagePullPolicy: {{ .Values.global.imagePullPolicy }}
          ports:
            - containerPort: {{ .Values.service.internalPort }}
          readinessProbe:
            httpGet:
              path: /login
              port: {{ .Values.service.internalPort }}
          env:
          - name: GRAFANA_PORT
            value: {{ .Values.service.internalPort | quote }}
{{- if .Values.security.enabled }}
          - name: GF_SECURITY_ADMIN_USER
            valueFrom:
              secretKeyRef:
                name: grafana
                key: username
          - name: GF_SECURITY_ADMIN_PASSWORD
            valueFrom:
              secretKeyRef:
                name: grafana
                key: password
          - name: GF_AUTH_BASIC_ENABLED
            value: "true"
          - name: GF_AUTH_ANONYMOUS_ENABLED
            value: "false"
          - name: GF_AUTH_DISABLE_LOGIN_FORM
            value: "false"
{{- else }}
          - name: GF_AUTH_BASIC_ENABLED
            value: "false"
          - name: GF_AUTH_ANONYMOUS_ENABLED
            value: "true"
          - name: GF_AUTH_ANONYMOUS_ORG_ROLE
            value: Admin
{{- end }}
          - name: GF_PATHS_DATA
            value: /data/grafana
          resources:
{{- if .Values.resources }}
{{ toYaml .Values.resources | indent 12 }}
{{- else }}
{{ toYaml .Values.global.defaultResources | indent 12 }}
{{- end }}
          volumeMounts:
          - name: data
            mountPath: /data/grafana
      affinity:
      {{- include "nodeaffinity" . | indent 6 }}
      volumes:
      - name: data
        emptyDir: {}
