#!/usr/bin/env bash

# Copyright 2020 The Kubermatic Kubernetes Platform contributors.
#
# 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.

### This script sets up a local KKP installation in kind, deploys a
### couple of test Presets and Users and then runs the e2e tests for the
### nodeport-proxy.

set -euo pipefail

cd $(dirname $0)/..
source hack/lib.sh

DOCKER_REPO="${DOCKER_REPO:-quay.io/kubermatic}"
GOOS="${GOOS:-linux}"
TAG="$(git rev-parse HEAD)"
KIND_CLUSTER_NAME="${KIND_CLUSTER_NAME:-kubermatic}"

type kind > /dev/null || fatal \
  "Kind is required to run this script, please refer to: https://kind.sigs.k8s.io/docs/user/quick-start/#installation"

function clean_up {
  echodate "Deleting cluster ${KIND_CLUSTER_NAME}"
  kind delete cluster --name "${KIND_CLUSTER_NAME}" || true
}
appendTrap clean_up EXIT

# Only start docker daemon / download cache in CI envorinment.
if [[ ! -z "${JOB_NAME:-}" ]] && [[ ! -z "${PROW_JOB_ID:-}" ]]; then
  start_docker_daemon_ci
  make download-gocache

  echodate "Preloading the kindest/node image"
  docker load --input /kindest.tar
fi

# build Docker images
make -C cmd/nodeport-proxy docker \
  GOOS="${GOOS}" \
  DOCKER_REPO="${DOCKER_REPO}" \
  TAG="${TAG}"

# setup Kind cluster
time kind create cluster --name "${KIND_CLUSTER_NAME}"

# load nodeport-proxy image
time kind load docker-image "${DOCKER_REPO}/nodeport-proxy:${TAG}" --name "$KIND_CLUSTER_NAME"

# gather the logs of all Envoys
if [ -n "${ARTIFACTS:-}" ]; then
  protokol --kubeconfig "${HOME}/.kube/config" --output "$ARTIFACTS/logs" --namespace 'np-proxy-test-*' > /dev/null 2>&1 &
  protokol --kubeconfig "${HOME}/.kube/config" --output "$ARTIFACTS/logs" --namespace 'nodeport-proxy-*' > /dev/null 2>&1 &
fi

# run tests
go_test nodeport_proxy_e2e -tags e2e -v -race ./pkg/test/e2e/nodeport-proxy \
  -kubeconfig "${HOME}/.kube/config" \
  -kubermatic-tag "${TAG}" \
  -log-debug
