{{- if .Values.enabled }}
{{- $namespace := .Release.Namespace -}}
{{- $scope := $ -}}
{{- range .Values.application.manifest }}
{{- if .enabled -}}
{{- $applicationName := .name | trunc 63 }}
{{- $applicationShortName := trimPrefix "cmii-uav-platform" $applicationName }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $applicationName }}
  namespace: {{ $namespace }}
  labels:
    cmii.type: {{ $.Values.application.type }}
    cmii.app: {{ $applicationName }}
    helm.sh/chart: {{ include "frontend-app.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:
  replicas: {{ $.Values.replicaCount }}
  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:
      imagePullSecrets:
        - name: harborsecret
      securityContext:
        {{- toYaml $.Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ $applicationName }}
          image: "{{ default $.Values.global.image.repository }}/{{ $applicationName }}:{{ .tag }}"
          imagePullPolicy: {{ default $.Values.global.image.pullPolicy }}
          env:
            - name: K8S_NAMESPACE
              value: {{ $namespace }}
            - name: APPLICATION_NAME
              value: {{ $applicationName }}
          ports:
            - name: platform-9528
              containerPort: 9528
              protocol: TCP
          resources:
            limits:
              cpu: "1"
              memory: 1Gi
            requests:
              cpu: 500m
              memory: 500Mi
          volumeMounts:
            - name: nginx-conf
              mountPath: /usr/local/nginx/conf/nginx.conf
              subPath: nginx.conf
            - name: default-nginx-conf
              mountPath: /etc/nginx/conf.d/default.conf
              subPath: default.conf
            - name: tenant-prefix
              subPath: ingress-config.js
              mountPath: /home/cmii-platform/dist/ingress-config.js
      volumes:
        - name: nginx-conf
          configMap:
            name: nginx-cm
            items:
              - key: nginx.conf
                path: nginx.conf
        - name: default-nginx-conf
          configMap:
            name: default-nginx-cm
            items:
              - key: default.conf
                path: default.conf
        - name: tenant-prefix
          configMap:
            {{- if eq $applicationShortName "" }}
            name: tenant-prefix-platform
            {{- else if eq $applicationShortName "-hyperspectral" }}
            name: tenant-prefix-hyper
            {{- else }}
            name: tenant-prefix{{ $applicationShortName }}
            {{- end }}
            items:
              - key: ingress-config.js
                path: ingress-config.js
{{- end }}
{{- end }}
{{- end }}
