apiVersion: v1
kind: ConfigMap
metadata:
  name: community-grafana-v1
  namespace: community-monitor
data:
  datasource.yaml: |
    apiVersion: 1

    datasources:
      - name: Prometheus
        type: prometheus
        access: proxy
        orgId: 1
        url: http://community-prometheus/
        isDefault: true
        version: 1
        editable: false
      - name: Loki
        type: loki
        access: proxy
        url: http://loki.loki.svc.cluster.local:3100
        version: 1

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: community-monitor
  labels:
    app: community-grafana
spec:
  selector:
    matchLabels:
      app: community-grafana
  replicas: 1
  template:
    metadata:
      labels:
        app: community-grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana:9.4.7
        volumeMounts:
        - name: data-volume
          mountPath: /var/lib/grafana
        - name: grafana-conf
          mountPath: /etc/grafana/provisioning/datasources/datasource.yaml
          subPath: datasource.yaml
        livenessProbe:
          httpGet:
            path: /api/health
            port: 3000
          periodSeconds: 10
          initialDelaySeconds: 10
          timeoutSeconds: 4
        ports:
        - containerPort: 3000
          name: http
        env:
          - name: GF_SECURITY_ADMIN_PASSWORD
            value: OSJGrnSgRh2Ow40jF5gpyaZtacpLIS1hSVFtQUnb
          - name: GF_INSTALL_PLUGINS
            value: grafana-clock-panel,grafana-simple-json-datasource
        securityContext:
          runAsNonRoot: true
          runAsUser: 1000
        resources:
          limits:
            cpu: "2"
            memory: 512Mi
          requests:
            cpu: "0.25"
            memory: 128Mi
      volumes:
      - name: data-volume
        persistentVolumeClaim:
          claimName: community-grafana-ufs-01
      - name: grafana-conf
        configMap:
          name: community-grafana-v1
          defaultMode: 420

---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: community-grafana
  name: community-grafana
  namespace: community-monitor
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: http
  selector:
    app: community-grafana

---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: community-grafana-ufs-01
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /community-monitor/grafana/
    server: 10.19.173.98
  mountOptions:
    - nolock
    - nfsvers=4.0

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: community-grafana-ufs-01
  namespace: community-monitor
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
