{{- if eq .Values.global.grafana_location "on-cluster" }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drycc-monitor-grafana
  labels:
    heritage: drycc
  annotations:
    component.drycc.cc/version: {{ .Values.grafana.image_tag }}
spec:
  replicas: 1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  selector:
    matchLabels:
      app: drycc-monitor-grafana
  template:
    metadata:
      labels:
        app: drycc-monitor-grafana
    spec:
      containers:
      - name: drycc-monitor-grafana
        image: {{.Values.grafana.image_registry}}/{{.Values.grafana.image_org}}/grafana:{{.Values.grafana.image_tag}}
        imagePullPolicy: {{.Values.grafana.image_pull_policy}}
{{- if or (.Values.grafana.limits_cpu) (.Values.grafana.limits_memory)}}
        resources:
          limits:
{{- if (.Values.grafana.limits_cpu) }}
            cpu: {{.Values.grafana.limits_cpu}}
{{- end}}
{{- if (.Values.grafana.limits_memory) }}
            memory: {{.Values.grafana.limits_memory}}
{{- end}}
{{- end}}
        env:
        {{- if eq .Values.global.influxdb_location "off-cluster" }}
        - name: "INFLUXDB_URL"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: url
        {{- else }}
        - name: "INFLUXDB_URL"
          value: http://$(DRYCC_INFLUXDB_SERVICE_HOST):$(DRYCC_INFLUXDB_SERVICE_PORT_TRANSPORT)
        {{- end }}
        - name: "INFLUXDB_BUCKET"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: bucket
        - name: "INFLUXDB_ORG"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: org
        - name: "INFLUXDB_TOKEN"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: token
        - name: "BIND_PORT"
          value: "3000"
        - name: "DEFAULT_USER"
          value: {{.Values.grafana.user}}
        - name: "DEFAULT_USER_PASSWORD"
          value: {{.Values.grafana.password}}
        - name: "KUBERNETES_CLUSTER_DOMAIN"
          value: {{.Values.global.cluster_domain}}
        {{- range $key, $value := .Values.grafana.environment }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}
        ports:
        - containerPort: 3000
          name: ui
{{- if .Values.grafana.persistence.enabled }}
        volumeMounts:
        - name: grafana-data
          mountPath: /var/lib/grafana
      volumes:
      - name: grafana-data
        persistentVolumeClaim:
          claimName: drycc-monitor-grafana
{{- end }}
{{- end }}
