# Copyright 2021 The Kubeflow Authors
#
# 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
#
#      http://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.

name: Build KFP v2 go images & binaries
inputs:
- name: context
  type: Folder
  description: |
    Input context, it should be a folder.
- {name: destination, type: URI, description: destination should not contain tag}
outputs:
- {name: digest_launcher_v2, type: URI, description: KFP launcher v2 image digest}
- {name: digest_driver, type: URI, description: KFP driver image digest}
- {name: backend_compiler, type: Binary, description: KFP v2 backend compiler}
metadata:
  annotations:
    author: Yuan Gong <gongyuan94@gmail.com>
implementation:
  container:
    image: golang:1.17-alpine
    command:
    - sh
    - -exc
    - |
        context_path="$0"
        destination="$1"
        digest_launcher_v2_output_path="$2"
        digest_driver_output_path="$3"
        backend_compiler_output_path="$4"

        cd "${context_path}/backend/src/v2"

        # install dependencies
        apk update
        apk add make bash curl
        ./test/scripts/install-docker-cred-gcr.sh

        # prepare parent folders
        mkdir -p "$(dirname $digest_launcher_v2_output_path)"
        mkdir -p "$(dirname $digest_driver_output_path)"
        mkdir -p "$(dirname $backend_compiler_output_path)"

        # authenticate to gcr
        docker-credential-gcr configure-docker

        # build images
        go mod download || (sleep 10 && go mod download) # try download twice to avoid temporary network failure
        make install-ko
        export DEV_IMAGE_PREFIX="${destination}"
        make image-launcher-v2-dev > "${digest_launcher_v2_output_path}"
        make image-driver-dev > "${digest_driver_output_path}"
        # build backend compiler as a binary
        make build/compiler
        cp build/compiler "${backend_compiler_output_path}"
    - inputPath: context
    - inputValue: destination
    - outputPath: digest_launcher_v2
    - outputPath: digest_driver
    - outputPath: backend_compiler
