apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: grafana
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  serviceName: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      initContainers:
        - name: init-chown-data
          image: "busybox:1.31.1"
          securityContext:
            runAsUser: 0
          command: ["chown", "-R", "472:472", "/var/lib/grafana"]
          volumeMounts:
            - name: data
              mountPath: "/var/lib/grafana"
      containers:
        - name: grafana
          image: "grafana/grafana:7.0.3"
          volumeMounts:
            - name: config
              mountPath: "/etc/grafana/grafana.ini"
              subPath: grafana.ini
            - name: data
              mountPath: "/var/lib/grafana"
            - name: config
              mountPath: "/etc/grafana/provisioning/datasources/datasources.yaml"
              subPath: datasources.yaml
            - name: config
              mountPath: "/etc/grafana/provisioning/dashboards/dashboards.yaml"
              subPath: dashboards.yaml
            - name: config-dashboard-container
              mountPath: "/var/lib/grafana/dashboards/default/container.json"
              subPath: container.json
            - name: config-dashboard-node
              mountPath: "/var/lib/grafana/dashboards/default/node.json"
              subPath: node.json
          ports:
            - name: grafana
              containerPort: 3000
              protocol: TCP
          env:
            - name: GF_SECURITY_ADMIN_USER
              value: admin
            - name: GF_SECURITY_ADMIN_PASSWORD
              value: "12345"
          livenessProbe:
            httpGet:
              path: /api/health
              port: 3000
            initialDelaySeconds: 60
            timeoutSeconds: 30
            failureThreshold: 10
          readinessProbe:
            httpGet:
              path: /api/health
              port: 3000
      volumes:
        - name: config
          configMap:
            name: grafana
        - name: config-dashboard-container
          configMap:
            name: grafana-dashboard-container
        - name: config-dashboard-node
          configMap:
            name: grafana-dashboard-node
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        volumeMode: Filesystem
        resources:
          requests:
            storage: 1Gi 
        storageClassName: standard
        selector:
          matchLabels:
            app: grafana
