# Copyright 2021 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.

timeout: 1200s
options:
  substitution_option: ALLOW_LOOSE
  machineType: 'N1_HIGHCPU_8'


# Execution environment:
# HOME=/builder/home
# pwd=/workspace
# BUILDER_OUTPUT=/builder/outputs
# GOCACHE=/gocache

# When creating trggers, replace the tag with either _PR_NUMBER, TAG_NAME or BRANCH_NAME
# PR_NUMBER needs a prefix - TAG will have it
substitutions:
  _TAG: tmp
  _PR: ""

steps:
  # About 50 sec, to isolte loading of the builder image
  - name: gcr.io/wlhe-cr/crm-builder
    entrypoint: /bin/sh
    id: debug
    waitFor:
      - "-"
    env:
      - TAG=${_TAG}${_PR}
    args:
      - -c
      - |
        env

  - name: gcr.io/wlhe-cr/crm-builder
    id: push
    waitFor:
      - "-"
    env:
      - PROJECT_ID=${PROJECT_ID}
      - TAG=${_TAG}${_PR}
    args:
      - -c
      - |
        make build docker/krun push/krun docker/hgate push/hgate

  - name: gcr.io/wlhe-cr/crm-builder
    id: fortio
    waitFor:
      - "push"
    env:
      - TAG=${_TAG}${_PR}
      - FORTIO_IMAGE=gcr.io/${PROJECT_ID}/fortio-mesh:${_TAG}${_PR}
      - GOLDEN_IMAGE=gcr.io/${PROJECT_ID}/krun:${_TAG}${_PR}
      - FORTIO_IMAGE=gcr.io/${PROJECT_ID}/fortio-mesh:${_TAG}${_PR}
    args:
      - -c
      - |
        cd samples/fortio
        make image push

  - name: gcr.io/wlhe-cr/crm-builder
    id: test
    env:
      - TAG=${_TAG}${_PR}
      - CLUSTER_NAME=asm-cr
      - CLUSTER_LOCATION=us-central1-c
      - KUBECONFIG=/workspace/kubeconfig
    waitFor:
      - "-"
    args:
      - -c
      - |
        gcloud container clusters get-credentials $${CLUSTER_NAME} --zone us-central1-c --project $PROJECT_ID
        make test

  # Deploy the in-cluster connector. Note that the runner doesn't have RBAC permissions
  # The cluster must be setup with the RBAC manually.
  - name: gcr.io/wlhe-cr/crm-builder
    id: hgatedeploy
    env:
      - TAG=${_TAG}${_PR}
      - CLUSTER_NAME=asm-cr
      - CLUSTER_LOCATION=us-central1-c
      - KUBECONFIG=/workspace/kubeconfig
    waitFor:
      - push
    args:
      - -c
      - |
        gcloud container clusters get-credentials $${CLUSTER_NAME} --zone us-central1-c --project $PROJECT_ID
        make deploy/hgate

  # Deploy the canary, run tests
  # Missing alpha command in 'gcr.io/google.com/cloudsdktool/cloud-sdk', also too large.
  # This is built with 'make gcp/builder-gcloud', alpine based.
  - name: gcr.io/wlhe-cr/crm-builder
    entrypoint: /bin/sh
    env:
      - TAG=${_TAG}${_PR}
      - CLUSTER_NAME=asm-cr
      - CLUSTER_LOCATION=us-central1-c
      - KUBECONFIG=/workspace/kubeconfig
    waitFor:
      - fortio
      - hgatedeploy
    args:
      - -c
      - |
        make e2e

  # TODO: combine gcloud, go, etc in single image - Istio build image is huge
  # Also includes gcloud 349.0.0
  # https://github.com/GoogleCloudPlatform/cloud-builders-community/tree/master/ko is a good
  # base, includes ko, kubectl, gcloud
  # They use: CLOUDSDK_COMPUTE_ZONE, CLOUDSDK_CONTAINER_CLUSTER, CLOUDSDK_COREPROJECT
#go get -u github.com/jstemmer/go-junit-report
#2>&1 go test -timeout 1m -v ./... | tee sponge.log
#/go/bin/go-junit-report -set-exit-code < sponge.log > ${SHORT_SHA}_test_log.xml



