Skip to content

Commit ae68d3e

Browse files
Merge pull request #1906 from alexander-demicev/ngrokfix
Remove ngrok operator for dev-env
2 parents 32484b2 + 4fba31c commit ae68d3e

File tree

1 file changed

+147
-29
lines changed

1 file changed

+147
-29
lines changed

scripts/turtles-dev.sh

Lines changed: 147 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ RANCHER_CHARTS_BASE_BRANCH=${RANCHER_CHARTS_BASE_BRANCH}
4040

4141
BASEDIR=$(dirname "$0")
4242

43+
if pgrep -x ngrok > /dev/null; then
44+
echo "Stopping existing ngrok processes..."
45+
pkill -x ngrok
46+
sleep 2
47+
fi
48+
4349
kind create cluster --config "$BASEDIR/kind-cluster-with-extramounts.yaml" --name $CLUSTER_NAME
4450
docker pull $RANCHER_IMAGE
4551
kind load docker-image $RANCHER_IMAGE --name $CLUSTER_NAME
@@ -48,7 +54,6 @@ kubectl rollout status deployment coredns -n kube-system --timeout=90s
4854

4955
helm repo add rancher-$RANCHER_CHANNEL https://releases.rancher.com/server-charts/$RANCHER_CHANNEL --force-update
5056
helm repo add jetstack https://charts.jetstack.io --force-update
51-
helm repo add ngrok https://charts.ngrok.com --force-update
5257
helm repo add gitea-charts https://dl.gitea.com/charts/ --force-update
5358
helm repo update
5459

@@ -57,36 +62,36 @@ helm install cert-manager jetstack/cert-manager \
5762
--create-namespace \
5863
--set crds.enabled=true
5964

60-
helm upgrade ngrok ngrok/ngrok-operator \
61-
--namespace ngrok \
62-
--create-namespace \
63-
--install \
64-
--wait \
65-
--timeout 5m \
66-
--set credentials.apiKey=$NGROK_API_KEY \
67-
--set credentials.authtoken=$NGROK_AUTHTOKEN
68-
kubectl apply -f test/e2e/data/rancher/ingress-class-patch.yaml
69-
7065
helm install gitea gitea-charts/gitea \
7166
-f test/e2e/data/gitea/values.yaml \
7267
--set gitea.admin.password=$GITEA_PASSWORD \
7368
--wait
7469

75-
envsubst <test/e2e/data/gitea/ingress.yaml | kubectl apply -f -
70+
cleanup() {
71+
echo "Cleaning up background processes..."
72+
[ -n "$NGROK_PID" ] && kill $NGROK_PID 2>/dev/null && echo "Stopped ngrok (PID: $NGROK_PID)"
73+
[ -n "$RANCHER_PORT_FORWARD_PID" ] && kill $RANCHER_PORT_FORWARD_PID 2>/dev/null && echo "Stopped Rancher port-forward (PID: $RANCHER_PORT_FORWARD_PID)"
74+
[ -n "$GITEA_PORT_FORWARD_PID" ] && kill $GITEA_PORT_FORWARD_PID 2>/dev/null && echo "Stopped Gitea port-forward (PID: $GITEA_PORT_FORWARD_PID)"
75+
[ -f "$NGROK_CONFIG_FILE" ] && rm -f "$NGROK_CONFIG_FILE" && echo "Removed ngrok config file"
76+
}
77+
trap cleanup EXIT INT TERM
7678

7779
# Build and load the controller image
7880
make docker-build-prime
7981
kind load docker-image $TURTLES_IMAGE --name $CLUSTER_NAME
8082

81-
# Create Gitea repo for the Rancher charts fork
82-
until [ "$(curl -s -o /dev/null -w "%{http_code}" https://gitea.$RANCHER_HOSTNAME)" = "200" ]; do echo "Waiting for gitea"; sleep 1; done;
83-
curl -X POST "https://gitea:$GITEA_PASSWORD@gitea.$RANCHER_HOSTNAME/api/v1/user/repos" \
84-
-H 'Accept: application/json' \
85-
-H 'Content-Type: application/json' \
86-
-d '{"name":"charts"}'
87-
# Push to repo
88-
git -C $RANCHER_CHARTS_REPO_DIR remote add fork https://gitea:$GITEA_PASSWORD@gitea.$RANCHER_HOSTNAME/gitea/charts.git
89-
git -C $RANCHER_CHARTS_REPO_DIR push fork --force
83+
# Start port-forwarding for Gitea
84+
echo "Starting port-forward for Gitea..."
85+
kubectl port-forward --namespace default svc/gitea-http 10001:3000 > /tmp/gitea-port-forward.log 2>&1 &
86+
GITEA_PORT_FORWARD_PID=$!
87+
88+
# Wait for Gitea to be accessible locally
89+
echo "Waiting for Gitea to be accessible on localhost:10001..."
90+
until curl -s -o /dev/null -w "%{http_code}" http://localhost:10001 | grep -q "200\|302\|301"; do
91+
echo "Waiting for local gitea port-forward..."
92+
sleep 2
93+
done
94+
echo "Gitea is accessible locally!"
9095

