{{- if .Values.server.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "woodpecker-ci.fullname" . }}-server
  labels:
    woodpecker.component: "server"
    {{- include "woodpecker-ci.labels" . | nindent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      woodpecker.component: "server"
      {{- include "woodpecker-ci.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        woodpecker.component: "server"
        {{- include "woodpecker-ci.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "woodpecker-ci.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.server.image.repository }}:{{ .Values.server.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.server.image.pullPolicy }}
          env:
            - name: WOODPECKER_HOST
              value: {{ include "woodpecker-ci.serverHost" . | quote }}
            - name: WOODPECKER_OPEN
              value: {{ .Values.server.registration | quote }}
            {{- range $key, $value := .Values.common.environment.variables }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
          {{- if .Values.common.environment.secrets }}
          envFrom:
          {{- range .Values.common.environment.secrets}}
            - secretRef:
                name: {{ . }}
          {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: 8000
              protocol: TCP
            - name: grpc
              containerPort: 9000
              protocol: TCP
          volumeMounts:
            - name: sqlite-volume
              mountPath: /var/lib/woodpecker
      volumes:
        - name: sqlite-volume
          persistentVolumeClaim:
            claimName: {{ include "woodpecker-ci.fullname" . }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}