{{- if .Values.controller.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "katib.fullname" . }}-controller
  namespace: {{ include "katib.namespace" . }}
  labels:
    {{- include "katib.controller.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.controller.replicas }}
  selector:
    matchLabels:
      {{- include "katib.controller.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "katib.controller.selectorLabels" . | nindent 8 }}
        sidecar.istio.io/inject: "false"
        {{- with .Values.controller.labels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "{{ .Values.controller.ports.metrics }}"
        {{- with .Values.controller.annotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ include "katib.controller.serviceAccountName" . }}
      {{- with .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.controller.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: katib-controller
          image: {{ include "katib.image" (dict "registry" .Values.global.imageRegistry "repository" .Values.controller.image.repository "tag" (.Values.controller.image.tag | default .Values.global.imageTag) "global" .Values.global) }}
          {{- include "katib.imagePullPolicy" (dict "pullPolicy" .Values.controller.image.pullPolicy "Values" .Values) | nindent 10 }}
          command: {{ toYaml .Values.controller.command.command | nindent 12 }}
          args:
            {{- toYaml .Values.controller.command.args | nindent 12 }}
          ports:
            - containerPort: {{ .Values.controller.ports.webhook }}
              name: {{ .Values.controller.service.ports.webhook.name }}
              protocol: TCP
            - containerPort: {{ .Values.controller.ports.metrics }}
              name: {{ .Values.controller.service.ports.metrics.name }}
              protocol: TCP
            - containerPort: {{ .Values.controller.ports.healthz }}
              name: {{ .Values.controller.service.ports.healthz.name }}
              protocol: TCP
          readinessProbe:
            httpGet:
              path: {{ .Values.controller.probes.readiness.path }}
              port: {{ .Values.controller.probes.readiness.port }}
          livenessProbe:
            httpGet:
              path: {{ .Values.controller.probes.liveness.path }}
              port: {{ .Values.controller.probes.liveness.port }}
          env:
            - name: KATIB_CORE_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            {{- with .Values.controller.env }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          volumeMounts:
            - mountPath: /tmp/cert
              name: cert
              readOnly: true
            - mountPath: /katib-config.yaml
              name: katib-config
              subPath: katib-config.yaml
              readOnly: true
            {{- with .Values.controller.volumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- with .Values.controller.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.controller.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      volumes:
        - name: cert
          secret:
            defaultMode: 420
            secretName: {{ include "katib.fullname" . }}-webhook-cert
        - name: katib-config
          configMap:
            name: {{ include "katib.fullname" . }}-config
        {{- with .Values.controller.volumes }}
        {{- toYaml . | nindent 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 }}
{{- end }}