# helm_charts_deployment-agent.yaml
{{- if and (not .Values.server.kubernetes.enabled) (not .Values.runner.enabled) -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "drone.fullname" . }}-agent
  labels:
    app: {{ template "drone.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
    component: agent
spec:
  selector:
    matchLabels:
      app: {{ template "drone.name" . }}
      release: "{{ .Release.Name }}"
      component: agent
  replicas: {{ .Values.agent.replicas }}
  template:
    metadata:
      annotations:
        checksum/secrets: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum }}
{{- if .Values.agent.annotations }}
{{ toYaml .Values.agent.annotations | indent 8 }}
{{- end }}
      labels:
        app: {{ template "drone.name" . }}
        release: "{{ .Release.Name }}"
        component: agent
    spec:
{{- if .Values.agent.schedulerName }}
      schedulerName: "{{ .Values.agent.schedulerName }}"
{{- end }}
{{- if .Values.agent.affinity }}
      affinity:
{{ toYaml .Values.agent.affinity | indent 8 }}
{{- end }}
{{- if .Values.agent.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.agent.nodeSelector | indent 8 }}
{{- end }}
{{- with .Values.agent.tolerations }}
      tolerations:
        {{- toYaml . | nindent 6 }}
{{- end }}
      serviceAccountName: {{ template "drone.serviceAccountName" . }}
      containers:
      - name: agent
        image: "{{ .Values.images.agent.repository }}:{{ .Values.images.agent.tag }}"
        imagePullPolicy: {{ .Values.images.agent.pullPolicy }}
        ports:
        - name: http
          containerPort: 3000
          protocol: TCP
        env:
          - name: DRONE_RPC_SERVER
            value: "{{ template "drone.rpcServer" . }}"
          - name: DRONE_RPC_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ template "drone.fullname" . }}
                key: secret
{{- if .Values.dind.enabled }}
          - name: DOCKER_HOST
            value: tcp://localhost:2375
{{- end }}
          {{- range $key, $value := .Values.agent.env }}
          - name: {{ $key }}
            value: {{ $value | quote }}
          {{- end }}
        resources:
{{ toYaml .Values.agent.resources | indent 10 }}
{{- if not .Values.dind.enabled }}
        securityContext:
          privileged: true
        volumeMounts:
          - mountPath: /var/run/docker.sock
            name: docker-socket
      {{- with .Values.agent.volumeMounts }}
          {{- toYaml . | nindent 10 }}
      {{- end }}
      volumes:
        - name: docker-socket
          hostPath:
            path: /var/run/docker.sock
{{- else }}
      {{- with .Values.agent.volumeMounts }}
        volumeMounts:
          {{- toYaml . | nindent 10 }}
      {{- end }}
      - name: dind
        image: "{{ .Values.images.dind.repository }}:{{ .Values.images.dind.tag }}"
        imagePullPolicy: {{ .Values.images.dind.pullPolicy }}
{{- if .Values.dind.command }}
        command: {{ .Values.dind.command }}
{{ end }}
{{- if .Values.dind.args }}
        args: {{ .Values.dind.args }}
{{ end }}
        env:
        - name: DOCKER_DRIVER
          value: {{ .Values.dind.driver }}
        {{ range $key, $value := .Values.dind.env }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{ end }}
        securityContext:
          privileged: true
        resources:
{{ toYaml .Values.dind.resources | indent 10 }}
        volumeMounts:
          - name: docker-graph-storage
            mountPath: /var/lib/docker
      {{- with .Values.dind.volumeMounts }}
          {{- toYaml . | nindent 10 }}
      {{- end }}
      volumes:
      - name: docker-graph-storage
        emptyDir: {}
{{- end }}
    {{- with .Values.agent.volumes }}
        {{- toYaml . | nindent 6 }}
    {{- end }}
{{- end -}}