9196
helm install rancher rancher-$RANCHER_CHANNEL/rancher \
9297
--namespace cattle-system \
@@ -95,21 +100,134 @@ helm install rancher rancher-$RANCHER_CHANNEL/rancher \
95100
--set replicas=1 \
96101
--set hostname="$RANCHER_HOSTNAME" \
97102
--set image.tag=$RANCHER_IMAGE_TAG \
98-
--set extraEnv[0].name=CATTLE_CHART_DEFAULT_URL \
99-
--set extraEnv[0].value=https://gitea.$RANCHER_HOSTNAME/gitea/charts.git \
100-
--set extraEnv[1].name=CATTLE_CHART_DEFAULT_BRANCH \
101-
--set extraEnv[1].value=$RANCHER_CHARTS_BASE_BRANCH \
102-
--set extraEnv[2].name=CATTLE_RANCHER_TURTLES_VERSION \
103-
--set extraEnv[2].value=$RANCHER_CHART_DEV_VERSION \
104103
--set debug=true \
105104
--version="$RANCHER_VERSION" \
106105
--wait
107106

108-
kubectl apply -f test/e2e/data/rancher/rancher-service-patch.yaml
109-
envsubst <test/e2e/data/rancher/ingress.yaml | kubectl apply -f -
107+
# Start port-forwarding for Rancher
108+
echo "Starting port-forward for Rancher..."
109+
kubectl port-forward --namespace cattle-system svc/rancher 10000:80 > /tmp/rancher-port-forward.log 2>&1 &
110+
RANCHER_PORT_FORWARD_PID=$!
111+
echo "Rancher port-forward started with PID: $RANCHER_PORT_FORWARD_PID"
112+
113+
# Wait for Rancher to be accessible locally
114+
echo "Waiting for Rancher to be accessible on localhost:10000..."
115+
until curl -s -o /dev/null -w "%{http_code}" http://localhost:10000 | grep -q "200\|302\|301"; do
116+
echo "Waiting for local rancher port-forward..."
117+
sleep 2
118+
done
119+
echo "Rancher is accessible locally!"
120+
121+
# Now both services are ready, start ngrok with both endpoints
122+
NGROK_CONFIG_FILE="/tmp/ngrok-turtles-dev.yml"
123+
cat > "$NGROK_CONFIG_FILE" <<EOF
124+
version: 2
125+
authtoken: $NGROK_AUTHTOKEN
126+
tunnels:
127+
rancher:
128+
proto: http
129+
addr: http://localhost:10000
130+
hostname: $RANCHER_HOSTNAME
131+
gitea:
132+
proto: http
133+
addr: http://localhost:10001
134+
hostname: gitea.$RANCHER_HOSTNAME
135+
EOF
136+
137+
echo "Starting ngrok with both Rancher and Gitea endpoints..."
138+
ngrok start --all --config "$NGROK_CONFIG_FILE" --log stdout > /tmp/ngrok-turtles-dev.log 2>&1 &
139+
NGROK_PID=$!
140+
echo "ngrok started with PID: $NGROK_PID"
141+
sleep 5
142+
143+
if ! kill -0 $NGROK_PID 2>/dev/null; then
144+
echo "ERROR: ngrok failed, check logs:"
145+
cat /tmp/ngrok-turtles-dev.log
146+
exit 1
147+
fi
148+
149+
# Wait for both endpoints to be accessible via ngrok
150+
echo "Waiting for Gitea to be accessible via ngrok (https://gitea.$RANCHER_HOSTNAME)..."
151+
RETRY_COUNT=0
152+
MAX_RETRIES=30
153+
until [ "$(curl -s -o /dev/null -w "%{http_code}" https://gitea.$RANCHER_HOSTNAME)" = "200" ]; do
154+
RETRY_COUNT=$((RETRY_COUNT+1))
155+
if [ $RETRY_COUNT -gt $MAX_RETRIES ]; then
156+
echo "ERROR: Gitea not accessible via ngrok after $MAX_RETRIES attempts"
157+
echo "ngrok logs:"
158+
cat /tmp/ngrok-turtles-dev.log
159+
exit 1
160+
fi
161+
echo "Waiting for gitea via ngrok (attempt $RETRY_COUNT/$MAX_RETRIES)..."
162+
sleep 2
163+
done
164+
echo "Gitea is accessible via ngrok!"
165+
166+
echo "Waiting for Rancher to be accessible via ngrok (https://$RANCHER_HOSTNAME)..."
167+
RETRY_COUNT=0
168+
until [ "$(curl -s -o /dev/null -w "%{http_code}" https://$RANCHER_HOSTNAME)" = "200" ] || [ "$(curl -s -o /dev/null -w "%{http_code}" https://$RANCHER_HOSTNAME)" = "302" ]; do
169+
RETRY_COUNT=$((RETRY_COUNT+1))
170+
if [ $RETRY_COUNT -gt $MAX_RETRIES ]; then
171+
echo "ERROR: Rancher not accessible via ngrok after $MAX_RETRIES attempts"
172+
echo "ngrok logs:"
173+
cat /tmp/ngrok-turtles-dev.log
174+
exit 1
175+
fi
176+
echo "Waiting for rancher via ngrok (attempt $RETRY_COUNT/$MAX_RETRIES)..."
177+
sleep 2
178+
done
179+
echo "Rancher is accessible via ngrok!"
180+
181+
# Now setup Gitea repo and push charts
182+
curl -X POST "https://gitea:$GITEA_PASSWORD@gitea.$RANCHER_HOSTNAME/api/v1/user/repos" \
183+
-H 'Accept: application/json' \
184+
-H 'Content-Type: application/json' \
185+
-d '{"name":"charts"}'
186+
187+
git -C $RANCHER_CHARTS_REPO_DIR remote add fork https://gitea:$GITEA_PASSWORD@gitea.$RANCHER_HOSTNAME/gitea/charts.git
188+
189+
# These git config are needed to make the push work fine through ngrok tunnel
190+
git -C $RANCHER_CHARTS_REPO_DIR config http.postBuffer 1048576000 # 1 GB
191+
git -C $RANCHER_CHARTS_REPO_DIR config http.lowSpeedLimit 0
192+
git -C $RANCHER_CHARTS_REPO_DIR config http.lowSpeedTime 999999
193+
git -C $RANCHER_CHARTS_REPO_DIR config http.version HTTP/1.1
194+
git -C $RANCHER_CHARTS_REPO_DIR config pack.windowMemory 256m
195+
git -C $RANCHER_CHARTS_REPO_DIR config pack.packSizeLimit 256m
196+
git -C $RANCHER_CHARTS_REPO_DIR config pack.threads 1
197+
198+
echo "Pushing charts repository to Gitea (this may take several minutes)..."
199+
PUSH_RETRIES=3
200+
PUSH_COUNT=0
201+
while [ $PUSH_COUNT -lt $PUSH_RETRIES ]; do
202+
PUSH_COUNT=$((PUSH_COUNT+1))
203+
echo "Push attempt $PUSH_COUNT/$PUSH_RETRIES..."
204+
205+
if git -C $RANCHER_CHARTS_REPO_DIR push fork --force 2>&1 | tee /tmp/git-push.log; then
206+
echo "Successfully pushed charts repository!"
207+
break
208+
else
209+
if [ $PUSH_COUNT -lt $PUSH_RETRIES ]; then
210+
echo "Push failed, waiting 10 seconds before retry..."
211+
sleep 10
212+
else
213+
echo "ERROR: Failed to push charts repository after $PUSH_RETRIES attempts."
214+
exit 1
215+
fi
216+
fi
217+
done
218+
110219
envsubst <test/e2e/data/rancher/rancher-setting-patch.yaml | kubectl apply -f -
111220
kubectl apply -f test/e2e/data/rancher/system-store-setting-patch.yaml
112221

222+
# Update Rancher deployment with environment variables pointing to Gitea charts
223+
kubectl set env deployment/rancher -n cattle-system \
224+
CATTLE_CHART_DEFAULT_URL=https://gitea.$RANCHER_HOSTNAME/gitea/charts.git \
225+
CATTLE_CHART_DEFAULT_BRANCH=$RANCHER_CHARTS_BASE_BRANCH \
226+
CATTLE_RANCHER_TURTLES_VERSION=$RANCHER_CHART_DEV_VERSION
227+
228+
# Wait for Rancher to restart with new config
229+
kubectl rollout status deployment/rancher -n cattle-system --timeout=300s
230+
113231
install_local_providers_chart() {
114232
make build-providers-chart
115233

0 commit comments

Comments
 (0)