{{- if .Values.metrics.enabled }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "nextcloud.fullname" . }}-metrics
  labels:
    {{- include "nextcloud.labels"  ( dict "component" "metrics"  "rootContext" $ ) | nindent 4 }}
spec:
  replicas: {{ .Values.metrics.replicaCount }}
  selector:
    matchLabels:
      {{- include "nextcloud.selectorLabels"  ( dict "component" "metrics"  "rootContext" $ ) | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- toYaml .Values.metrics.podAnnotations | nindent 8 }}
      labels:
        {{- include "nextcloud.selectorLabels"  ( dict "component" "metrics"  "rootContext" $ ) | nindent 8 }}
        {{- with .Values.metrics.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.metrics.image.pullSecrets }}
      imagePullSecrets:
        {{- range . }}
        - name: {{ . }}
        {{- end}}
      {{- end }}
      containers:
        - name: metrics-exporter
          image: "{{ .Values.metrics.image.repository }}:{{ .Values.metrics.image.tag }}"
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy }}
          env:
            {{- if or .Values.metrics.token .Values.nextcloud.existingSecret.tokenKey }}
            - name: NEXTCLOUD_AUTH_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.nextcloud.existingSecret.secretName | default (include "nextcloud.fullname" .) }}
                  key: {{ .Values.nextcloud.existingSecret.tokenKey | default "nextcloud-token" }}
            {{- else }}
            - name: NEXTCLOUD_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.nextcloud.existingSecret.secretName | default (include "nextcloud.fullname" .) }}
                  key: {{ .Values.nextcloud.existingSecret.usernameKey }}
            - name: NEXTCLOUD_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.nextcloud.existingSecret.secretName | default (include "nextcloud.fullname" .) }}
                  key: {{ .Values.nextcloud.existingSecret.passwordKey }}
            {{- end }}
            # NEXTCLOUD_SERVER is used by metrics-exporter to reach the Nextcloud (K8s-)Service to grab the serverinfo api endpoint
            {{- if not .Values.metrics.server }}
            - name: NEXTCLOUD_SERVER # deployment.namespace.svc.cluster.local
              value: "http{{ if .Values.metrics.https }}s{{ end }}://{{ template "nextcloud.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local:{{ .Values.service.port }}"
            {{- else }}
            - name: NEXTCLOUD_SERVER
              value: {{ .Values.metrics.server }}
            {{- end }}
            - name: NEXTCLOUD_TIMEOUT
              value: {{ .Values.metrics.timeout }}
            - name: NEXTCLOUD_TLS_SKIP_VERIFY
              value: {{ .Values.metrics.tlsSkipVerify | quote }}
            - name: NEXTCLOUD_INFO_APPS
              value: {{ .Values.metrics.info.apps | quote }}
            - name: NEXTCLOUD_INFO_UPDATE
              value: {{ .Values.metrics.info.update | quote }}
          ports:
            - name: metrics
              containerPort: 9205
          {{- with .Values.metrics.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.metrics.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.metrics.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 12 }}
      {{- end }}
      {{- with .Values.metrics.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.metrics.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.metrics.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}
