-
Notifications
You must be signed in to change notification settings - Fork 563
feat: allow port forward to deployment and service #2749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3dffec5
feat: allow port forward to deployment and service
mstruebing 3f250b8
chore: adjust exampels to use `??` instead of `||` and use node directly
mstruebing d8e9ed0
chore: remove unnecessary timeout
mstruebing bdda1c1
chore: extract duplicated logic into reusable function
mstruebing 0742245
chore: use async-await and try-catch instead of promise
mstruebing 0ad0554
chore: make typescript happy
mstruebing d89f2f2
chore: revert changes to watch example
mstruebing ae9b57c
chore: replace magic numbers
mstruebing 87b79a4
test: restructure test to be more readable
mstruebing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import * as k8s from '@kubernetes/client-node'; | ||
| import net from 'node:net'; | ||
|
|
||
| const kc = new k8s.KubeConfig(); | ||
| kc.loadFromDefault(); | ||
|
|
||
| const forward = new k8s.PortForward(kc); | ||
|
|
||
| const namespace = process.argv[2] ?? 'default'; | ||
| const deploymentName = process.argv[3] ?? 'demo-deployment'; | ||
| const localPort = parseInt(process.argv[4] ?? '8080', 10); | ||
| const remotePort = parseInt(process.argv[5] ?? '8080', 10); | ||
|
|
||
| // This creates a local server that forwards traffic to a deployment in Kubernetes | ||
| // by resolving the deployment to its first ready pod and port-forwarding to that pod. | ||
| // Usage: node port-forward-deployment.js [namespace] [deploymentName] [localPort] [remotePort] | ||
| // Example: node port-forward-deployment.js default my-app 8080 3000 | ||
| // This is equivalent to: kubectl port-forward deployment/my-app 8080:3000 -n default | ||
|
|
||
| const server = net.createServer(async (socket) => { | ||
| try { | ||
| await forward.portForwardDeployment(namespace, deploymentName, [remotePort], socket, null, socket); | ||
| } catch (error) { | ||
| console.error(`Error port-forwarding to deployment ${namespace}/${deploymentName}:`, error.message); | ||
| socket.destroy(); | ||
| } | ||
| }); | ||
|
|
||
| server.listen(localPort, '127.0.0.1', () => { | ||
| console.log(`Port forward server listening on http://127.0.0.1:${localPort}`); | ||
| console.log(`Forwarding to deployment: ${namespace}/${deploymentName}:${remotePort}`); | ||
| console.log(`Press Ctrl+C to stop`); | ||
| }); | ||
|
|
||
| server.on('error', (error) => { | ||
| console.error('Server error:', error); | ||
| }); | ||
|
|
||
| process.on('SIGINT', () => { | ||
| console.log('\nShutting down port-forward server...'); | ||
| server.close(); | ||
| process.exit(0); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import * as k8s from '@kubernetes/client-node'; | ||
| import net from 'node:net'; | ||
|
|
||
| const kc = new k8s.KubeConfig(); | ||
| kc.loadFromDefault(); | ||
|
|
||
| const forward = new k8s.PortForward(kc); | ||
|
|
||
| const namespace = process.argv[2] ?? 'default'; | ||
| const serviceName = process.argv[3] ?? 'demo-service'; | ||
| const localPort = parseInt(process.argv[4] ?? '8080', 10); | ||
| const remotePort = parseInt(process.argv[5] ?? '8080', 10); | ||
|
|
||
| // This creates a local server that forwards traffic to a service in Kubernetes | ||
| // by resolving the service to its first ready pod and port-forwarding to that pod. | ||
| // Usage: node port-forward-service.js [namespace] [serviceName] [localPort] [remotePort] | ||
| // Example: node port-forward-service.js default my-service 8080 80 | ||
| // This is equivalent to: kubectl port-forward svc/my-service 8080:80 -n default | ||
|
|
||
| const server = net.createServer(async (socket) => { | ||
| try { | ||
| await forward.portForwardService(namespace, serviceName, [remotePort], socket, null, socket); | ||
| } catch (error) { | ||
| console.error(`Error port-forwarding to service ${namespace}/${serviceName}:`, error.message); | ||
| socket.destroy(); | ||
| } | ||
| }); | ||
|
|
||
| server.listen(localPort, '127.0.0.1', () => { | ||
| console.log(`Port forward server listening on http://127.0.0.1:${localPort}`); | ||
| console.log(`Forwarding to service: ${namespace}/${serviceName}:${remotePort}`); | ||
| console.log(`Press Ctrl+C to stop`); | ||
| }); | ||
|
|
||
| server.on('error', (error) => { | ||
| console.error('Server error:', error); | ||
| }); | ||
|
|
||
| process.on('SIGINT', () => { | ||
| console.log('\nShutting down port-forward server...'); | ||
| server.close(); | ||
| process.exit(0); | ||
| }); |
46 changes: 46 additions & 0 deletions
46
examples/typescript/port-forward/port-forward-deployment.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import * as k8s from '@kubernetes/client-node'; | ||
| import net from 'node:net'; | ||
|
|
||
| const kc = new k8s.KubeConfig(); | ||
| kc.loadFromDefault(); | ||
|
|
||
| const forward = new k8s.PortForward(kc); | ||
|
|
||
| const namespace = process.argv[2] ?? 'default'; | ||
| const deploymentName = process.argv[3] ?? 'demo-deployment'; | ||
| const localPort = parseInt(process.argv[4] ?? '8080', 10); | ||
| const remotePort = parseInt(process.argv[5] ?? '8080', 10); | ||
|
|
||
| // This creates a local server that forwards traffic to a deployment in Kubernetes | ||
| // by resolving the deployment to its first ready pod and port-forwarding to that pod. | ||
| // Usage: node port-forward-deployment.ts [namespace] [deploymentName] [localPort] [remotePort] | ||
| // Example: node port-forward-deployment.ts default my-app 8080 3000 | ||
| // This is equivalent to: kubectl port-forward deployment/my-app 8080:3000 -n default | ||
|
|
||
| const server = net.createServer(async (socket) => { | ||
| try { | ||
| await forward.portForwardDeployment(namespace, deploymentName, [remotePort], socket, null, socket); | ||
| } catch (error) { | ||
| console.error( | ||
| `Error port-forwarding to deployment ${namespace}/${deploymentName}:`, | ||
| (error as Error).message, | ||
| ); | ||
| socket.destroy(); | ||
| } | ||
| }); | ||
|
|
||
| server.listen(localPort, '127.0.0.1', () => { | ||
| console.log(`Port forward server listening on http://127.0.0.1:${localPort}`); | ||
| console.log(`Forwarding to deployment: ${namespace}/${deploymentName}:${remotePort}`); | ||
| console.log(`Press Ctrl+C to stop`); | ||
| }); | ||
|
|
||
| server.on('error', (error: NodeJS.ErrnoException) => { | ||
| console.error('Server error:', error); | ||
| }); | ||
|
|
||
| process.on('SIGINT', () => { | ||
| console.log('\nShutting down port-forward server...'); | ||
| server.close(); | ||
| process.exit(0); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import * as k8s from '@kubernetes/client-node'; | ||
| import net from 'node:net'; | ||
|
|
||
| const kc = new k8s.KubeConfig(); | ||
| kc.loadFromDefault(); | ||
|
|
||
| const forward = new k8s.PortForward(kc); | ||
|
|
||
| const namespace = process.argv[2] ?? 'default'; | ||
| const serviceName = process.argv[3] ?? 'demo-service'; | ||
| const localPort = parseInt(process.argv[4] ?? '8080', 10); | ||
| const remotePort = parseInt(process.argv[5] ?? '8080', 10); | ||
|
|
||
| // This creates a local server that forwards traffic to a service in Kubernetes | ||
| // by resolving the service to its first ready pod and port-forwarding to that pod. | ||
| // Usage: node port-forward-service.ts [namespace] [serviceName] [localPort] [remotePort] | ||
| // Example: node port-forward-service.ts default my-service 8080 80 | ||
| // This is equivalent to: kubectl port-forward svc/my-service 8080:80 -n default | ||
|
|
||
| const server = net.createServer(async (socket) => { | ||
| try { | ||
| await forward.portForwardService(namespace, serviceName, [remotePort], socket, null, socket); | ||
| } catch (error) { | ||
| console.error(`Error port-forwarding to service ${namespace}/${serviceName}:`, error.message); | ||
| socket.destroy(); | ||
| } | ||
| }); | ||
|
|
||
| server.listen(localPort, '127.0.0.1', () => { | ||
| console.log(`Port forward server listening on http://127.0.0.1:${localPort}`); | ||
| console.log(`Forwarding to service: ${namespace}/${serviceName}:${remotePort}`); | ||
| console.log(`Press Ctrl+C to stop`); | ||
| }); | ||
|
|
||
| server.on('error', (error: NodeJS.ErrnoException) => { | ||
| console.error('Server error:', error); | ||
| }); | ||
|
|
||
| process.on('SIGINT', () => { | ||
| console.log('\nShutting down port-forward server...'); | ||
| server.close(); | ||
| process.exit(0); | ||
| }); |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like maybe it was left over from local testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed it, good catch.
Prettier autoformatted the file so I would leave it in here.