---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: coder

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: coder
  labels:
    {{- include "coder.labels" . | nindent 4 }}
spec:
  # NOTE: this is currently not used as coder v2 does not support high
  #       availability yet.
  # replicas: {{ .Values.coder.replicaCount }}
  replicas: 1
  selector:
    matchLabels:
      {{- include "coder.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "coder.selectorLabels" . | nindent 8 }}
    spec:
      serviceAccountName: coder
      restartPolicy: Always
      terminationGracePeriodSeconds: 60
      containers:
        - name: coder
          image: {{ include "coder.image" . | quote }}
          imagePullPolicy: {{ .Values.coder.image.pullPolicy }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          env:
            - name: CODER_ADDRESS
              value: "0.0.0.0:{{ include "coder.port" . }}"
            {{- if .Values.coder.tls.secretName }}
            - name: CODER_TLS_ENABLE
              value: "true"
            - name: CODER_TLS_CERT_FILE
              value: /etc/ssl/certs/coder/tls.crt
            - name: CODER_TLS_KEY_FILE
              value: /etc/ssl/certs/coder/tls.key
            {{- end }}
            {{- with .Values.coder.env -}}
            {{ toYaml . | nindent 12 }}
            {{- end }}
          ports:
            - name: {{ include "coder.portName" . | quote }}
              containerPort: {{ include "coder.port" . }}
              protocol: TCP
          readinessProbe:
            httpGet:
              path: /api/v2/buildinfo
              port: {{ include "coder.portName" . | quote }}
              scheme: {{ include "coder.scheme" . | quote }}
          livenessProbe:
            httpGet:
              path: /api/v2/buildinfo
              port: {{ include "coder.portName" . | quote }}
              scheme: {{ include "coder.scheme" . | quote }}
          {{- if .Values.coder.tls.secretName }}
          volumeMounts:
            - name: tls
              mountPath: /etc/ssl/certs/coder
              readOnly: true
          {{- end }}

      {{- if .Values.coder.tls.secretName }}
      volumes:
        - name: tls
          secret:
            secretName: {{ .Values.coder.tls.secretName | quote }}
      {{- end }}
