##
## Grafana Deployment for Fogbreak AI dashboards.
##
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: fogbreak-ai
  labels:
    app: fogbreak
    component: grafana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: fogbreak
      component: grafana
  template:
    metadata:
      labels:
        app: fogbreak
        component: grafana
    spec:
      containers:
        - name: grafana
          image: grafana/grafana:10.4.0
          ports:
            - name: http
              containerPort: 3000
          env:
            - name: GF_SECURITY_ADMIN_USER
              value: "admin"
            - name: GF_SECURITY_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: fogbreak-ai-secrets
                  key: grafana-password
                  optional: true
            - name: GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH
              value: "/var/lib/grafana/dashboards/fogbreak-ai.json"
          resources:
            requests:
              cpu: "250m"
              memory: "256Mi"
            limits:
              cpu: "500m"
              memory: "512Mi"
          volumeMounts:
            - name: dashboards
              mountPath: /var/lib/grafana/dashboards
            - name: datasources
              mountPath: /etc/grafana/provisioning/datasources
            - name: dashboard-providers
              mountPath: /etc/grafana/provisioning/dashboards
            - name: storage
              mountPath: /var/lib/grafana
      volumes:
        - name: dashboards
          configMap:
            name: grafana-dashboards
        - name: datasources
          configMap:
            name: grafana-datasources
        - name: dashboard-providers
          configMap:
            name: grafana-dashboard-providers
        - name: storage
          persistentVolumeClaim:
            claimName: grafana-storage
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-storage
  namespace: fogbreak-ai
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: fogbreak-ai
spec:
  type: ClusterIP
  selector:
    app: fogbreak
    component: grafana
  ports:
    - name: http
      port: 3000
      targetPort: 3000
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-datasources
  namespace: fogbreak-ai
data:
  datasources.yaml: |
    apiVersion: 1
    datasources:
      - name: Prometheus
        type: prometheus
        access: proxy
        url: http://prometheus:9090
        isDefault: true
        editable: false
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-dashboard-providers
  namespace: fogbreak-ai
data:
  dashboards.yaml: |
    apiVersion: 1
    providers:
      - name: fogbreak
        orgId: 1
        folder: Fogbreak AI
        type: file
        disableDeletion: false
        editable: true
        options:
          path: /var/lib/grafana/dashboards
          foldersFromFilesStructure: false
