# Copyright 2023 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: flux-cluster-stats
              mountPath: "/var/lib/grafana/dashboards/kubernetes/flux-cluster-stats.json"
              subPath: flux-cluster-stats.json
            - name: flux-control-plane
              mountPath: "/var/lib/grafana/dashboards/kubernetes/flux-control-plane.json"
              subPath: flux-control-plane.json
            - name: flux-logs
              mountPath: "/var/lib/grafana/dashboards/kubernetes/flux-logs.json"
              subPath: flux-logs.json
            - 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
            - 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
           # NOTE: temporarily disabled these dashboards until grafana has auth access
           # - 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
        - name: kube-state-metrics
          configMap:
            name: kube-state-metrics
        - name: node-exporter-full
          configMap:
            name: node-exporter-full
        - name: flux-cluster-stats
          configMap:
            name: flux-cluster-stats
        - name: flux-control-plane
          configMap:
            name: flux-control-plane
        - name: flux-logs
          configMap:
            name: flux-logs
        - name: prow
          configMap:
            name: prow-dashboards
        - name: test-infra
          configMap:
            name: test-infra-dashboard
      nodeSelector:
        node-group: stable
      tolerations:
      - effect: NoSchedule
        key: node-group
        operator: Equal
        value: stable
