Created
June 14, 2023 08:06
-
-
Save gabrielfalcao/1859c235769bc4c30dff347117ff3911 to your computer and use it in GitHub Desktop.
shell script to forward all the kubernetes service ports to localhost while automatically handling port numbers < 1024 (e.g.: 443 to 8443)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
for json in $(kubectl get svc -o json | jq -c '.items[] | select(.kind == "Service") | {name:.metadata.name, port:.spec.ports[0].port}'); do | |
service=$(echo "${json}" | jq .name | tr -d '"') | |
remote_port=$(echo "${json}" | jq .port | tr -d '"') | |
local_port=$((remote_port)) | |
if [ $((local_port)) -lt 1024 ]; then | |
local_port=$((local_port + 8000)) | |
fi | |
echo kubectl port-forward "service/${service}" "${local_port}:${remote_port}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment