apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "krawl.fullname" . }}
  labels:
    {{- include "krawl.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "krawl.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "krawl.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}
        {{- with .Values.securityContext }}
        securityContext:
          {{- toYaml . | nindent 12 }}
        {{- end }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - name: http
          containerPort: {{ .Values.config.server.port }}
          protocol: TCP
        env:
        - name: CONFIG_LOCATION
          value: "config.yaml"
        {{- if .Values.timezone }}
        - name: TZ
          value: {{ .Values.timezone | quote }}
        {{- end }}
        volumeMounts:
        - name: config
          mountPath: /app/config.yaml
          subPath: config.yaml
          readOnly: true
        - name: wordlists
          mountPath: /app/wordlists.json
          subPath: wordlists.json
          readOnly: true
        {{- if .Values.database.persistence.enabled }}
        - name: database
          mountPath: /app/data
        {{- end }}
        {{- with .Values.resources }}
        resources:
          {{- toYaml . | nindent 12 }}
        {{- end }}
      volumes:
      - name: config
        configMap:
          name: {{ include "krawl.fullname" . }}-config
      - name: wordlists
        configMap:
          name: {{ include "krawl.fullname" . }}-wordlists
      {{- if .Values.database.persistence.enabled }}
      - name: database
        {{- if .Values.database.persistence.existingClaim }}
        persistentVolumeClaim:
          claimName: {{ .Values.database.persistence.existingClaim }}
        {{- else }}
        persistentVolumeClaim:
          claimName: {{ include "krawl.fullname" . }}-db
        {{- 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 }}
