Created
March 19, 2025 11:34
-
-
Save JonCanning/b6c0c7b950e60aeabcf4dc106828bb33 to your computer and use it in GitHub Desktop.
k8s port forward using https://google.github.io/zx/
This file contains hidden or 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 zx | |
const account = process.argv[3]; | |
const cluster = process.argv[4]; | |
const target = process.argv[5]; | |
const port = process.argv[6]; | |
if (!target || !port) { | |
console.error("Usage: forward.mjs <account> <cluster> <target> <port>"); | |
console.error("Example: forward.mjs 1234567890 cluster-dev my-api 8000"); | |
process.exit(1); | |
} | |
await $`aws eks update-kubeconfig --name ${cluster} --region eu-west-1 --profile ${account}_administrator-access`; | |
const podInfo = await $`kubectl get pods --all-namespaces -o json`; | |
const result = JSON.parse(podInfo.stdout).items.find((p) => | |
p.metadata?.name.startsWith(target) | |
); | |
if (!result) { | |
console.error(`Pod not found for target ${target}`); | |
process.exit(1); | |
} | |
const { namespace, name } = result.metadata; | |
if (!namespace || !name) { | |
console.error(`Pod not found for target ${target}`); | |
process.exit(1); | |
} | |
console.log(`Forwarding ${namespace}/${name} to ${port}`); | |
await $`kubectl port-forward -n ${namespace} pod/${name} ${port}:${port}`.pipe( | |
process.stdout | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment