{{- $namespace := .Release.Namespace -}}
  {{- $scope := $ -}}
  {{- range .Values.application.manifest }}
  {{- if .enabled -}}
  {{- $applicationName := .name | trunc 63 }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $applicationName }}
  namespace: {{ $namespace }}
  labels:
    cmii.type: {{ $.Values.application.type }}
    {{- if .modules  }}
    cmii.modules: {{ .modules }}
    {{- end }}
    cmii.app: {{ $applicationName }}
    helm.sh/chart: {{ include "uavcloud-be-core.chart" $scope }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
    app.kubernetes.io/chart-version: {{ $.Chart.AppVersion | quote }}
    app.kubernetes.io/app-version: {{ $.Values.global.image.tag | quote }}
spec:
  {{- if not $.Values.autoscaling.enabled }}
  replicas: {{ $.Values.replicaCount }}
  {{- end }}
  strategy:
    rollingUpdate:
      maxUnavailable: 1
  selector:
    matchLabels:
      cmii.type: {{ $.Values.application.type }}
      cmii.app: {{ $applicationName }}
  template:
    metadata:
      {{- with $.Values.podAnnotations }}
      annotations:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        cmii.type: {{ $.Values.application.type }}
        cmii.app: {{ $applicationName }}
    spec:
      {{- if $.Values.global.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" $.Values.global.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        {{- /*        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.global.podAffinityPreset "context" $) | nindent 10 -}}*/}}
        {{- /*        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.global.podAntiAffinityPreset "context" $) | nindent 10 }}*/}}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" $.Values.global.nodeAffinityPreset.type "key" $.Values.global.nodeAffinityPreset.key "values" $.Values.global.nodeAffinityPreset.values) | nindent 10 -}}
      {{- end }}
      imagePullSecrets:
        - name: harborsecret
      securityContext:
      {{- toYaml $.Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ $applicationName }}
          image: "{{ $.Values.global.image.repository  }}/{{ $applicationName }}:{{ .tag }}"
          imagePullPolicy: {{ $.Values.global.image.pullPolicy }}
          env:
            - name: K8S_NAMESPACE
              value: "{{ $namespace }}"
            - name: APPLICATION_NAME
              value: "{{ $applicationName }}"
            - name: CUST_JAVA_OPTS
              value: "{{ $.Values.env.CUST_JAVA_OPTS }}"
            - name: NACOS_REGISTRY
              value: "{{ $.Values.env.NACOS_REGISTRY }}"
            - name: NACOS_DISCOVERY_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: NACOS_DISCOVERY_PORT
              value: "8080"
            - name: BIZ_CONFIG_GROUP
            - name: SYS_CONFIG_GROUP
            - name: IMAGE_VERSION
              value: {{ .tag }}
            - name: NACOS_USERNAME
              value: {{ $.Values.env.NACOS_USERNAME | quote }}
            - name: NACOS_PASSWORD
              value: {{ $.Values.env.NACOS_PASSWORD | quote }}
          ports:
            - name: pod-port
              containerPort: 8080
              protocol: TCP
          {{- if .config }}
          {{- if .config.resources }}
          resources:
          {{- toYaml .config.resources | nindent 12 }}
          {{- end }}
          {{- else }}
          {{- include "uavcloud-be-core.resources" $scope | nindent 10 }}
          {{- end }}
          {{- include "uavcloud-be-core.healthProbe" $scope | nindent 10 }}
          volumeMounts:
            - name: glusterfs-backend-log-volume
              mountPath: /cmii/logs
              readOnly: false
              subPath: {{ $namespace }}/{{ $applicationName }}
            {{- if .config }}
            {{- if .config.pvc }}
            - name: data-cache-volume
              mountPath: {{ .config.pvc.cache.path }}
              readOnly: false
              subPath: {{ $namespace }}/{{ $applicationName }}
            {{- end }}
        {{- end }}
      {{- /*{{- if $.Values.sidecar.proxysql.enabled }}
        - name: proxysql
          image: "{{ $.Values.global.image.repository | default $.Values.sidecar.proxysql.image.repository }}:{{ $.Values.sidecar.proxysql.image.tag }}"
          imagePullPolicy: {{ $.Values.sidecar.proxysql.image.pullPolicy }}
          ports:
            - name: proxysql
              containerPort: {{ $.Values.sidecar.proxysql.containerPort.proxysql }}
              protocol: TCP
            - name: admin
              containerPort: {{ $.Values.sidecar.proxysql.containerPort.admin }}
              protocol: TCP
          volumeMounts:
            - name: proxysql-config
              mountPath: /usr/local/bin/probe-proxysql.bash
              subPath: probe-proxysql.bash
            - name: proxysql-config
              mountPath: /etc/proxysql.cnf
              subPath: proxysql.cnf
          livenessProbe:
            exec:
              command: ["/bin/bash","/usr/local/bin/probe-proxysql.bash"]
          readinessProbe:
            exec:
              command: ["/bin/bash","/usr/local/bin/probe-proxysql.bash"]
          resources:
            {{- toYaml $.Values.sidecar.proxysql.resources | nindent 12 }}
        {{- end }}*/}}
      volumes:
        - name: glusterfs-backend-log-volume
          persistentVolumeClaim:
            claimName: glusterfs-backend-log-pvc
        {{- if .config }}
        {{- if .config.pvc }}
        - name: data-cache-volume
          persistentVolumeClaim:
            claimName: {{ $applicationName }}-cache
        {{- end }}
        {{- end }}
  {{- /*{{- if $.Values.sidecar.proxysql.enabled }}
        - name: proxysql-config
          configMap:
            name: {{ $.Values.sidecar.proxysql.configmap }}
        {{- end }}*/}}
---
{{- end }}
  {{- end }}
