# Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
#
# 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: apps/v1
kind: Deployment
metadata:
  name: wso2apim
  namespace: wso2
spec:
  replicas: 1
  selector:
    matchLabels:
      deployment: wso2apim
  minReadySeconds: 30
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        deployment: wso2apim
    spec:
      initContainers:
        - name: init-wait-mysql
          image: alpine
          command: ["sh", "-c", "for i in $(seq 1 300); do nc -zvw1 wso2apim-with-analytics-rdbms-service 3306 && exit 0 || sleep 3; done; exit 1"]
        - name: init-wait-analytics-worker
          image: alpine
          command: ["sh", "-c", "for i in $(seq 1 300); do nc -zvw1 wso2apim-analytics-service 7712 && exit 0 || sleep 3; done; exit 1"]
        - name: init-wait-analytics-dashboard
          image: alpine
          command: ["sh", "-c", "for i in $(seq 1 300); do nc -zvw1 wso2apim-dashboard-analytics-service 9643 && exit 0 || sleep 3; done; exit 1"]
        - name: init-mysql-connector-download
          image: busybox:1.32
          command:
            - /bin/sh
            - "-c"
            - |
              set -e
              connector_version=8.0.17
              wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/${connector_version}/mysql-connector-java-${connector_version}.jar -P /mysql-connector-jar/
          volumeMounts:
            - name: mysql-connector-jar
              mountPath: /mysql-connector-jar
      containers:
        - name: wso2apim
          image: wso2/wso2am:3.2.0
          resources:
            limits:
              memory: "2Gi"
            requests:
              memory: "2Gi"
          livenessProbe:
            exec:
              command:
                - /bin/bash
                - -c
                - nc -z localhost 9443
            initialDelaySeconds: 240
            periodSeconds: 10
          readinessProbe:
            exec:
              command:
                - /bin/bash
                - -c
                - nc -z localhost 9443
            initialDelaySeconds: 240
            periodSeconds: 10
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8280
              protocol: "TCP"
            - containerPort: 8243
              protocol: "TCP"
            - containerPort: 9763
              protocol: "TCP"
            - containerPort: 9443
              protocol: "TCP"
            - containerPort: 5672
              protocol: "TCP"
            - containerPort: 9611
              protocol: "TCP"
            - containerPort: 9711
              protocol: "TCP"
          volumeMounts:
            - name: mysql-connector-jar
              mountPath: /home/wso2carbon/wso2-artifact-volume/repository/components/dropins
            - name: apim-conf
              mountPath: /home/wso2carbon/wso2-config-volume/repository/conf
      volumes:
        - name: mysql-connector-jar
          emptyDir: {}
        - name: apim-conf
          configMap:
            name: apim-conf