knight42 / krelay

A better alternative to `kubectl port-forward` that can forward TCP or UDP traffic to IP/Host which is accessible inside the cluster.
MIT License
244 stars 14 forks source link

feature request: Forwarding to multiple endpoints in one session #25

Closed minhnhupham closed 5 months ago

minhnhupham commented 12 months ago

We have a use case where developers are testing their apps locally, the apps connect to multiple endpoints both on the cluster and external to the cluster. Currently we have to launch multiple krelay sessions to accomplish this, it seems a bit resource heavy to do this. Ideally we'll like something like this to be supported: kubectl relay ip/10.14.204.99 55432:5432 ip/10.14.204.100 45432:5432

Where we can specify one session and one krelay pod on the cluster to relay to multiple different endpoints. Is this something that can be supported?

cenkalti commented 6 months ago

Right now, you can redirect several ports to one resource using this command:

kubectl relay ip/10.14.204.99 55432:5432 55433:5433

However, if you try to use this command for many resources, you'll run into a problem:

kubectl relay ip/10.14.204.99 55432:5432 55433:5433 ip/10.14.204.100 1234:1234
-----------------------------------------^

Is the argument a resource or a port detail? This can be confusing.

Because of this, I suggest changing the format of the command interface like this:

kubectl relay \
    --port-forward "ip/10.14.204.99 55432:5432 55433:5433" \
    --port-forward "ip/10.14.204.100 1234:1234"

This change will still work with the existing argument syntax.

I realize this solution may not be perfect, but it was the first one that came to mind.

cenkalti commented 6 months ago

Another option would be writing the definitions to a file and make krelay read from there.

Example:

$ kubectl relay --port-forward def.yaml
port-forward:
  - resource: "ip/10.14.204.99"
    ports:
      - "55432:5432"
      - "55433:5433"
  - resource: "ip/10.14.204.100"
    ports:
      - "1234:1234"
cenkalti commented 6 months ago

@knight42, are you open to this modification? I can create a PR for it.

knight42 commented 6 months ago

@cenkalti Thanks for your enlightening advice! How about we extend the option B and introduce a configuration file which we can save multiple targets into it. Ref #13.

Let's say we save the user preferences in ~/.config/krelay/config.yml:

$ cat ~/.config/krelay/config.yml
targets:
- name: a
  resource: "ip/10.14.204.99"
  ports:
  - "55432:5432"
  - "55433:5433"
- name: b
  resource: "ip/10.14.204.100"
  ports:
  - "1234"

then we can run krelay like this kubectl relay --targets a,b or simply kubectl relay -t a,b. WDYT?

cenkalti commented 6 months ago

Great idea with the configuration file for flexibility. Using ~/.config/krelay/config.yml and the command examples are straightforward and useful. Good suggestion!

sergeykad commented 6 months ago

It is a great idea, but would be it possible to make the file location configurable as was initially suggested? Alternatively, is it possible to have a similar functionality without the extra configuration file?

Currently, we share krelay configurations via Git as part of a shell script. Unfortunately, storing the file under ~/.config would make this sharing process impossible.

knight42 commented 6 months ago

would be it possible to make the file location configurable as was initially suggested

Yeah the path to the config file should definitely be configurable.

Alternatively, is it possible to have a similar functionality without the extra configuration file

That would be an option too. Actually I kinda lean towards the following syntax proposed above, regarding the introduced complexity of a new config file.

kubectl relay \
    --port-forward "ip/10.14.204.99 55432:5432 55433:5433" \
    --port-forward "ip/10.14.204.100 1234:1234"
knight42 commented 5 months ago

FYI I finally decided to put all the targets in a single file, the path to which can be specified by a new flag -f/--file. For more details, please refer to #33 .

While #33 is WIP now, the main work has been completed, you could give it a try if possible. Any advice is welcome :)

cenkalti commented 5 months ago

Works great! Thanks @knight42 🍰

knight42 commented 5 months ago

Works great! Thanks @knight42 🍰

Glad to hear that 😄 !