Skip to content

Commit 6cdffd3

Browse files
committed
fix: improve operator test script cleanup and namespace handling
- Fix CNF_TYPE_DIR path construction (missing slash) - Add comprehensive cleanup of test labels from all resources - Add support for keeping operators installed after test. + suffix keeps operator installed in test-<package manifest> namespace, - suffix runs the test in test-<package manifest> namespace then deletes the operator - Force openshift-storage namespace for storage operators - Add special handling for odf-csi-addons-operator and mcg-operator - Add MultiClusterEngine CR creation for multicluster-engine operator - Handle existing operator subscriptions in skip cleanup mode
1 parent e640b03 commit 6cdffd3

File tree

1 file changed

+189
-49
lines changed

1 file changed

+189
-49
lines changed

script/run-basic-batch-operators-test.sh

Lines changed: 189 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ CATALOG_SOURCE_TEMPLATE="$(pwd)"/CatalogSource.yaml.template
2222
DOCKER_CONFIG=config.json
2323

2424
# Location of telco/non-telco classification file
25-
CNF_TYPE_DIR="$(pwd)"cmd/certsuite/claim/show/csv
25+
CNF_TYPE_DIR="$(pwd)"/cmd/certsuite/claim/show/csv
2626

2727
# Operator catalog name
2828
OPERATOR_CATALOG_NAME="operator-catalog"
@@ -99,17 +99,52 @@ cleanup() {
9999
# Leftovers specific to certain operators
100100
oc delete Validating_webhook_configuration sriov-operator-webhook-config || true
101101
oc delete Mutating_webhook_configuration sriov-operator-webhook-config || true
102+
103+
# Remove all test labels from all namespaces
104+
echo_color "$BLUE" "Removing test labels from all resources in all namespaces"
105+
106+
# Remove operator labels from CSVs in all namespaces
107+
oc get csv --all-namespaces -o json 2>/dev/null | \
108+
jq -r '.items[] | select(.metadata.labels."redhat-best-practices-for-k8s.com/operator" != null) | .metadata.namespace + " " + .metadata.name' 2>/dev/null | \
109+
while read -r ns name; do
110+
[ -n "$ns" ] && [ -n "$name" ] && oc label csv -n "$ns" "$name" redhat-best-practices-for-k8s.com/operator- 2>/dev/null || true
111+
done
112+
113+
# Remove generic labels from deployments in all namespaces
114+
oc get deployment --all-namespaces -o json 2>/dev/null | \
115+
jq -r '.items[] | select(.metadata.labels."redhat-best-practices-for-k8s.com/generic" != null) | .metadata.namespace + " " + .metadata.name' 2>/dev/null | \
116+
while read -r ns name; do
117+
[ -n "$ns" ] && [ -n "$name" ] && oc label deployment -n "$ns" "$name" redhat-best-practices-for-k8s.com/generic- 2>/dev/null || true
118+
done
119+
120+
# Remove generic labels from statefulsets in all namespaces
121+
oc get statefulset --all-namespaces -o json 2>/dev/null | \
122+
jq -r '.items[] | select(.metadata.labels."redhat-best-practices-for-k8s.com/generic" != null) | .metadata.namespace + " " + .metadata.name' 2>/dev/null | \
123+
while read -r ns name; do
124+
[ -n "$ns" ] && [ -n "$name" ] && oc label statefulset -n "$ns" "$name" redhat-best-practices-for-k8s.com/generic- 2>/dev/null || true
125+
done
126+
127+
# Remove generic labels from pods in all namespaces
128+
oc get pods --all-namespaces -o json 2>/dev/null | \
129+
jq -r '.items[] | select(.metadata.labels."redhat-best-practices-for-k8s.com/generic" != null) | .metadata.namespace + " " + .metadata.name' 2>/dev/null | \
130+
while read -r ns name; do
131+
[ -n "$ns" ] && [ -n "$name" ] && oc label pod -n "$ns" "$name" redhat-best-practices-for-k8s.com/generic- 2>/dev/null || true
132+
done
102133
}
103134

