Skip to content

Support full isolated sessions w/ dagger-in-dagger. - #3787

Merged
sipsma merged 8 commits into
dagger:mainfrom
sipsma:engine-experiment
Nov 18, 2022
Merged

sipsma merged 8 commits into
dagger:mainfrom
sipsma:engine-experiment

Conversation

@sipsma

@sipsma sipsma commented Nov 11, 2022 •

Copy link
Copy Markdown
Contributor

Signed-off-by: Erik Sipsma [email protected]

Goal

Get engine session nesting fully working, which enables tests of dagger-in-dagger that involve local dirs. Now, when running dagger-in-dagger a new engine session is started up (the same as when outside dagger) and local directories refer to directories local to the container (not the host).

Pros:

  1. Dagger-in-dagger works fully, including localdir stuff.
  2. Dagger-in-dagger is actually safe-ish now? It's no longer directly possible for an exec using dagger-in-dagger to access host resources
    • There are still roundabout ways that they might be able to do this due to the fact that buildkitd internally has a bunch of codepaths that will try "any connected session", however that's an independent issue we were always going to need to solve anyways; the previous problem of local dirs going back to the host is solved by this I think.
  3. This gets rid of the need for the shim to setup localhost->unix proxying because we can just re-use engine-session's ability to do that already.
    • I honestly wonder if shim and engine-session could become the same thing... but that might be too far
    • That could make sense though... every exec has a shim mounted in anyways, why not just make that be engine-session too?
  4. I think this might answer a ton of questions about extensions.
    • E.g. if extensions re-use this mechanism, then they might already have isolated schemas rather than a global one

Cons

  1. Directly exposes buildkitd API
    • However, I actually don't think this is inherent at all. We wanted to wrap buildkitd anyways, so once we do that, we can just replace all occurrences of "buildkit" above with "dagger-runner" and no longer directly expose APIs we don't have direct control over.
  2. Goes further down the path of reliance on a local engine-session bin
    • We're already kind of going down that path though

Outline of solution

  1. ExperimentalPrivilegedNesting now results in the exposed API socket providing the runner API (aka buildkit api for now) instead of an endpoint to the current session.
  2. Dagger-in-dagger now requires the engine session binary be present. When present, it can be used by sdks via the new bin:// engineconn, which can point to an existing binary (or if bin:// is empty, $PATH will be searched for dagger-engine-session)
    • Ugliness: The engine-session binary is not provided out of the box when ExperimentalPrivilegedNesting is set. It has to be provided by the user right now and put somewhere in the PATH.
      • I wanted to just have our code mount in the engine session binary from the engine image, but I realized that's much easier said than done right now. We need to solve that problem for a bunch of reasons, but I'm making a follow up issue for it, not practical for this PR.
    • Ugliness: When you provide bin:// as the engineconn, the engine-session binary still needs to be told where the runner it should connect to is (default provisioner obviously doesn't work). This is done through a new env var DAGGER_RUNNER_HOST (which is essentially like BUILDKIT_HOST as of now)
      • This is highly specific to our test case needs. In the longer run, I suspect that we'll actually be able to just make the shim do the equivalent of dagger exec and wrap all the processes in the exec in a single session.
      • Our test cases however need to each run in separate sessions, in which case bin:// will still need to be used and we will still need to be able to tell the invoked binary that it should find the runner api at unix:///dagger-runner.sock.
      • DAGGER_RUNNER_HOST may thus be a long-term feature, but it should only be needed for our test cases; users in general should not need to learn about it or use it.
  3. I managed to get extensions to continue working too
    • Ugliness: they work by essentially just retaining the old behavior where /dagger.sock is just an endpoint to the current session.
    • There's thus two different socket session providers now: RunnerProxy is used for Execs w/ ExperimentalPrivilegedNesting enabled and SessionProxy is used when running extensions
    • The only reason for this is that updating extensions to have the engine-session binary automatically mounted in is too much work for this PR. Once that is possible, then extensions can just update to use the new SDKs and should be good to go.

