{{- if .Values.controller.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "goldilocks.fullname" . }}-controller
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ include "goldilocks.name" . }}
    helm.sh/chart: {{ include "goldilocks.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/component: controller
    {{- if .Values.controller.deployment.additionalLabels }}
    {{ toYaml .Values.controller.deployment.additionalLabels | nindent 4 }}
    {{- end }}
  {{- with .Values.controller.deployment.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  {{- if .Values.controller.revisionHistoryLimit }}
  revisionHistoryLimit: {{ .Values.controller.revisionHistoryLimit }}
  {{- end }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "goldilocks.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/component: controller
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "goldilocks.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/component: controller
        {{- if .Values.controller.deployment.additionalLabels }}
        {{ toYaml .Values.controller.deployment.additionalLabels | nindent 8 }}
        {{- end }}
      {{- with .Values.controller.deployment.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "controller.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.controller.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /goldilocks
            - controller
            - -v{{ .Values.controller.logVerbosity }}
            {{- range $name, $value := .Values.controller.flags }}
            - --{{ $name }}={{ $value }}
            {{- end }}
        {{- if .Values.controller.securityContext }}
          securityContext:
            {{- toYaml .Values.controller.securityContext | nindent 12 }}
        {{- end }}
          resources:
            {{- toYaml .Values.controller.resources | nindent 12 }}
          {{- if .Values.controller.deployment.extraVolumeMounts }}
          volumeMounts:
          {{ toYaml .Values.controller.deployment.extraVolumeMounts | nindent 12 }}
          {{- end }}
{{- if .Values.controller.deployment.extraVolumes }}
      volumes:
{{ toYaml .Values.controller.deployment.extraVolumes | indent 8}}
{{- end }}
      {{- with .Values.controller.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.controller.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.controller.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.controller.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
    {{- end }}
{{- end }}
