{{- 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.monitor.grafana.docker_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.monitor.grafana.docker_registry}}{{.Values.monitor.grafana.org}}/grafana:{{.Values.monitor.grafana.docker_tag}}
        imagePullPolicy: {{.Values.monitor.grafana.pull_policy}}
{{- if or (.Values.monitor.grafana.limits_cpu) (.Values.monitor.grafana.limits_memory)}}
        resources:
          limits:
{{- if (.Values.monitor.grafana.limits_cpu) }}
            cpu: {{.Values.monitor.grafana.limits_cpu}}
{{- end}}
{{- if (.Values.monitor.grafana.limits_memory) }}
            memory: {{.Values.monitor.grafana.limits_memory}}
{{- end}}
{{- end}}
        env:
        {{- if eq .Values.global.influxdb_location "off-cluster" }}
        - name: "INFLUXDB_SERVICE_URL"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: url
        - name: "INFLUXDB_DATABASE"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: database
        - name: "INFLUXDB_USER"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: user
        - name: "INFLUXDB_PASSWORD"
          valueFrom:
            secretKeyRef:
              name: influxdb-creds
              key: password
        {{- else }}
        - name: "INFLUXDB_URLS"
          value: http://$(DRYCC_MONITOR_INFLUXDB_SERVICE_HOST):$(DRYCC_MONITOR_INFLUXDB_SERVICE_PORT_TRANSPORT)
        {{- end }}
        - name: "BIND_PORT"
          value: "3500"
        - name: "DEFAULT_USER"
          value: {{.Values.monitor.grafana.user}}
        - name: "DEFAULT_USER_PASSWORD"
          value: {{.Values.monitor.grafana.password}}
        - name: "ALLOW_SIGN_UP"
          value: {{.Values.monitor.grafana.allow_sign_up | quote}}
        ports:
        - containerPort: 3500
          name: ui
{{- if .Values.monitor.grafana.persistence.enabled }}
        volumeMounts:
        - name: grafana-data
          mountPath: /var/lib/grafana
      volumes:
      - name: grafana-data
        persistentVolumeClaim:
          claimName: drycc-monitor-grafana
{{- end }}
{{- end }}
