# Copyright (c) Facebook, Inc. and its affiliates.

# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: whatsapp-master-autoscaler
  namespace: default
spec:
  maxReplicas: 3
  minReplicas: 2
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: whatsapp-master-deployment
  targetCPUUtilizationPercentage: 60
---
apiVersion: v1
kind: Service
metadata:
  name: whatsapp-master-service
  namespace: default
spec:
  clusterIP: None
  ports:
  - port: 6250
    targetPort: 6250
    protocol: TCP
    name: messaging
  - port: 6251
    targetPort: 6251
    protocol: TCP
    name: contacts
  - port: 6252
    targetPort: 6252
    protocol: TCP
    name: control
  - port: 6253
    targetPort: 6253
    protocol: TCP
    name: healthcheck
  selector:
    name: whatsapp-master
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: whatsapp-master-deployment
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      name: whatsapp-master
  template:
    metadata:
      labels:
        name: whatsapp-master
    spec:
      restartPolicy: "Always"
      containers:
      - name: whatsapp-master
        image: docker.whatsapp.biz/coreapp:v$VERSION
        imagePullPolicy: IfNotPresent
        resources:
          limits:
            memory: 128Mi
          requests:
            cpu: "0.15"
            memory: 128Mi
        ports:
        - containerPort: 6250
          protocol: TCP
          name: messaging
        - containerPort: 6251
          protocol: TCP
          name: contacts
        - containerPort: 6252
          protocol: TCP
          name: control
        - containerPort: 6253
          protocol: TCP
          name: healthcheck
        env:
        - name: WA_DB_ENGINE
          valueFrom:
            secretKeyRef:
              name: whatsapp-config
              key: wa-db-engine
        - name: WA_TCP_LISTEN_ADDRESS
          value: 'any'
        - name: WA_APP_MULTICONNECT
          value: '1'
        - name: WA_CONFIG_ON_DB
          value: '1'
        - name: COREAPP_EXTERNAL_PORTS
          value: '6250,6251,6252,6253'
        - name: WA_MASTER_NODE
          value: '1'
        - name: WA_DB_HOSTNAME
          valueFrom:
            secretKeyRef:
              name: whatsapp-config
              key: wa-db-hostname
        - name: WA_DB_PORT
          valueFrom:
            secretKeyRef:
              name: whatsapp-config
              key: wa-db-port
        - name: WA_DB_USERNAME
          valueFrom:
            secretKeyRef:
              name: whatsapp-config
              key: wa-db-username
        - name: WA_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: whatsapp-config
              key: wa-db-password
        - name: COREAPP_HOSTNAME
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        volumeMounts:
        - name: whatsapp-media
          mountPath: /usr/local/wamedia
        securityContext:
          capabilities:
            drop:
              - MKNOD
      volumes:
      - name: whatsapp-media
        persistentVolumeClaim:
          claimName: media-volume-claim
