{{- if .Values.kubeops.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "kubeapps.kubeops.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels:
    app.kubernetes.io/component: kubeops
    {{- 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.kubeops.replicaCount }}
  {{- if .Values.kubeops.updateStrategy }}
  strategy: {{- toYaml .Values.kubeops.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: kubeops
  template:
    metadata:
      {{- if .Values.kubeops.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: kubeops
        {{- if .Values.kubeops.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "kubeapps.imagePullSecrets" . | indent 6 }}
      serviceAccountName: {{ template "kubeapps.kubeops.serviceAccountName" . }}
      {{- if .Values.kubeops.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubeops.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.kubeops.podAffinityPreset "component" "kubeops" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.kubeops.podAntiAffinityPreset "component" "kubeops" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.kubeops.nodeAffinityPreset.type "key" .Values.kubeops.nodeAffinityPreset.key "values" .Values.kubeops.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.kubeops.schedulerName }}
      schedulerName: {{ .Values.kubeops.schedulerName }}
      {{- end }}
      {{- if .Values.kubeops.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubeops.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubeops.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubeops.priorityClassName }}
      priorityClassName: {{ .Values.kubeops.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.kubeops.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.kubeops.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      # Increase termination timeout to let remaining operations to finish before ending the pods
      # This is because new releases/upgrades/deletions are synchronous operations
      terminationGracePeriodSeconds: {{ .Values.kubeops.terminationGracePeriodSeconds }}
      {{- if .Values.kubeops.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.initContainers "context" $) | trim | nindent 8 }}
      {{- end }}
      containers:
        - name: kubeops
          image: {{ include "kubeapps.kubeops.image" . }}
          imagePullPolicy: {{ .Values.kubeops.image.pullPolicy | quote }}
          {{- if .Values.kubeops.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.kubeops.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.kubeops.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.kubeops.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /kubeops
          {{- end }}
          {{- if .Values.kubeops.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --user-agent-comment=kubeapps/{{ .Chart.AppVersion }}
            - --assetsvc-url={{ printf "http://%s:%d" (include "kubeapps.assetsvc.fullname" .) (int .Values.assetsvc.service.ports.http) }}
            {{- if .Values.clusters }}
            - --clusters-config-path=/config/clusters.conf
            {{- end }}
            {{- if .Values.pinnipedProxy.enabled }}
            - --pinniped-proxy-url={{ printf "http://%s.%s:%d" (include "kubeapps.pinniped-proxy.fullname" .) .Release.Namespace (int .Values.pinnipedProxy.service.ports.pinnipedProxy) }}
            {{- end }}
            {{- if .Values.kubeops.burst }}
            - --burst={{ .Values.kubeops.burst }}
            {{- end }}
            {{- if .Values.kubeops.qps }}
            - --qps={{ .Values.kubeops.qps }}
            {{- end }}
            {{- if .Values.kubeops.namespaceHeaderName }}
            - --namespace-header-name={{ .Values.kubeops.namespaceHeaderName }}
            {{- end }}
            {{- if .Values.kubeops.namespaceHeaderPattern }}
            - --namespace-header-pattern={{ .Values.kubeops.namespaceHeaderPattern }}
            {{- end }}
            {{- range .Values.kubeops.extraFlags }}
            - {{ . }}
            {{- end }}
          {{- end }}
          env:
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: PORT
              value: {{ .Values.kubeops.containerPorts.http | quote }}
            {{- if .Values.kubeops.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.kubeops.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.kubeops.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.kubeops.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.kubeops.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.kubeops.containerPorts.http }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.kubeops.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.kubeops.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /live
              port: http
          {{- else if .Values.kubeops.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.kubeops.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.kubeops.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: http
          {{- else if .Values.kubeops.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.kubeops.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.kubeops.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.kubeops.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.kubeops.resources }}
          resources: {{- toYaml .Values.kubeops.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.clusters }}
            - name: clusters-config
              mountPath: /config
            - name: ca-certs
              mountPath: /etc/additional-clusters-cafiles
            {{- end }}
            {{- if .Values.kubeops.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.kubeops.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.sidecars "context" $) | trim | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.clusters }}
        - name: clusters-config
          configMap:
            name: {{ template "kubeapps.clusters-config.fullname" . }}
        - name: ca-certs
          emptyDir: {}
        {{- end }}
        {{- if .Values.kubeops.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.kubeops.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
