apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "rmqco.msgTopologyOperator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: messaging-topology-operator
    app.kubernetes.io/part-of: rabbitmq
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.msgTopologyOperator.replicaCount }}
  {{- if .Values.msgTopologyOperator.updateStrategy }}
  strategy: {{- toYaml .Values.msgTopologyOperator.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: messaging-topology-operator
  template:
    metadata:
      {{- if .Values.msgTopologyOperator.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: messaging-topology-operator
        app.kubernetes.io/part-of: rabbitmq
        {{- if .Values.msgTopologyOperator.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "rmqco.msgTopologyOperator.serviceAccountName" . }}
      {{- include "rmqco.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.msgTopologyOperator.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.msgTopologyOperator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.msgTopologyOperator.schedulerName }}
      schedulerName: {{ .Values.msgTopologyOperator.schedulerName | quote }}
      {{- end }}
      {{- if .Values.msgTopologyOperator.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.msgTopologyOperator.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.msgTopologyOperator.podAffinityPreset "component" "messaging-topology-operator" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.msgTopologyOperator.podAntiAffinityPreset "component" "messaging-topology-operator" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.msgTopologyOperator.nodeAffinityPreset.type "key" .Values.msgTopologyOperator.nodeAffinityPreset.key "values" .Values.msgTopologyOperator.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.msgTopologyOperator.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.msgTopologyOperator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.msgTopologyOperator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.msgTopologyOperator.priorityClassName }}
      priorityClassName: {{ .Values.msgTopologyOperator.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.msgTopologyOperator.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.msgTopologyOperator.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.msgTopologyOperator.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.msgTopologyOperator.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.msgTopologyOperator.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: rabbitmq-cluster-operator
          image: {{ template "rmqco.msgTopologyOperator.image" . }}
          imagePullPolicy: {{ .Values.msgTopologyOperator.image.pullPolicy }}
          {{- if .Values.msgTopologyOperator.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.msgTopologyOperator.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.msgTopologyOperator.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /manager
          {{- end }}
          {{- if .Values.msgTopologyOperator.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --metrics-bind-address=:{{ .Values.msgTopologyOperator.containerPorts.metrics }}
          {{- end }}
          env:
            - name: OPERATOR_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            {{- if .Values.msgTopologyOperator.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.msgTopologyOperator.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.msgTopologyOperator.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.msgTopologyOperator.resources }}
          resources: {{- toYaml .Values.msgTopologyOperator.resources | nindent 12 }}
          {{- end }}
          ports:
            - name: http-webhook
              containerPort: 9443
              protocol: TCP
            - name: http-metrics
              containerPort: {{ .Values.msgTopologyOperator.containerPorts.metrics }}
              protocol: TCP
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.msgTopologyOperator.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.msgTopologyOperator.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: http-metrics
          {{- else if .Values.msgTopologyOperator.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.msgTopologyOperator.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.msgTopologyOperator.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: http-metrics
          {{- else if .Values.msgTopologyOperator.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.msgTopologyOperator.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.msgTopologyOperator.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: http-metrics
          {{- else if .Values.msgTopologyOperator.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.msgTopologyOperator.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - mountPath: /tmp/k8s-webhook-server/serving-certs
              name: cert
              readOnly: true
            {{- if .Values.msgTopologyOperator.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.msgTopologyOperator.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.msgTopologyOperator.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: cert
          secret:
            defaultMode: 420
            secretName: {{ template "rmqco.msgTopologyOperator.webhook.secretName" . }}
        {{- if .Values.msgTopologyOperator.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.msgTopologyOperator.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
