Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require (
k8s.io/code-generator v0.18.6
k8s.io/gengo v0.0.0-20200205140755-e0e292d8aa12
k8s.io/klog v1.0.0
knative.dev/test-infra v0.0.0-20200825022047-cb4bb218c5e5
knative.dev/test-infra v0.0.0-20200828211307-9d4372c9b1c7
sigs.k8s.io/boskos v0.0.0-20200729174948-794df80db9c9
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1836,8 +1836,8 @@ knative.dev/test-infra v0.0.0-20200505052144-5ea2f705bb55/go.mod h1:WqF1Azka+FxP
knative.dev/test-infra v0.0.0-20200513011557-d03429a76034/go.mod h1:aMif0KXL4g19YCYwsy4Ocjjz5xgPlseYV+B95Oo4JGE=
knative.dev/test-infra v0.0.0-20200519015156-82551620b0a9/go.mod h1:A5b2OAXTOeHT3hHhVQm3dmtbuWvIDP7qzgtqxA3/2pE=
knative.dev/test-infra v0.0.0-20200707183444-aed09e56ddc7/go.mod h1:RjYAhXnZqeHw9+B0zsbqSPlae0lCvjekO/nw5ZMpLCs=
knative.dev/test-infra v0.0.0-20200825022047-cb4bb218c5e5 h1:iqbpe5rccMW2r1Mu1Prw4gJ9xi7yqahmtxzckTUHBeI=
knative.dev/test-infra v0.0.0-20200825022047-cb4bb218c5e5/go.mod h1:Pmg2c7Z7q7BGFUV/GOpU5BlrD3ePJft4MPqx8AYBplc=
knative.dev/test-infra v0.0.0-20200828211307-9d4372c9b1c7 h1:yIzao6i9Hu51SCdpyqBEnhpt7G12FqFrxHK6ZnQnu8o=
knative.dev/test-infra v0.0.0-20200828211307-9d4372c9b1c7/go.mod h1:Pmg2c7Z7q7BGFUV/GOpU5BlrD3ePJft4MPqx8AYBplc=
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
Expand Down
25 changes: 10 additions & 15 deletions vendor/knative.dev/test-infra/scripts/e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,26 @@ function setup_test_cluster() {
set -o errexit
set -o pipefail

header "Test cluster setup"
kubectl get nodes

header "Setting up test cluster"

kubectl get nodes
# Set the actual project the test cluster resides in
# It will be a project assigned by Boskos if test is running on Prow,
# otherwise will be ${E2E_GCP_PROJECT_ID} set up by user.
E2E_PROJECT_ID="$(gcloud config get-value project)"
export E2E_PROJECT_ID
readonly E2E_PROJECT_ID

local k8s_user
k8s_user=$(gcloud config get-value core/account)
local k8s_cluster
k8s_cluster=$(kubectl config current-context)

is_protected_cluster "${k8s_cluster}" && \
abort "kubeconfig context set to ${k8s_cluster}, which is forbidden"

# If cluster admin role isn't set, this is a brand new cluster
# Setup the admin role and also KO_DOCKER_REPO if it is a GKE cluster
if [[ -z "$(kubectl get clusterrolebinding cluster-admin-binding 2> /dev/null)" && "${k8s_cluster}" =~ ^gke_.* ]]; then
acquire_cluster_admin_role "${k8s_user}" "${E2E_CLUSTER_NAME}" "${E2E_GKE_CLUSTER_REGION}" "${E2E_GKE_CLUSTER_ZONE}"
# Incorporate an element of randomness to ensure that each run properly publishes images.
export KO_DOCKER_REPO=gcr.io/${E2E_PROJECT_ID}/${REPO_NAME}-e2e-img/${RANDOM}
fi
# Acquire cluster admin role for the current user.
acquire_cluster_admin_role "${k8s_cluster}"

# Setup KO_DOCKER_REPO if it is a GKE cluster. Incorporate an element of randomness to ensure that each run properly publishes images.
[[ "${k8s_cluster}" =~ ^gke_.* ]] && export KO_DOCKER_REPO=gcr.io/${E2E_PROJECT_ID}/${REPO_NAME}-e2e-img/${RANDOM}

# Safety checks
is_protected_gcr "${KO_DOCKER_REPO}" && \
Expand All @@ -92,7 +85,7 @@ function setup_test_cluster() {
export KO_DATA_PATH="${REPO_ROOT_DIR}/.git"

# Do not run teardowns if we explicitly want to skip them.
(( ! SKIP_TEARDOWNS )) && trap teardown_test_resources EXIT
(( ! SKIP_TEARDOWNS )) && add_trap teardown_test_resources EXIT

# Handle failures ourselves, so we can dump useful info.
set +o errexit
Expand Down Expand Up @@ -130,6 +123,7 @@ function fail_test() {
SKIP_TEARDOWNS=0
SKIP_ISTIO_ADDON=0
E2E_SCRIPT=""
CLOUD_PROVIDER="gke"

# Parse flags and initialize the test cluster.
function initialize() {
Expand Down Expand Up @@ -166,6 +160,7 @@ function initialize() {
[[ $# -ge 2 ]] || abort "missing parameter after $1"
shift
case ${parameter} in
--cloud-provider) CLOUD_PROVIDER="$1" ;;
--kubetest2-flag) extra_kubetest2_flags+=("$1") ;;
--cluster-creation-flag) extra_cluster_creation_flags+=("$1") ;;
*) abort "unknown option ${parameter}" ;;
Expand All @@ -186,7 +181,7 @@ function initialize() {
readonly SKIP_TEARDOWNS

if (( ! run_tests )); then
create_gke_test_cluster extra_kubetest2_flags extra_cluster_creation_flags e2e_script_command
create_test_cluster "${CLOUD_PROVIDER}" extra_kubetest2_flags extra_cluster_creation_flags e2e_script_command
else
setup_test_cluster
fi
Expand Down
98 changes: 68 additions & 30 deletions vendor/knative.dev/test-infra/scripts/infra-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function dump_cluster_state() {
}

# On a Prow job, save some metadata about the test for Testgrid.
# Parameters: $1 - cluster provider name
function save_metadata() {
(( ! IS_PROW )) && return
local geo_key="Region"
Expand All @@ -116,37 +117,82 @@ function save_metadata() {
fi
local cluster_version
cluster_version="$(kubectl version --short=true)"
run_kntest metadata set --key="E2E:Provider" --value="$1"
run_kntest metadata set --key="E2E:${geo_key}" --value="${geo_value}"
run_kntest metadata set --key="E2E:Machine" --value="${E2E_GKE_CLUSTER_MACHINE}"
run_kntest metadata set --key="E2E:Version" --value="${cluster_version}"
run_kntest metadata set --key="E2E:MinNodes" --value="${E2E_MIN_CLUSTER_NODES}"
run_kntest metadata set --key="E2E:MaxNodes" --value="${E2E_MAX_CLUSTER_NODES}"
}

# Create a GKE test cluster with kubetest2 and call the current script again.
# Parameters: $1 - extra kubetest2 flags
# $2 - extra cluster creation flags
# $3 - test command to run by the kubetest2 tester
function create_gke_test_cluster() {
# Sets the current user as cluster admin for the given cluster.
# Parameters: $1 - cluster context name
function acquire_cluster_admin_role() {
if [[ -z "$(kubectl get clusterrolebinding cluster-admin-binding 2> /dev/null)" ]]; then
if [[ "$1" =~ ^gke_.* ]]; then
kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole=cluster-admin --user="$(gcloud config get-value core/account)"
else
kubectl create clusterrolebinding cluster-admin-binding-"${USER}" \
--clusterrole=cluster-admin --user="${USER}"
fi
fi
}

# Create a test cluster and run the tests if provided.
# Parameters: $1 - cluster provider name, e.g. gke
# $2 - extra kubetest2 flags
# $3 - extra cluster creation flags
# $4 - test command to run by the kubetest2 tester
function create_test_cluster() {
# Fail fast during setup.
set -o errexit
set -o pipefail
local -n _extra_kubetest2_flags=$1
local -n _extra_cluster_creation_flags=$2
local -n _tester_command=$3

if function_exists cluster_setup; then
cluster_setup || fail_test "cluster setup failed"
fi

case "$1" in
gke) create_gke_test_cluster "$2" "$3" "$4" ;;
kind) create_kind_test_cluster "$2" "$3" "$4" ;;
*) echo "unsupported provider: $1"; exit 1 ;;
esac

local result="$?"
# Ignore any errors below, this is a best-effort cleanup and shouldn't affect the test result.
set +o errexit
set +o pipefail
function_exists cluster_teardown && cluster_teardown
echo "Artifacts were written to ${ARTIFACTS}"
echo "Test result code is ${result}"
exit "${result}"
}

# Create a KIND test cluster with kubetest2 and run the test command.
# Parameters: $1 - extra kubetest2 flags
# $2 - extra cluster creation flags
# $3 - test command to run by the kubetest2 tester
function create_kind_test_cluster() {
# TODO(chizhg): implement with kubetest2
return 0
}

# Create a GKE test cluster with kubetest2 and run the test command.
# Parameters: $1 - extra kubetest2 flags
# $2 - extra cluster creation flags
# $3 - test command to run by the kubetest2 tester after the cluster is created (optional)
function create_gke_test_cluster() {
local -n _extra_kubetest2_flags=$1
local -n _extra_cluster_creation_flags=$2

echo "Cluster will have a minimum of ${E2E_MIN_CLUSTER_NODES} and a maximum of ${E2E_MAX_CLUSTER_NODES} nodes."
local _kubetest2_flags=(
"gke"
"--create-command=${E2E_GKE_COMMAND_GROUP} container clusters create --quiet --enable-autoscaling
--min-nodes=${E2E_MIN_CLUSTER_NODES} --max-nodes=${E2E_MAX_CLUSTER_NODES}
--cluster-version=${E2E_CLUSTER_VERSION}
--scopes=${E2E_GKE_SCOPES} --enable-basic-auth --no-issue-client-certificate
--no-enable-ip-alias --no-enable-autoupgrade
--scopes=${E2E_GKE_SCOPES} --no-enable-autoupgrade
${_extra_cluster_creation_flags[@]}"
"--environment=${E2E_GKE_ENVIRONMENT}"
"--cluster-name=${E2E_CLUSTER_NAME}"
Expand All @@ -163,7 +209,7 @@ function create_gke_test_cluster() {
_kubetest2_flags+=("--project=${gcloud_project}")
echo "gcloud project is ${gcloud_project}"
else
echo "Using boskos for the test cluster"
echo "Using boskos for provisioning the GCP project to create the test cluster"
fi

if (( IS_BOSKOS )); then
Expand All @@ -176,24 +222,19 @@ function create_gke_test_cluster() {
fi

# Create cluster and run the tests
create_gke_test_cluster_with_retries _kubetest2_flags _tester_command
local result="$?"
# Ignore any errors below, this is a best-effort cleanup and shouldn't affect the test result.
set +o errexit
set +o pipefail
function_exists cluster_teardown && cluster_teardown
echo "Artifacts were written to ${ARTIFACTS}"
echo "Test result code is ${result}"
exit "${result}"
create_gke_test_cluster_with_retries _kubetest2_flags "$3"
}

# TODO(chizhg): move this to kubetest2 gke deployer.
# Retry backup regions/zones if cluster creations failed due to stockout.
# Parameters: $1 - kubetest2 flags other than geo flag
# $2 - test command to run by the kubetest2 tester
# $2 - test command to run by the kubetest2 tester after the cluster is created (optional)
function create_gke_test_cluster_with_retries() {
local -n kubetest2_flags=$1
local -n tester_command=$2
local -n command=$2
if (( ${#command[@]} )); then
tester_command=("--test=exec" "--" "${command[@]}")
fi
local cluster_creation_log=/tmp/${REPO_NAME}-cluster_creation-log
# zone_not_provided is a placeholder for e2e_cluster_zone to make for loop below work
local zone_not_provided="zone_not_provided"
Expand All @@ -220,19 +261,16 @@ function create_gke_test_cluster_with_retries() {

header "Creating test cluster ${E2E_CLUSTER_VERSION} in ${cluster_creation_zone}"
if run_go_tool k8s-sigs.io/kubetest2 \
kubetest2 "${kubetest2_flags[@]}" --region="${cluster_creation_zone}" \
--test=exec -- "${tester_command[@]}" 2>&1 \
kubetest2 "${kubetest2_flags[@]}" --region="${cluster_creation_zone}" "${tester_command[@]}" 2>&1 \
| tee "${cluster_creation_log}"; then
# Save some metadata about cluster creation for using in prow and testgrid
save_metadata
save_metadata "gke"
return 0
fi
# Retry if cluster creation failed because of:
# - stockout (https://github.com/knative/test-infra/issues/592)
# - latest GKE not available in this region/zone yet (https://github.com/knative/test-infra/issues/694)
# Retry if cluster creation failed because of stockout (https://github.com/knative/test-infra/issues/592)
# or Nodes failing to start correctly.
# shellcheck disable=SC2143
[[ -z "$(grep -Fo 'does not have enough resources available to fulfill' "${cluster_creation_log}")" \
&& -z "$(grep -Fo 'ResponseError: code=400, message=No valid versions with the prefix' "${cluster_creation_log}")" \
&& -z "$(grep -Po 'ResponseError: code=400, message=Master version "[0-9a-z\-\.]+" is unsupported' "${cluster_creation_log}")" \
&& -z "$(grep -Po 'only \d+ nodes out of \d+ have registered; this is likely due to Nodes failing to start correctly' "${cluster_creation_log}")" ]] \
&& return 1
done
Expand Down
38 changes: 0 additions & 38 deletions vendor/knative.dev/test-infra/scripts/library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -327,44 +327,6 @@ function dump_app_logs() {
done
}

# Sets the given user as cluster admin.
# Parameters: $1 - user
# $2 - cluster name
# $3 - cluster region
# $4 - cluster zone, optional
function acquire_cluster_admin_role() {
echo "Acquiring cluster-admin role for user '$1'"
local geoflag="--region=$3"
[[ -n $4 ]] && geoflag="--zone=$3-$4"
# Get the password of the admin and use it, as the service account (or the user)
# might not have the necessary permission.
local password=$(gcloud --format="value(masterAuth.password)" \
container clusters describe $2 ${geoflag})
if [[ -n "${password}" ]]; then
# Cluster created with basic authentication
kubectl config set-credentials cluster-admin \
--username=admin --password=${password}
else
local cert=$(mktemp)
local key=$(mktemp)
echo "Certificate in ${cert}, key in ${key}"
gcloud --format="value(masterAuth.clientCertificate)" \
container clusters describe $2 ${geoflag} | base64 --decode > ${cert}
gcloud --format="value(masterAuth.clientKey)" \
container clusters describe $2 ${geoflag} | base64 --decode > ${key}
kubectl config set-credentials cluster-admin \
--client-certificate=${cert} --client-key=${key}
fi
kubectl config set-context $(kubectl config current-context) \
--user=cluster-admin
kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole=cluster-admin \
--user=$1
# Reset back to the default account
gcloud container clusters get-credentials \
$2 ${geoflag} --project $(gcloud config get-value project)
}

# Run a command through tee and capture its output.
# Parameters: $1 - file where the output will be stored.
# $2... - command to run.
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ k8s.io/utils/buffer
k8s.io/utils/integer
k8s.io/utils/pointer
k8s.io/utils/trace
# knative.dev/test-infra v0.0.0-20200825022047-cb4bb218c5e5
# knative.dev/test-infra v0.0.0-20200828211307-9d4372c9b1c7
## explicit
knative.dev/test-infra/scripts
# sigs.k8s.io/boskos v0.0.0-20200729174948-794df80db9c9
Expand Down