apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "gha-controller.name" . }}
  namespace: {{ include "gha-controller.namespace" . }}
  labels:
    {{- include "gha-controller.labels" . | nindent 4 }}
spec:
  replicas: {{ default 1 .Values.controller.replicaCount }}
  selector:
    matchLabels:
      {{- include "gha-controller.selector-labels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- include "gha-controller-template.annotations" . | nindent 8 }}
      labels:
        {{- include "gha-controller-template.labels" . | nindent 8 }}
    spec:
      {{- $pod := (.Values.controller.pod | default dict) -}}
      {{- if and (hasKey .Values.controller "pod") (not (kindIs "map" $pod)) -}}
        {{- fail "controller.pod must be an object" -}}
      {{- end -}}
      {{- $podSpec := (index $pod "spec" | default dict) -}}
      {{- if and (hasKey $pod "spec") (not (kindIs "map" $podSpec)) -}}
        {{- fail "controller.pod.spec must be an object" -}}
      {{- end -}}


      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "gha-controller.service-account-name" . }}
      containers:
        -
      {{- include "gha-controller-template.manager-container" . | nindent 10 }}
        {{- $extraContainers := (index $podSpec "containers" | default list) -}}
        {{- range $extraContainers }}
        -
      {{- toYaml . | nindent 10 }}
        {{- end }}
      terminationGracePeriodSeconds: {{ default 10 (index $podSpec "terminationGracePeriodSeconds") }}
      volumes:
        - name: tmp
          emptyDir: {}
      {{- $podVolumes := (index $podSpec "volumes" | default list) -}}
      {{- range $podVolumes }}
        - {{- toYaml . | nindent 10 }}
      {{- end }}
      {{- $runnerPodSpecExtraFields := (omit $podSpec "containers" "serviceAccountName" "terminationGracePeriodSeconds" "volumes") -}}
      {{- if gt (len $runnerPodSpecExtraFields) 0 }}
      {{- toYaml $runnerPodSpecExtraFields | nindent 6 }}
      {{- end }}