{{- $fullName := include "ntfy.fullname" . -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "ntfy.fullname" . }}
  labels:
    {{- include "ntfy.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "ntfy.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "ntfy.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "ntfy.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          args:
          - serve
          - '--cache-file'
          - '/var/cache/ntfy/cache.db'
          - '--auth-file'
          - '/var/lib/ntfy/user.db'
          - '--auth-default-access'
          - 'deny-all'
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
          - mountPath: /var/cache/ntfy
            name: cache
          - mountPath: /var/lib/ntfy
            name: data
          - mountPath: /etc/ntfy/server.yml
            name: config
            subPath: server.yml
      volumes:
      - name: cache
      {{- if .Values.persistence.cache.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.cache.existingClaim | default ( print $fullName "-cache") }}
      {{- else }}
        emptyDir: {}
      {{- end }}
      - name: data
      {{- if .Values.persistence.data.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.data.existingClaim | default ( print $fullName "-data") }}
      {{- else }}
        emptyDir: {}
      {{- end }}
      - name: config
        configMap:
          name: {{ $fullName }}-config
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}