apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: grafana
  namespace: argocd
  finalizers:
    - resources-finalizer.argocd.argoproj.io
spec:
  project: apps
  sources:
    - chart: grafana
      repoURL: https://grafana.github.io/helm-charts
      targetRevision: 7.0.6
      helm:
        values: |
          env:
            GF_AUTH_ANONYMOUS_ENABLED: true
            GF_SERVER_DOMAIN: graphs.tbrnt.ch
            GF_SERVER_ROOT_URL: https://graphs.tbrnt.ch
            GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS: victoriametrics-datasource

          ingress:
            enabled: true
            hosts:
              - graphs.tbrnt.ch
            tls:
              - hosts:
                - graphs.tbrnt.ch
                secretName: graphs-tbrnt-ch-cert
            annotations:
              cert-manager.io/cluster-issuer: letsencrypt-prod
              traefik.ingress.kubernetes.io/router.middlewares: kube-system-redirect-https@kubernetescrd

          persistence:
            enabled: true
            size: 1Gi
            storageClassName: local-path

          rbac:
            namespaced: true

          admin:
            existingSecret: admin-creds

          sidecar:
            datasources:
              initDatasources: true
              enabled: true

          extraInitContainers:
            - name: "load-vm-ds-plugin"
              image: "docker.io/curlimages/curl:latest"
              command: [ "/bin/sh" ]
              workingDir: "/var/lib/grafana"
              securityContext:
                runAsUser: 472
                runAsNonRoot: true
                runAsGroup: 472
              args:
              - "-c"
              - |
                set -ex
                mkdir -p /var/lib/grafana/plugins/
                ver=$(curl -s https://api.github.com/repos/VictoriaMetrics/grafana-datasource/releases/latest | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' | head -1)
                curl -L https://github.com/VictoriaMetrics/grafana-datasource/releases/download/$ver/victoriametrics-datasource-$ver.tar.gz -o /var/lib/grafana/plugins/plugin.tar.gz
                tar -xf /var/lib/grafana/plugins/plugin.tar.gz -C /var/lib/grafana/plugins/
                rm /var/lib/grafana/plugins/plugin.tar.gz
              volumeMounts:
              - name: storage
                mountPath: /var/lib/grafana
    - path: apps/grafana
      repoURL: https://git.tbrnt.ch/tobru/gitops-zurrli.git
      targetRevision: HEAD
  destination:
    namespace: grafana
    server: https://kubernetes.default.svc
  syncPolicy:
    syncOptions:
      - CreateNamespace=true
    automated:
      prune: true