Skip to content

Proxy INBOUND network connections, from client to actions #1258

Closed as not planned

Description

@shykes

Opportunity

Currently Dagger can proxy OUTBOUND network connections, from the DAG to the client. For example a container executed inside the DAG can connect to a unix socket that is proxied to a unix socket on the client.

It would be amazing if Dagger also supported the reverse: proxy INBOUND network connections from the client to the DAG. For example, a container listening for tcp connections inside the DAG could receive connections proxied from a TCP port on the client. This expands considerably what is possible inside the DAG: for example it could implement docker compose-like functionality and stand up a development stack, with a more reliable API for accessing ports.

Implementation challenges

There are several implementation challenges:

1. Proxy architecture

To quote @aluzzardi :

Dagger can't forward TO unix sockets inside the container -- it can forward FROM unix sockets inside the container TO a host socket.
Doing this in the opposite way is probably doable but requires some major design/research.

2. Requires an exec helper

In order to access a TCP or UDP listener inside the receiving container’s namespace, an exec helper is required. In other words Dagger must run its own code inside the container.

NOTE: this doesn’t apply to unix sockets, and doesn’t apply to tcp/udp listeners on the client side (ie tcp->unix or udp->unix) is possible without helper, but of course less useful.

3. Assumes dagger up is long-running

Inbound network connections suppose long-running services to receive them. And long-running services assume a long-running dagger up… This is a major design change.

Currently dagger up is expected to terminate. If it doesn’t after a reasonable timeout delay, Dagger is expected to consider that an error and terminate, report an error etc.

Therefore we would need to adapt the behavior of the dagger up command, and possible modify APIs to accomodate the distinction between long-running and short-running commands.

