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

Add port forward #446

Merged
merged 9 commits into from
Jan 31, 2022
Merged

Add port forward #446

merged 9 commits into from
Jan 31, 2022

Conversation

kazk
Copy link
Member

@kazk kazk commented Mar 1, 2021

Super rough sketch I mentioned almost 2 months ago. #127 (comment)

I don't like this at all, but an example with Nginx (from Python client example) works and can be a starting point for discussion. Any feedback/ideas are appreciated, but I honestly don't remember much, so I might not be able to answer "why?".

Protocol

WebSocket messages are binary ([channel, ...data]) like /attach and /exec.

For /portforward, channels are associated with the specified ports and each port uses 2 channels: a duplex data channel (2*i) and a read-only error channel (2*i + 1) for ith port.

The first message for a channel is always 3 bytes and describes this association:

  1. channel (u8)
  2. port number (u16 LE)

Idea

EDIT: Maybe this can be done on top of the lower level API provided by kube in this PR. I won't be trying this for now.

Make Portforwarder tower::MakeService (Service of Service)? I haven't tried, but it might be possible, and it'll make it a lot more usable.

let portforwarder = pods.portforward("example", &[80]).await?;
// takes port and returns Service<Request<Body>>
let mut forwarder = portforwarder.into_service();
let mut svc = forwarder.call(port).await?;
let response = svc.call(request).await?;

  • Error handling
  • Add example similar to kubectl port-forward example local_port:remote_port.

@kazk kazk mentioned this pull request Mar 1, 2021
11 tasks
kube/src/api/portforward.rs Outdated Show resolved Hide resolved
kube/src/api/portforward.rs Outdated Show resolved Hide resolved
kube/src/api/portforward.rs Outdated Show resolved Hide resolved
kube/src/api/subresource.rs Outdated Show resolved Hide resolved
@clux
Copy link
Member

clux commented Mar 1, 2021

Hey, this is great! A lot of the hairy channel management (spawning a task for each port makes sense) looks like it's done. Error handling perhaps needs a bit more improvements, but you've already got a nice stream interfaces on top of Api. The returned Portforwarder letting you drill into the Port with the various methods is very user friendly.

Well, insofar as letting people write raw http requests into a stream can be user friendly.
I do think we should strive towards a way to have people dump more ergonomic requests in rather than raw-text, even though I guess they can construct http requests themselves and serialize them manually.

EDIT: But I guess we can't go all in HTTP as this is meant to be protocol agnostic.

The Service idea does sound interesting though! At least if we can something like the interface you posted.

@clux
Copy link
Member

clux commented May 22, 2021

Ah, forgot about this. Yeah, the example still works, and presents the basic functionality. We certainly can merge it as it stands as an initial version of it. But also feel like it might be better to have the canonical kubectl port-forward style example (where we actually listen on that port and forward the tcp traffic) working before releasing.

Then on the other hand, people needing that probably aren't writing up against kube do do that atm, and just use kubectl.

@partiallyordered
Copy link
Contributor

I've started using this branch and it's working admirably, thanks both 😀 . I don't know if it fits here, but consider merging my PR here containing some example code putting hyper on top of the port-forward: kazk#4 before merging #446 .

@partiallyordered
Copy link
Contributor

partiallyordered commented Jul 5, 2021

Come to think of it, I might also be able to extract a tokio-tungstenite-based websocket example from my code in the next few days if that's interesting.

@kazk kazk self-assigned this Oct 26, 2021
@clux clux added the client-gold gold client requirements label Oct 30, 2021
@kazk kazk force-pushed the portforward branch 2 times, most recently from 4445087 to 61d2add Compare January 18, 2022 05:35
@codecov-commenter
Copy link

codecov-commenter commented Jan 18, 2022

Codecov Report

Merging #446 (20a91e5) into master (3381e69) will decrease coverage by 1.90%.
The diff coverage is 14.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #446      +/-   ##
==========================================
- Coverage   71.92%   70.02%   -1.91%     
==========================================
  Files          56       57       +1     
  Lines        3775     3906     +131     
