apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grafana
  name: grafana
spec:
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      securityContext:
        fsGroup: 472
        supplementalGroups:
          - 0
      containers:
        - name: grafana
          image: grafana/grafana-oss:8.3.3-ubuntu
          imagePullPolicy: IfNotPresent
          env:
            - name: GF_AUTH_GENERIC_OAUTH_NAME
              value: KeyCloak
            - name: GF_AUTH_GENERIC_OAUTH_ENABLED
              value: "true"
            - name: GF_AUTH_GENERIC_OAUTH_ALLOW_SIGN_UP
              value: "true"
            - name: GF_AUTH_GENERIC_OAUTH_CLIENT_ID
              value: grafana
            - name: GF_AUTH_GENERIC_OAUTH_CLIENT_SECRET
              value: $(CLIENT_SECRET)
            - name: GF_AUTH_GENERIC_OAUTH_TLS_SKIP_VERIFY_INSECURE
              value: "true"
            - name: GF_AUTH_GENERIC_OAUTH_EMAIL_ATTRIBUTE_NAME
              value: email
            # - name: GF_AUTH_GENERIC_OAUTH_ALLOWED_DOMAINS
            #   value: "*"
            - name: GF_AUTH_GENERIC_OAUTH_AUTH_URL
              value: https://$(KEYCLOAK_URL)/auth/realms/master/protocol/openid-connect/auth
            - name: GF_AUTH_GENERIC_OAUTH_TOKEN_URL
              value: https://$(KEYCLOAK_URL)/auth/realms/master/protocol/openid-connect/token
            - name: GF_AUTH_GENERIC_OAUTH_API_URL
              value: https://$(KEYCLOAK_URL)/auth/realms/master/protocol/openid-connect/userinfo
            - name: GF_AUTH_GENERIC_OAUTH_SCOPES
              value: "roles profile email"
            - name: GF_SERVER_ROOT_URL
              value: https://$(GRAFANA_HOST)  
            - name: GF_AUTH_GENERIC_OAUTH_ROLE_ATTRIBUTE_PATH
              value: contains(realm_access.roles[*], 'Admin') && 'Admin' || contains(realm_access.roles[*], 'editor') && 'editor' || 'Admin'
          ports:
            - containerPort: 3000
              name: http-grafana
              protocol: TCP
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /robots.txt
              port: 3000
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 30
            successThreshold: 1
            timeoutSeconds: 2
          livenessProbe:
            failureThreshold: 3
            initialDelaySeconds: 30
            periodSeconds: 10
            successThreshold: 1
            tcpSocket:
              port: 3000
            timeoutSeconds: 1
          resources:
            requests:
              cpu: 250m
              memory: 750Mi
          volumeMounts:
            - mountPath: /var/lib/grafana
              name: grafana-pv
      volumes:
        - name: grafana-pv
          persistentVolumeClaim:
            claimName: grafana-pvc