Path to cleaning up remaining ugliness

  1. We should merge the engine session functionality into the shim and make the shim essentially just do the equivalent of dagger exec when nested dagger is enabled.
    • This will eliminate the requirement that dagger-engine-session binary be mounted into these execs by the user
  2. If we also use the shim in extensions, we will fix the weirdness there and there can just be one /dagger.sock (RunnerProxy) rather than the current split where theres both RunnerProxy for execs and SessionProxy for extensions.

Remaining TODOs for this PR

  • I've gotten a few ephemeral failures of TestPlatformCrossCompile. It's not clear whether/how this could be related to the changes here, but it's happened maybe 1-in-5 engine test runs, so it's too flaky to ignore. Need to look into it more

@sipsma
sipsma requested review from aluzzardi and vito November 11, 2022 01:55
@aluzzardi

Copy link
Copy Markdown
Contributor

Wrapping my head around it ... I think there's a bunch of pros, would get us closer to the "full" session architecture in a very incremental way

A few comments / questions:

  • This means that DAGGER_HOST effectively becomes BUILDKIT_HOST
  • Everything uses their own engine, there's no way to share an engine
    • dagger listen and DAGGER_HOST=<that dagger listen> wouldn't work anymore (unless I'm mistaken)
    • No continuity between engines
      • What happens with logs/tracing/etc with extensions / dagger-in-dagger?
  • The fact that we're translating DAGGER_HOST=http:// to (more or less) BUILDKIT_HOST=tcp:// can be confusing
    • Since dagger is a GraphQL API, when I see http:// I just assume that's a GraphQL API endpoint
  • Not only it solves dagger-in-dagger, it also solves cache re-use and a bunch of side problems
    • Bootstrap engine, dev engine and engine-in-engine share the same cache

@sipsma

sipsma commented Nov 11, 2022 •

Copy link
Copy Markdown
Contributor Author

This means that DAGGER_HOST effectively becomes BUILDKIT_HOST

Not literally (BUILDKIT_HOST is still totally ignored by our code now), but spiritually more or less yes, but that also is a reflection of the fact that our "remote engine" is currently just buildkitd under a different name, like you said earlier.

Honestly DAGGER_RUNNER_HOST might be a better name now?

As we wrap buildkit more, then the fact that we are really just connecting to buildkit will become less and less true, which is a good thing I think. It basically just evolves cleanly as our architecture evolves.

There's also still technically a difference in that docker-image and docker-container still do all the provisioning stuff rather than pass-through directly to buildkit.

Everything uses their own engine, there's no way to share an engine

Yes this doesn't make router shared, it keeps router inside of engine-session as before. But I'm not sure it rules out a shared router forever though? I feel like we're in the same state as before, except that now at least we have isolated "sessions" both for clients outside dagger and inside dagger.

Honestly, I'm starting to question what benefits we even get by making router multitenant. We absolutely get tons of benefits by making the runner (aka buildkitd right now) multitenant. But I'm less sure about router now. But I easily could be forgetting obvious things here.

dagger listen and DAGGER_HOST= wouldn't work anymore (unless I'm mistaken)

Actually I think dagger listen still works at least the same as before. dagger listen uses the sdk, which invokes dagger-engine-session as a subprocess and then just proxies requests to that subprocess. Or honestly maybe dagger-engine-session can just be configured to listen on something besides localhost and then it actually just literally is dagger listen?

Either way, I'm not sure this change impacts the options there, but I again could be missing something.

The fact that we're translating DAGGER_HOST=http:// to (more or less) BUILDKIT_HOST=tcp:// can be confusing

Agree more or less, but I actually think that might just mean we should get rid of DAGGER_HOST=http:// and just make it DAGGER_HOST=tcp://. Basically, what you specify there is not the protocol. It's the transport that engine-session should use to talk to the remote runner (which is currently buildkitd, subject to change as we evolve).

Not only it solves dagger-in-dagger, it also solves cache re-use and a bunch of side problems

Yeah when I re-run locally the engine tests are all fully cached every time until I actually make a change to them, it's pretty nice!

@vito

vito commented Nov 12, 2022

Copy link
Copy Markdown
Contributor

