{{- if .Values.assetsvc.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "kubeapps.assetsvc.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: assetsvc
    {{- 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.assetsvc.replicaCount }}
  {{- if .Values.assetsvc.updateStrategy }}
  strategy: {{- toYaml .Values.assetsvc.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: assetsvc
  template:
    metadata:
      {{- if .Values.assetsvc.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: assetsvc
        {{- if .Values.assetsvc.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "kubeapps.imagePullSecrets" . | indent 6 }}
      {{- if .Values.assetsvc.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.assetsvc.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.assetsvc.podAffinityPreset "component" "assetsvc" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.assetsvc.podAntiAffinityPreset "component" "assetsvc" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.assetsvc.nodeAffinityPreset.type "key" .Values.assetsvc.nodeAffinityPreset.key "values" .Values.assetsvc.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.assetsvc.schedulerName }}
      schedulerName: {{ .Values.assetsvc.schedulerName }}
      {{- end }}
      {{- if .Values.assetsvc.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.assetsvc.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.assetsvc.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.assetsvc.priorityClassName }}
      priorityClassName: {{ .Values.assetsvc.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.assetsvc.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.assetsvc.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.assetsvc.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.initContainers "context" $) | trim | nindent 8 }}
      {{- end }}
      containers:
        - name: assetsvc
          image: {{ include "kubeapps.assetsvc.image" . }}
          imagePullPolicy: {{ .Values.assetsvc.image.pullPolicy | quote }}
          {{- if .Values.assetsvc.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.assetsvc.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.assetsvc.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.assetsvc.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /assetsvc
          {{- end }}
          {{- if .Values.assetsvc.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --database-user=postgres
            - --database-name={{ .Values.postgresql.auth.database }}
            - --database-url={{ printf "%s-hl:%d" (include "kubeapps.postgresql.host" .) (int (include "kubeapps.postgresql.port" .)) | quote }}
            {{- range .Values.assetsvc.extraFlags }}
            - {{ . }}
            {{- end }}
          {{- end }}
          env:
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: postgres-password
                  name: {{ include "kubeapps.postgresql.secretName" . }}
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: PORT
              value: {{ .Values.assetsvc.containerPorts.http | quote }}
            {{- if .Values.assetsvc.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.assetsvc.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.assetsvc.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.assetsvc.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.assetsvc.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.assetsvc.containerPorts.http }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.assetsvc.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.assetsvc.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /live
              port: http
          {{- else if .Values.assetsvc.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.assetsvc.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.assetsvc.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: http
          {{- else if .Values.assetsvc.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.assetsvc.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.assetsvc.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.assetsvc.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.assetsvc.resources }}
          resources: {{- toYaml .Values.assetsvc.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.assetsvc.extraVolumeMounts }}
          volumeMounts: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.assetsvc.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.sidecars "context" $) | trim | nindent 8 }}
        {{- end }}
      {{- if .Values.assetsvc.extraVolumes }}
      volumes: {{- include "common.tplvalues.render" (dict "value" .Values.assetsvc.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
{{- end }}
