{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.operator.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "kuberay.operator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.operator.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: kuberay
    app.kubernetes.io/component: operator
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.operator.autoscaling.hpa.enabled }}
  replicas: {{ .Values.operator.replicaCount }}
  {{- end }}
  {{- if .Values.operator.updateStrategy }}
  strategy: {{- toYaml .Values.operator.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.operator.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: kuberay
      app.kubernetes.io/component: operator
  template:
    metadata:
      {{- if .Values.operator.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.operator.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: kuberay
        app.kubernetes.io/component: operator
    spec:
      serviceAccountName: {{ template "kuberay.operator.serviceAccountName" . }}
      {{- include "kuberay.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.operator.automountServiceAccountToken }}
      {{- if .Values.operator.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.operator.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.operator.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.operator.podAffinityPreset "component" "operator" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.operator.podAntiAffinityPreset "component" "operator" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.operator.nodeAffinityPreset.type "key" .Values.operator.nodeAffinityPreset.key "values" .Values.operator.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.operator.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.operator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.operator.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.priorityClassName }}
      priorityClassName: {{ .Values.operator.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.operator.schedulerName }}
      schedulerName: {{ .Values.operator.schedulerName | quote }}
      {{- end }}
      {{- if .Values.operator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.operator.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.operator.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.operator.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.operator.initContainers }}
      initContainers:
        {{- include "common.tplvalues.render" (dict "value" .Values.operator.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: kuberay-operator
          image: {{ template "kuberay.operator.image" . }}
          imagePullPolicy: {{ .Values.operator.image.pullPolicy }}
          {{- if .Values.operator.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.operator.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.operator.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.operator.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.operator.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.operator.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --metrics-addr=:{{ .Values.operator.containerPorts.metrics }}
            - --health-probe-bind-address=:{{ .Values.operator.containerPorts.health }}
            {{- if not .Values.operator.watchAllNamespaces }}
            {{- $namespaces := default (list (include "common.names.namespace" .)) .Values.operator.watchNamespaces }}
            - --watch-namespace={{ join "," $namespaces }}
            {{- end }}
          {{- end }}
          {{- if .Values.operator.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          envFrom:
            {{- if .Values.operator.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.operator.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.operator.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.operator.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.operator.resources }}
          resources: {{- toYaml .Values.operator.resources | nindent 12 }}
          {{- else if ne .Values.operator.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.operator.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http-metrics
              containerPort: {{ .Values.operator.containerPorts.metrics }}
            - name: http-health
              containerPort: {{ .Values.operator.containerPorts.health }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.operator.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.operator.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http-health
          {{- end }}
          {{- if .Values.operator.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.operator.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: http-health
          {{- end }}
          {{- if .Values.operator.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.operator.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: http-health
          {{- end }}
          {{- end }}
          {{- if .Values.operator.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.operator.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
              {{- if .Values.operator.extraVolumeMounts }}
              {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraVolumeMounts "context" $) | nindent 12 }}
              {{- end }}
        {{- if .Values.operator.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.operator.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
          {{- if .Values.operator.extraVolumes }}
          {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraVolumes "context" $) | nindent 8 }}
          {{- end }}
{{- end }}
