Skip to content

Breaking API change: run services via gateway, revise API - #5322

Closed
vito wants to merge 45 commits into
dagger:mainfrom
vito:services-v2
Closed

vito wants to merge 45 commits into
dagger:mainfrom
vito:services-v2

Conversation

@vito

@vito vito commented Jun 15, 2023 •

Copy link
Copy Markdown
Contributor

based on #5297

Now that we're able to stream arbitrary progress to the frontend, it's possible to run services via the gateway instead.

There were a few tricky parts to this.

Problem A: It doesn't seem possible to take an arbitrary Container and run it via the Gateway instead. The Container already "bakes in" an LLB definition that refers to the result of running the service. The only thing you can really do is try to Solve() it.

Solution A: change the API such that Services are no longer just Containers. Instead, you call Container.Service which takes the exact same params as Container.WithExec, only it returns a Service instead of a Container. Note: we now have to maintain two different ways to run containers (Container.WithExec + Service.Start).

The new API is as follows (this is a BREAKING CHANGE):

type Container {
  # REMOVED
  # hostname: String!

  # REMOVED
  # endpoint(...): String!

  withServiceBinding(
    alias: String!
    service: ServiceID! # BREAKING: used to be ContainerID
  ): Container!

  # NEW: return a Service that will run the container
  service: Service!
}

extend type Query {
  service(id: ServiceID!): Service!
}

scalar ServiceID

type Service {
  id: ServiceID!
  hostname: String!
  endpoint(
    port: Int
    scheme: String
  ): String!
}

Problem B: it's harder to generate service hostnames. Previously we could just take the digest that WithExec baked in, but now that we're not doing that, we have to come up with our own content-addressing scheme. The naive thing to try is taking a digest of the Service, akin to a ContainerID, but ContainerIDs aren't stable because *pb.Definition contains fields that can change order while maintaining the same semantics.

Solution B: I implemented a helper to take a stable digest of an arbitrary struct. This works by recursing through structs, maps, slices, etc. and digesting types, with a special-case for *pb.Definition which was the source of unstable hashing as its inner Def field is topographically ordered.

Problem C: bizarre behavior with running the same service across Dagger sessions (i.e. parallel test runs), causing each session to start the same service + hostname, and some of them to fail when resolving the host to an IP.

Solution C: dedupe services within a session, but not across sessions. Maintain a global map of all services, keeping track of their starting/running state to handle parallelism, and injecting a "session ID" into each Service so that equivalent containers don't share hostnames across Dagger sessions.

TODO:

  • Complete the session's vertex when it exits
  • Try out the TUI DX for running services

@vito
vito force-pushed the services-v2 branch 3 times, most recently from 3339146 to 13c2745 Compare June 15, 2023 21:09
Comment thread core/service.go Outdated

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.

nit: could leave unexported from what I can tell?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah definitely. This was a bit of a hacky step because injecting it everywhere seemed like a huge pain; basically every object needs it. I'll just make it private for now.

Comment thread core/service.go Resolved
Comment thread core/service.go Outdated

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.

I think we'll want a defer to call gc.Release(context.Background()) in case we exit this func early due to an error

Comment thread core/service.go Resolved
@vito
vito force-pushed the services-v2 branch 2 times, most recently from 980fb4c to 9456f34 Compare June 16, 2023 20:48
@vito

vito commented Jun 19, 2023

Copy link
Copy Markdown
Contributor Author

Current status: 9456f34 removed ExperimentalServiceHost from the Git and HTTP APIs in favor of a smaller change to support the Services tests (adding lazy: true to Container.Directory/File). But I later found we're dependent on Git service support for the new Project tests, and it's not just an implementation detail like the Service tests.

One option is to change the Project tests to use an external test repo instead of a self-served one. We would just need an external repo fixture.

But instead of just ripping those APIs out and calling it a day, I'm going to try supporting them via upstream changes to Buildkit. More cross-pollination between our APIs makes for richer possibilities, rather than saying "services are for containers only."

So far I've added ExtraHosts support to llb.HTTP and llb.Git, the only catch being we need to start the dependent services at API call time rather than Directory/File usage time, because we need an IP to pass to the llb.HTTP or llb.Git op. Not a huge deal, except that it's hard to know when we can "detach" the service. Maybe we just leave it running and document that as a caveat to this likely-rare API use case? Going further than this would mean adding programmable host resolution to Buildkit, similar to secrets, which sounds very cool but I don't know if it would be accepted upstream. Maybe I'll experiment with it anyway, if it's a small change.

Anyway it seems to be working for the most part, and now I'm just chasing down a handful of failing tests (13 by last count but there's some double-counting involved). Haven't pushed anything yet since I'm still hacking on things via local dependency bind-mounts/etc. to iterate more quickly.

@vito

vito commented Jun 19, 2023 •

Copy link
Copy Markdown
Contributor Author

Update for today: ok, I think DNS is pretty hard to avoid with all of the crazy nesting things we do. The project tests in particular need to be able to pass a git://from a parent session to a child session.

I pushed the old work to a branch and brought DNS back for this PR, with a twist: using search domains to namespace services.

Each service container now has a hostname like d3prnq0oeduqm.m31h3p7msle94, where the first segment is the service's actual hostname as returned by the API, and the second segment is a session-wide domain. I had to change Buildkit to support configuring search domains in llb.HTTP and llb.Git, and I modified the shim to retrieve the search domain as a secret (to avoid cache busting) and inject it in the container's /etc/resolv.conf. Putting it all together, we now have session-local container-to-container traffic with session-independent caching.

