apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "badhouseplants-net.fullname" . }}
  labels:
    {{- include "badhouseplants-net.labels" . | nindent 4 }}
  {{- with .Values.deployAnnotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "badhouseplants-net.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "badhouseplants-net.selectorLabels" . | nindent 8 }}
    spec:
      initContainers:
        - name: {{ .Values.rclone.container.name }}
          image: "{{ .Values.rclone.image.repository}}:{{ .Values.rclone.image.tag}}"
          imagePullPolicy: {{ .Values.rclone.image.pullPolicy }}
          env:
            - name: RCLONE_CONFIG
              value: /tmp/rclone.conf
          command:
            - sh
          args:
            - "-c"
            - {{ .Values.rclone.command }}
          volumeMounts:
            - name: rclone-config
              mountPath: "/tmp"
              readOnly: true
            - name: {{ .Values.volumes.rclone.name }}
              mountPath: /static
              readOnly: false
          resources:
            {{- toYaml .Values.rclone.container.resources | nindent 12 }}
        - name: {{ .Values.hugo.container.name }}
          env: 
          {{- range $key, $value := .Values.hugo.env}}
            - name: {{ $key }}
              value: {{ $value }}
          {{- end}}
          args:
            - --baseURL
            - {{ .Values.hugo.baseURL }}
            {{- if .Values.hugo.buildDrafts }}
            - --buildDrafts
            {{- end }}
          volumeMounts:
            - name: {{ .Values.volumes.rclone.name }}
              mountPath: /src/static
              readOnly: true
            - name: {{ .Values.volumes.public.name }}
              mountPath: /src/public
              readOnly: false
          image: "{{ .Values.hugo.image.repository}}:{{ .Values.hugo.image.tag}}"
          imagePullPolicy: {{ .Values.hugo.image.pullPolicy }}
          resources:
            {{- toYaml .Values.hugo.container.resources | nindent 12 }}
      containers:
        - name: {{ .Values.nginx.container.name }}
          image: "{{ .Values.nginx.image.repository}}:{{ .Values.nginx.image.tag}}"
          imagePullPolicy: {{ .Values.nginx.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          resources:
            {{- toYaml .Values.nginx.container.resources | nindent 12 }}
          volumeMounts:
            - name: {{ .Values.volumes.public.name }}
              mountPath: /var/www
              readOnly: true
            - name: nginx-config
              mountPath:  /etc/nginx/conf.d
              readOnly: true
      volumes:
        - name: rclone-config
          secret:
            secretName: rclone-config
        - name: {{ .Values.volumes.rclone.name }}
          emptyDir:
            sizeLimit: {{ .Values.volumes.rclone.sizeLimit }}
        - name: {{ .Values.volumes.public.name }}
          emptyDir:
            sizeLimit: {{ .Values.volumes.public.sizeLimit }}
        - name: nginx-config
          configMap:
            name: nginx-config

