apiVersion: apps/v1
kind: Deployment
metadata:
  name: "{{ template "app.name" . }}"
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "app.labels" . | nindent 4 }}
    app.kubernetes.io/instance: {{ include "app.revision" . }}
    app.kubernetes.io/part-of: rse
spec:
  replicas: {{ .Values.replicaCount }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      {{- include "app.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "app.labels" . | nindent 8 }}
      annotations:
        {{- include "app.metricsAnnotations" . | nindent 8 }}
{{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
    spec:
      securityContext:
{{- include "app.securitycontext" . | nindent 8 }}
      imagePullSecrets:
        - name: {{ .Values.image.pullSecrets }}
      containers:
      - name: {{ template "app.name" . }}
        image: "{{ .Values.image.repository }}/{{ .Values.image.name }}:{{ default .Chart.AppVersion .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
          - name: PORT
            value: {{ .Values.service.port | quote }}
          - name: DATABASE_URL
            value: {{ template "app.postgresql.connectionstring" (merge (dict "application" "true") .) }}
          - name: NATS_URL
            value: "{{ .Values.ssiAbstraction.nats.protocol }}://{{ .Values.ssiAbstraction.nats.url }}:{{ .Values.ssiAbstraction.nats.port }}"
          - name: ECSURL
            value: "{{ .Values.ssiAbstraction.elastic.protocol }}://{{ .Values.ssiAbstraction.elastic.url }}:{{ .Values.ssiAbstraction.elastic.port }}/"
          - name: AGENT_HOST
            value: "{{ .Values.ssiAbstraction.agent.protocol }}://{{ .Values.ssiAbstraction.agent.host }}"
          - name: AGENT_URL_PATH
            value: {{ .Values.ssiAbstraction.agent.urlPath }}
          - name: AGENT_NAME
            value: {{ .Values.ssiAbstraction.agent.name }}
          - name: AGENT_INBOUND_PORT
            value: ":{{ .Values.ssiAbstraction.agent.inboundPort }}"
          - name: AGENT_PUBLIC_DID_SEED
            value: {{ .Values.ssiAbstraction.agent.publicDidSeed  | quote }}
          - name: AGENT_AUTO_ACCEPT_CONNECTION
            value: {{ .Values.ssiAbstraction.agent.autoAccept.connection  | quote }}
          - name: AGENT_AUTO_ACCEPT_CREDENTIAL
            value: {{ .Values.ssiAbstraction.agent.autoAccept.credential  | quote }}
          - name: AGENT_LEDGER_ID
            value: {{ .Values.ssiAbstraction.agent.ledgerId  | quote }}
          - name: LOG_LEVEL
            value: {{ .Values.log.level | default "INFO" }}
          - name: LOG_ENCODING
            value: {{ .Values.log.encoding | default "json" }}
          - name: AGENT_WALLET_KEY
            value: {{ .Values.ssiAbstraction.agent.wallet.key  | quote }}
          - name: AGENT_WALLET_ID
            value: {{ .Values.ssiAbstraction.agent.wallet.id  | quote }}
{{- if .Values.extraVars }}
{{ toYaml .Values.extraVars | indent 8 }}
{{- end }}
        ports:
        {{- if .Values.metrics.enabled }}
        - name: monitoring
          containerPort: {{ .Values.metrics.port }}
        {{- end }}
        - name: http
          containerPort: {{ .Values.service.port }}
        - name: peer
          containerPort: {{ .Values.ssiAbstraction.agent.peerPort }}
        readinessProbe:
          httpGet:
            path: /v1/health
            port: {{ .Values.service.port }}
          initialDelaySeconds: 5
          periodSeconds: 5
          successThreshold: 2
          failureThreshold: 2
          timeoutSeconds: 5
        resources:
{{ toYaml .Values.resources | indent 10 }}