Activity

  1. added this to the milestone on Dec 17, 2021
  2. changed the title [-]Europa: proxy INBOUND network connections, from client to DAG[/-] [+]Proxy INBOUND network connections, from client to actions[/+] on Apr 8, 2022
  3. lyzhang1999 commented on Apr 14, 2022

    @lyzhang1999
    Contributor

    until now, do we have any solution for communicate dagger bash.#Run between Minikube or Kind?

  4. sipsma commented on May 3, 2022

    @sipsma
    Contributor

    With the recent addition of core.#Start (which uses BuildKit's NewContainer API instead of an ExecOp) I think there's a path to accomplishing this today without upstream changes to BuildKit. It will be more hacky and less flexible than it should be with upstream support, but could get us started.

    The idea would be to use the stdio streams of NewContainer to proxy network connections. This would require writing a small go program that proxies its stdin to a network connection it makes to some configurable ip/port that the application is listening on in its container. It's actually similar to @helderco's idea here in terms of having a small static go program that we can mount into containers to plug in functionality.

    The simplest way of doing this is probably to just exec into the core.#Start container that has the application that's listening on a port (NewContainer API supports exec'ing in at arbitrary points).

    Depending on buildkitd's CNI configuration, there's other possibilities too but they get a bit more complicated with the only benefit being that we wouldn't need to bind mount the stdio proxy into the NewContainer running the user's application.

  5. marcosnils commented on May 3, 2022

    @marcosnils
    Contributor

    Depending on buildkitd's CNI configuration, there's other possibilities too but they get a bit more complicated with the only benefit being that we wouldn't need to bind mount the stdio proxy into the NewContainer running the user's application.

    I had a brief conversation with @aluzzardi about this and we should also start thinking about the networking aspect. Nowadays, all the buildkit containers run under the host namespace. This means that if two or more core.Start tasks try to bind to the same port, that will fail.

    Speaking with Andrea and Erik we should start mapping what's required to avoid this host network thing. After some initial conversations I'm not sure if it's relatively straightforward to change the host network thing and use something like the docker bridged network.

  6. sipsma commented on May 3, 2022

    @sipsma
    Contributor

    This means that if two or more core.Start tasks try to bind to the same port, that will fail.

    For users that don't need the service in core.#Start to have outbound internet access (i.e. a web server that only needs its local files), we can allow them to just start up the container with NetMode_NONE, which tells Buildkit to create a new network namespace for it but don't put anything in it. Then the application can just listen on its (private) localhost and we can take care of the port forwarding to it as described in my previous comment.

    • I don't know if we have exposed this setting in Dagger yet, but it's pretty trivial to if needed

    For use cases that require the application to have outbound access to an external network (including possibly the internet), then CNI will be our best bet. Buildkit has builtin support for it, an example bridge plugin conf here.

  7. sipsma commented on May 4, 2022

    @sipsma
    Contributor

    Not sure if this is the best issue for this, but will capture it here for now. @marcosnils @aluzzardi and I talked about some options for what we can do to address this and the related issue here:

    • Current networking setup: All containers created by buildkitd run in same netns as buildkitd. In our default buildkitd running inside docker, that will be the host netns (as we set --net=host when starting the container)

    • Current problems

      • core.Start containers will compete over ports if they try to listen on the same ones
      • no way to forward traffic from dagger client to a core.Start container
      • no good way for other containers to "discover" core.Start endpoint (would just have to hardcode port and interface to connect to)
    • Possible solution: Change containers created by buildkitd to be sandboxed by default, with option to enable host networking per-container

      • Challenges:
        • Discovery is still difficult; if another container wants to connect to the service in core.Start we'd need to do one of the following
          • setup a DNS service
          • enable getting the IP address of a core.Start container (as an output in cue) and have users manually set it up in the ExtraHosts field of the client container
          • more fleshed out support for automatically configuring ExtraHosts based on cue fields of services a container should have access to (need to be designed)
        • Need Buildkit to be configured w/ CNI.
          • For our default-path that uses docker, this means we can no longer use the moby/buildkit image (as that doesn't have CNI) and would have to manage our own. There may also be hacks like bind mounting in CNI configuration and binaries, but that is still complicated
          • For the user-managed buildkitd, we need to document how to setup CNI w/ buildkitd
        • Doesn't really help the need for forwarding traffic from client to core.Start container, that's mostly orthogonal to this solution

    • Decision for now

      • Don't change sandboxing mode, even the quick-and-hacky options are a fair bit of effort, better off just designing good longer-term solution
      • Use cases for core.Start will currently be limited to those where port contention is not an issue and where users can just hardcode the service endpoint/port in their client containers (or use alternatives like unix sockets over a cache mount)
    • Longer-term solution

      • client: network interface for configuring port mapping between client and containers
        • Can use stdio to proxy client network connections to services in core. This could also become a more generic service that incorporates similar needs such as capturing stdout/stderr as proposed elsewhere
      • Robust solution for exposing the service in a core.Start to other containers in a way that doesn't require manual mapping by users
  8. mircubed commented on Jun 1, 2022

    @mircubed
    Contributor

    @sipsma should we close this issue and create a new one based on the work needed for the "short-term" and "long-term" decision?

  9. sipsma commented on Jun 1, 2022

    @sipsma
    Contributor

    @sipsma should we close this issue and create a new one based on the work needed for the "short-term" and "long-term" decision?

    @mircubed
    I actually think we should leave this as is. The short-term decision was basically "don't try to hack this together in a quick-and-ugly way", the long-term decision is to implement the feature described here in a clean way. Sorry about the confusion, I just wanted to capture the discussion somewhere at the time but didn't have a good place to back then.

  10. mircubed commented on Jun 1, 2022

    @mircubed
    Contributor

    Ok, thanks for the clarification @sipsma! I will keep it open since we still need to move forward with the work for the long-term solution. I will work on figuring out the right priority for this specific one.

  11. reopened this on Oct 28, 2022
  12. shykes commented on Dec 21, 2022

    @shykes
    ContributorAuthor

    Closing this in favor of the more comprehensive #4080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions