apiVersion: apps/v1
kind: Deployment
metadata:
  name: sabinolabs
  namespace: sabino-labs
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sabinolabs
      version: "v1"
  template:
    metadata:
      labels:
        app: sabinolabs
        version: "v1"
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: app
                      operator: In
                      values:
                        - sabinolabs
                topologyKey: kubernetes.io/hostname
              weight: 100
      initContainers:
        - name: init-ds
          image: busybox:latest
          command:
            - "/bin/sh"
            - "-c"
            - |
              while true
              do
                rt=$(nc -z -w 1 sabinolabs-mysql 3306)
                if [ $? -eq 0 ]; then
                  echo "DB is UP"
                  break
                fi
                echo "DB is not yet reachable;sleep for 10s before retry"
                sleep 10
              done
      containers:
        - name: sabinolabs-app
          image: sabinolabs
          env:
            - name: SPRING_PROFILES_ACTIVE
              value: prod
            - name: SPRING_CLOUD_CONSUL_HOST
              value: consul.sabino-labs.svc.cluster.local
            - name: SPRING_CLOUD_CONSUL_PORT
              value: "8500"
            - name: SPRING_CLOUD_CONSUL_DISCOVERY_PREFER_IP_ADDRESS
              value: "false"
            - name: SPRING_CLOUD_CONSUL_DISCOVERY_HOSTNAME
              value: sabinolabs
            - name: SPRING_CLOUD_CONSUL_DISCOVERY_SERVICE_NAME
              value: sabinolabs
            - name: SPRING_DATASOURCE_URL
              value: jdbc:mysql://sabinolabs-mysql.sabino-labs.svc.cluster.local:3306/sabinolabs?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC&createDatabaseIfNotExist=true
            - name: SPRING_LIQUIBASE_URL
              value: jdbc:mysql://sabinolabs-mysql.sabino-labs.svc.cluster.local:3306/sabinolabs?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC&createDatabaseIfNotExist=true
            - name: MANAGEMENT_METRICS_EXPORT_PROMETHEUS_ENABLED
              value: "true"
            - name: SPRING_SLEUTH_PROPAGATION_KEYS
              value: "x-request-id,x-ot-span-context"
            - name: JAVA_OPTS
              value: " -Xmx256m -Xms256m"
            - name: SERVER_SHUTDOWN
              value: graceful
          resources:
            requests:
              memory: "512Mi"
              cpu: "500m"
            limits:
              memory: "1Gi"
              cpu: "1"
          ports:
            - name: http
              containerPort: 5004
          readinessProbe:
            httpGet:
              path: /management/health/readiness
              port: http
            initialDelaySeconds: 20
            periodSeconds: 15
            failureThreshold: 6
          livenessProbe:
            httpGet:
              path: /management/health/liveness
              port: http
            initialDelaySeconds: 120
