apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: monitoring-system
  labels:
    app.kubernetes.io/name: grafana
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app.kubernetes.io/name: grafana
  template:
    metadata:
      labels:
        app.kubernetes.io/name: grafana
    spec:
      initContainers:
        - name: init-permission
          image: busybox:1.33.1
          imagePullPolicy: IfNotPresent
          command:
            - chown
            - -R
            - 472:472
            - /var/lib/grafana
          securityContext:
            runAsUser: 0
          volumeMounts:
            - name: grafana-config
              mountPath: /var/lib/grafana
      containers:
        - name: dashboard-watcher
          image: quay.io/kiwigrid/k8s-sidecar:1.12.2
          imagePullPolicy: IfNotPresent
          env:
            - name: LABEL
              value: grafana_dashboard
            - name: FOLDER
              value: /tmp/dashboards
            - name: RESOURCE
              value: both
            - name: NAMESPACE
              value: ALL
          volumeMounts:
            - name: watcher-dashboard-volume
              mountPath: /tmp/dashboards
        - name: grafana
          image: grafana/grafana:8.1.1
          imagePullPolicy: IfNotPresent
          resources:
            requests:
              cpu: 100m
              memory: 100Mi
            limits:
              cpu: 200m
              memory: 200Mi
          ports:
            - name: http-port
              protocol: TCP
              containerPort: 3000
          volumeMounts:
            - name: grafana-config
              mountPath: /var/lib/grafana
            - name: grafana-configs
              mountPath: /etc/grafana/grafana.ini
              subPath: grafana.ini
            - name: grafana-configs
              mountPath: /etc/grafana/provisioning/datasources/datasources.yaml
              subPath: datasources.yaml
            - name: grafana-configs
              mountPath: /etc/grafana/provisioning/dashboards/watcher-dashboardproviders.yaml
              subPath: watcher-dashboardproviders.yaml
            - name: watcher-dashboard-volume
              mountPath: /tmp/dashboards
          livenessProbe:
            httpGet:
              path: /api/health
              port: http-port
            initialDelaySeconds: 60
            timeoutSeconds: 30
            periodSeconds: 5
            successThreshold: 1
            failureThreshold: 10
          readinessProbe:
            httpGet:
              path: /api/health
              port: http-port
            initialDelaySeconds: 60
            timeoutSeconds: 30
            periodSeconds: 5
            successThreshold: 1
            failureThreshold: 10
          env:
            - name: GF_SECURITY_ADMIN_USER
              valueFrom:
                secretKeyRef:
                  name: grafana-secrets
                  key: gf_security_admin_user
            - name: GF_SECURITY_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: grafana-secrets
                  key: gf_security_admin_password
            - name: GF_INSTALL_PLUGINS
              valueFrom:
                configMapKeyRef:
                  name: grafana-configs
                  key: plugins
            - name: GF_PATHS_DATA
              value: /var/lib/grafana/data
            - name: GF_PATHS_LOGS
              value: /var/log/grafana
            - name: GF_PATHS_PLUGINS
              value: /var/lib/grafana/plugins
      nodeSelector:
        kubernetes.io/os: linux
      securityContext:
        runAsUser: 472
        runAsGroup: 472
        fsGroup: 472
      serviceAccountName: grafana
      volumes:
        - name: grafana-config
          persistentVolumeClaim:
            claimName: grafana-config
        - name: grafana-configs
          configMap:
            name: grafana-configs
        - name: watcher-dashboard-volume
          emptyDir: {}
