apiVersion: apps/v1
kind: Deployment
metadata:
  name: bikeshare-api
  namespace: mobility
  labels:
    app: bikeshare-api
    region: greater-toronto
spec:
  replicas: 3
  selector:
    matchLabels:
      app: bikeshare-api
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: bikeshare-api
        region: greater-toronto
    spec:
      containers:
        - name: api
          image: registry.example.ca/mobility/bikeshare-api:2.8.1
          ports:
            - name: http
              containerPort: 8080
          env:
            - name: APP_ENV
              value: production
            - name: NETWORK_ID
              value: gta-bikeshare
            - name: SERVICE_AREA
              value: toronto-waterfront
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: bikeshare-api-secrets
                  key: database-url
            - name: REDIS_URL
              valueFrom:
                secretKeyRef:
                  name: bikeshare-api-secrets
                  key: redis-url
            - name: GBFS_REFRESH_INTERVAL_SECONDS
              value: "30"
            - name: RESERVATION_TTL_SECONDS
              value: "600"
            - name: DEFAULT_TIMEZONE
              value: America/Toronto
          resources:
            requests:
              cpu: 250m
              memory: 256Mi
            limits:
              cpu: "1"
              memory: 768Mi
          readinessProbe:
            httpGet:
              path: /health/ready
              port: http
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 3
          livenessProbe:
            httpGet:
              path: /health/live
              port: http
            initialDelaySeconds: 30
            periodSeconds: 20
            timeoutSeconds: 3
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  name: bikeshare-api
  namespace: mobility
  labels:
    app: bikeshare-api
spec:
  type: ClusterIP
  selector:
    app: bikeshare-api
  ports:
    - name: http
      port: 80
      targetPort: http
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: bikeshare-api
  namespace: mobility
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: bikeshare-api
  minReplicas: 3
  maxReplicas: 12
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
    scaleUp:
      stabilizationWindowSeconds: 60
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70