Skip to content

Commit 5636e11

Browse files
authored
Update gitea setup to query healthz url and add retry mech for repo creation (#351)
1 parent 00d48c7 commit 5636e11

1 file changed

Lines changed: 79 additions & 4 deletions

File tree

scripts/install-dev-gitea-setup.sh

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ function h1() {
3131
echo "** $*"
3232
echo
3333
}
34+
35+
# Retry function for curl commands
36+
function retry_curl() {
37+
local max_attempts=$1
38+
local delay=$2
39+
shift 2
40+
local attempt=1
41+
42+
while [ $attempt -le $max_attempts ]; do
43+
if "$@"; then
44+
return 0
45+
fi
46+
echo "Attempt $attempt/$max_attempts failed, retrying in ${delay} seconds..."
47+
sleep $delay
48+
attempt=$((attempt + 1))
49+
done
50+
51+
echo "ERROR: Command failed after $max_attempts attempts: $*"
52+
return 1
53+
}
3454

3555
h1 Install Gitea and init test repos
3656
mkdir -p "${git_root}/.build"
@@ -60,13 +80,52 @@ fi
6080
kpt fn render
6181
kpt live init || true
6282
kpt live apply --inventory-policy=adopt
63-
echo "Waiting for gitea to become ready..."
64-
kubectl wait pod --all --for=condition=Ready --namespace=gitea --timeout=90s
83+
echo "Waiting for gitea deployment to become ready..."
84+
kubectl wait deployment gitea --for=condition=Available --namespace=gitea --timeout=120s
85+
echo "Waiting for gitea pod to become ready..."
86+
kubectl wait pod -l app=gitea --for=condition=Ready --namespace=gitea --timeout=90s
6587

6688
###############################################################
6789

6890
h1 Create git repos in gitea
69-
curl -v -k -H "content-type: application/json" "http://nephio:secret@localhost:3000/api/v1/user/repos" --data "{\"name\":\"$git_repo_name\"}"
91+
92+
# Ensure port-forward is working and wait for Gitea API to be ready
93+
echo "Setting up port-forward to Gitea service..."
94+
kubectl port-forward -n gitea svc/gitea-lb 3000:3000 >/dev/null 2>&1 &
95+
PORT_FORWARD_PID=$!
96+
sleep 3
97+
98+
# Wait for Gitea API to be ready with retry logic
99+
echo "Waiting for Gitea API to be ready..."
100+
max_attempts=30
101+
attempt=1
102+
while [ $attempt -le $max_attempts ]; do
103+
if curl -s -f "http://localhost:3000/api/healthz" >/dev/null 2>&1; then
104+
echo "Gitea API is ready after $attempt attempts"
105+
break
106+
fi
107+
echo "Attempt $attempt/$max_attempts: Gitea API not ready, waiting 5 seconds..."
108+
sleep 5
109+
attempt=$((attempt + 1))
110+
done
111+
112+
if [ $attempt -gt $max_attempts ]; then
113+
echo "ERROR: Gitea API failed to become ready after $max_attempts attempts"
114+
kill $PORT_FORWARD_PID 2>/dev/null || true
115+
exit 1
116+
fi
117+
118+
# Create repository with retry logic (handle existing repos gracefully)
119+
if curl -s -f "http://nephio:secret@localhost:3000/api/v1/repos/nephio/$git_repo_name" >/dev/null 2>&1; then
120+
echo "Repository $git_repo_name already exists, skipping creation"
121+
else
122+
if retry_curl 5 3 curl -s -f -H "content-type: application/json" "http://nephio:secret@localhost:3000/api/v1/user/repos" --data "{\"name\":\"$git_repo_name\"}"; then
123+
echo "Successfully created repository $git_repo_name"
124+
else
125+
echo "ERROR: Failed to create repository $git_repo_name"
126+
exit 1
127+
fi
128+
fi
70129
TMP_DIR=$(mktemp -d)
71130
cd "$TMP_DIR"
72131
git clone "http://nephio:secret@localhost:3000/nephio/$git_repo_name.git"
@@ -86,7 +145,18 @@ cd "${git_root}"
86145
rm -fr "$TMP_DIR"
87146

88147
test_git_repo_name="test-blueprints"
89-
curl -v -k -H "content-type: application/json" "http://nephio:secret@localhost:3000/api/v1/user/repos" --data "{\"name\":\"$test_git_repo_name\"}"
148+
149+
# Create test-blueprints repository with retry logic (handle existing repos gracefully)
150+
if curl -s -f "http://nephio:secret@localhost:3000/api/v1/repos/nephio/$test_git_repo_name" >/dev/null 2>&1; then
151+
echo "Repository $test_git_repo_name already exists, skipping creation"
152+
else
153+
if retry_curl 5 3 curl -s -f -H "content-type: application/json" "http://nephio:secret@localhost:3000/api/v1/user/repos" --data "{\"name\":\"$test_git_repo_name\"}"; then
154+
echo "Successfully created repository $test_git_repo_name"
155+
else
156+
echo "ERROR: Failed to create repository $test_git_repo_name"
157+
exit 1
158+
fi
159+
fi
90160
TMP_DIR=$(mktemp -d)
91161
cd "$TMP_DIR"
92162
git clone "${git_root}/test/pkgs/test-pkgs/test-blueprints.bundle" -b main
@@ -100,3 +170,8 @@ git push -u origin --tags
100170

101171
cd "${git_root}"
102172
rm -fr "$TMP_DIR"
173+
174+
# Clean up port-forward
175+
echo "Cleaning up port-forward..."
176+
kill $PORT_FORWARD_PID 2>/dev/null || true
177+
echo "Gitea setup completed successfully"

0 commit comments

Comments
 (0)