{{- if eq (include "drone.providerOK" .) "true" }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "drone.fullname" . }}-server
  labels:
    app: {{ template "drone.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
    component: server
spec:
  selector:
    matchLabels:
      app: {{ template "drone.name" . }}
      release: "{{ .Release.Name }}"
      component: server
  replicas: 1
  template:
    metadata:
      annotations:
        checksum/secrets: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum }}
{{- if .Values.metrics.prometheus.enabled }}
        prometheus.io/scrape: "true"
        prometheus.io/port: "8000"
{{- end }}
{{- if .Values.server.annotations }}
{{ toYaml .Values.server.annotations | indent 8 }}
{{- end }}
      labels:
        app: {{ template "drone.name" . }}
        release: "{{ .Release.Name }}"
        component: server
    spec:
{{- if .Values.server.schedulerName }}
      schedulerName: "{{ .Values.server.schedulerName }}"
{{- end }}
{{- if .Values.server.affinity }}
      affinity:
{{ toYaml .Values.server.affinity | indent 8 }}
{{- end }}
{{- if .Values.server.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.server.nodeSelector | indent 8 }}
{{- end }}
{{- with .Values.server.tolerations }}
      tolerations:
        {{- toYaml . | nindent 6 }}
{{- end }}
      serviceAccountName: {{ template "drone.serviceAccountName" . }}
{{- if .Values.images.server.pullSecret }}
      imagePullSecrets:
        - name: {{ .Values.images.server.pullSecret }}
{{- end }}
      containers:
      - name: server
        image: "{{ .Values.images.server.repository }}:{{ .Values.images.server.tag }}"
        imagePullPolicy: {{ .Values.images.server.pullPolicy }}
        env:
        {{- if (or .Values.licenseKey .Values.licenseKeySecret) }}
          - name: DRONE_LICENSE
            value: "/etc/drone.key"
        {{- end }}
        {{- if .Values.server.kubernetes.enabled }}
          - name: DRONE_KUBERNETES_ENABLED
            value: "true"
          - name: DRONE_KUBERNETES_NAMESPACE
            value: {{ default .Release.Namespace .Values.server.kubernetes.namespace }}
          - name: DRONE_KUBERNETES_SERVICE_ACCOUNT
            value: {{ template "drone.pipelineServiceAccount" . }}
        {{- else }}
          - name: DRONE_AGENTS_ENABLED
            value: "true"
        {{- end }}
          - name: DRONE_GIT_ALWAYS_AUTH
            value: {{ .Values.server.alwaysAuth | quote }}
          - name: DRONE_SERVER_HOST
          {{- if hasKey .Values.server "host" }}
            value: "{{ .Values.server.host }}"
          {{- else }}
            value: "{{ template "drone.fullname" . }}"
          {{- end }}
          - name: DRONE_SERVER_PORT
            value: ":{{ .Values.server.httpPort }}"
          - name: DRONE_RPC_PROTO
            value: "{{ .Values.server.rpcProtocol }}"
          - name: DRONE_RPC_HOST
            value: {{ template "drone.fullname" . }}.{{ .Release.Namespace }}:{{ .Values.service.httpPort }}
          - name: DRONE_SERVER_PROTO
            value: {{ .Values.server.protocol }}
          - name: DRONE_DATABASE_DRIVER
            value: {{ .Values.server.database.driver }}
          - name: DRONE_DATABASE_DATASOURCE
            value: {{ .Values.server.database.dataSource }}
          - name: DRONE_LOGS_COLOR
            value: {{ .Values.server.logs.color | quote }}
          - name: DRONE_LOGS_DEBUG
            value: {{ .Values.server.logs.debug | quote }}
          - name: DRONE_LOGS_PRETTY
            value: {{ .Values.server.logs.pretty | quote }}
          - name: DRONE_LOGS_TRACE
            value: {{ .Values.server.logs.trace | quote }} 
          - name: DRONE_LOGS_TEXT
            value: {{ .Values.server.logs.text | quote }} 
          {{- if .Values.server.adminUser }}
          - name: DRONE_USER_CREATE
            value: username:{{ .Values.server.adminUser }},machine:false,admin:true
          {{- end }}
          - name: DRONE_RPC_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ template "drone.fullname" . }}
                key: secret
          {{- range $key, $value := .Values.server.env }}
          - name: {{ $key }}
            value: {{ $value | quote }}
          {{- end }}
          {{ template "drone.providerEnvs" . }}
          {{- range $secret, $keys := .Values.server.envSecrets }}
          {{- range $keys }}
          - name: {{ . }}
            valueFrom:
              secretKeyRef:
                name: {{ $secret }}
                key: {{ . | quote }}
          {{- end }}
          {{- end }}
        ports:
        - name: http
          containerPort: {{ .Values.server.httpPort }}
          protocol: TCP
        - name: https
          containerPort: 443
          protocol: TCP
        - name: grpc
          containerPort: 9000
          protocol: TCP
        livenessProbe:
          httpGet:
            path: /
            port: http
        resources:
{{ toYaml .Values.server.resources | indent 10 }}
        volumeMounts:
        {{ if eq .Values.sourceControl.provider "bitbucketServer" -}}
        - name: bitbucket-private-key
          mountPath: /etc/bitbucket
          readOnly: true
        {{ end }}
        {{ if (or .Values.licenseKey .Values.licenseKeySecret) -}}
        - name: license-key
          mountPath: /etc/drone.key
          subPath: drone.key
          readOnly: true
        {{ end }}
        - name: data
          mountPath: /var/lib/drone
{{- with .Values.server.extraContainers }}
{{ tpl . $ | indent 6 }}
{{- end }}
      volumes:
      {{ if eq .Values.sourceControl.provider "bitbucketServer" -}}
      - name: bitbucket-private-key
        secret:
          secretName: {{ template "drone.sourceControlSecret" . }}
          items:
            - key: {{ .Values.sourceControl.bitbucketServer.privateKey }}
              path: key.pem
      {{ end -}}
      {{ if (or .Values.licenseKey .Values.licenseKeySecret) -}}
      - name: license-key
        secret:
          secretName: {{ template "drone.licenseKeySecret" . }}
          items:
            - key: license-key
              path: drone.key
      {{ end -}}
      - name: data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.existingClaim | default (include "drone.fullname" .) }}
      {{- else }}
        emptyDir: {}
      {{- end -}}
      {{- if .Values.server.securityContext }}
      securityContext:
{{ toYaml .Values.server.securityContext | indent 8 }}
      {{- end }}
{{- with .Values.server.extraVolumes }}
{{ tpl . $ | indent 6 }}
{{- end }}
{{ end }}
