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: "{{ .API_PORT }}"
    spec:
      hostNetwork: true
      restartPolicy: Always
      terminationGracePeriodSeconds: 40
      serviceAccountName: fly
      initContainers:
      - name: init-fly
        image: busybox:1.28
        command: [ "sh", "-c", "sysctl -w net.core.rmem_max=2500000" ]
        securityContext:
            privileged: true
      containers:
        - name: {{ .NAME }}
          image: {{ .IMAGE_NAME }}
          imagePullPolicy: Always
          securityContext:
            privileged: true
          readinessProbe:
            initialDelaySeconds: 30
            periodSeconds: 20
            timeoutSeconds: 3
            failureThreshold: 3
            httpGet:
              path: /api/ready
              port: {{ .API_PORT }}
          livenessProbe:
            initialDelaySeconds: 30
            periodSeconds: 30
            timeoutSeconds: 3
            failureThreshold: 3
            httpGet:
              path: /api/health
              port: {{ .API_PORT }}
          env:
            - name: ENVIRONMENT
              value: {{ .ENVIRONMENT }}
            - name: API_PORT
              value: "{{ .API_PORT }}"
            - name: MONGODB_URI
              valueFrom:
                secretKeyRef:
                  name: mongodb
                  key: mongo-uri
            - name: MONGODB_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: config
                  key: mongo-database
            - name: P2P_NETWORK
              value: {{ .P2P_NETWORK }}
            - name: P2P_PORT
              value: "{{ .P2P_PORT }}"
            - name: PPROF_ENABLED
              value: "{{ .PPROF_ENABLED }}"
            - name: SQS_URL
              value: {{ .SQS_URL }}
            - name: AWS_REGION
              value: {{ .SQS_AWS_REGION }}
            - name: REDIS_URI
              valueFrom:
                configMapKeyRef:
                  name: config
                  key: redis-uri
            - name: REDIS_PREFIX
              valueFrom:
                configMapKeyRef:
                  name: config
                  key: redis-prefix
            - name: REDIS_VAA_CHANNEL
              value: "{{ .REDIS_VAA_CHANNEL }}"
            - name: MAX_HEALTH_TIME_SECONDS
              value: "{{ .MAX_HEALTH_TIME_SECONDS }}"
            - name: ALERT_API_KEY
              valueFrom:
                secretKeyRef:
                  name: opsgenie
                  key: api-key
            - name: ALERT_ENABLED
              value: "{{ .ALERT_ENABLED }}"
            - name: METRICS_ENABLED
              value: "{{ .METRICS_ENABLED }}"
            - name: OBSERVATIONS_CHANNEL_SIZE
              value: "{{ .OBSERVATIONS_CHANNEL_SIZE }}"
            - name: VAAS_CHANNEL_SIZE
              value: "{{ .VAAS_CHANNEL_SIZE }}"
            - name: HEARTBEATS_CHANNEL_SIZE
              value: "{{ .HEARTBEATS_CHANNEL_SIZE }}"
            - name: GOVERNOR_CONFIG_CHANNEL_SIZE
              value: "{{ .GOVERNOR_CONFIG_CHANNEL_SIZE }}"
            - name: GOVERNOR_STATUS_CHANNEL_SIZE
              value: "{{ .GOVERNOR_STATUS_CHANNEL_SIZE }}"
          resources:
            limits:
              memory: {{ .RESOURCES_LIMITS_MEMORY }}
              cpu: {{ .RESOURCES_LIMITS_CPU }}
            requests:
              memory: {{ .RESOURCES_REQUESTS_MEMORY }}
              cpu: {{ .RESOURCES_REQUESTS_CPU }}
      tolerations:
        - key: "dedicated"
          operator: "Equal"
          value: "wormholescan"
          effect: "NoSchedule"
        - key: "dedicated"
          operator: "Equal"
          value: "wormholescan"
          effect: "NoExecute"
      nodeSelector:
        eks.amazonaws.com/nodegroup-selector: "wormholescan"
