---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
  namespace: contracts
  labels:
    app: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
      annotations:
        traffic.sidecar.istio.io/excludeOutboundPorts: "8200"
        vault.hashicorp.com/agent-init-first: "true"
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/agent-pre-populate-only: "true"
        vault.hashicorp.com/auth-path: auth/kubernetes
        vault.hashicorp.com/role: contracts
        vault.hashicorp.com/agent-inject-secret-contracts-db: secrets/data/postgresql/apps/contracts
        vault.hashicorp.com/agent-inject-template-contracts-db: |-
          {{- with secret "secrets/data/postgresql/apps/contracts" -}}
          DB_URL=postgresql://{{ index .Data.data "username" }}:{{ index .Data.data "password" }}@postgresql.contracts.svc.cluster.local:5432/contracts_db?sslmode=disable
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-contracts-jwt-public: secrets/data/vault/common/rsa_keys
        vault.hashicorp.com/agent-inject-template-contracts-jwt-public: |-
          {{- with secret "secrets/data/vault/common/rsa_keys" -}}
          {{ index .Data.data "public_key" }}
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-contracts-rabbitmq: secrets/data/rabbitmq/apps/contracts
        vault.hashicorp.com/agent-inject-template-contracts-rabbitmq: |-
          {{- with secret "secrets/data/rabbitmq/apps/contracts" -}}
          CONTRACTS_RABBITMQ_VHOST={{ index .Data.data "vhost" }}
          CONTRACTS_RABBITMQ_USERNAME={{ index .Data.data "username" }}
          CONTRACTS_RABBITMQ_PASSWORD={{ index .Data.data "password" }}
          CONTRACTS_RABBITMQ_HOST=rabbitmq.rabbitmq.svc.cluster.local
          CONTRACTS_RABBITMQ_PORT=5672
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-contracts-s3: secrets/data/minio/apps/contracts
        vault.hashicorp.com/agent-inject-template-contracts-s3: |-
          {{- with secret "secrets/data/minio/apps/contracts" -}}
          CONTRACTS_S3_ENDPOINT={{ index .Data.data.client "endpoint" }}
          CONTRACTS_S3_REGION={{ index .Data.data.client "region" }}
          CONTRACTS_S3_BUCKET=contracts
          CONTRACTS_S3_ACCESS_KEY_ID={{ index .Data.data "access_key" }}
          CONTRACTS_S3_SECRET_ACCESS_KEY={{ index .Data.data "secret_key" }}
          {{- end -}}
        vault.hashicorp.com/agent-inject-secret-contracts-kafka: secrets/data/kafka/apps/contracts
        vault.hashicorp.com/agent-inject-template-contracts-kafka: |-
          {{- with secret "secrets/data/kafka/apps/contracts" -}}
          CONTRACTS_KAFKA_BOOTSTRAP_SERVERS={{ index .Data.data.auth "bootstrap_servers" }}
          CONTRACTS_KAFKA_SECURITY_PROTOCOL={{ index .Data.data.auth "security_protocol" }}
          CONTRACTS_KAFKA_SASL_MECHANISM={{ index .Data.data.auth "sasl_mechanism" }}
          CONTRACTS_KAFKA_USERNAME={{ index .Data.data "username" }}
          CONTRACTS_KAFKA_PASSWORD={{ index .Data.data "password" }}
          {{- end -}}
    spec:
      serviceAccountName: contracts-vault
      containers:
        - name: backend
          image: cr.yandex/crp3ccidau046kdj8g9q/contracts:prod_d3bbd9fc
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh", "-ec"]
          args:
            - |
              set -a
              [ -f /vault/secrets/contracts-db ] && . /vault/secrets/contracts-db
              [ -f /vault/secrets/contracts-jwt-public ] && export PUBLIC_KEY="$(cat /vault/secrets/contracts-jwt-public)"
              [ -f /vault/secrets/contracts-rabbitmq ] && . /vault/secrets/contracts-rabbitmq
              [ -f /vault/secrets/contracts-s3 ] && . /vault/secrets/contracts-s3
              [ -f /vault/secrets/contracts-kafka ] && . /vault/secrets/contracts-kafka
              set +a
              exec /usr/local/bin/http
          ports:
            - name: http
              containerPort: 8000
              protocol: TCP
          env:
            - name: ADDRESS
              value: ":8000"
            - name: ENABLE_SSL
              value: "false"
      imagePullSecrets:
        - name: regcred