Still a lot of cleanup to do, and TBD whether Buildkit will be OK merging these changes, but all the services tests are passing now.

I'm not 100% sure how this will all work with nesting, but I have a rough idea to just propagate search domains from the parent to the child (with the child's domain taking precedence) if needed.

@vito

vito commented Jun 20, 2023 •

Copy link
Copy Markdown
Contributor Author

Current status: all tests passing! 🎉 The linter is yelling at me for Service.Start cyclomatic complexity - and rightly so.

The last batch of changes add the aforementioned nesting behavior whereby the search domain from the outer session is propagated to the child, which appends it to its search domain list. This was necessary to get the project tests passing. I also had to make it selectively add the search domain for llb.Git because using the new search domains feature was breaking Dockerfiles that use a frontend that doesn't know about the new source.git.searchdomains capability.

I'm going to try to do this a different way in Buildkit, though. Rather than passing SearchDomains to llb.Git and llb.HTTP, I want to try configuring it as a session.Attachable. That way the domain value never has to enter into LLB. It's easy to make the HTTP and Git sources not count it towards the CacheKey, but there are other parts of Dagger that would be busted, namely service hostnames and ContainerIDs, because it will still result in a different LLB definition each time.

@vito
vito force-pushed the services-v2 branch 3 times, most recently from f09e213 to 5dda952 Compare June 21, 2023 18:50
@vito

vito commented Jun 22, 2023

Copy link
Copy Markdown
Contributor Author

Buildkit session.Attachable went smoothly, upstream PR is here: moby/buildkit#3960

Now we have a non-hacky way to pass dynamic DNS search domains to Exec, Git, and HTTP without busting the cache. No more abusing secrets. 🎉

@vito vito changed the title Run sessions via gateway, revise session API (BREAKING) Breaking API change: run services via gateway, revise API Jun 23, 2023
@vito
vito force-pushed the services-v2 branch 5 times, most recently from de9bf0c to ee8ad07 Compare June 30, 2023 21:56
@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.

vito added 4 commits July 18, 2023 11:34
There were a few tricky parts to this.

Problem A: It doesn't seem possible to take an arbitrary Container and
run it via the Gateway instead. The Container already "bakes in" an LLB
definition that refers to the _result_ of running the service. The only
thing you can really do is try to Solve() it.

Solution A: change the API such that Services are no longer just
Containers. Instead, you call Container.Service which takes the exact
same params as Container.WithExec, only it returns a Service instead of
a Container.

Problem B: it's harder to generate service hostnames. Previously we
could just take the digest that WithExec baked in, but now that we're
not doing that, we have to come up with our own content-addressing
scheme. The naive thing to try is taking a digest of the Service, akin
to a ContainerID, ContainerIDs aren't stable.

Solution B: I implemented a helper to take a stable digest of an
arbitrary struct. This works by recursing through structs, maps, slices,
etc. and digesting types, with a special-case for *pb.Definition which
was the source of unstable hashing as its inner Def field is
topographically ordered.

Problem C: bizarre behavior with running the same service across Dagger
sessions (i.e. parallel test runs), causing each session to start the
same service + hostname, and some of them to fail when resolving the
host to an IP.

Solution C: dedupe services within a session, but not across sessions.
Maintain a global map of all services, keeping track of their
starting/running state to handle parallelism, and injecting a "session
ID" into each Service so that equivalent containers don't share
hostnames across Dagger sessions.

Signed-off-by: Alex Suraci <[email protected]>
The demos for running `hostname` should probably be removed, as regular
containers no longer have a hostname.

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
Collecting the container IP in this way doesn't work. Containers are
one-time-use. Need to have the service container report its IP to the
health-check container instead.

Signed-off-by: Alex Suraci <[email protected]>
vito added 22 commits July 18, 2023 11:47
This reverts commit 1f17879.

Bringing this back, going to try using search domains instead.

Static host -> IP mapping is too difficult with all the fancy nesting
strategies we tend to reach for.

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
This is needed so that llb.HTTP and llb.Git can respect custom DNS
config without relying on /etc/resolv.conf. Technically we're already
overriding /etc/resolv.conf, but something was causing Go to not attempt
to reload the config without a sleep(5 seconds).

Signed-off-by: Alex Suraci <[email protected]>
TODO cleanup, reword, rebase, add details

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]>
Otherwise this causes failures with Dockerfile builds that use
non-bleeding-edge frontends.

Really should never need these domains without a service anyway, which
was the original plan.

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
now better-tested

Signed-off-by: Alex Suraci <[email protected]>
the Dagger CLI and shim depends on network now for some shared consts,
so we need to keep its dependencies tidy and cross-platform

Signed-off-by: Alex Suraci <[email protected]>
Previously you had to pass the same set of params to .service as you
would to .withExec. Now you just call .withExec as before and call
.service to return it as a Service.

The previous approach was toilsome in client use because you had to use
a different struct type with all the same params.

Technically we don't need to change the API at all anymore, so a later
might bring the old API back to maintain compatibility for now.

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]>
@vito vito mentioned this pull request Jul 19, 2023
@github-actions

github-actions Bot commented Aug 2, 2023

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.

@vito

vito commented Aug 2, 2023

Copy link
Copy Markdown
Contributor Author

Lots of baggage here, so opening a new PR for clarity: #5557

@vito vito closed this Aug 2, 2023
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.

2 participants