apiVersion: apps/v1
kind: Deployment
metadata:
  name: bike-share-regional-api
  namespace: mobility
  labels:
    app: bike-share-api
    region: greater-toronto
spec:
  replicas: 4
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 2
  selector:
    matchLabels:
      app: bike-share-api
  template:
    metadata:
      labels:
        app: bike-share-api
        region: greater-toronto
    spec:
      containers:
        - name: api
          image: registry.example.ca/mobility/bike-share-api:3.8.1
          ports:
            - name: http
              containerPort: 8080
          env:
            - name: REGION_ID
              value: ca-on-gta
            - name: STATION_REFRESH_INTERVAL
              value: "30s"
            - name: TRIP_RESERVATION_TTL
              value: "10m"
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: bike-share-database
                  key: url
            - name: REDIS_URL
              valueFrom:
                secretKeyRef:
                  name: bike-share-cache
                  key: url
          resources:
            requests:
              cpu: 250m
              memory: 256Mi
            limits:
              cpu: "1"
              memory: 768Mi
          readinessProbe:
            httpGet:
              path: /health/ready
              port: http
            initialDelaySeconds: 10
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /health/live
              port: http
            initialDelaySeconds: 30
            periodSeconds: 15
---
apiVersion: v1
kind: Service
metadata:
  name: bike-share-regional-api
  namespace: mobility
spec:
  type: ClusterIP
  selector:
    app: bike-share-api
  ports:
    - name: http
      port: 80
      targetPort: http
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: bike-share-regional-api
  namespace: mobility
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: bike-share-regional-api
  minReplicas: 4
  maxReplicas: 16
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 65