Skip to content

Commit 74042dc

Browse files
authored
Use Boskos for proper resource cleanup (#565)
Signed-off-by: Amulyam24 <[email protected]>
1 parent 6a5649b commit 74042dc

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

hack/boskos.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
22+
23+
USER="cluster-api-provider-ibmcloud"
24+
RESOURCE_TYPE="${RESOURCE_TYPE:-"powervs-project"}"
25+
26+
release_account(){
27+
url="http://${BOSKOS_HOST}/release?name=${BOSKOS_RESOURCE_NAME}&dest=dirty&owner=${USER}"
28+
status_code=$(curl -w '%{http_code}' -X POST ${url})
29+
30+
if [[ ${status_code} != 200 ]]; then
31+
echo "Got invalid response- ${status_code}"
32+
exit 1
33+
fi
34+
}
35+
36+
checkout_account(){
37+
url="http://${BOSKOS_HOST}/acquire?type=${RESOURCE_TYPE}&state=free&dest=busy&owner=${USER}"
38+
resource_name=$(curl -X POST ${url} | jq -r '.name')
39+
[ $? = 0 ] && status_code=200
40+
41+
if [[ ${status_code} == 200 ]]; then
42+
echo "export BOSKOS_RESOURCE_NAME=${resource_name}"
43+
else
44+
echo "Got invalid response- ${status_code}"
45+
exit 1
46+
fi
47+
}
48+
49+
heartbeat_account(){
50+
count=0
51+
url="http://${BOSKOS_HOST}/update?name=${BOSKOS_RESOURCE_NAME}&state=busy&owner=${USER}"
52+
while [ ${count} -lt 120 ]
53+
do
54+
status_code=$(curl -s -o /dev/null -w '%{http_code}' -X POST ${url})
55+
if [[ ${status_code} != 200 ]]; then
56+
echo "Got invalid response - ${status_code}"
57+
exit 1
58+
fi
59+
count=$(( $count + 1 ))
60+
sleep 60
61+
done
62+
}

scripts/ci-e2e.sh

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,26 @@ export PATH="${GOPATH_BIN}:${PATH}"
2727
source "${REPO_ROOT}/hack/ensure-go.sh"
2828
# shellcheck source=../hack/ensure-kubectl.sh
2929
source "${REPO_ROOT}/hack/ensure-kubectl.sh"
30+
# shellcheck source=../hack/boskos.sh
31+
source ${REPO_ROOT}/hack/boskos.sh
3032

3133
ARTIFACTS="${ARTIFACTS:-${PWD}/_artifacts}"
3234
mkdir -p "${ARTIFACTS}/logs/"
3335

3436
ARCH=$(uname -m)
35-
PVSADM_VERSION=${PVSADM_VERSION:-"v0.1.3"}
37+
PVSADM_VERSION=${PVSADM_VERSION:-"v0.1.4-alpha.3"}
3638
E2E_FLAVOR=${E2E_FLAVOR:-}
39+
REGION=${REGION:-"us-south"}
40+
RESOURCE_GROUP=${RESOURCE_GROUP:-"prow-resource-group"}
3741

3842
trap cleanup EXIT
3943

4044
cleanup(){
4145
# Delete the created ports for the network instance
4246
[ -n "${NEW_PORT}" ] && ./pvsadm delete port --network ${IBMPOWERVS_NETWORK_NAME} --port-id ${PORT_ID} --instance-id ${IBMPOWERVS_SERVICE_INSTANCE_ID}
47+
48+
# stop the boskos heartbeat
49+
[[ -z ${HEART_BEAT_PID:-} ]] || kill -9 "${HEART_BEAT_PID}" || true
4350
}
4451

4552
install_pvsadm(){
@@ -50,8 +57,26 @@ install_pvsadm(){
5057
chmod +x ./pvsadm
5158
}
5259

60+
create_powervs_network_instance(){
61+
# Install ibmcloud CLI tool
62+
curl -fsSL https://clis.cloud.ibm.com/install/linux | sh
63+
64+
# Login to IBM Cloud using the API Key
65+
ibmcloud login -a cloud.ibm.com -r ${REGION} -g ${RESOURCE_GROUP}
66+
67+
# Install power-iaas command-line plug-in and target the required service instance
68+
ibmcloud plugin install power-iaas
69+
CRN=$(ibmcloud resource service-instance ${IBMPOWERVS_SERVICE_INSTANCE_ID} --output json | jq -r '.[].crn')
70+
ibmcloud pi service-target ${CRN}
71+
72+
# Create the network instance
73+
ibmcloud pi network-create-public ${IBMPOWERVS_NETWORK_NAME} --dns-servers "8.8.8.8 9.9.9.9"
74+
75+
}
76+
5377
init_network_powervs(){
5478
install_pvsadm
79+
create_powervs_network_instance
5580

5681
# Creating ports using the pvsadm tool
5782
./pvsadm create port --description "capi-port-e2e" --network ${IBMPOWERVS_NETWORK_NAME} --instance-id ${IBMPOWERVS_SERVICE_INSTANCE_ID}
@@ -68,10 +93,37 @@ prerequisites_powervs(){
6893
# Assigning PowerVS variables
6994
export IBMPOWERVS_IMAGE_NAME=${IBMPOWERVS_IMAGE_NAME:-"capibm-powervs-centos-streams8-1-22-4"}
7095
export IBMPOWERVS_SERVICE_INSTANCE_ID=${IBMPOWERVS_SERVICE_INSTANCE_ID:-"0f28d13a-6e33-4d86-b6d7-a9b46ff7659e"}
71-
export IBMPOWERVS_NETWORK_NAME=${IBMPOWERVS_NETWORK_NAME:-"capi-e2e-test"}
96+
export IBMPOWERVS_NETWORK_NAME=${BOSKOS_RESOURCE_NAME:-"capi-e2e-test"}
7297
}
7398

7499
main(){
100+
# If BOSKOS_HOST is set then acquire an IBM Cloud resource from Boskos.
101+
if [ -n "${BOSKOS_HOST:-}" ]; then
102+
# Check out the resource from Boskos and store the produced environment
103+
# variables in a temporary file.
104+
account_env_var_file="$(mktemp)"
105+
checkout_account 1> "${account_env_var_file}"
106+
checkout_account_status="${?}"
107+
108+
# If the checkout process was a success then load the
109+
# environment variables into this process.
110+
[ "${checkout_account_status}" = "0" ] && . "${account_env_var_file}"
111+
112+
# Always remove the account environment variable file which
113+
# could contain sensitive information.
114+
rm -f "${account_env_var_file}"
115+
116+
if [ ! "${checkout_account_status}" = "0" ]; then
117+
echo "error getting account from boskos" 1>&2
118+
exit "${checkout_account_status}"
119+
fi
120+
121+
# Run the heart beat process to tell Boskos that we are still
122+
# using the checked out resource periodically.
123+
heartbeat_account >> "$ARTIFACTS/logs/boskos.log" 2>&1 &
124+
HEART_BEAT_PID=$(echo $!)
125+
fi
126+
75127
if [[ "${E2E_FLAVOR}" == "powervs" ]]; then
76128
prerequisites_powervs
77129
init_network_powervs
@@ -81,6 +133,9 @@ main(){
81133
make test-e2e
82134
test_status="${?}"
83135
echo TESTSTATUS="${test_status}"
136+
137+
# If Boskos is being used then release the IBM Cloud resource back to Boskos.
138+
[ -z "${BOSKOS_HOST:-}" ] || release_account >> "$ARTIFACTS/logs/boskos.log" 2>&1
84139
}
85140

86141
main "$@"

0 commit comments

Comments
 (0)