apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "hugs.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "hugs.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount | default 1 | int }}
  selector:
    matchLabels:
      {{- include "hugs.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "hugs.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- if .Values.serviceAccountName }}
      serviceAccountName: {{ .Values.serviceAccountName }}
      {{- end }}
      {{- if .Values.podSecurityContext }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      {{- end }}
      tolerations:
        {{- if contains "nvidia" .Values.image.name }}
        - key: nvidia.com/gpu
          operator: Exists
          effect: NoSchedule
        {{- else if contains "neuron" .Values.image.name }}
        - key: aws.amazon.com/neuron
          operator: Exists
          effect: NoSchedule
        {{- else if contains "amd" .Values.image.name }}
        - key: amd.com/gpu
          operator: Exists
          effect: NoSchedule
        {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          {{- if .Values.securityContext }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          {{- end }}
          image: "{{ include "hugs.image_uri" . }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ $.Values.env.PORT | default 80 | int }}
              protocol: TCP
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /health
              port: {{ $.Values.env.PORT | default 80 | int }}
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds | default 360 }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds | default 30 }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds | default 5 }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold | default 3 }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /health
              port: {{ $.Values.env.PORT | default 80 | int }}
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds | default 360 }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds | default 15 }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds | default 5 }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold | default 3 }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if .Values.env }}
          env:
            {{- range $key, $value := .Values.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
          {{- end }}
          {{- with .Values.volumeMounts }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.volumes }}
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
