Skip to content

Commit 6331a68

Browse files
authored
MULTIARCH-4696: harden the subnet delete using jq/--no-run-if-empty (openshift#58949)
* MULTIARCH-4696: harden the subnet delete using jq/--no-run-if-empty Signed-off-by: Paul Bastide <[email protected]> * MULTIARCH-4696: harden for security group overload Signed-off-by: Paul Bastide <[email protected]> * MULTIARCH-4696: deprovision issues with id_rsa and harden for pathing Signed-off-by: Paul Bastide <[email protected]> * MULTIARCH-4696: clean up the network cleanup Signed-off-by: Paul Bastide <[email protected]> * MULTIARCH-4696: synchronize the vpc deprovision and the precleanup step Signed-off-by: Paul Bastide <[email protected]> --------- Signed-off-by: Paul Bastide <[email protected]>
1 parent 9c4a746 commit 6331a68

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

ci-operator/step-registry/upi/deprovision/powervs/cluster/upi-deprovision-powervs-cluster-commands.sh

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ function cleanup_prior() {
121121
WORKSPACE_NAME="$(cat ${SHARED_DIR}/WORKSPACE_NAME)"
122122
VPC_NAME="${WORKSPACE_NAME}-vpc"
123123
export VPC_NAME
124+
124125
# PowerVS Instances
125126
echo "Cleaning up target PowerVS workspace"
126127
for CRN in $(ibmcloud pi workspace ls 2> /dev/null | grep "${WORKSPACE_NAME}" | awk '{print $1}' || true)
@@ -145,38 +146,54 @@ function cleanup_prior() {
145146
sleep 5
146147
done
147148
sleep 60
149+
150+
# Dev: functions don't work inline with xargs
151+
echo "Delete network non-'ocp-net' on PowerVS region"
152+
ibmcloud pi subnet ls --json | jq -r '[.networks[] | select(.name | contains("ocp-net") | not)] | .[]?.networkID' | xargs --no-run-if-empty -I {} ibmcloud pi subnet delete {} || true
153+
echo "Done deleting non-'ocp-net' on PowerVS"
154+
148155
echo "[STATUS:Done] Deleting the contents in ${CRN}"
149156
done
150157

151158
# VPC Instances
152159
# VPC LBs
153-
# TODO: FIXME - need to be selective so as not to blow out other workflows being run
154-
echo "Cleaning up the VPC Load Balancers"
160+
# VPC Instances
161+
# VPC LBs
162+
WORKSPACE_NAME="multi-arch-comp-${LEASED_RESOURCE}-1"
163+
VPC_NAME="${WORKSPACE_NAME}-vpc"
164+
165+
echo "Target region - ${VPC_REGION}"
155166
ibmcloud target -r "${VPC_REGION}" -g "${RESOURCE_GROUP}"
156-
for RESOURCE_TGT in $(ibmcloud is subnets --output json | jq -r '.[].id')
167+
168+
echo "Cleaning up the Security Groups"
169+
ibmcloud is security-groups --vpc "${VPC_NAME}" --resource-group-name "${RESOURCE_GROUP}" --output json \
170+
| jq -r '[.[] | select(.name | contains("ocp-sec-group"))] | .[]?.name' \
171+
| xargs --no-run-if-empty -I {} ibmcloud security-group-delete {} --vpc "${VPC_NAME}" --force\
172+
|| true
173+
174+
echo "Cleaning up the VPC Load Balancers"
175+
for SUB in $(ibmcloud is subnets --output json 2>&1 | jq --arg vpc "${VPC_NAME}" -r '.[] | select(.vpc.name | contains($vpc)).id')
157176
do
158-
VALID_SUB=$(ibmcloud is subnet "${RESOURCE_TGT}" --output json | jq -r '. | select(.vpc.name | contains("'${VPC_NAME}'"))')
159-
if [ -n "${VALID_SUB}" ]
160-
then
161-
# Searches the VSIs and LBs to delete them
162-
for VSI in $(ibmcloud is subnet "${VALID_SUB}" --vpc "${VPC_NAME}" --output json --show-attached | jq -r '.instances[].name')
163-
do
164-
ibmcloud is instance-delete "${VSI}" --force || true
165-
done
166-
167-
for LB in $(ibmcloud is subnet "${VALID_SUB}" --vpc "${VPC_NAME}" --output json --show-attached | jq -r '.load_balancers[].name')
168-
do
169-
ibmcloud is load-balancer-delete "${LB}" --force --vpc "${VPC_NAME}" || true
170-
done
171-
sleep 60
172-
fi
177+
echo "Subnet: ${SUB}"
178+
# Searches the VSIs and LBs to delete them
179+
for VSI in $(ibmcloud is subnet "${SUB}" --vpc "${VPC_NAME}" --output json --show-attached | jq -r '.instances[]?.name')
180+
do
181+
ibmcloud is instance-delete "${VSI}" --force || true
182+
done
183+
184+
echo "Deleting LB in ${SUB}"
185+
for LB in $(ibmcloud is subnet "${SUB}" --vpc "${VPC_NAME}" --output json --show-attached | jq -r '.load_balancers[].name')
186+
do
187+
ibmcloud is load-balancer-delete "${LB}" --force --vpc "${VPC_NAME}" || true
188+
done
189+
sleep 60
173190
done
174191

175192
# VPC Images
176193
# TODO: FIXME add filtering by date.... ?
177194
for RESOURCE_TGT in $(ibmcloud is images --owner-type user --resource-group-name "${RESOURCE_GROUP}" --output json | jq -r '.[].id')
178195
do
179-
ibmcloud is image-delete "${RESOURCE_TGT}"
196+
ibmcloud is image-delete "${RESOURCE_TGT}" -f
180197
done
181198

182199
echo "Done cleaning up prior runs"
@@ -185,19 +202,19 @@ function cleanup_prior() {
185202
# Destroy the cluster based on the set configuration / tfvars
186203
function destroy_upi_cluster() {
187204
echo "destroy terraform to build PowerVS UPI cluster"
188-
cp "${SHARED_DIR}"/var-multi-arch-upi.tfvars "${IBMCLOUD_HOME}"/ocp-install-dir/var-multi-arch-upi.tfvars
189-
echo "UPI TFVARS copied: ${IBMCLOUD_HOME}"/ocp-install-dir/var-multi-arch-upi.tfvars
205+
cp "${SHARED_DIR}"/var-multi-arch-upi.tfvars "${IBMCLOUD_HOME}"/ocp4-upi-powervs/var-multi-arch-upi.tfvars
206+
echo "UPI TFVARS copied: ${IBMCLOUD_HOME}"/ocp4-upi-powervs/data/var-multi-arch-upi.tfvars
190207

191-
cp "${CLUSTER_PROFILE_DIR}"/ssh-privatekey "${IBMCLOUD_HOME}"/ocp4-upi-powervs/data/id_rsa.pub
192-
cp "${CLUSTER_PROFILE_DIR}"/ssh-publickey "${IBMCLOUD_HOME}"/ocp4-upi-powervs/data/id_rsa
193-
chmod 0600 "${IBMCLOUD_HOME}"/ocp-install-dir/id_rsa
208+
cp "${CLUSTER_PROFILE_DIR}"/ssh-privatekey "${IBMCLOUD_HOME}"/ocp4-upi-powervs/data/id_rsa
209+
cp "${CLUSTER_PROFILE_DIR}"/ssh-publickey "${IBMCLOUD_HOME}"/ocp4-upi-powervs/data/id_rsa.pub
210+
chmod 0600 "${IBMCLOUD_HOME}"/ocp4-upi-powervs/data/id_rsa
194211

195-
cp "${SHARED_DIR}"/terraform.tfstate "${IBMCLOUD_HOME}"/ocp4-upi-powervs/terraform.tfstate
196-
cd "${IBMCLOUD_HOME}"/ocp4-upi-powervs && \
212+
cp "${SHARED_DIR}"/terraform.tfstate "${IBMCLOUD_HOME}"/ocp4-upi-powervs/data/terraform.tfstate
213+
cd "${IBMCLOUD_HOME}"/ocp-install-dir/ocp4-upi-powervs && \
197214
"${IBMCLOUD_HOME}"/ocp-install-dir/terraform init && \
198215
"${IBMCLOUD_HOME}"/ocp-install-dir/terraform destroy -auto-approve \
199-
-var-file "${IBMCLOUD_HOME}"/ocp-install-dir/var-multi-arch-upi.tfvars \
200-
-state "${IBMCLOUD_HOME}"/ocp4-upi-powervs/terraform.tfstate
216+
-var-file "${IBMCLOUD_HOME}"/ocp4-upi-powervs/data/var-multi-arch-upi.tfvars \
217+
-state "${IBMCLOUD_HOME}"/ocp4-upi-powervs/data/terraform.tfstate
201218
}
202219

203220
############################################################

ci-operator/step-registry/upi/install/powervs/cluster/upi-install-powervs-cluster-commands.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,23 @@ function cleanup_prior() {
191191
192192
# Dev: functions don't work inline with xargs
193193
echo "Delete network non-'ocp-net' on PowerVS region"
194-
ibmcloud pi subnet ls | grep -v ocp-net | awk '{print $1}' | xargs -I {} ibmcloud pi subnet delete {} || true
194+
ibmcloud pi subnet ls --json | jq -r '[.networks[] | select(.name | contains("ocp-net") | not)] | .[]?.networkID' | xargs --no-run-if-empty -I {} ibmcloud pi subnet delete {} || true
195195
echo "Done deleting non-'ocp-net' on PowerVS"
196196
197197
# VPC Instances
198198
# VPC LBs
199199
WORKSPACE_NAME="multi-arch-comp-${LEASED_RESOURCE}-1"
200200
VPC_NAME="${WORKSPACE_NAME}-vpc"
201+
echo "Target region - ${VPC_REGION}"
202+
ibmcloud target -r "${VPC_REGION}" -g "${RESOURCE_GROUP}"
203+
204+
echo "Cleaning up the Security Groups"
205+
ibmcloud is security-groups --vpc "${VPC_NAME}" --resource-group-name "${RESOURCE_GROUP}" --output json \
206+
| jq -r '[.[] | select(.name | contains("ocp-sec-group"))] | .[]?.name' \
207+
| xargs --no-run-if-empty -I {} ibmcloud security-group-delete {} --vpc "${VPC_NAME}" --force\
208+
|| true
201209
202210
echo "Cleaning up the VPC Load Balancers"
203-
ibmcloud target -r "${VPC_REGION}" -g "${RESOURCE_GROUP}"
204211
for SUB in $(ibmcloud is subnets --output json 2>&1 | jq --arg vpc "${VPC_NAME}" -r '.[] | select(.vpc.name | contains($vpc)).id')
205212
do
206213
echo "Subnet: ${SUB}"

0 commit comments

Comments
 (0)