{{- $root := . -}}
{{- range $component, $settings := dict "web" .Values.web "worker" .Values.worker }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "who-need-help.fullname" $root }}-{{ $component }}
  labels:
    {{- include "who-need-help.labels" $root | nindent 4 }}
    app.kubernetes.io/component: {{ $component }}
spec:
  replicas: {{ $settings.replicas }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1
  selector:
    matchLabels:
      {{- include "who-need-help.selectorLabels" $root | nindent 6 }}
      app.kubernetes.io/component: {{ $component }}
  template:
    metadata:
      {{- with $root.Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "who-need-help.selectorLabels" $root | nindent 8 }}
        app.kubernetes.io/component: {{ $component }}
        {{- with $root.Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      subdomain: {{ include "who-need-help.fullname" $root }}-headless
      securityContext:
        runAsNonRoot: true
        runAsUser: 65534
        fsGroup: 65534
      initContainers:
        - name: await-migrations
          image: "{{ $root.Values.image.repository }}:{{ $root.Values.image.tag }}"
          imagePullPolicy: {{ $root.Values.image.pullPolicy }}
          command: ["/app/bin/await_migrations"]
          envFrom:
            - secretRef:
                name: {{ include "who-need-help.secretName" $root }}
          env:
            - name: APP_ROLE
              value: migrate
            - name: ERL_ZFLAGS
              value: {{ printf "+Q %d" (int $root.Values.app.erlangPortLimit) | quote }}
            - name: CLUSTER_INTERFACE
              value: {{ $root.Values.app.clusterInterface | quote }}
            - name: EMAIL_DELIVERY_PROVIDER
              value: {{ $root.Values.app.emailDeliveryProvider | quote }}
            - name: UNISENDER_GO_BASE_URL
              value: {{ $root.Values.app.unisenderGoBaseUrl | quote }}
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: ["ALL"]
      containers:
        - name: {{ $component }}
          image: "{{ $root.Values.image.repository }}:{{ $root.Values.image.tag }}"
          imagePullPolicy: {{ $root.Values.image.pullPolicy }}
          {{- if eq $component "worker" }}
          command: ["/app/bin/who_need_help", "start"]
          {{- end }}
          envFrom:
            - secretRef:
                name: {{ include "who-need-help.secretName" $root }}
          env:
            - name: APP_ROLE
              value: {{ $component }}
            - name: ERL_ZFLAGS
              value: {{ printf "+Q %d" (int $root.Values.app.erlangPortLimit) | quote }}
            - name: CLUSTER_INTERFACE
              value: {{ $root.Values.app.clusterInterface | quote }}
            - name: PHX_SERVER
              value: {{ if eq $component "web" }}"true"{{ else }}"false"{{ end }}
            - name: PHX_HOST
              value: {{ $root.Values.app.host | quote }}
            - name: PHX_SCHEME
              value: {{ $root.Values.app.scheme | quote }}
            - name: PHX_URL_PORT
              value: {{ $root.Values.app.urlPort | quote }}
            - name: PORT
              value: {{ $root.Values.app.port | quote }}
            - name: POOL_SIZE
              value: {{ $settings.poolSize | quote }}
            - name: OBAN_MAINTENANCE_CONCURRENCY
              value: {{ $root.Values.worker.maintenanceConcurrency | quote }}
            - name: OBAN_PUSH_CONCURRENCY
              value: {{ $root.Values.worker.pushConcurrency | quote }}
            - name: EMAIL_DELIVERY_PROVIDER
              value: {{ $root.Values.app.emailDeliveryProvider | quote }}
            - name: SMTP_RELAY
              value: {{ $root.Values.app.smtpRelay | quote }}
            - name: SMTP_PORT
              value: {{ $root.Values.app.smtpPort | quote }}
            - name: UNISENDER_GO_BASE_URL
              value: {{ $root.Values.app.unisenderGoBaseUrl | quote }}
            - name: CODEX_SESSION_ID
              value: {{ $root.Values.app.codexSessionId | quote }}
            - name: RATE_LIMIT_POLICIES_JSON
              value: {{ $root.Values.app.rateLimitPoliciesJson | quote }}
            - name: MAP_TILE_URL
              value: {{ $root.Values.app.mapTileUrl | quote }}
            - name: DNS_CLUSTER_QUERY
              value: "{{ include "who-need-help.fullname" $root }}-headless.{{ $root.Release.Namespace }}.svc.cluster.local"
          ports:
            - name: http
              containerPort: 4000
          readinessProbe:
            httpGet:
              path: /healthz/ready
              port: http
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /healthz/live
              port: http
            periodSeconds: 10
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities:
              drop: ["ALL"]
      {{- with $root.Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with $root.Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with $root.Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
---
{{- end }}
