apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana-core
  labels:
    app: grafana
    component: core
spec:
  replicas: 1
  selector:
    matchLabels:
        app: grafana
        component: core
  template:
    metadata:
      labels:
        app: grafana
        component: core
    spec:
      serviceAccountName: prometheus
      securityContext:
        runAsUser: 472
      containers:
      - image: grafana/grafana:9.4.3
        name: grafana-core
        imagePullPolicy: IfNotPresent
        env:
          - name: GF_PATHS_PROVISIONING
            value: "/opt/grafana-provision"
          - name: GF_SERVER_ROOT_URL
            value: "http://{{ .DOMAIN_NAME }}/grafana"
          - name: GF_SERVER_SERVE_FROM_SUB_PATH
            value: "true"
          - name: GF_AUTH_ANONYMOUS_ENABLED
            value: "true"
          - name: GF_AUTH_ANONYMOUS_ORG_NAME
            value: "Main Org."
          - name: GF_SECURITY_ADMIN_PASSWORD
            value: "{{ .GRAFANA_ADMIN_PASSWORD }}"
          - name: GF_USERS_VIEWERS_CAN_EDIT
            value: "true"
        readinessProbe:
          httpGet:
            path: /login
            port: 3000
          initialDelaySeconds: 30
          timeoutSeconds: 1
        volumeMounts:
        - name: grafana-persistent-storage
          mountPath: /var/lib/grafana
        - name: grafana-datasource-provision
          mountPath: /opt/grafana-provision/datasources
        - name: grafana-dashboard-provision
          mountPath: /opt/grafana-provision/dashboards
        - name: grafana-dashboards
          mountPath: /var/lib/grafana/dashboards
        ports:
        - name: grafana-web
          containerPort: 3000
      volumes:
      - name: grafana-persistent-storage
        emptyDir: {}
      - name: grafana-datasource-provision
        configMap:
          name: grafana-datasource-provision
      - name: grafana-dashboard-provision
        configMap:
          name: grafana-dashboard-provision
      - name: grafana-dashboards
        configMap:
          name: grafana-dashboards
      nodeSelector:
        node-name: main-node
---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  labels:
    app: grafana
    component: core
spec:
  type: NodePort
  ports:
    - name: grafana-web
      port: 80
      targetPort: grafana-web
  selector:
    app: grafana
    component: core

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-grafana
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: grafana
            port:
              name: grafana-web
        path: /grafana
        pathType: Prefix
