Skip to content

Spike: host-to-container networking - #4738

Closed
vito wants to merge 10 commits into
dagger:mainfrom
vito:h2c-networking
Closed

vito wants to merge 10 commits into
dagger:mainfrom
vito:h2c-networking

Conversation

@vito

@vito vito commented Mar 8, 2023

Copy link
Copy Markdown
Contributor

Adds the following API:

extend type Container {
  withExposedPort(
    # ...
    "Make this port reachable via the given port on the host"
    publish: Int
  )
}

Whenever a service runs, either via run (#4723) or withServiceBinding, the Dagger session will listen on any published ports and forward traffic to the container via the shim.

Note that this is a distinct behavior of run as opposed to exitCode/stdout/stderr.

vito added 4 commits March 8, 2023 00:40
This is a convenience API to replace this pattern:

```go
code, err := container.ExitCode(ctx)
if err != nil {
  return err
}
if code != 0 {
  return fmt.Errorf("exit code: %d", code)
}
```

Right now it's redundant because ExitCode returns an error when it
encounters a nonzero exit code, which is a bug (dagger#3192). Anyone relying
on that behavior and ignoring the code return value should switch to
this new API instead.

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
@vito
vito requested review from shykes and sipsma March 8, 2023 18:24

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome! Left a few minor comments, looks great overall!

My only broad nit-pick is that there might be some performance benefits to using the socket session attachable mechanism instead of proxying through stdio pipe of gateway containers. The idea being that when a container is spun up that publishes a port, there will be a unix socket provided that is "intercepted" by the shim when it is updating the bundle and it uses that to proxy the connection instead of stdio pipes.

The main possible benefit is that it avoids the need to spin up a new container every time a connection is made on the host. It also avoids having to go through pipes, which I'm sure work fine but have weird limitations around the maximum size of a single read/write and a relatively low default buffer size (IIRC) as compared with socket defaults.

That being said, I highly doubt that this would be noticeable for most straightforward use cases, probably only makes a difference for relatively high load scenarios (which are probably rare), so this isn't a blocker at all. If this approach is about the same complexity as the existing one and you feel like trying it maybe it's worth a shot, but otherwise not a big deal at all. Super excited to have the feature in place!

Comment thread cmd/dagger/main.go
Comment on lines +25 to 27
// NB(vito): we now use Logrus in other places so it's a bit unfortunate to
// disable this, solution TBD
logrus.StandardLogger().SetOutput(io.Discard)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We only want to disable logrus.StandardLogger() because it gets spammed with useless messages by the other library, we are free to use our own logger I think. We could re-use the pattern in buildkit of bklog.G(ctx) but just avoid use of StandardLogger maybe.

Comment thread core/container.go Outdated

proxyContainer, err := gw.NewContainer(ctx, containerReq)
if err != nil {
logrus.Warnf("create publish container for :%d: %s", publish, err)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When running as a subprocess dagger session needs to write any log messages to stderr, not stdout, just double checking that's where these will end up.

Comment thread core/container.go Outdated
publish := *port.Publish

// NB: listen synchronously
l, err := net.Listen(port.Protocol.Network(), fmt.Sprintf(":%d", publish))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So this will listen on all interfaces on the host by default? If so I think we should make this configurable and probably default to just localhost, just so you have to be more explicit about exposing the service to every host network.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So this will listen on all interfaces on the host by default? If so I think we should make this configurable and probably default to just localhost, just so you have to be more explicit about exposing the service to every host network.

does this require another experimental flag?

@vito

vito commented Mar 8, 2023 •

Copy link
Copy Markdown
Contributor Author

@sipsma Would definitely like to avoid the container-per-request overhead. Just threw something together for tomorrow's demo. An earlier iteration just did docker exec -i dagger-engine.dev socat - tcp:hostname:port so the new approach is slightly better. 😂

I don't quite grok how to use the SSH socket attachable yet but I can take a deeper look. I considered it briefly but it seemed more suited for proxying in the other direction (container -> host), and it didn't seem like there was much I could re-use, but I might be missing something.

I'm currently running into a weird issue where after a handful of connections it hangs trying to start the proxy process. It's able to create the container, but the Container.Start call hangs. Not sure why yet, but all the more reason to use something more lightweight. Hopefully it doesn't happen in the demo. 😅

edit 1: I'm becoming convinced this is behavior of the thing I'm running, not the proxy. Works fine with simpler examples (e.g. redis), only happens with WordPress so far.

edit 2: The last batch of commits seems to fix it. 🤔 (Closing proxy when stdin ends, and running in the host network.)

vito added 5 commits March 8, 2023 17:37
much faster, since we don't really need a bridge for these. could
probably do this for the health check too.

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
@shykes

shykes commented Mar 9, 2023 •

Copy link
Copy Markdown
Contributor

Love the feature, don't love the API... The distinction between "exposing" and "publishing" a port was... not a highlight of the Docker UX, and I am not eager to perpetuate the problem in Dagger.

As a strawman, I would like to bring back my original suggestion from the API draft. Basically it makes explicit the concept of proxy services. In the initial proposal, proxy services and container services had a common type, Service, which I found elegant. I'm now realizing that by slicing up the design, and removing the Service type in the c2c slice, we may have closed that door...

Thoughts?

@sipsma

sipsma commented Mar 9, 2023

Copy link
Copy Markdown
Contributor

I don't quite grok how to use the SSH socket attachable yet but I can take a deeper look. I considered it briefly but it seemed more suited for proxying in the other direction (container -> host), and it didn't seem like there was much I could re-use, but I might be missing something.

Ah yeah I was thinking the shim could call connect on the unix socket and then the client-side of the session attachable blocks until a connection on it's client-side listener is made, at which time it will accept the connection and start copying between the two, but I just realized that because we aren't the ones directly calling accept on the unix socket in the server, buildkitd code is, we can't implement that blocking behavior we'd need... There's probably ways of getting it to work but it would become a mess.

That's sad but not a big deal since it seems like you got the gateway container idea working fully. The only other possibility I can think of is to maybe add our own session attachable implementation and see if we could use that somehow (plausible now that we control both client and server) but that's way way too complicated to be worth it for now, so never mind :-)

@marcosnils

Copy link
Copy Markdown
Contributor

The distinction between "exposing" and "publishing" a port was... not a highlight of the Docker UX, and I am not eager to perpetuate the problem in Dagger

Interesting to know.. FWIW I never personally found or heard in the wild that the publish UX caused that many issues. It's funny to see that even all the other container orchestrators like k8s and nomad also used the same design.

@shykes

shykes commented Mar 9, 2023

Copy link
Copy Markdown
Contributor

Interesting to know.. FWIW I never personally found or heard in the wild that the publish UX caused that many issues. It's funny to see that even all the other container orchestrators like k8s and nomad also used the same design.

That’s the thing, publish doesn’t cause issues because nobody uses it anymore for development or prod. It’s often hidden away and basically disabled by wrappers, including of course kubernetes. Even in compose you typically configure port mapping in the yaml.

In the early days it was similar to ‘cmd’ and ‘entrypoint’: which is which? Then later the problem became more fundamental: publish where? We had plans to extend the publish concept beyond “a nat rule on the local host”, add a new extension point so that you could eg. publish your port on a firewall, punch holes through your VPC, register a route on an http proxy. But the ecosystem was already fragmenting, the various wrappers just wanted docker to be a dumb single-host runner while they fought each other with incompatible extension points… So Docker gave up on unifying the ecosystem around the engine API and shipped its own wrapper. ‘publish’ remained a single-host concept, impossible to extend. And gradually became irrelevant, like ONBUILD, links etc.

So, I think the same question is relevant in our case: ‘publish’ where? It’s not intuitive that the answer is always ‘on the client host’. Especially since we have container { publish } with a very different answer.

@vito

vito commented Mar 10, 2023 •

Copy link
Copy Markdown
Contributor Author

Especially since we have container { publish } with a very different answer.

Haha, I completely forgot about that. Definitely worth avoiding the term "publish" then.

Shower thought: maybe this is where we bring Socket back into the fray?

extend type Container {
  "Returns a Socket routing to the specified port, or the first exposed port if unspecified."
  socket(port: Int): Socket!
}

extend type Socket {
  "Binds the socket in the host address space."
  bind(
    address: String!,
    family: NetworkFamily=IP, # or UNIX?
  ): Proxy!
}

type Proxy {
  close: Void!
}

Just a sketch. Things I like:

  • Parallels between Container.Endpoint and Container.Socket (selecting the default port)
  • Parallels to bind(2) (takes an existing socket and binds it to an address space)
    • Note the use of NetworkFamily instead of NetworkProtocol, I'm a bit fuzzy on this but the idea is to just match whatever protocol is used on the other side; I don't think it makes much sense to proxy TCP->UDP (right?), more interesting is Unix vs IP. What's confusing is Go mixes both as just 'network':

      Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and "unixpacket".

  • Supports mounting a service's socket into a client container as a Unix socket via Container.WithMountedSocket
  • Supports mounting a service's socket to the host as a Unix socket (family=UNIX)
  • Integrating Socket into the rest of the API, which has been kind of on its own since its introduction

Things that are weird:

  • Supports binding a Host Unix socket to another address.

Things I don't like:

  • Having to add a whole new Proxy type just to be able to stop the proxy. I could have made bind just block and return Void! and have the client cancel the request to stop it, but I'd rather give client have a guarantee that the address is bound when the API returns to avoid race conditions.

This might be a narrower slice of what you accomplished with the earlier Proxy proposal though.

As a strawman, I would like to bring back my original suggestion from the API draft. Basically it makes explicit the concept of proxy services. In the initial proposal, proxy services and container services had a common type, Service, which I found elegant. I'm now realizing that by slicing up the design, and removing the Service type in the c2c slice, we may have closed that door...

FWIW, I don't think any door needs to stay closed - we're pre-1.0 after all, and if addressing the rest of networking requires a more fleshed-out API, to me any change is on the table. There just needs to be enough carrot with the stick, and h2c networking seems like as good a carrot as any.

I liked the symmetry that you found between the Proxy and Service, but I also like where we ended up without it, where services are really just inter-networked containers. We didn't end up needing them to fulfill a Service interface, and decided to avoid explicit service lifecycle management as much as we can. One way of looking at it is we chose to align Containers with Services rather than align Services with Proxies.

@shykes

shykes commented Mar 11, 2023

Copy link
Copy Markdown
Contributor

I liked the symmetry that you found between the Proxy and Service, but I also like where we ended up without it, where services are really just inter-networked containers. We didn't end up needing them to fulfill a Service interface, and decided to avoid explicit service lifecycle management as much as we can. One way of looking at it is we chose to align Containers with Services rather than align Services with Proxies.

I think the main benefit of a common Service type, backed by either container { service } or host { proxy } would be to avoid having both container { withContainerServiceBinding } and container { withProxyServiceBinding } just because there are two different bindable types.

@shykes

shykes commented Mar 11, 2023

Copy link
Copy Markdown
Contributor

I think the main benefit of a common Service type, backed by either container { service } or host { proxy } would be to avoid having both container { withContainerServiceBinding } and container { withProxyServiceBinding } just because there are two different bindable types.

Possible alternative solution: host { proxy } returns a Container, which can be bound the usual way.

  • Upside: no extra type, only one way to bind
  • Downside: more complicated implementation under the hood; we’re kind of lying because technically the proxy isn’t a container, and consequently its implementation of most container fields would probably have to return an error. No rootfs, no env variables, etc.

I think I still prefer adding container { service }: Service! and host { proxy }: Service!

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions

github-actions Bot commented Apr 3, 2023

Copy link
Copy Markdown
Contributor

This PR was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions Bot closed this Apr 3, 2023
@verdverm

Copy link
Copy Markdown
Contributor

+1 for networking upgrades, I need to be able to talk to host containers from dagger

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants