apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana-server
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      # Used to match pre-existing pods that may be affected during updates.
      run: grafana-server
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  # Pod template.
  template:
    metadata:
      labels:
        # Note: run=grafana-server should match a service config with a
        # public IP and port so that it is publically accessible.
        run: grafana-server
    spec:
      containers:
      # Check https://hub.docker.com/r/grafana/grafana/tags/ for the current
      # stable version.
      - image: grafana/grafana:8.1.1
        name: grafana-server
        # NOTE: the official Grafana Docker images may set various environment
        # variables which will override configurations in grafana.ini (which is
        # also in the this repository). Be aware that configurations you make in
        # that file may get overridden, and may necessitate overridding the
        # variables in the Docker image with environment variables here.
        # https://github.com/grafana/grafana-docker/blob/master/Dockerfile
        env:
        - name: GF_PATHS_PROVISIONING
          value: "/etc/provisioning"
          # doitintl-bigquery-datasource adds the BigQuery datasource.
        - name: GF_INSTALL_PLUGINS
          value: "doitintl-bigquery-datasource"
        - name: GF_SECURITY_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: grafana-secrets
              key: admin-password
        - name: GF_SERVER_ROOT_URL
          # The full public facing URL used to access grafana from a browser.
          # The root_url value is used in alert URLs generated by Grafana, as
          # well as for validation of OAuth requests.
          valueFrom:
            configMapKeyRef:
              name: grafana-env
              key: root_url
        - name: GF_AUTH_GOOGLE_CLIENT_SECRET
          valueFrom:
            configMapKeyRef:
              name: grafana-env
              key: gf_auth_google_client_secret
        - name: GF_AUTH_GOOGLE_CLIENT_ID
          valueFrom:
            configMapKeyRef:
              name: grafana-env
              key: gf_auth_google_client_id

        ports:
          - containerPort: 3000
        # Many ingresses expect an HTTP code 200 on port 80 at path '/', for
        # their backends, and otherwise will not serve them. Grafana does not
        # return 200 at '/', and so it requires a custom readinessProbe to be
        # served.
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /login
            port: 3000
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 30
        resources:
          requests:
            memory: "1Gi"
            cpu: "200m"
        # TODO: add support for SSL certificates, so logins are secure.
        volumeMounts:
        # Default configs place an sqlite3 database in /var/lib/grafana
        - mountPath: /var/lib/grafana
          name: grafana-storage
        # /etc/grafana/* should contain the M-Lab Grafana configs.
        - mountPath: /etc/grafana
          name: grafana-config
        # /etc/dashboards/* should contain the Grafana dashboard JSON configs.
        - mountPath: /etc/dashboards
          name: grafana-dashboards
        - mountPath: /etc/provisioning/dashboards
          name: grafana-dashboard-provisioning
        - mountPath: /etc/provisioning/datasources
          name: grafana-datasource-provisioning
      # Disks created manually, can be named here explicitly using
      # gcePersistentDisk instead of the persistentVolumeClaim.
      volumes:
      - name: grafana-storage
        persistentVolumeClaim:
          claimName: auto-grafana-disk0
      - name: grafana-config
        configMap:
          name: grafana-config
      - name: grafana-dashboards
        configMap:
          name: grafana-dashboards
      - name: grafana-dashboard-provisioning
        configMap:
          name: grafana-dashboard-provisioning
      - name: grafana-datasource-provisioning
        configMap:
          name: grafana-datasource-provisioning
