apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "woodpecker-agent.fullname" . }}
  labels:
    {{- include "woodpecker-agent.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "woodpecker-agent.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "woodpecker-agent.selectorLabels" . | nindent 8 }}
    spec:
      {{- if and (semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion) (.Values.topologySpreadConstraints) }}
      {{- with .Values.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "woodpecker-agent.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 3000
              protocol: TCP
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if .Values.dind.enabled }}
          volumeMounts:
            - name: sock-dir
              mountPath: /var/run
          {{- end }}
          env:
            {{- range $key, $value := .Values.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
          envFrom:
            {{- range .Values.extraSecretNamesForEnvFrom }}
            - secretRef:
                name: {{ . }}
            {{- end }}
        {{- if .Values.dind.enabled }}
        - name: dind
          image: {{ .Values.dind.image }}
          env:
            {{- range $key, $value := .Values.dind.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
          resources:
            {{- toYaml .Values.dind.resources | nindent 12 }}
          securityContext:
            privileged: true
          volumeMounts:
          - name: sock-dir
            mountPath: /var/run
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.dind.enabled }}
      volumes:
      - name: sock-dir
        emptyDir: {}
      {{- end }}
