Skip to content
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

Ensure portfoward's local_port keyword follows kubectl behavior #506

Merged
merged 4 commits into from
Oct 14, 2024

Conversation

jacobtomlinson
Copy link
Member

@jacobtomlinson jacobtomlinson commented Oct 14, 2024

Closes #505

Currently setting local_port=None (the default behaviour) when port forwarding chooses a random high port. However this does not match the default behaviour of kubectl. As pointed out in #505 there are three different things that local_port can be set to in kubectl.

If it is completely omitted it will match the remote_port.

# This binds remote port 80 to port 80 locally
kubectl port-forward po/nginx 80

If it is left empty it will choose a random high port.

# This binds to a random port locally
kubectl port-forward po/nginx :80

And finally if it is set then it will be explicitly set.

# This binds to port 8080 locally
kubectl port-forward po/nginx 8080:80

This is a little trickier to implement in a Pythonic way because we usually use None or 0 for choosing a random high port. To resolve this in this PR I've added a new optional union type that is either a literal string of "match" or "auto" or an int. If the value is set to None it will follow the behaviour or "auto" which provides backward compatibility for folks who may explicitly be setting the value to None.

BREAKING: The default value has been changed to "match" to mimic the default behaviour of kubectl.

So the kr8s Python API now follows the kubectl behaviour:

If it is completely omitted it will match the remote_port.

from kr8s.objects import Pod

# This binds remote port 80 to port 80 locally
Pod("nginx").portforward(remote_port=80).run_forever()

If it is set to "auto" or None it will choose a random high port.

from kr8s.objects import Pod

# This binds to a random port locally
Pod("nginx").portforward(remote_port=80, local_port="auto").run_forever()

# This is equivalent for backward compatibility
Pod("nginx").portforward(remote_port=80, local_port=None).run_forever()

# This is also equivalent
Pod("nginx").portforward(remote_port=80, local_port=0).run_forever()

And finally if it is set then it will be explicitly set.

from kr8s.objects import Pod

# This binds to port 8080 locally
Pod("nginx").portforward(remote_port=80, local_port=8080).run_forever()

@github-actions github-actions bot added the kr8s label Oct 14, 2024
@jacobtomlinson jacobtomlinson added bug Something isn't working kr8s breaking A breaking change labels Oct 14, 2024
@github-actions github-actions bot added the tests label Oct 14, 2024
Copy link

codecov bot commented Oct 14, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 95.08%. Comparing base (87063fc) to head (530aa66).
Report is 124 commits behind head on main.

Files with missing lines Patch % Lines
kr8s/_portforward.py 55.55% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #506      +/-   ##
==========================================
+ Coverage   94.61%   95.08%   +0.46%     
==========================================
  Files          29       30       +1     
  Lines        3141     3989     +848     
==========================================
+ Hits         2972     3793     +821     
- Misses        169      196      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jacobtomlinson jacobtomlinson changed the title Ensure portfowards local_port follows kubectl behavior Ensure portfoward's local_port keyword follows kubectl behavior Oct 14, 2024
@jacobtomlinson jacobtomlinson merged commit 25d74f4 into kr8s-org:main Oct 14, 2024
12 checks passed
@jacobtomlinson jacobtomlinson deleted the port-foorward-defaults branch October 14, 2024 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A breaking change bug Something isn't working kr8s tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make port forward local port default to the remote port
1 participant