==========================================
+ Hits         2715     2735      +20     
- Misses       1060     1171     +111     
Impacted Files Coverage Δ
kube-client/src/api/mod.rs 86.66% <ø> (ø)
kube-client/src/api/portforward.rs 0.00% <0.00%> (ø)
kube-client/src/api/subresource.rs 54.54% <41.66%> (-1.42%) ⬇️
kube-core/src/subresource.rs 76.03% <77.77%> (+0.30%) ⬆️
kube-runtime/src/wait.rs 70.00% <0.00%> (+2.00%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3381e69...20a91e5. Read the comment docs.

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for fixing this up - we haven'd had real feedback on this in a year, so it's probably fine to add it to get more users. i am happy to approve, just left a few minor nits here first that i think could be easy wins if they work

kube-client/src/api/portforward.rs Outdated Show resolved Hide resolved
kube-client/src/api/portforward.rs Show resolved Hide resolved
kube-client/src/api/portforward.rs Show resolved Hide resolved
kube-client/src/api/portforward.rs Show resolved Hide resolved
examples/pod_portforward_hyper_http.rs Outdated Show resolved Hide resolved
examples/pod_portforward_hyper_http.rs Outdated Show resolved Hide resolved
examples/pod_portforward_hyper_http.rs Outdated Show resolved Hide resolved
@clux clux added the changelog-add changelog added category for prs label Jan 18, 2022
@clux clux added this to the 0.67.0 milestone Jan 18, 2022
@clux
Copy link
Member

clux commented Jan 20, 2022

I thought the port stuff could be done a little cleaner, but ultimately don't see a good way - so resolved my gripes with some comments. Added some minor suggested tweaks to docs

ultimately though, the functionality here works well and we should probably merge this to invite more users. How are you feeling about it now @kazk?

@clux clux modified the milestones: 0.67.0, 0.68.0 Jan 24, 2022
@clux
Copy link
Member

clux commented Jan 24, 2022

Leaving this out of the release as it is still a draft PR - although I would be happy merging it.

@kazk kazk force-pushed the portforward branch 2 times, most recently from 1c697d5 to 2a21db8 Compare January 29, 2022 11:12
@kazk
Copy link
Member Author

kazk commented Jan 29, 2022

Sorry for the delay.
I've refactored to avoid panics, and split each loop into smaller functions. It should be easier to understand and iterate on. It's still missing an example that binds to local port, but we can do it later/ask for help.

@kazk kazk marked this pull request as ready for review January 29, 2022 11:27
@kazk kazk changed the title Add /portforward support Add port forward Jan 29, 2022
@kazk
Copy link
Member Author

kazk commented Jan 30, 2022

I got an example to bind local port working. I'll push it after cleaning it up.

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, didn't realise you had such a large refactoring left in you. Thanks a lot, it's definitely cleaner!
Minor comments left, but no real suggestions other than the superficial example doc thing.

kube-client/src/api/portforward.rs Show resolved Hide resolved
kube-client/src/api/portforward.rs Show resolved Hide resolved
kube-client/src/api/portforward.rs Show resolved Hide resolved
kube-client/src/api/portforward.rs Show resolved Hide resolved
examples/pod_portforward_hyper_http.rs Outdated Show resolved Hide resolved
examples/pod_portforward_hyper_http.rs Outdated Show resolved Hide resolved
Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very cool full port-forward example in just over 100 lines with lot of helpful technical parts in it!

everything seems like it's working well! don't have any other outstanding comments.
looking forward to have this in :-)

Comment on lines +68 to +73
let context = Arc::new(Mutex::new(sender));
let make_service = make_service_fn(move |_conn| {
let context = context.clone();
let service = service_fn(move |req| handle(context.clone(), req));
async move { Ok::<_, Infallible>(service) }
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool quick use of tower!

@kazk
Copy link
Member Author

kazk commented Jan 31, 2022

Yeah, the local port example was easier than I had expected. I think this is good enough as a first version.
Merging now. Not squashing to get @partiallyordered in the contributors list.

@kazk kazk merged commit 2c5573b into kube-rs:master Jan 31, 2022
@kazk kazk deleted the portforward branch January 31, 2022 19:44
@clux
Copy link
Member

clux commented Feb 1, 2022

Very nice! I'll do a release later on today. Tons of stuff ready.
Will try to put a paragraph in the CHANGELOG about it as it's one of the biggest additions (and technically completes the penultimate requirement for client-gold)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-add changelog added category for prs client-gold gold client requirements
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants