apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.name }}-deployment
  labels:
    app: {{ .Values.name }}
    version: {{ .Values.version }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Values.name }}
      version: {{ .Values.version }}
  strategy:
    type: RollingUpdate
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "true"
        traffic.sidecar.istio.io/excludeOutboundPorts: '5432,6379,9093'
      labels:
        app: {{ .Values.name }}
        version: {{ .Values.version }}
        collect.logs: "json"
    spec:
      serviceAccountName: {{ .Values.name }}
      containers:
        - name: {{ .Values.name }}
          image: "{{ .Values.image.name }}:{{ .Values.image.version }}"
          imagePullPolicy: Always
          ports:
            - containerPort: {{ .Values.port }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: {{ .Values.probes.liveness.path }}
              port: {{ .Values.port }}
            failureThreshold: 1
            initialDelaySeconds: 180
            periodSeconds: 20
            successThreshold: 1
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: {{ .Values.probes.readiness.path }}
              port: {{ .Values.port }}
            failureThreshold: 5
            initialDelaySeconds: 60
            periodSeconds: 20
            successThreshold: 1
            timeoutSeconds: 5
          {{- if not .Values.global.disableRequestsLimits }}
          resources:
            limits:
              cpu: 400m
              memory: 800Mi
            requests:
              cpu: 400m
              memory: 800Mi
          {{- end }}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /app/config/config.yaml
              name: config
              readOnly: true
              subPath: config.yaml
            - mountPath: /app/secrets
              name: db-credentials
              readOnly: true
            - mountPath: /app/secrets/datafactoryexcerptceph
              name: {{ .Chart.Name }}-datafactory-excerpt-ceph-bucket-credentials
              readOnly: true
            - mountPath: /app/secrets/excerptsignatureceph
              name: {{ .Chart.Name }}-excerpt-signature-ceph-bucket-credentials
              readOnly: true
            - name: {{ .Chart.Name }}-redis-credentials
              mountPath: /app/secrets/redis
              readOnly: true
          env:
            - name: JAVA_OPTS
              value: {{ .Values.java.javaOpts }}
            - name: DB_URL
              value: {{ .Values.db.url }}
            - name: DB_NAME
              value: {{ .Values.db.name }}
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.db.secret }}
                  key: excerptSvcName
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.db.secret }}
                  key: excerptSvcPass
            - name: DATAFACTORY_EXCERPT_CEPH_BUCKET_NAME
              valueFrom:
                configMapKeyRef:
                  name: {{ .Values.datafactoryExcerptCeph.bucketName }}
                  key: BUCKET_NAME
            - name: DATAFACTORY_EXCERPT_CEPH_BUCKET_HOST
              valueFrom:
                configMapKeyRef:
                  name: {{ .Values.datafactoryExcerptCeph.bucketName }}
                  key: BUCKET_HOST
            - name: EXCERPT_SIGNATURE_CEPH_BUCKET_NAME
              valueFrom:
                configMapKeyRef:
                  name: {{ .Values.excerptSignatureCeph.bucketName }}
                  key: BUCKET_NAME
            - name: EXCERPT_SIGNATURE_CEPH_BUCKET_HOST
              valueFrom:
                configMapKeyRef:
                  name: {{ .Values.excerptSignatureCeph.bucketName }}
                  key: BUCKET_HOST
            - name: KAFKA_USER_KEYSTORE_CERTIFICATE
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.kafka.user }}
                  key: user.crt
            - name: KAFKA_USER_KEYSTORE_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.kafka.user }}
                  key: user.key
            - name: KAFKA_CLUSTER_TRUSTSTORE
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.kafka.clusterName }}-cluster-ca-cert
                  key: ca.crt
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.redis.secretName }}
                  key: password
      initContainers:
        - name: init-db
          image: {{ template "imageRegistry" . -}} busybox
          imagePullPolicy: IfNotPresent
          command: ["sh", "-c", "while ! nc -w 1 {{ .Values.db.url }} {{ .Values.db.port }} </dev/null; do echo waiting for {{ .Values.db.url }}; sleep 10; done;"]
        - name: init-kafka
          image: {{ template "imageRegistry" . -}} busybox
          imagePullPolicy: IfNotPresent
          command: [ "sh", "-c", "while ! nc -w 1 {{ .Values.kafka.url }} {{ .Values.kafka.port }} </dev/null; do echo waiting for {{ .Values.kafka.url }}; sleep 10; done;" ]
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
        - name: config
          configMap:
            name: excerpt-service-api-properties
        - name: db-credentials
          secret:
            secretName: {{ .Values.db.secret }}
            items:
              - key: excerptSvcPass
                path: data-platform.datasource.password
              - key: excerptSvcName
                path: data-platform.datasource.username
        - name: {{ .Chart.Name }}-datafactory-excerpt-ceph-bucket-credentials
          secret:
            secretName: {{ .Values.datafactoryExcerptCeph.bucketName }}
            items:
              - key: AWS_ACCESS_KEY_ID
                path: datafactory-excerpt-ceph.access-key
              - key: AWS_SECRET_ACCESS_KEY
                path: datafactory-excerpt-ceph.secret-key
        - name: {{ .Chart.Name }}-excerpt-signature-ceph-bucket-credentials
          secret:
            secretName: {{ .Values.excerptSignatureCeph.bucketName }}
            items:
              - key: AWS_ACCESS_KEY_ID
                path: excerpt-signature-ceph.access-key
              - key: AWS_SECRET_ACCESS_KEY
                path: excerpt-signature-ceph.secret-key
        - name: {{ .Chart.Name }}-redis-credentials
          secret:
            secretName: {{ .Values.redis.secretName }}
            items:
              - key: password
                path: redis.password
