# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Website App Kubernetes Deployment config. (https://kubernetes.io/docs/concepts/workloads/controllers/deployment/)
# This is to be extended by the dev/autopush/staging/prod overlay.
# The deployment contains Flask web server container , gRPC mixer container and ESP container that transcodes grpc to JSON.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: website-app
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      # maximum number of Pods that can be created over the desired number of Pods
      maxSurge: 1
      # maximum number of Pods that can be unavailable during the update process
      maxUnavailable: 25%
  selector:
    matchLabels:
      app: website-app
  template:
    metadata:
      labels:
        app: website-app
    spec:
      # This k8s service account binds to the GCP service account, and used
      # for GKE Workload Identity: https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity
      serviceAccountName: website-ksa
      volumes:
        - name: schema-mapping
          configMap:
            name: schema-mapping
      containers:
        - name: website
          image: gcr.io/datcom-ci/datacommons-website:latest
          imagePullPolicy: Always
          args: []
          ports:
            - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080
            failureThreshold: 1
            periodSeconds: 10
          resources:
            limits:
              memory: "3G"
              cpu: "500m"
            requests:
              memory: "3G"
              cpu: "500m"
          env:
            - name: FLASK_ENV
              valueFrom:
                configMapKeyRef:
                  name: website-configmap
                  key: flaskEnv
            # This is the GCP project that holds the secrets: api key, etc...
            - name: SECRET_PROJECT
              valueFrom:
                configMapKeyRef:
                  name: website-configmap
                  key: secretProject
            - name: WEBSITE_HASH
              valueFrom:
                configMapKeyRef:
                  name: githash-configmap
                  key: website_hash.txt
            - name: BIG_QUERY
              valueFrom:
                configMapKeyRef:
                  name: store-configmap
                  key: bigquery.version
            - name: BIG_TABLE
              valueFrom:
                configMapKeyRef:
                  name: store-configmap
                  key: bigtable.version
            # A dummy config used to bounce the server without any docker image
            # or data config change.
            - name: BOUNCE
              value: "dummy"
        - name: mixer
          image: gcr.io/datcom-ci/datacommons-mixer:latest
          imagePullPolicy: Always
          resources:
            limits:
              memory: "6G"
              cpu: "800m"
            requests:
              memory: "6G"
              cpu: "800m"
          args:
            - --mixer_project=$(MIXER_PROJECT)
            - --store_project=$(STORE_PROJECT)
            - --bq_dataset=$(BIG_QUERY)
            - --base_table=$(BIG_TABLE)
            - --serve_recon_service=true
            - --schema_path=/datacommons/mapping
          volumeMounts:
            - name: schema-mapping
              mountPath: /datacommons/mapping
          env:
            - name: MIXER_PROJECT
              valueFrom:
                configMapKeyRef:
                  name: mixer-configmap
                  key: mixerProject
            - name: STORE_PROJECT
              valueFrom:
                configMapKeyRef:
                  name: store-configmap
                  key: store.project
            - name: BIG_QUERY
              valueFrom:
                configMapKeyRef:
                  name: store-configmap
                  key: bigquery.version
            - name: BIG_TABLE
              valueFrom:
                configMapKeyRef:
                  name: store-configmap
                  key: bigtable.version
            - name: MIXER_HASH
              valueFrom:
                configMapKeyRef:
                  name: githash-configmap
                  key: mixer_hash.txt
          ports:
            - containerPort: 12345
          readinessProbe:
            exec:
              command: ["/bin/grpc_health_probe", "-addr=:12345"]
            periodSeconds: 10
          livenessProbe:
            exec:
              command: ["/bin/grpc_health_probe", "-addr=:12345"]
            periodSeconds: 10
            initialDelaySeconds: 10
        - name: esp
          image: gcr.io/endpoints-release/endpoints-runtime:1
          args:
            - --service=$(SERVICE_NAME)
            - --rollout_strategy=managed
            - --http_port=8081
            - --backend=grpc://127.0.0.1:12345
            - --cors_preset=basic
            - --healthz=healthz
          env:
            - name: SERVICE_NAME
              valueFrom:
                configMapKeyRef:
                  name: mixer-configmap
                  key: serviceName
          resources:
            limits:
              memory: "1G"
              cpu: "200m"
            requests:
              memory: "1G"
              cpu: "200m"
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8081
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8081
            periodSeconds: 5
            initialDelaySeconds: 5
          ports:
            - containerPort: 8081
