# Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
  name: dlt-config
data:
  CHANNEL_NAME: "channel1"
  CHAINCODE_NAME: "adrenalineDLT"
  MSP_ID: "Org1MSP"
  PEER_ENDPOINT: "10.1.1.96:7051"  #Change to required peer#
  PEER_HOST_ALIAS: "peer0.org1.adrenaline.com"
  CRYPTO_PATH: "/test-network/organizations/peerOrganizations/org1.adrenaline.com"
  KEY_DIRECTORY_PATH: "/test-network/organizations/peerOrganizations/org1.adrenaline.com/users/User1@org1.adrenaline.com/msp/keystore"
  CERT_DIRECTORY_PATH: "/test-network/organizations/peerOrganizations/org1.adrenaline.com/users/User1@org1.adrenaline.com/msp/signcerts"
  TLS_CERT_PATH: "/test-network/organizations/peerOrganizations/org1.adrenaline.com/peers/peer0.org1.adrenaline.com/tls/ca.crt"

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: dltservice
spec:
  selector:
    matchLabels:
      app: dltservice
  template:
    metadata:
      labels:
        app: dltservice
    spec:
      terminationGracePeriodSeconds: 5
      containers:
      - name: connector
        image: labs.etsi.org:5050/tfs/controller/dlt-connector:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
        - containerPort: 9192
        env:
        - name: LOG_LEVEL
          value: "INFO"
          ## for debug purposes
          #- name: DLT_GATEWAY_HOST
          #  value: "mock-blockchain.tfs-bchain.svc.cluster.local"
          #- name: DLT_GATEWAY_PORT
          #  value: "50051"
        readinessProbe:
          exec:
            command: ["/bin/grpc_health_probe", "-addr=:8080"]
        livenessProbe:
          exec:
            command: ["/bin/grpc_health_probe", "-addr=:8080"]
        resources:
          requests:
            cpu: 50m
            memory: 64Mi
          limits:
            cpu: 500m
            memory: 512Mi
      - name: gateway
        image: labs.etsi.org:5050/tfs/controller/dlt-gateway:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 50051
        resources:
          requests:
            cpu: 200m
            memory: 512Mi
          limits:
            cpu: 700m
            memory: 1024Mi
        volumeMounts:
        - mountPath: /test-network
          name: dlt-volume
          readOnly: true
        env:
        - name: CHANNEL_NAME
          valueFrom:
            configMapKeyRef:
              name: dlt-config
              key: CHANNEL_NAME
        - name: CHAINCODE_NAME
          valueFrom:
            configMapKeyRef:
              name: dlt-config
              key: CHAINCODE_NAME
        - name: MSP_ID
          valueFrom:
            configMapKeyRef:
              name: dlt-config
              key: MSP_ID
        - name: PEER_ENDPOINT
          valueFrom:
            configMapKeyRef:
              name: dlt-config
              key: PEER_ENDPOINT
        - name: PEER_HOST_ALIAS
          valueFrom:
            configMapKeyRef:
              name: dlt-config
              key: PEER_HOST_ALIAS
        - name: CRYPTO_PATH
          valueFrom:
            configMapKeyRef:
              name: dlt-config
              key: CRYPTO_PATH
        - name: KEY_DIRECTORY_PATH
          valueFrom:
            configMapKeyRef:
              name: dlt-config
              key: KEY_DIRECTORY_PATH
        - name: CERT_DIRECTORY_PATH
          valueFrom:
            configMapKeyRef:
              name: dlt-config
              key: CERT_DIRECTORY_PATH
        - name: TLS_CERT_PATH
          valueFrom:
            configMapKeyRef:
              name: dlt-config
              key: TLS_CERT_PATH
      volumes:
      - name: dlt-volume
        persistentVolumeClaim:
          claimName: dlt-pvc

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dlt-pvc
spec:
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Gi

---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: dlt-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadOnlyMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: "/home/ubuntu/fabric-samples/test-network"  #Update to correct host paths where the MSP is located.
  claimRef:
    name: dlt-pvc

---

apiVersion: v1
kind: Service
metadata:
  name: gatewayservice
spec:
  selector:
    app: dltservice
  ports:
    - protocol: TCP
      port: 50051
      targetPort: 50051
      nodePort: 32001
  type: NodePort

---

apiVersion: v1
kind: Service
metadata:
  name: dltservice
  labels:
    app: dltservice
spec:
  type: ClusterIP
  selector:
    app: dltservice
  ports:
    - name: grpc
      protocol: TCP
      port: 8080
      targetPort: 8080
    - name: metrics
      protocol: TCP
      port: 9192
      targetPort: 9192