Honestly, I'm starting to question what benefits we even get by making router multitenant. We absolutely get tons of benefits by making the runner (aka buildkitd right now) multitenant. But I'm less sure about router now. But I easily could be forgetting obvious things here.

Same. For me there's evidence that we want multiple or at least per-session routers (installing extensions) but I can't think of much evidence to the contrary.

Especially if we're likely to need SDKs to run a helper binary either way (multitenant router + sessions service helper || bring-your-own-router) - this scheme seems much simpler. At least for now.

@sipsma
sipsma force-pushed the engine-experiment branch 8 times, most recently from b604cb7 to d2a5d77 Compare November 16, 2022 22:44
@aluzzardi

Copy link
Copy Markdown
Contributor

Agree more or less, but I actually think that might just mean we should get rid of DAGGER_HOST=http:// and just make it DAGGER_HOST=tcp://

I think part of the confusion for me is we effectively have 2 DAGGER_HOST:

  • One is needed by the session to talk to the engine (e.g. buildkit)
  • One is needed by the end client, to fire up pure GraphQL queries (e.g. "engine")

I assume, as an end client (e.g. graphiql, some validation tool, a random Python GraphQL library), I don't care about parsing tcp:// and whatnot -- I want to pass that http:// DAGGER_HOST verbatim to the tool/library

@sipsma

sipsma commented Nov 16, 2022

Copy link
Copy Markdown
Contributor Author

@aluzzardi I made a few tweaks to the approach, updated the description of the PR with how it works now.

There's some ugliness remaining that I call out there; I think it overall still is worth merging this as an incremental step since it seems like a net improvement and fixing all the ugliness will expand the scope too much.

Also need to look into one flaky test, other than that it's ready to review.

I think part of the confusion for me is we effectively have 2 DAGGER_HOST:

Yes, I modified the approach a bit and ended up just leaving http for now. I still think it's kind of confusing in that we always speak http and the thing that changes is the transport, but that's a totally separate issue from this PR now; not touching it here.

RE: 2 DAGGER_HOST: yeah I actually did end up needing to do pretty much that, DAGGER_HOST is now the same as it was before (just with a new bin:// conn) but there also is a need for DAGGER_RUNNER_HOST when using bin:// in our dagger-in-dagger test cases specifically.

The upside is that DAGGER_HOST remains the same as before and the new complication of DAGGER_RUNNER_HOST is highly specific to the needs test cases; in general it's not something that is needed commonly by users. More in the updated PR description.

@sipsma
sipsma marked this pull request as ready for review November 16, 2022 23:45

@aluzzardi aluzzardi 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.

I'm missing one piece -- I don't understand how nesting works.

We're setting DAGGER_RUNNER_HOST (so the session can work), but not DAGGER_HOST -- this should trigger the provisioning of the session, but that can't be done without the docker socket.

Am I missing something?

EDIT: Got the answer from the PR description, I jumped into the code too quickly

Comment thread internal/engine/client.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.

If I'm readying this correctly:

  • unix is the equivalent of BUILDKIT_HOST
  • docker-container:// is the equivalent of BUILDKIT_HOST
  • docker-image:// creates the container, then it's the equivalent of docker-container:// as a BUILDKIT_HOST

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.

Yep that's all correct

Comment thread project/project.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.

This is basically temporary until we rework extensions to use the same mechanism as nesting?

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.

Yes exactly. I think we should:

  1. Put the engine session functionality into shim (want that anyways)
  2. Have shim wrap extension execution the same as it does exec

With those two changes, extensions will work the same as exec and this can all be simplified.

Comment thread sdk/go/internal/engineconn/bin/bin.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.

maybe nit: if you stop right here and don't do the ifs below, then bin://dagger-engine-session would work out of the box (cmd exec will look in PATH if it's not a relative/absolute path)

We'd lose early validation though of the binary being executable

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 good call. I left in some early checks but fixed it up so that we can support bin://dagger-engine-session; did the same in Python too. NodeJS will support that now too afaict but without the early checks, which is fine I think.

Comment thread sdk/go/internal/engineconn/unix/unix.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.

Is this for extensions (e.g. this is talking to the session, not buildkit)?

In that case, shouldn't this be http?

