Skip to content

Commit

Permalink
wait-for-port-listen: init, use for port forward
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Meyer <[email protected]>
  • Loading branch information
katexochen committed Jan 24, 2024
1 parent 1d1a96f commit db803fc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ set:
kubectl -n $ns port-forward pod/port-forwarder-coordinator 1313 &
PID=$!
trap "kill $PID" EXIT
sleep 1
nix run .#wait-for-port-listen -- 1313
t=$(date +%s)
nix run .#cli -- set \
-m ./{{ workspace_dir }}/manifest.json \
Expand All @@ -93,7 +93,7 @@ verify:
kubectl -n $ns port-forward pod/port-forwarder-coordinator 1314:1313 &
PID=$!
trap "kill $PID" EXIT
sleep 1
nix run .#wait-for-port-listen -- 1314
t=$(date +%s)
nix run .#cli -- verify \
-c localhost:1314 \
Expand Down
32 changes: 32 additions & 0 deletions packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,36 @@ rec {
pods
'';
};

wait-for-port-listen = writeShellApplication {
name = "wait-for-port-listen";
runtimeInputs = [ iproute2 ];
text = ''
port=$1
function ss-listen-on-port() {
ss \
--tcp \
--numeric \
--listening \
--no-header \
--ipv4 \
src ":$port"
}
tries=15 # 3 seconds
interval=0.2
while [[ "$tries" -gt 0 ]]; do
if [[ -n $(ss-listen-on-port) ]]; then
exit 0
fi
sleep "$interval"
tries=$((tries - 1))
done
echo "Port $port did not reach state LISTENING" >&2
exit 1
'';
};
}

0 comments on commit db803fc

Please sign in to comment.