apiVersion: v1
kind: Service
metadata:
  name: wrestlingdev-app
  labels:
    app: wrestlingdev
spec:
  ports:
    - port: 80
  selector:
    app: wrestlingdev
    tier: frontend
  clusterIP: None
---
apiVersion: apps/v1
# Use a statefulset instead of a deployment because we need to have a unique identity for each pod
# for solid queue to work properly and solid queue is running with puma threads
kind: StatefulSet
metadata:
  name: wrestlingdev-app
  labels:
    app: wrestlingdev
spec:
  replicas: 2
  serviceName: wrestlingdev-app
  selector:
    matchLabels:
      app: wrestlingdev
      tier: frontend
  template:
    metadata:
      labels:
        app: wrestlingdev
        tier: frontend
    spec:
      containers:
      - name: wrestlingdev-app
        image: jcwimer/wrestlingdev:prod
        imagePullPolicy: Always
        command: ["bundle"]
        args: ["exec", "rails", "server", "-e", "production", "-p", "80", "-b", "0.0.0.0"]
        ports:
        - containerPort: 80
        env:
        - name: RAILS_ENV
          value: production
        - name: SOLID_QUEUE_IN_PUMA
          value: "true"
        - name: REVERSE_PROXY_SSL_TERMINATION
          value: "true"
        - name: PASSENGER_POOL_SIZE
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: passenger_pool_size
        - name: WRESTLINGDEV_DB_NAME
          value: wrestlingdev
        - name: WRESTLINGDEV_DB_USR
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: dbusername
        - name: WRESTLINGDEV_DB_PWD
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: dbpassword
        - name: WRESTLINGDEV_DB_PORT
          value: "3306"
        - name: MEMCACHIER_SERVERS
          value: wrestlingdev-memcached:11211
        - name: WRESTLINGDEV_DB_HOST
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: dbhost
        - name: WRESTLINGDEV_DEVISE_SECRET_KEY
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: devisesecretkey
        - name: WRESTLINGDEV_SECRET_KEY_BASE
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: railssecretkey
        - name: WRESTLINGDEV_EMAIL_PWD
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: gmailpassword
        - name: WRESTLINGDEV_EMAIL
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: gmailemail
        - name: WRESTLINGDEV_INFLUXDB_DATABASE
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: influxdb_database
        - name: WRESTLINGDEV_INFLUXDB_HOST
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: influxdb_hostname
        - name: WRESTLINGDEV_INFLUXDB_PORT
          valueFrom:
           secretKeyRef:
             name: wrestlingdev-secrets
             key: influxdb_port
        # resources:
        #   limits:
        #     memory: "768Mi"
        #   requests:
        #     memory: "300Mi"
        #     cpu: "0.3"
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 180
          periodSeconds: 20
          timeoutSeconds: 10
# ---
# apiVersion: autoscaling/v2beta1
# kind: HorizontalPodAutoscaler
# metadata:
#   name: wrestlingdev-app-deployment-autoscale
# spec:
#   scaleTargetRef:
#     apiVersion: apps/v1
#     kind: Deployment
#     name: wrestlingdev-app-deployment
#   minReplicas: 2
#   maxReplicas: 5
#   metrics:
#   - type: Resource
#     resource:
#       name: cpu
#       targetAverageUtilization: 75
  # - type: Resource
  #   resource:
  #     name: memory
  #     targetAverageValue: 100Mi