104135
wait_delete_namespace() {
105136
local namespace_deleting=$1
106137
# Wait for the namespace to be removed
107-
if [ "$namespace_deleting" != "openshift-operators" ]; then
138+
if [ "$namespace_deleting" != "openshift-operators" ] && [ "$namespace_deleting" != "openshift-storage" ]; then
108139

109140
echo_color "$BLUE" "non openshift-operators namespace = $namespace_deleting, deleting "
110141
with_retry 2 0 oc wait namespace "$namespace_deleting" --for=delete --timeout=60s || true
111142

112143
force_delete_namespace_if_present "$namespace_deleting" >>"$LOG_FILE_PATH" 2>&1 || true
144+
else
145+
if [ "$namespace_deleting" = "openshift-storage" ]; then
146+
echo_color "$BLUE" "Skipping deletion of openshift-storage namespace"
147+
fi
113148
fi
114149
}
115150

@@ -286,7 +321,6 @@ wait_for_csv_to_appear_and_label() {
286321
local start_time=0
287322
local current_time=0
288323
local elapsed_time=0
289-
local command=""
290324
local status=0
291325

292326
start_time=$(date +%s)
@@ -311,21 +345,30 @@ wait_for_csv_to_appear_and_label() {
311345
done
312346

313347
# Label CSV with "redhat-best-practices-for-k8s.com/operator=target"
314-
command=$(with_retry 5 10 oc get csv -n "$csv_namespace" -o custom-columns=':.metadata.name,:.metadata.namespace,:.kind' | grep -v openshift-operator-lifecycle-manager | sed '/^ *$/d' | awk '{print " with_retry 5 10 oc label " $3 " -n " $2 " " $1 " redhat-best-practices-for-k8s.com/operator=target "}')
315-
eval "$command"
348+
# Label each CSV individually to handle errors better
349+
oc get csv -n "$csv_namespace" -o name 2>>"$LOG_FILE_PATH" | while read -r csv; do
350+
if oc label "$csv" -n "$csv_namespace" redhat-best-practices-for-k8s.com/operator=target --overwrite 2>>"$LOG_FILE_PATH"; then
351+
echo_color "$BLUE" "Labeled $csv"
352+
else
353+
echo_color "$GREY" "Failed to label $csv (may not exist in this namespace)"
354+
fi
355+
done
356+
357+
# Wait a bit for labels to propagate
358+
sleep 2
316359

317360
# Wait for the CSV to be succeeded
318361
echo_color "$BLUE" "Wait for CSV to be succeeded"
319-
with_retry 30 0 oc wait csv -l redhat-best-practices-for-k8s.com/operator=target -n "$ns" --for=jsonpath=\{.status.phase\}=Succeeded --timeout=5s || status="$?"
362+
with_retry 60 5 oc wait csv -l redhat-best-practices-for-k8s.com/operator=target -n "$csv_namespace" --for=jsonpath=\{.status.phase\}=Succeeded --timeout=10s || status="$?"
320363
return $status
321364
}
322365

323366
force_delete_namespace_if_present() {
324367
local a_namespace=$1
325368
local pid=0
326369

327-
# Do not delete the redhat-operators namespace
328-
if [ "$a_namespace" = "openshift-operators" ]; then
370+
# Do not delete the openshift-operators or openshift-storage namespaces
371+
if [ "$a_namespace" = "openshift-operators" ] || [ "$a_namespace" = "openshift-storage" ]; then
329372
return 0
330373
fi
331374

@@ -364,9 +407,17 @@ report_failure() {
364407
local status=$1
365408
local ns=$2
366409
local package_name=$3
367-
local message=$4
410+
local skip_cleanup=$4
411+
local message=$5
368412

369-
with_retry 3 5 oc operator uninstall -X "$package_name" -n "$ns" || true
413+
# Skip uninstall for operators with + suffix or legacy lvms/odf operators
414+
if [ "$skip_cleanup" = true ] || [ "$package_name" = "lvms-operator" ] || [ "$package_name" = "odf-operator" ]; then
415+
echo_color "$BLUE" "Skipping uninstall and namespace deletion for $package_name"
416+
else
417+
with_retry 3 5 oc operator uninstall -X "$package_name" -n "$ns" || true
418+
wait_delete_namespace "$ns"
419+
fi
420+
370421
# Add per operator links
371422
{
372423
# Add error message
@@ -379,8 +430,6 @@ report_failure() {
379430
# New line
380431
echo "<br>"
381432
} >>"$REPORT_FOLDER"/"$INDEX_FILE"
382-
383-
wait_delete_namespace "$ns"
384433
}
385434

386435
get_suggested_namespace() {
@@ -573,23 +622,59 @@ while IFS=, read -r package_name catalog_index; do
573622

574623
echo_color "$GREY" "********* package= $package_name catalog index= $catalog_index **********"
575624

625+
# Check for suffix indicators (+ or -)
626+
skip_cleanup=false
627+
force_test_namespace=false
628+
actual_package_name="$package_name"
629+
630+
if [[ "$package_name" == *+ ]]; then
631+
# + suffix means skip uninstall and namespace deletion
632+
skip_cleanup=true
633+
actual_package_name="${package_name%+}"
634+
echo_color "$BLUE" "Package has + suffix: will skip cleanup for $actual_package_name"
635+
elif [[ "$package_name" == *- ]]; then
636+
# - suffix means use test-<packagename> namespace with normal processing
637+
force_test_namespace=true
638+
actual_package_name="${package_name%-}"
639+
echo_color "$BLUE" "Package has - suffix: will use test-$actual_package_name namespace"
640+
fi
641+
576642
# Wait for the cluster to be reachable
577643
echo_color "$BLUE" "Wait for cluster to be reachable"
578644
wait_cluster_ok
579645

580646
# Wait for package to be reachable
581-
echo_color "$BLUE" "Wait for package $package_name to be reachable"
582-
wait_package_ok "$package_name"
647+
echo_color "$BLUE" "Wait for package $actual_package_name to be reachable"
648+
wait_package_ok "$actual_package_name"
583649

584650
# Variable to hold return status
585651
status=0
586652

587-
ns=$(get_suggested_namespace "$package_name")
588-
if [ "$ns" = "" ] || [ "$ns" = "openshift-operators" ]; then
589-
echo_color "$BLUE" "no suggested namespace for $package_name, using: test-operator"
590-
ns="test-operator"
653+
# Determine namespace based on suffix and suggested namespace
654+
# Special case: odf-csi-addons-operator and mcg-operator always use openshift-storage
655+
if [ "$actual_package_name" = "odf-csi-addons-operator" ] || [ "$actual_package_name" = "mcg-operator" ]; then
656+
ns="openshift-storage"
657+
echo_color "$BLUE" "using openshift-storage namespace for $actual_package_name"
658+
elif [ "$force_test_namespace" = true ]; then
659+
# - suffix: always use test-<packagename>
660+
ns="test-$actual_package_name"
661+
echo_color "$BLUE" "using forced test namespace for $actual_package_name: $ns"
591662
else
592-
echo_color "$BLUE" "using suggested namespace for $package_name: $ns "
663+
# Normal logic or + suffix
664+
ns=$(get_suggested_namespace "$actual_package_name")
665+
if [ "$ns" = "" ] || [ "$ns" = "openshift-operators" ]; then
666+
if [ "$skip_cleanup" = true ]; then
667+
# + suffix with no suggested namespace: use test-<packagename>
668+
ns="test-$actual_package_name"
669+
echo_color "$BLUE" "no suggested namespace for $actual_package_name, using: $ns"
670+
else
671+
# No suffix with no suggested namespace: use test-operator
672+
ns="test-operator"
673+
echo_color "$BLUE" "no suggested namespace for $actual_package_name, using: test-operator"
674+
fi
675+
else
676+
echo_color "$BLUE" "using suggested namespace for $actual_package_name: $ns"
677+
fi
593678
fi
594679
echo_color "$GREY" "namespace= $ns"
595680

@@ -598,24 +683,50 @@ while IFS=, read -r package_name catalog_index; do
598683
echo_color "$RED" "Warning, cluster cleanup failed"
599684
fi
600685

601-
# If a namespace is present, it is probably stuck deleting from previous runs. Force delete it.
602-
echo_color "$BLUE" "Remove namespace if present"
603-
if ! force_delete_namespace_if_present "$ns" >>"$LOG_FILE_PATH" 2>&1; then
604-
echo_color "$RED" "Error, force deleting namespace failed"
605-
fi
686+
# Skip namespace creation for openshift-storage
687+
if [ "$ns" = "openshift-storage" ]; then
688+
echo_color "$BLUE" "Skipping namespace creation for openshift-storage (using existing namespace)"
689+
elif [ "$skip_cleanup" = true ]; then
690+
# For operators with + suffix, preserve existing namespace if present
691+
if oc get namespace "$ns" &>/dev/null; then
692+
echo_color "$BLUE" "Namespace $ns already exists, preserving it (+ suffix)"
693+
else
694+
echo_color "$BLUE" "Creating namespace $ns"
695+
if ! oc create namespace "$ns"; then
696+
echo_color "$RED" "Error, creating namespace $ns failed"
697+
fi
698+
fi
699+
else
700+
# Normal processing: force delete namespace if present, then create it
701+
echo_color "$BLUE" "Remove namespace if present"
702+
if ! force_delete_namespace_if_present "$ns" >>"$LOG_FILE_PATH" 2>&1; then
703+
echo_color "$RED" "Error, force deleting namespace failed"
704+
fi
606705

607-
if ! oc create namespace "$ns"; then
608-
echo_color "$RED" "Error, creating namespace $ns failed"
706+
if ! oc create namespace "$ns"; then
707+
echo_color "$RED" "Error, creating namespace $ns failed"
708+
fi
609709
fi
610710

611711
# Install the operator in a custom namespace
612712
echo_color "$BLUE" "install operator"
613-
if ! oc operator install --create-operator-group "$package_name" -n "$ns"; then
614-
echo_color "$RED" "Operator installation failed but will still waiting for CSV"
713+
install_status=0
714+
install_output=$(oc operator install --create-operator-group "$actual_package_name" -n "$ns" 2>&1) || install_status=$?
715+
716+
if [ "$install_status" -ne 0 ]; then
717+
# Check if it's a "already exists" error and we're using + suffix
718+
if [ "$skip_cleanup" = true ] && echo "$install_output" | grep -q "already exists"; then
719+
echo_color "$BLUE" "Operator subscription already exists (expected with + suffix), continuing..."
720+
else
721+
echo_color "$RED" "Operator installation failed but will still waiting for CSV"
722+
echo "$install_output" >>"$LOG_FILE_PATH"
723+
fi
724+
else
725+
echo "$install_output"
615726
fi
616727

617728
# Setting report directory
618-
report_dir="$REPORT_FOLDER"/"$package_name"
729+
report_dir="$REPORT_FOLDER"/"$actual_package_name"
619730

620731
# Store the results of CNF test in a new directory
621732
if ! mkdir -p "$report_dir"; then
@@ -631,11 +742,30 @@ while IFS=, read -r package_name catalog_index; do
631742
echo_color "$BLUE" "Wait for CSV to appear and label resources unde test"
632743
if ! wait_for_csv_to_appear_and_label "$ns"; then
633744
echo_color "$RED" "Operator failed to install, continue"
634-
report_failure "$status" "$ns" "$package_name" "Operator installation failed, skipping test"
745+
report_failure "$status" "$ns" "$actual_package_name" "$skip_cleanup" "Operator installation failed, skipping test"
635746
continue
636747
fi
637748

638-
echo_color "$BLUE" "operator $package_name installed"
749+
echo_color "$BLUE" "operator $actual_package_name installed"
750+
751+
# Special handling for multicluster-engine operator with + suffix
752+
if [ "$actual_package_name" = "multicluster-engine" ] && [ "$skip_cleanup" = true ]; then
753+
echo_color "$BLUE" "Creating MultiClusterEngine custom resource"
754+
if cat <<EOF | oc apply -f - >>"$LOG_FILE_PATH" 2>&1
755+
apiVersion: multicluster.openshift.io/v1
756+
kind: MultiClusterEngine
757+
metadata:
758+
name: multiclusterengine
759+
spec: {}
760+
EOF
761+
then
762+
echo_color "$BLUE" "MultiClusterEngine custom resource created successfully"
763+
echo_color "$BLUE" "Waiting for MultiClusterEngine to be ready..."
764+
sleep 30
765+
else
766+
echo_color "$RED" "Failed to create MultiClusterEngine CR"
767+
fi
768+
fi
639769

640770
echo_color "$BLUE" "Wait to ensure all pods are running"
641771
# Extra wait to ensure that all pods are running
@@ -669,7 +799,7 @@ while IFS=, read -r package_name catalog_index; do
669799
--config-file=/config/certsuite_config.yaml \
670800
--output-dir=/reports \
671801
--label-filter=all >>"$LOG_FILE_PATH" 2>&1 || {
672-
report_failure "$status" "$ns" "$package_name" "CNF suite exited with errors"
802+
report_failure "$status" "$ns" "$actual_package_name" "$skip_cleanup" "CNF suite exited with errors"
673803
continue
674804
}
675805

@@ -679,20 +809,30 @@ while IFS=, read -r package_name catalog_index; do
679809
echo_color "$RED" "Error, failed to unlabel the operator"
680810
fi
681811

682-
# remove the operator
683-
echo_color "$BLUE" "Remove operator"
684-
if ! oc operator uninstall -X "$package_name" -n "$ns"; then
685-
echo_color "$RED" "Operator failed to un-install, continue"
686-
fi
812+
# Check if cleanup should be skipped (+ suffix or legacy lvms/odf operators)
813+
if [ "$skip_cleanup" = true ] || [ "$actual_package_name" = "lvms-operator" ] || [ "$actual_package_name" = "odf-operator" ]; then
814+
echo_color "$BLUE" "Skipping uninstall and namespace deletion for $actual_package_name"
815+
else
816+
# remove the operator
817+
echo_color "$BLUE" "Remove operator"
818+
if ! oc operator uninstall -X "$actual_package_name" -n "$ns"; then
819+
echo_color "$RED" "Operator failed to un-install, continue"
820+
fi
687821

688-
# Delete the namespace
689-
if ! oc delete namespace "$ns" --wait=false; then
690-
echo_color "$RED" "Error, failed to delete namespace: $ns"
691-
fi
822+
# Skip namespace deletion for openshift-storage
823+
if [ "$ns" = "openshift-storage" ]; then
824+
echo_color "$BLUE" "Skipping namespace deletion for openshift-storage"
825+
else
826+
# Delete the namespace
827+
if ! oc delete namespace "$ns" --wait=false; then
828+
echo_color "$RED" "Error, failed to delete namespace: $ns"
829+
fi
692830

693-
echo_color "$BLUE" "Wait for cleanup to finish"
694-
if ! wait_delete_namespace "$ns"; then
695-
echo_color "$RED" "Error, fail to wait for the namespace to be deleted"
831+
echo_color "$BLUE" "Wait for cleanup to finish"
832+
if ! wait_delete_namespace "$ns"; then
833+
echo_color "$RED" "Error, fail to wait for the namespace to be deleted"
834+
fi
835+
fi
696836
fi
697837

698838
# Check parsing claim file
@@ -705,7 +845,7 @@ while IFS=, read -r package_name catalog_index; do
705845
-v "${CNF_TYPE_DIR}:/cnftype:Z" \
706846
"${CERTSUITE_IMAGE_NAME}:${CERTSUITE_IMAGE_TAG}" \
707847
/usr/local/bin/certsuite claim \
708-
show csv -t /cnftype/cnf-type.json -c /reports/claim.json -n "$package_name" "$add_headers" >>"$REPORT_FOLDER"/results.csv; then
848+
show csv -t /cnftype/cnf-type.json -c /reports/claim.json -n "$actual_package_name" "$add_headers" >>"$REPORT_FOLDER"/results.csv; then
709849
echo_color "$RED" "failed to parse claim file"
710850
fi
711851

@@ -718,16 +858,16 @@ while IFS=, read -r package_name catalog_index; do
718858
# Add per operator links
719859
{
720860
# Add parser link
721-
echo "Results for: <b>$package_name</b>, parsed details:"
722-
echo '<a href="/'"$REPORT_FOLDER_RELATIVE"'/'"$package_name"'/results.html?claimfile=/'"$REPORT_FOLDER_RELATIVE"'/'"$package_name"'/claim.json">'"link"'</a>'
861+
echo "Results for: <b>$actual_package_name</b>, parsed details:"
862+
echo '<a href="/'"$REPORT_FOLDER_RELATIVE"'/'"$actual_package_name"'/results.html?claimfile=/'"$REPORT_FOLDER_RELATIVE"'/'"$actual_package_name"'/claim.json">'"link"'</a>'
723863

724864
# Add log link
725865
echo ", log: "
726-
echo '<a href="/'"$REPORT_FOLDER_RELATIVE"'/'"$package_name"'/certsuite.log">'"link"'</a>'
866+
echo '<a href="/'"$REPORT_FOLDER_RELATIVE"'/'"$actual_package_name"'/certsuite.log">'"link"'</a>'
727867

728868
# Add certsuite_config link
729869
echo ", certsuite_config: "
730-
echo '<a href="/'"$REPORT_FOLDER_RELATIVE"'/'"$package_name"'/certsuite_config.yml">'"link"'</a>'
870+
echo '<a href="/'"$REPORT_FOLDER_RELATIVE"'/'"$actual_package_name"'/certsuite_config.yml">'"link"'</a>'
731871

732872
# new line
733873
echo "<br>"

0 commit comments

Comments
 (0)