apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "monero.fullname" . }}
  labels:
    {{- include "monero.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "monero.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "monero.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "monero.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if hasKey .Values.image "ports" }}
          ports:
          {{- range .Values.image.ports }}
            - name: {{ .name }}
              containerPort: {{ .containerPort }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          
          {{- if hasKey .Values.image "command" }}    
          command:
          {{- toYaml .Values.image.command | nindent 12 }}
          {{- end }}
          
          {{- if hasKey .Values.image "args" }}    
          args:
          {{- toYaml .Values.image.args | nindent 12 }}
          {{- end }}
          
          {{- if hasKey .Values.image "envVariables" }}    
          env:
          {{- toYaml .Values.image.envVariables | nindent 12 }}
          {{- end }}

          {{- if hasKey .Values.image "volumeMounts" }}    
          volumeMounts:
          {{- range .Values.image.volumeMounts }}
            - mountPath: {{ .mountPath }}
              name: {{ $.Release.Name}}-{{ .name }}
          {{- end }}
          {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if hasKey .Values "volumes" }}    
      volumes:
        {{- range .Values.volumes }}
          - configMap: 
              defaultMode: {{ .configMap.defaultMode }}
              name: {{ $.Release.Name}}-{{ .configMap.name }}
            name: {{ $.Release.Name}}-{{ .name }}
        {{- end }}
      {{- end }}