Skip to content

Commit e977f85

Browse files
committed
fix(deploy,olm): Allow installing previous (non-latest) versions of RHDH operator from catalog
Signed-off-by: Pavel Macík <pavel.macik@gmail.com>
1 parent f03b9ed commit e977f85

File tree

4 files changed

+113
-85
lines changed

4 files changed

+113
-85
lines changed

ci-scripts/rhdh-setup/common.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
TMP_DIR=${TMP_DIR:-$(python3 -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' .tmp)}
4+
mkdir -p "$TMP_DIR"
5+
6+
cli="oc"
7+
8+
log() {
9+
echo "{\"level\":\"${2:-info}\",\"ts\":\"$(date -u -Ins)\",\"message\":\"$1\"}"
10+
}
11+
12+
log_info() {
13+
log "$1" "info"
14+
}
15+
16+
log_warn() {
17+
log "$1" "warn"
18+
}
19+
20+
log_error() {
21+
log "$1" "error"
22+
}
23+
24+
log_token() {
25+
log "$1" "$2" >>"$TMP_DIR/get_token.log"
26+
}
27+
28+
log_token_info() {
29+
log_token "$1" "info"
30+
}
31+
32+
log_token_err() {
33+
log_token "$1" "error"
34+
}
35+
36+
wait_and_approve_install_plans() {
37+
namespace=${1:-namespace}
38+
initial_timeout=${2:-300}
39+
component_prefix=${3:-}
40+
description=${4:-"install plans in $namespace"}
41+
timeout_timestamp=$(python3 -c "from datetime import datetime, timedelta; t_add=int('$initial_timeout'); print(int((datetime.now() + timedelta(seconds=t_add)).timestamp()))")
42+
interval=10
43+
44+
log_info "Waiting for unapproved install plans in $namespace namespace..."
45+
46+
# Wait for install plans to appear with timeout
47+
install_plans=""
48+
for ((i = 0; i < initial_timeout; i += interval)); do
49+
install_plans=$($cli get installplan -n "$namespace" --sort-by=.metadata.creationTimestamp -o json | jq -r --arg prefix "$component_prefix" '.items[] | select(any(.spec.clusterServiceVersionNames[]; startswith($prefix))) | select(.spec.approved == false) | .metadata.name' 2>/dev/null)
50+
51+
echo "install_plans: $install_plans"
52+
if [ -n "$install_plans" ]; then
53+
break
54+
fi
55+
56+
if [ "$(date "+%s")" -gt "$timeout_timestamp" ]; then
57+
log_error "Timeout waiting for $description"
58+
exit 1
59+
fi
60+
61+
log_info "Waiting ${interval}s for $description..."
62+
sleep "$interval"
63+
done
64+
65+
# Approve each install plan found
66+
if [ -n "$install_plans" ]; then
67+
log_info "Found unapproved install plans in $namespace namespace, approving all..."
68+
for install_plan in $install_plans; do
69+
log_info "Approving install plan '$install_plan'..."
70+
$cli patch installplan "$install_plan" -n "$namespace" --type merge --patch '{"spec":{"approved":true}}'
71+
done
72+
return $?
73+
else
74+
log_error "No unapproved install plans found in $namespace namespace within timeout"
75+
exit 1
76+
fi
77+
}
78+
79+
export -f log_info log_warn log_error log_token log_token_info log_token_err wait_and_approve_install_plans

ci-scripts/rhdh-setup/create_resource.sh

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
44
# shellcheck disable=SC1090,SC1091
55
source "$(python3 -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$SCRIPT_DIR"/../../test.env)"
66

7+
# shellcheck disable=SC1091
8+
source "${SCRIPT_DIR}/common.sh"
9+
710
export TMP_DIR WORKDIR
811

912
POPULATION_CONCURRENCY=${POPULATION_CONCURRENCY:-10}
@@ -19,7 +22,7 @@ COOKIE="$TMP_DIR/cookie.jar"
1922

2023
keycloak_url() {
2124
f="$TMP_DIR/keycloak.url"
22-
if command -v flock >/dev/null 2>&1; then
25+
if command -v flock >/dev/null 2>&1; then
2326
exec 4>"$kc_lockfile"
2427
flock 4 || {
2528
echo "Failed to acquire lock"
@@ -30,7 +33,7 @@ keycloak_url() {
3033
echo -n "https://$(oc get routes keycloak -n "${RHDH_NAMESPACE}" -o jsonpath='{.spec.host}')" >"$f"
3134
fi
3235
flock -u 4
33-
elif command -v shlock >/dev/null 2>&1; then
36+
elif command -v shlock >/dev/null 2>&1; then
3437
LOCKFILE="$TMP_DIR/kc_lockfile"
3538
trap 'rm -f "$LOCKFILE"' EXIT
3639

@@ -148,8 +151,8 @@ clone_and_upload() {
148151

149152
files=()
150153
while IFS= read -r line; do
151-
files+=("$line")
152-
done <<< "$out"
154+
files+=("$line")
155+
done <<<"$out"
153156

154157
for filename in "${files[@]}"; do
155158
cp -vf "$filename" "$(basename "$filename")"
@@ -224,7 +227,7 @@ get_group_id_by_name() {
224227
response=$(curl -s -k --location --request GET "$(keycloak_url)/auth/admin/realms/backstage/groups?search=${group_name}" \
225228
-H 'Content-Type: application/json' \
226229
-H "Authorization: Bearer $token" 2>&1)
227-
230+
228231
if [[ "$response" == "["* ]] && [[ "$response" == *"]" ]] && [[ "$response" != "[]" ]]; then
229232
group_id=$(echo "$response" | jq -r --stream --arg name "$group_name" '
230233
[., inputs] |
@@ -255,19 +258,19 @@ assign_parent_group() {
255258
max_attempts=5
256259
attempt=1
257260
parent_id=""
258-
while (( attempt <= max_attempts )); do
261+
while ((attempt <= max_attempts)); do
259262
parent_id="$(get_group_id_by_name "$parent_group_name")"
260263
[ -n "$parent_id" ] && [ "$parent_id" != "null" ] && break
261264
log_warn "Parent $parent_group_name not found (attempt $attempt). Waiting..." >>"$TMP_DIR/create_group.log"
262-
((attempt++));
265+
((attempt++))
263266
done
264267
if [ -z "$parent_id" ] || [ "$parent_id" = "null" ]; then
265268
log_error "Parent $parent_group_name missing after $max_attempts attempts; cannot create $child_name" 2>&1 | tee -a "$TMP_DIR/create_group.log"
266269
return 1
267270
fi
268271

269272
attempt=1
270-
while (( attempt <= max_attempts )); do
273+
while ((attempt <= max_attempts)); do
271274
token=$(get_token)
272275
response="$(curl -s -k --location --request POST "$(keycloak_url)/auth/admin/realms/backstage/groups/${parent_id}/children" \
273276
-H 'Content-Type: application/json' -H "Authorization: Bearer $token" \
@@ -277,7 +280,7 @@ assign_parent_group() {
277280
return 0
278281
fi
279282
log_warn "Unable to create child $child_name under $parent_group_name at attempt $attempt. [$response]" >>"$TMP_DIR/create_group.log"
280-
((attempt++));
283+
((attempt++))
281284
done
282285
log_error "Unable to create child $child_name under $parent_group_name in $max_attempts attempts" 2>&1 | tee -a "$TMP_DIR/create_group.log"
283286
return 1
@@ -304,7 +307,7 @@ create_group() {
304307
return
305308
fi
306309
log_warn "Unable to create $groupname at attempt $attempt. [$response]" >>"$TMP_DIR/create_group.log"
307-
((attempt++));
310+
((attempt++))
308311
done
309312
log_error "Unable to create the $groupname group in $max_attempts attempts, giving up!" 2>&1 | tee -a "$TMP_DIR/create_group.log"
310313
return 1
@@ -327,7 +330,7 @@ create_group() {
327330
return
328331
fi
329332
log_warn "Unable to create $groupname at attempt $attempt. [$response]" >>"$TMP_DIR/create_group.log"
330-
((attempt++));
333+
((attempt++))
331334
done
332335
log_error "Unable to create the $groupname group in $max_attempts attempts, giving up!" 2>&1 | tee -a "$TMP_DIR/create_group.log"
333336
return 1
@@ -405,7 +408,7 @@ create_groups() {
405408
[ "$N" -gt "$GROUP_COUNT" ] && N="$GROUP_COUNT"
406409
seq 1 "$N" | xargs -P1 -I{} bash -lc "create_group \"\$1\"" _ {}
407410
if [ "$GROUP_COUNT" -gt "$N" ]; then
408-
seq $((N+1)) "$GROUP_COUNT" | xargs -P"${POPULATION_CONCURRENCY}" -I{} bash -lc "create_group \"\$1\"" _ {}
411+
seq $((N + 1)) "$GROUP_COUNT" | xargs -P"${POPULATION_CONCURRENCY}" -I{} bash -lc "create_group \"\$1\"" _ {}
409412
fi
410413
else
411414
seq 1 "$GROUP_COUNT" | xargs -P"${POPULATION_CONCURRENCY}" -I{} bash -lc "create_group \"\$1\"" _ {}
@@ -466,7 +469,7 @@ create_user() {
466469
done
467470

468471
if [[ $attempt -gt $max_attempts ]]; then
469-
log_error "Unable to create the $username user in $max_attempts attempts, giving up!" 2>&1| tee -a "$TMP_DIR/create_user.log"
472+
log_error "Unable to create the $username user in $max_attempts attempts, giving up!" 2>&1 | tee -a "$TMP_DIR/create_user.log"
470473
fi
471474
}
472475

@@ -478,33 +481,6 @@ create_users() {
478481
}
479482

480483
token_lockfile="$TMP_DIR/token.lockfile"
481-
log() {
482-
echo "{\"level\":\"${2:-info}\",\"ts\":\"$(date -u -Ins)\",\"message\":\"$1\"}"
483-
}
484-
485-
log_info() {
486-
log "$1" "info"
487-
}
488-
489-
log_warn() {
490-
log "$1" "warn"
491-
}
492-
493-
log_error() {
494-
log "$1" "error"
495-
}
496-
497-
log_token() {
498-
log "$1" "$2" >>"$TMP_DIR/get_token.log"
499-
}
500-
501-
log_token_info() {
502-
log_token "$1" "info"
503-
}
504-
505-
log_token_err() {
506-
log_token "$1" "error"
507-
}
508484

509485
keycloak_token() {
510486
curl -s -k "$(keycloak_url)/auth/realms/master/protocol/openid-connect/token" -d username=admin -d "password=$1" -d 'grant_type=password' -d 'client_id=admin-cli' | jq -r ".expires_in_timestamp = $(python3 -c 'from datetime import datetime, timedelta; t_add=int(30); print(int((datetime.now() + timedelta(seconds=t_add)).timestamp()))')"
@@ -534,7 +510,7 @@ rhdh_token() {
534510
--data-urlencode "redirect_uri=${REDIRECT_URL}" \
535511
--data-urlencode "scope=openid email profile" \
536512
--data-urlencode "response_type=code" \
537-
"$(keycloak_url)/auth/realms/$REALM/protocol/openid-connect/auth" 2>&1| tee "$TMP_DIR/auth_url.log" | grep -oE 'action="[^"]+"' | grep -oE '"[^"]+"' | tr -d '"')
513+
"$(keycloak_url)/auth/realms/$REALM/protocol/openid-connect/auth" 2>&1 | tee "$TMP_DIR/auth_url.log" | grep -oE 'action="[^"]+"' | grep -oE '"[^"]+"' | tr -d '"')
538514

539515
execution=$(echo "$AUTH_URL" | grep -oE 'execution=[^&]+' | grep -oE '[^=]+$')
540516
tab_id=$(echo "$AUTH_URL" | grep -oE 'tab_id=[^&]+' | grep -oE '[^=]+$')
@@ -547,7 +523,7 @@ rhdh_token() {
547523
--data-urlencode "tab_id=${tab_id}" \
548524
--data-urlencode "execution=${execution}" \
549525
--write-out "%{redirect_url}" \
550-
"$AUTHENTICATE_URL" 2>&1| tee "$TMP_DIR/code_url.log")
526+
"$AUTHENTICATE_URL" 2>&1 | tee "$TMP_DIR/code_url.log")
551527

552528
code=$(echo "$CODE_URL" | grep -oE 'code=[^&]+' | grep -oE '[^=]+$')
553529
session_state=$(echo "$CODE_URL" | grep -oE 'session_state=[^&]+' | grep -oE '[^=]+$')

ci-scripts/rhdh-setup/deploy.sh

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
55
# shellcheck disable=SC1090,SC1091
66
source "$(python3 -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$SCRIPT_DIR"/../../test.env)"
77

8+
# shellcheck disable=SC1091
9+
source "${SCRIPT_DIR}/common.sh"
10+
811
# shellcheck disable=SC1091
912
source "${SCRIPT_DIR}/create_resource.sh"
1013

@@ -123,47 +126,6 @@ wait_for_crd() {
123126
done
124127
}
125128

126-
wait_and_approve_install_plans() {
127-
namespace=${1:-namespace}
128-
initial_timeout=${2:-300}
129-
description=${3:-"install plans in $namespace"}
130-
timeout_timestamp=$(python3 -c "from datetime import datetime, timedelta; t_add=int('$initial_timeout'); print(int((datetime.now() + timedelta(seconds=t_add)).timestamp()))")
131-
interval=10
132-
133-
log_info "Waiting for unapproved install plans in $namespace namespace..."
134-
135-
# Wait for install plans to appear with timeout
136-
install_plans=""
137-
for ((i = 0; i < initial_timeout; i += interval)); do
138-
install_plans=$($cli get installplan -n "$namespace" --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[?(@.spec.approved==false)].metadata.name}' 2>/dev/null)
139-
140-
if [ -n "$install_plans" ]; then
141-
break
142-
fi
143-
144-
if [ "$(date "+%s")" -gt "$timeout_timestamp" ]; then
145-
log_error "Timeout waiting for $description"
146-
exit 1
147-
fi
148-
149-
log_info "Waiting ${interval}s for $description..."
150-
sleep "$interval"
151-
done
152-
153-
# Approve each install plan found
154-
if [ -n "$install_plans" ]; then
155-
log_info "Found unapproved install plans in $namespace namespace, approving all..."
156-
for install_plan in $install_plans; do
157-
log_info "Approving install plan '$install_plan'..."
158-
$cli patch installplan "$install_plan" -n "$namespace" --type merge --patch '{"spec":{"approved":true}}'
159-
done
160-
return $?
161-
else
162-
log_error "No unapproved install plans found in $namespace namespace within timeout"
163-
exit 1
164-
fi
165-
}
166-
167129
is_orchestrator_infra_installed() {
168130
helm list -n "${RHDH_NAMESPACE}" -q | grep -q "^${RHDH_HELM_RELEASE_NAME}-orchestrator-infra$"
169131
return $?
@@ -343,14 +305,14 @@ backstage_install() {
343305
if ${ENABLE_RBAC}; then
344306
cp template/backstage/rbac-config.yaml "${TMP_DIR}/rbac-config.yaml"
345307
if [[ $RBAC_POLICY == "$RBAC_POLICY_REALISTIC" ]]; then
346-
cat template/backstage/realistic-rbac-config.yaml >> "${TMP_DIR}/rbac-config.yaml"
308+
cat template/backstage/realistic-rbac-config.yaml >>"${TMP_DIR}/rbac-config.yaml"
347309
fi
348310
create_rbac_policy "$RBAC_POLICY"
349311
cat "$TMP_DIR/group-rbac.yaml" >>"$TMP_DIR/rbac-config.yaml"
350312
if [[ "$INSTALL_METHOD" == "helm" ]] && ${ENABLE_ORCHESTRATOR}; then
351313
cat template/backstage/helm/orchestrator-rbac-patch.yaml >>"$TMP_DIR/rbac-config.yaml"
352314
if [[ $RBAC_POLICY == "$RBAC_POLICY_REALISTIC" ]]; then
353-
cat template/backstage/helm/realistic-orchestrator-rbac-patch.yaml>>"${TMP_DIR}/rbac-config.yaml"
315+
cat template/backstage/helm/realistic-orchestrator-rbac-patch.yaml >>"${TMP_DIR}/rbac-config.yaml"
354316
fi
355317
fi
356318
until $clin create -f "$TMP_DIR/rbac-config.yaml"; do $clin delete configmap rbac-policy --ignore-not-found=true; done

ci-scripts/rhdh-setup/install-rhdh-catalog-source.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
2121
# shellcheck disable=SC1090,SC1091
2222
source "$(python3 -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$SCRIPT_DIR"/../../test.env)"
2323

24+
# shellcheck disable=SC1091
25+
source "${SCRIPT_DIR}/common.sh"
26+
2427
set -e
2528

2629
RED='\033[0;31m'
@@ -279,5 +282,13 @@ if [ -n "${RHDH_OLM_OPERATOR_RESOURCES_CPU_LIMITS}" ]; then yq -i '.spec.config.
279282
if [ -n "${RHDH_OLM_OPERATOR_RESOURCES_MEMORY_REQUESTS}" ]; then yq -i '.spec.config.resources.requests.memory = "'"${RHDH_OLM_OPERATOR_RESOURCES_MEMORY_REQUESTS}"'"' "$subscription"; fi
280283
if [ -n "${RHDH_OLM_OPERATOR_RESOURCES_MEMORY_LIMITS}" ]; then yq -i '.spec.config.resources.limits.memory = "'"${RHDH_OLM_OPERATOR_RESOURCES_MEMORY_LIMITS}"'"' "$subscription"; fi
281284
if [ -n "${RHDH_OLM_OPERATOR_RESOURCES_EPHEMERAL_STORAGE_REQUESTS}" ]; then yq -i '.spec.config.resources.requests.ephemeral-storage = "'"${RHDH_OLM_OPERATOR_RESOURCES_EPHEMERAL_STORAGE_REQUESTS}"'"' "$subscription"; fi
285+
if [ -n "${RHDH_OLM_OPERATOR_VERSION:-}" ]; then
286+
yq -i '.spec.startingCSV = "rhdh-operator.v'"${RHDH_OLM_OPERATOR_VERSION}"'"' "$subscription"
287+
yq -i '.spec.installPlanApproval = "Manual"' "$subscription"
288+
fi
282289

283290
oc apply -f "$subscription"
291+
292+
if [ -n "${RHDH_OLM_OPERATOR_VERSION:-}" ]; then
293+
wait_and_approve_install_plans "$NAMESPACE_SUBSCRIPTION" 300 "rhdh-operator.v${RHDH_OLM_OPERATOR_VERSION}" "install plan for $TO_INSTALL"
294+
fi

0 commit comments

Comments
 (0)