apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway
  labels:
    app: gateway
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gateway
  template:
    metadata:
      labels:
        app: gateway
    spec:
      initContainers:
        - name: init
          image: ellerbrock/alpine-bash-curl-ssl:0.3.0
          command:
            - /bin/bash
            - '-c'
            - |
              set -e

              function fetch_repo_version()
              {
                local TYPE="$1"
                local TYPE_LC="$2"
                local URL="https://storage.googleapis.com/fullstaq-ruby-server-edition-${TYPE_LC}-repo/versions/latest_version.txt"
                local VERSION

                echo " --> Checking $TYPE repo latest version" >&2
                echo "     $URL" >&2
                CODE=$(curl -sSL -w '%{http_code}' -o output.txt "$URL")
                if [[ "$CODE" =~ ^2 ]]; then
                  VERSION=$(cat output.txt)
                  echo "     Result: $VERSION" >&2
                  echo "$VERSION"
                elif [[ "$CODE" = 404 ]]; then
                  echo "     Result: unspecified (defaulting to 1)" >&2
                  echo 1
                else
                  echo " --> Error fetching $URL: server returned HTTP code $CODE" >&2
                  return 1
                fi
              }

              APT_LATEST_VERSION=$(fetch_repo_version APT apt)
              YUM_LATEST_VERSION=$(fetch_repo_version YUM yum)

              for F in /mnt/config/*; do
                NAME=$(basename "$F")
                echo " --> Processing $NAME"
                sed "s|{APT_LATEST_VERSION}|$APT_LATEST_VERSION|g; s|{YUM_LATEST_VERSION}|$YUM_LATEST_VERSION|g" \
                  "$F" > "/mnt/processed-config/$NAME"
              done
          resources:
            requests:
              cpu: 250m
              memory: 32Mi
              ephemeral-storage: 32Mi
            limits:
              cpu: 250m
              memory: 32Mi
              ephemeral-storage: 32Mi
          volumeMounts:
            - name: config
              mountPath: /mnt/config
            - name: processed-config
              mountPath: /mnt/processed-config
      containers:
        - name: nginx
          image: nginx:1.19-alpine
          imagePullPolicy: Always
          ports:
            - containerPort: 80
              protocol: TCP
          readinessProbe:
            initialDelaySeconds: 2
            timeoutSeconds: 10
            httpGet:
              port: 80
              path: /
              httpHeaders:
                - name: User-Agent
                  value: Kubernetes Readiness Probe
          resources:
            requests:
              cpu: 250m
              memory: 512Mi
              ephemeral-storage: 512Mi
            limits:
              cpu: 250m
              memory: 512Mi
              ephemeral-storage: 512Mi
          volumeMounts:
            - name: processed-config
              mountPath: /etc/nginx/conf.d
      volumes:
        - name: config
          configMap:
            name: gateway
        - name: processed-config
          emptyDir: {}
