apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "kubeapps.kubeops.fullname" . }}
  labels:{{ include "kubeapps.extraAppLabels" . | nindent 4 }}
    app: {{ template "kubeapps.kubeops.fullname" . }}
spec:
  replicas: {{ .Values.kubeops.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "kubeapps.kubeops.fullname" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "kubeapps.kubeops.fullname" . }}
        app.kubernetes.io/name: {{ template "kubeapps.name" . }}
        release: {{ .Release.Name }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
{{- include "kubeapps.imagePullSecrets" . | indent 6 }}
      serviceAccountName: {{ template "kubeapps.kubeops.fullname" . }}
      # Increase termination timeout to let remaining operations to finish before killing the pods
      # This is because new releases/upgrades/deletions are synchronous operations
      terminationGracePeriodSeconds: 300
      {{- if .Values.kubeops.affinity }}
      affinity: {{- include "kubeapps.tplValue" (dict "value" .Values.kubeops.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubeops.nodeSelector }}
      nodeSelector: {{- include "kubeapps.tplValue" (dict "value" .Values.kubeops.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubeops.tolerations }}
      tolerations: {{- include "kubeapps.tplValue" (dict "value" .Values.kubeops.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      containers:
        - name: kubeops
          image: {{ template "kubeapps.image" (list .Values.kubeops.image .Values.global) }}
          imagePullPolicy: {{ .Values.kubeops.image.pullPolicy | quote }}
          command:
            - /kubeops
          args:
            - --user-agent-comment=kubeapps/{{ .Chart.AppVersion }}
            - --assetsvc-url=http://{{ template "kubeapps.assetsvc.fullname" . }}:{{ .Values.assetsvc.service.port }}
            {{- if .Values.clusters }}
            - --clusters-config-path=/config/clusters.conf
            {{- end }}
          {{- if .Values.clusters }}
          volumeMounts:
            - name: kubeops-config
              mountPath: /config
            - name: ca-certs
              mountPath: /etc/additional-clusters-cafiles
          {{- end }}
          env:
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          ports:
            - name: http
              containerPort: {{ .Values.kubeops.service.port }}
          {{- if .Values.kubeops.livenessProbe }}
          livenessProbe: {{- toYaml .Values.kubeops.livenessProbe | nindent 12 }}
          {{- end }}
          {{- if .Values.kubeops.readinessProbe }}
          readinessProbe: {{- toYaml .Values.kubeops.readinessProbe | nindent 12 }}
          {{- end }}
          {{- if .Values.kubeops.resources }}
          resources: {{- toYaml .Values.kubeops.resources | nindent 12 }}
          {{- end }}
      {{- if .Values.clusters }}
      volumes:
        - name: kubeops-config
          configMap:
            name: {{ template "kubeapps.kubeops-config.fullname" . }}
        - name: ca-certs
          emptyDir: {}
      {{- end }}
