apiVersion: apps/v1
kind: Deployment
metadata:
  name: centralledger-service
  labels:
    app.kubernetes.io/name: centralledger-service
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: centralledger-service
  template:
    metadata:
      labels:
        app.kubernetes.io/name: centralledger-service
    spec:
      containers:
      - command:
        - node
        - src/api/index.js
        env:
        - name: LOG_FILTER
          value: error, warn, audit, trace, info, perf, verbose, debug, silly
        - name: CSL_LOG_TRANSPORT
          value: console
        - name: EVENT_SDK_SIDECAR_DISABLED
          value: "true"
        - name: CLEDG_DATABASE__SCHEMA
          valueFrom:
            configMapKeyRef:
              name: centralledger-mysql
              key: MYSQL_DATABASE
        - name: CLEDG_DATABASE__USER
          valueFrom:
            configMapKeyRef:
              name: centralledger-mysql
              key: MYSQL_USER
        - name: CLEDG_DATABASE__HOST
          valueFrom:
            configMapKeyRef:
              name: centralledger-mysql
              key: MYSQL_SERVICE
        - name: CLEDG_DATABASE__PASSWORD
          valueFrom:
            secretKeyRef:
              name: centralledger-mysql
              key: MYSQL_PASSWORD
        - name: LIB_RESOURCE_VERSIONS
          value: transfers=1.1,participants=1.0,quotes=1.0
        envFrom:
        - configMapRef:
            name: centralledger-kafka
        image: mojaloop/central-ledger:v13.14.3
        imagePullPolicy: Always
        livenessProbe:
          httpGet:
            path: /health
            port: 3001
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 15
        name: centralledger-service
        ports:
        - containerPort: 3001
          name: http-api
          protocol: TCP
        readinessProbe:
          httpGet:
            path: /health
            port: 3001
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 15
      initContainers:
      - command:
        - sh
        - -c
        - until mysql -h "$CLEDG_DATABASE__HOST" -P 3306 -u "$CLEDG_DATABASE__USER"
          --password="$CLEDG_DATABASE__PASSWORD" "$CLEDG_DATABASE__SCHEMA" -e 'select version()';
          do echo waiting for MySQL; sleep 2; done;
        image: mysql:latest
        imagePullPolicy: Always
        name: wait-for-mysql
        env:
        - name: CLEDG_DATABASE__SCHEMA
          valueFrom:
            configMapKeyRef:
              name: centralledger-mysql
              key: MYSQL_DATABASE
        - name: CLEDG_DATABASE__USER
          valueFrom:
            configMapKeyRef:
              name: centralledger-mysql
              key: MYSQL_USER
        - name: CLEDG_DATABASE__HOST
          valueFrom:
            configMapKeyRef:
              name: centralledger-mysql
              key: MYSQL_SERVICE
        - name: CLEDG_DATABASE__PASSWORD
          valueFrom:
            secretKeyRef:
              name: centralledger-mysql
              key: MYSQL_PASSWORD
      - command:
        - sh
        - -c
        - npm run migrate
        env:
        - name: CLEDG_MIGRATIONS__RUN_DATA_MIGRATIONS
          value: "true"
        - name: CLEDG_DATABASE__SCHEMA
          valueFrom:
            configMapKeyRef:
              name: centralledger-mysql
              key: MYSQL_DATABASE
        - name: CLEDG_DATABASE__USER
          valueFrom:
            configMapKeyRef:
              name: centralledger-mysql
              key: MYSQL_USER
        - name: CLEDG_DATABASE__HOST
          valueFrom:
            configMapKeyRef:
              name: centralledger-mysql
              key: MYSQL_SERVICE
        - name: CLEDG_DATABASE__PASSWORD
          valueFrom:
            secretKeyRef:
              name: centralledger-mysql
              key: MYSQL_PASSWORD
        image: mojaloop/central-ledger:v13.14.3
        imagePullPolicy: Always
        name: run-migration
      restartPolicy: Always
