# Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved.
#
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
#
apiVersion: apps/v1
kind: Deployment
metadata:
  name: catalogue
  labels:
    name: catalogue
spec:
  replicas: 1
  selector:
    matchLabels:
      name: catalogue
  template:
    metadata:
      labels:
        name: catalogue
    spec:
      terminationGracePeriodSeconds: 5
      initContainers:
      - name: decode-wallet
        image: oraclelinux:8-slim
        command: ["/bin/sh","-c"]
        args: 
        - for i in `ls -1 /tmp/wallet | grep -v user_name`; do cat /tmp/wallet/$i  | base64 --decode > /wallet/$i; done; ls -l /wallet/*;
        volumeMounts:
        - name: wallet-raw
          mountPath: /tmp/wallet
          readOnly: false
        - name: wallet
          mountPath: /wallet
      - name: init-service-database
        image: iad.ocir.io/oracle/ateam/mushop-dbtools:21
        command: ["/bin/sh","-c"]
        args: 
        - sqlplus ADMIN/\"$( echo "$OADB_ADMIN_PW" | jq -r 'keys[0] as $k | "\(.[$k])"' )\"@$OADB_SERVICE @service.sql ${OADB_USER^^} $OADB_PW
        env:
          - name: OADB_USER
            valueFrom:
              secretKeyRef:
                name: catalogue-oadb-connection
                key: oadb_user
          - name: OADB_PW
            valueFrom:
              secretKeyRef:
                name: catalogue-oadb-connection
                key: oadb_pw
          - name: OADB_SERVICE
            valueFrom:
              secretKeyRef:
                name: catalogue-oadb-connection
                key: oadb_service
          - name: OADB_ADMIN_PW
            valueFrom:
              secretKeyRef:
                name: catalogue-oadb-admin
                key: oadb_admin_pw
        volumeMounts:
          - name: wallet
            mountPath: /usr/lib/oracle/21/client64/lib/network/admin/
            readOnly: true
          - name: service-sql
            mountPath: /work/
      containers:
        - name: catalogue
          image: iad.ocir.io/oracle/ateam/mushop-catalogue:1.3
          imagePullPolicy: Always
          command: ["/catalogue"]
          args:
          - -port=80
          ports:
          - containerPort: 80
          env:
          - name: ZIPKIN
            value: ""
          - name: OADB_USER
            valueFrom:
              secretKeyRef:
                name: catalogue-oadb-connection
                key: oadb_user
          - name: OADB_PW
            valueFrom:
              secretKeyRef:
                name: catalogue-oadb-connection
                key: oadb_pw
          - name: OADB_SERVICE
            valueFrom:
              secretKeyRef:
                name: catalogue-oadb-connection
                key: oadb_service
          securityContext:
            capabilities:
              drop:
                - all
              add:
                - NET_BIND_SERVICE
            readOnlyRootFilesystem: true
          livenessProbe:
            httpGet:
              path: /health
              port: 80
            initialDelaySeconds: 300
            periodSeconds: 3
          readinessProbe:
            httpGet:
              path: /health
              port: 80
            initialDelaySeconds: 180
            periodSeconds: 3          
          resources:
            requests:
              cpu: 100m
              memory: 64Mi
            limits:
              cpu: 200m
              memory: 128Mi
          volumeMounts:
          - name: wallet
            mountPath: /usr/lib/oracle/21/client64/lib/network/admin/
            readOnly: true
      volumes:
      - name: wallet-raw
        secret:
          secretName: catalogue-oadb-wallet-binding
      - name: wallet
        emptyDir: {}
      - name: service-sql
        configMap:
          name: catalogue-sql
          items:
          - key: catalogue.sql
            path: service.sql
