{{- if eq .Values.controller.type "Deployment" }}
{{- include "home-assistant.validateController" . | trim }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "home-assistant.fullname" . }}
  labels:
    {{- include "home-assistant.labels" . | nindent 4 }}
{{- if .Values.deploymentAnnotations }}
  annotations:
    {{- toYaml .Values.deploymentAnnotations | nindent 4 }}
{{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "home-assistant.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "home-assistant.selectorLabels" . | nindent 8 }}
      annotations:
        {{- if .Values.configuration.enabled }}
        checksum/init-script: {{ include (print $.Template.BasePath "/configmap-init-script.yaml") . | sha256sum }}
        checksum/hass-configuration: {{ include (print $.Template.BasePath "/configmap-hass-config.yaml") . | sha256sum }}
        {{- end }}
      {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
    {{- if .Values.hostNetwork }}
      hostNetwork: true
    {{- end }}
    {{- if .Values.dnsPolicy }}
      dnsPolicy: {{ .Values.dnsPolicy }}
    {{- end }}
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "home-assistant.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
    {{- if .Values.dnsConfig }}
      dnsConfig:
        {{- toYaml .Values.dnsConfig | nindent 8 }}
    {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- with .Values.envFrom }}
          envFrom:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.env }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: 8123
              protocol: TCP
              {{- if .Values.hostPort.enabled }}
              hostPort: {{ .Values.hostPort.port }}
              {{- end }}
          {{- if .Values.additionalPorts }}
            {{- .Values.additionalPorts | toYaml | nindent 12 }}
          {{- end }}
          {{- with $.Values.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $.Values.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $.Values.startupProbe }}
          startupProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
          - mountPath: /config
            name: {{ include "home-assistant.fullname" . }}-pvc
          {{- if .Values.additionalMounts }}
            {{- .Values.additionalMounts | toYaml | nindent 10 }}
          {{- end }}
        {{- if .Values.addons.codeserver.enabled }}
        - name: codeserver
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          args:
          - --auth
          - none
          - --user-data-dir
          - "/config/.vscode"
          - --extensions-dir
          - "/config/.vscode"
          - --port
          - "12321"
          - "/config"
          image: "{{ .Values.addons.codeserver.image.repository }}:{{ .Values.addons.codeserver.image.tag }}"
          imagePullPolicy: "{{ .Values.addons.codeserver.image.pullPolicy }}"
          ports:
          - containerPort: 12321
            name: codeserver
            protocol: TCP
          {{- with .Values.addons.codeserver.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
          - mountPath: /config
            name: {{ include "home-assistant.fullname" . }}-pvc
          {{- if .Values.addons.codeserver.additionalMounts }}
            {{- .Values.addons.codeserver.additionalMounts | toYaml | nindent 10 }}
          {{- end }}
        {{- end }}
      {{- if or (.Values.configuration.enabled) .Values.initContainers }}
      initContainers:
        {{- if .Values.initContainers }}
        {{- toYaml .Values.initContainers | nindent 8 }}
        {{- end }}
        {{- if .Values.configuration.enabled }}
        - name: {{ .Values.configuration.initContainer.name }}
          image: {{ .Values.configuration.initContainer.image }}
          {{- if .Values.configuration.initContainer.securityContext }}
          securityContext:
            {{- toYaml .Values.configuration.initContainer.securityContext | nindent 12 }}
          {{- end }}
          {{- if .Values.configuration.initContainer.command }}
          command: {{ toYaml .Values.configuration.initContainer.command | nindent 12 }}
          {{- end }}
          {{- if .Values.configuration.initContainer.args }}
          args: {{ toYaml .Values.configuration.initContainer.args | nindent 12 }}
          {{- end }}
          {{- if .Values.configuration.initContainer.env }}
          env:
            {{- toYaml .Values.configuration.initContainer.env | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- range .Values.configuration.initContainer.volumeMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              {{- if .subPath }}
              subPath: {{ .subPath }}
              {{ end }}
            {{- end }}
            - mountPath: /config
              name: {{ include "home-assistant.fullname" $ }}-pvc
        {{- 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 .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
    {{- end }}
      volumes:
      {{- if .Values.configuration.enabled }}
      - name: init-volume
        configMap:
          name: init-script
      - name: config-volume
        configMap:
          name: hass-configuration
      {{- end }}
      {{- if not .Values.persistence.enabled }}
      - name: {{ include "home-assistant.fullname" . }}-pvc
        emptyDir: {}
    {{- else if .Values.persistence.existingClaim }}
      - name: {{ include "home-assistant.fullname" . }}-pvc
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.existingClaim }}
    {{- else }}
      - name: {{ include "home-assistant.fullname" . }}-pvc
        persistentVolumeClaim:
          claimName: {{ include "home-assistant.fullname" . }}-pvc
    {{- end }}
      {{- if .Values.additionalVolumes }}
        {{- .Values.additionalVolumes | toYaml | nindent 6 }}
      {{- end }}
{{- end }}
