---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: monitoring
  name: grafana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: grafana
    spec:
      securityContext:
        fsGroup: 472
        runAsUser: 472
      initContainers:
      - name: init-chown-data
        image: "busybox:latest"
        imagePullPolicy: IfNotPresent
        securityContext:
          runAsUser: 0
        command: ["chown", "-R", "472:472", "/var/lib/grafana"]
        resources:
          limits:
            cpu: 100m
            memory: 128Mi
          requests:
            cpu: 100m
            memory: 128Mi
        volumeMounts:
        - name: storage
          mountPath: "/var/lib/grafana"
      containers:
      - name: grafana
        image: "grafana/grafana:8.3.3"
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: config
          mountPath: "/etc/grafana/grafana.ini"
          subPath: grafana.ini
        - name: storage
          mountPath: "/var/lib/grafana"
        - name: dashboards
          mountPath: "/etc/grafana/provisioning/dashboards/dashboardproviders.yaml"
          subPath: dashboardproviders.yaml
        - name: datasources
          mountPath: "/etc/grafana/provisioning/datasources/datasources.yaml"
          subPath: datasources.yaml
        - name: prometheus-dashboard
          mountPath: "/var/lib/grafana/dashboards/kubernetes/prometheus.json"
          subPath: prometheus.json
        - name: prometheus-operator-dashboard
          mountPath: "/var/lib/grafana/dashboards/kubernetes/prometheus-operator.json"
          subPath: prometheus-operator.json
        - name: grafana-dashboard
          mountPath: "/var/lib/grafana/dashboards/kubernetes/grafana.json"
          subPath: grafana.json
        - name: cert-manager-dashboard
          mountPath: "/var/lib/grafana/dashboards/kubernetes/cert-manager.json"
          subPath: cert-manager.json
        ports:
        - name: grafana
          containerPort: 3000
          protocol: TCP
        env:
        - name: GF_SECURITY_ADMIN_USER
          valueFrom:
            secretKeyRef:
              name: grafana
              key: admin-user
        - name: GF_SECURITY_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: grafana
              key: admin-password
        livenessProbe:
          failureThreshold: 10
          httpGet:
            path: /api/health
            port: 3000
          initialDelaySeconds: 60
          timeoutSeconds: 30
        readinessProbe:
          httpGet:
            path: /api/health
            port: 3000
        resources:
          limits:
            cpu: "2"
            memory: 2Gi
          requests:
            cpu: "0.5"
            memory: 512Mi
      volumes:
      - name: config
        configMap:
          name: grafana
      - name: dashboards
        configMap:
          name: dashboards
      - name: storage
        persistentVolumeClaim:
          claimName: grafana
      - name: datasources
        configMap:
          name: datasources
      - name: prometheus-dashboard
        configMap:
          name: prometheus-dashboard
      - name: prometheus-operator-dashboard
        configMap:
          name: prometheus-operator-dashboard
      - name: grafana-dashboard
        configMap:
          name: grafana-dashboard
      - name: cert-manager-dashboard
        configMap:
          name: cert-manager-dashboard
