apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "gha-runner-scale-set-controller.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "gha-runner-scale-set-controller.labels" . | nindent 4 }}
    actions.github.com/controller-service-account-namespace: {{ .Release.Namespace }}
    actions.github.com/controller-service-account-name: {{ include "gha-runner-scale-set-controller.serviceAccountName" . }}
    {{- if .Values.flags.watchSingleNamespace }}
    actions.github.com/controller-watch-single-namespace: {{ .Values.flags.watchSingleNamespace }}
    {{- end }}
spec:
  replicas: {{ default 1 .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "gha-runner-scale-set-controller.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/default-container: "manager"
      {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        app.kubernetes.io/part-of: gha-runner-scale-set-controller
        app.kubernetes.io/component: controller-manager
        app.kubernetes.io/version: {{ .Chart.Version }}
        {{- include "gha-runner-scale-set-controller.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "gha-runner-scale-set-controller.serviceAccountName" . }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.priorityClassName }}
      priorityClassName: "{{ . }}"
      {{- end }}
      containers:
      - name: manager
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        args:
        - "--auto-scaling-runner-set-only"
        {{- if gt (int (default 1 .Values.replicaCount)) 1 }}
        - "--enable-leader-election"
        - "--leader-election-id={{ include "gha-runner-scale-set-controller.fullname" . }}"
        {{- end }}
        {{- with .Values.imagePullSecrets }}
        - "--auto-scaler-image-pull-secrets={{ include "gha-runner-scale-set-controller.imagePullSecretsNames" . }}"
        {{- end }}
        {{- with .Values.flags.logLevel }}
        - "--log-level={{ . }}"
        {{- end }}
        {{- with .Values.flags.watchSingleNamespace }}
        - "--watch-single-namespace={{ . }}"
        {{- end }}
        command:
        - "/manager"
        env:
        - name: CONTROLLER_MANAGER_CONTAINER_IMAGE
          value: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        - name: CONTROLLER_MANAGER_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        {{- with .Values.env }}
          {{- if kindIs "slice" . }}
            {{- toYaml . | nindent 8 }}
          {{- end }}
        {{- end }}
        {{- with .Values.resources }}
        resources:
          {{- toYaml . | nindent 12 }}
        {{- end }}
        {{- with .Values.securityContext }}
        securityContext:
          {{- toYaml . | nindent 12 }}
        {{- end }}
        volumeMounts:
        - mountPath: /tmp
          name: tmp
      terminationGracePeriodSeconds: 10
      volumes:
      - name: tmp
        emptyDir: {}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
