{{- $kubefullname := include "kubernetes.fullname" . }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "kubernetes.fullname" . }}
  labels:
  {{- include "kubernetes.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
  {{- include "kubernetes.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
    {{- include "kubernetes.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "kubernetes.serviceAccountName" . }}
      securityContext:
      {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.plecoImageTag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: [ "pleco", "start" ]
          args:
            - --level
            - {{ .Values.environmentVariables.LOG_LEVEL | default "info" }}
            {{ if eq .Values.enabledFeatures.disableDryRun true }}
            - --check-interval
            - "{{ .Values.enabledFeatures.checkInterval | default 120 }}"
            {{ end }}
            {{ if eq .Values.enabledFeatures.disableDryRun true }}
            - --disable-dry-run
            {{ end }}
            {{ if .Values.enabledFeatures.kubernetes }}
            - --kube-conn
            - {{ .Values.enabledFeatures.kubernetes }}
            {{ end }}
            {{ if eq .Values.enabledFeatures.rds true}}
            - --enable-rds
            {{ end }}
            {{ if eq .Values.enabledFeatures.elasticache true}}
            - --enable-elasticache
            {{ end }}
            {{ if eq .Values.enabledFeatures.documentdb true}}
            - --enable-documentdb
          {{ end }}
          env:
            {{ range $key, $value := .Values.environmentVariables -}}
            - name: "{{ $key }}"
              valueFrom:
                secretKeyRef:
                  name: {{ $kubefullname }}
                  key: {{ $key }}
            {{ end }}
          resources:
      {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
  {{- toYaml . | nindent 8 }}
  {{- end }}