Otherwise no way to connect to dagger listen, and this can't be done in Python (at all) / TypeScript (without hacks)

@sipsma sipsma Nov 17, 2022 •

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.

Oh yeah so this is needed to get extensions to work, but you're right that it means we'd need to add unix to the nodejs SDK, so I was very confused as to why tests were passing after reading your comment...

Then realized we removed the yarn extension test a few days ago: f745097

  • This was just so we didn't have to waste time maintaining extensions in the midst of the release work, which makes total sense to me

So yeah, Python and NodeJS extensions won't work currently with this. I can see if there's any other updates possible in the short term, but I think no matter what the long term fix will be the same as elsewhere: include engine-session functionality with each extension exec (probably via the shim), at which time this will disappear.

I'm inclined to just accept that nodejs+python extensions are broken at the moment when using dagger-in-dagger and wait for that fix; given the test case was removed I think that is probably the state of them anyways. We at least still have Go extensions working to test the the project codepaths are basically in tact. Let me know if you disagree though.

@sipsma

sipsma commented Nov 17, 2022

Copy link
Copy Markdown
Contributor Author

@aluzzardi

We're setting DAGGER_RUNNER_HOST (so the session can work), but not DAGGER_HOST -- this should trigger the provisioning of the session, but that can't be done without the docker socket.

This update currently results in users setting DAGGER_HOST rather than setting it automatically for them (e.g. here).

This is due the ugliness I mentioned in the description: we can't automatically mount dagger-engine-session yet until we merge that functionality into the shim (which is currently easier said than done I think, saving it for a follow up).


New thought: we could set DAGGER_HOST=bin:// automatically actually, which will kick in the "find dagger-engine-session in $PATH" behavior and save users from having to set it manually.

  • Caveat: I did not find a nodejs library for searching $PATH that was both compatible with our version of node and also reasonable quality (see comment here)
  • However, I can look some more or worst case just write something myself quickly

Let me know what you think, that probably cleans up a little bit of ugliness in the short term and makes things overall more clear. Will update if you agree.

@aluzzardi

Copy link
Copy Markdown
Contributor

Ugliness: The engine-session binary is not provided out of the box when ExperimentalPrivilegedNesting is set. It has to be provided by the user right now and pointed to via DAGGER_HOST by them too.

Right. No easy way around that

Ugliness: When you provide bin:// as the engineconn, the engine-session binary still needs to be told where the runner it should connect to is (default provisioner obviously doesn't work). This is done through a new env var DAGGER_RUNNER_HOST (which is essentially like BUILDKIT_HOST as of now)

Default provisioner would work outside of our very specific CI case, right?

I think it's semi ugly, but it'll be needed eventually when wanting to run with a particular runner.

@sipsma

sipsma commented Nov 17, 2022

Copy link
Copy Markdown
Contributor Author

Default provisioner would work outside of our very specific CI case, right?

Yes exactly, default will still work the same before. This is only needed in our very non-default use case of wanting to test dagger-in-dagger but have every individual test case be its own session.

I think it's semi ugly, but it'll be needed eventually when wanting to run with a particular runner.

True, in the long term we can choose to embrace this more too if the user need arises

@aluzzardi

aluzzardi commented Nov 17, 2022 •

Copy link
Copy Markdown
Contributor

New thought: we could set DAGGER_HOST=bin:// automatically actually, which will kick in the "find dagger-engine-session in $PATH" behavior and save users from having to set it manually.

Caveat: I did not find a nodejs library for searching $PATH that was both compatible with our version of node and also reasonable quality (see comment here)

See other comment -- DAGGER_HOST=bin://dagger-session should do the trick (at least in Go). When not specifying a abs/rel path, it's implied it's in $PATH (e.g. cmd.Exec("ls")). Maybe Python/Node have the same behavior, in which case we could just set it by default

@sipsma

sipsma commented Nov 17, 2022

Copy link
Copy Markdown
Contributor Author

See other comment -- DAGGER_HOST=bin://dagger-session should do the trick (at least in Go). When not specifying a abs/rel path, it's implied it's in $PATH (e.g. cmd.Exec("ls")). Maybe Python/Node have the same behavior, in which case we could just set it by default

