apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      name: grafana
      labels:
        app: grafana
    spec:
      initContainers:
      - name: permission-fix
        image: private-repo:5000/busybox:1.28   # Use an image that allows permission modification
        command: ["/bin/sh", "-c"]
        args:
          - chown -R 472:472 /var/lib/grafana  # Change ownership to the required user/group
        volumeMounts:
        - mountPath: /var/lib/grafana
          name: grafana-storage
      containers:
      - name: grafana
        image: private-repo:5000/grafana:10.2.2
        #lifecycle:
        #  postStart:
        #    exec:
        #      command:
        #      - "/bin/sh"
        #      - "-c"
        #      - |
        #        chown -R grafana:grafana /var/lib/grafana
        #        chmod -R 755 /var/lib/grafana
        ports:
        - name: grafana
          containerPort: 3000
        volumeMounts:
          - mountPath: /var/lib/grafana
            name: grafana-storage
          - mountPath: /etc/grafana/provisioning/datasources
            name: grafana-datasources
            readOnly: false
      volumes:
      - name: grafana-storage
        persistentVolumeClaim:
          claimName: grafana-pvc
      - name: grafana-datasources
        configMap:
          defaultMode: 420
          name: grafana-datasources
      nodeSelector:
        name: node-1   
