# Copyright 2024 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: monitoring
  name: grafana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: grafana
    spec:
      serviceAccountName: grafana
      securityContext:
        fsGroup: 472
        runAsUser: 472
      containers:
        - name: grafana
          image: grafana/grafana:9.5.15
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: config
              mountPath: "/etc/grafana/grafana.ini"
              subPath: grafana.ini
            - name: datasources
              mountPath: "/etc/grafana/provisioning/datasources/datasources.yaml"
              subPath: datasources.yaml
            - name: dashboards
              mountPath: "/etc/grafana/provisioning/dashboards/dashboardproviders.yaml"
              subPath: dashboardproviders.yaml
            - name: prow
              mountPath: "/var/lib/grafana/dashboards/kubernetes/prow-builds.json"
              subPath: builds.json
            - name: prow
              mountPath: "/var/lib/grafana/dashboards/kubernetes/prow-jobs.json"
              subPath: jobs.json
            - name: prow
              mountPath: "/var/lib/grafana/dashboards/kubernetes/prow-organisations.json"
              subPath: organisations.json
            - name: prow
              mountPath: "/var/lib/grafana/dashboards/kubernetes/prow-repositories.json"
              subPath: repositories.json
            # NOTE: temporarily disabled these dashboards until grafana has auth access
            # - name: test-infra
            #   mountPath: "/var/lib/grafana/dashboards/kubernetes/boskos-http.json"
            #   subPath: boskos-http.json
            # - name: test-infra
            #   mountPath: "/var/lib/grafana/dashboards/kubernetes/boskos.json"
            #   subPath: boskos.json
            # - name: test-infra
            #   mountPath: "/var/lib/grafana/dashboards/kubernetes/prow.json"
            #   subPath: prow.json
            # - name: kube-state-metrics
            #   mountPath: "/var/lib/grafana/dashboards/kubernetes/kube-state-metrics.json"
            #   subPath: kube-state-metrics.json
            # - name: node-exporter-full
            #   mountPath: "/var/lib/grafana/dashboards/kubernetes/node-exporter-full.json"
            #   subPath: node-exporter-full.json
          ports:
            - name: grafana
              containerPort: 3000
              protocol: TCP
          livenessProbe:
            failureThreshold: 10
            httpGet:
              path: /api/health
              port: 3000
            initialDelaySeconds: 60
            timeoutSeconds: 30
          readinessProbe:
            httpGet:
              path: /api/health
              port: 3000
          resources:
            requests:
              cpu: 500m
              memory: 1Gi
            limits:
              cpu: 2000m
              memory: 4Gi
      volumes:
        - name: config
          configMap:
            name: grafana
        - name: datasources
          configMap:
            name: datasources
        - name: dashboards
          configMap:
            name: dashboards
        # NOTE: temporarily disabled these dashboards until grafana has auth access
        # - name: kube-state-metrics
        #   configMap:
        #     name: kube-state-metrics
        # - name: node-exporter-full
        #   configMap:
        #     name: node-exporter-full
        - name: prow
          configMap:
            name: prow-dashboards
        # NOTE: temporarily disabled these dashboards until grafana has auth access
        # - name: test-infra
        #   configMap:
        #     name: test-infra-dashboard