Good point, I'll check if nodejs has that behavior too

@sipsma sipsma changed the title Experiment w/ getting dagger-in-dagger working w/ localdirs. Support full isolated sessions w/ dagger-in-dagger. Nov 17, 2022
@sipsma

sipsma commented Nov 17, 2022 •

Copy link
Copy Markdown
Contributor Author

@aluzzardi Updated with:

  • Defaulting to bin:// and the other fixes around path handling you suggested
    • It turns out that even though I couldn't find it documented anywhere, nodejs does seem to have the behavior of searching PATH (not sure if from execa lib or the std lib it uses underneath), so just relying on that now
  • Added a util to mount ~/.docker into the dagger-in-dagger test containers if it exists on the host. I started getting rate limits from docker after a while without it...
  • A possible fix to the flake I mentioned previously. It was of course extremely rare locally so I'm not 100% sure it's gone, but hasn't occurred in the last few CI runs so feeling okay about it

cc @helderco I had to add a new connector to the Python SDK for this. I tried to use inheritance to minimize code dupe but let me know if there's any other cleanups or better approaches

Among other things, this enables local dirs to work as expected when
using dagger-in-dagger (they are the actual localdir of the container).

Signed-off-by: Erik Sipsma <[email protected]>
The examples tests before this were accidentally passing. The workdir
test was actually pulling the README.md from the host workdir, not the
local one. Now that dagger-in-dagger uses the actual localdir of the
test, we need to include the file that it was trying to read.

Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Not totally sure but think this could be the cause of some flakiness of
the TestPlatformCrossCompile test case (saw in the logs that failures
happen when there is also an error from a registry indicating that port
5000 is already taken...)

Also included a few tweaks to make test case failures slightly easier to
debug.

Signed-off-by: Erik Sipsma <[email protected]>
Otherwise running tests locally results in tons of rate limit errors
eventually.

Signed-off-by: Erik Sipsma <[email protected]>
@helderco

helderco commented Nov 18, 2022 •

Copy link
Copy Markdown
Contributor

Thank you for rebasing, I'm 👀 now.

@helderco helderco 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.

I love the separation 👍

Give me a moment and I'll update the Python part.

Comment thread sdk/python/src/dagger/connectors/bin.py Resolved
@sipsma

sipsma commented Nov 18, 2022

Copy link
Copy Markdown
Contributor Author

Give me a moment and I'll update the Python part.

Thank you @helderco! Appreciate it greatly!

@aluzzardi aluzzardi 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.

LGTM!

/cc @shykes if you want to take a look at the implications of DAGGER_RUNNER_HOST etc. This is only used by our test suite and ExperimentalPrivilegedNesting = true, but still.

@shykes

shykes commented Nov 18, 2022

Copy link
Copy Markdown
Contributor

LGTM!

/cc @shykes if you want to take a look at the implications of DAGGER_RUNNER_HOST etc. This is only used by our test suite and ExperimentalPrivilegedNesting = true, but still.

Thanks. What would be helpful is a summary of supported values for DAGGER_RUNNER_HOST. I'm guessing it's a URL? If so, what schemes are supported and what do they do? I think this information should live in a file somewhere in the repo - but I don't know where. Wherever it's most likely to stay up-to-date I'd say. If that means it's a comment in a source file somewhere, I'm ok with that :) We just need it written down somewhere other than an issue comment IMO.

@sipsma

sipsma commented Nov 18, 2022

Copy link
Copy Markdown
Contributor Author

Thanks. What would be helpful is a summary of supported values for DAGGER_RUNNER_HOST. I'm guessing it's a URL? If so, what schemes are supported and what do they do? I think this information should live in a file somewhere in the repo - but I don't know where. Wherever it's most likely to stay up-to-date I'd say. If that means it's a comment in a source file somewhere, I'm ok with that :) We just need it written down somewhere other than an issue comment IMO.

@shykes Yeah that makes total sense. I am going to merge this PR at this exact moment so it unblocks others but I will follow up today with a PR adding a more cohesive summary to a code comment as right now it exists but is scattered across comments in multiple files (made an issue for it here #3921)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants