---
apiVersion: v1
kind: Service
metadata:
  name: {{ .NAME }}
  namespace: {{ .NAMESPACE }}
  labels:
    app: {{ .NAME }}
spec:
  selector:
    app: {{ .NAME }}
  ports:
    - port: 80
      targetPort: 8000
      name: {{ .NAME }}
      protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .NAME }}
  namespace: {{ .NAMESPACE }}
spec:
  replicas: {{ .REPLICAS }}
  selector:
    matchLabels:
      app: {{ .NAME }}
  template:
    metadata:
      labels:
        app: {{ .NAME }}
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "8000"
    spec:
      containers:
        - name: {{ .NAME }}
          env:
            - name: ENVIRONMENT
              value: {{ .ENVIRONMENT }}
            - name: MONITORING_PORT
              value: "8000"
            - name: LOG_LEVEL
              value: "INFO"
            - name: MONGODB_URI
              valueFrom:
                secretKeyRef:
                  name: mongodb
                  key: mongo-uri
            - name: MONGODB_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: config
                  key: mongo-database
            - name: PIPELINE_SQS_URL
              valueFrom:
                configMapKeyRef:
                  name: tx-tracker
                  key: pipeline-sqs-url
            - name: NOTIFICATIONS_SQS_URL
              valueFrom:
                configMapKeyRef:
                  name: tx-tracker
                  key: notifications-sqs-url
            - name: AWS_REGION
              valueFrom:
                configMapKeyRef:
                  name: tx-tracker
                  key: aws-region
            - name: P2P_NETWORK
              value: {{ .P2P_NETWORK }}
            - name: METRICS_ENABLED
              value: "{{ .METRICS_ENABLED }}"
            - name: RPC_PROVIDER_PATH
              value: "/opt/tx-tracker/rpc-provider.json"
            - name: CONSUMER_WORKERS_SIZE
              value: "1"
          image: {{ .IMAGE_NAME }}
          imagePullPolicy: Always
          livenessProbe:
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 2
            failureThreshold: 4
            httpGet:
              path: /api/health
              port: 8000
          readinessProbe:
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 1
            failureThreshold: 2
            httpGet:
              path: /api/ready
              port: 8000
          resources:
            limits:
              cpu: {{ .RESOURCES_LIMITS_CPU }}
              memory: {{ .RESOURCES_LIMITS_MEMORY }}
            requests:
              cpu: {{ .RESOURCES_REQUESTS_CPU }}
              memory: {{ .RESOURCES_REQUESTS_MEMORY }}
          volumeMounts:
            - name: tx-tracker-config
              mountPath: /opt/tx-tracker
      restartPolicy: Always
      serviceAccountName: tx-tracker
      terminationGracePeriodSeconds: 45
      volumes:
        - name: tx-tracker-config
          secret:
            secretName: rpc-provider
            items:
            - key: rpc-provider.json
              path: rpc-provider.json