Skip to content

add hack/ helpers for bootstrapping engine image using stable SDK - #3755

Merged
sipsma merged 11 commits into
dagger:mainfrom
vito:mage-mod
Nov 28, 2022
Merged

sipsma merged 11 commits into
dagger:mainfrom
vito:mage-mod

Conversation

@vito

@vito vito commented Nov 9, 2022 •

Copy link
Copy Markdown
Contributor

fixes #3739

  • track bootstrap SDK version in separate Go module
  • move mage code under magefiles/ so it can be optionally run with its own go.mod/go.sum without clashing with the root go.mod
    • while also automatically picked up by regular mage CLI when you don't care about bootstrapping
  • run mage boot:run TARGET to run a target using bootstrap SDK version
    • e.g. mage boot:run engine:dev
  • run mage boot:dev TARGET to run a target using dev engine image built using bootstrap SDK version
    • e.g. mage boot:dev engine:test

@vito
vito requested review from aluzzardi and sipsma November 9, 2022 20:49
@vito vito mentioned this pull request Nov 9, 2022

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

Had some more ideas about approaches here, just kind of spitballing.

Given the setup from this PR: #3756

What do you think about a slightly different approach like this:

  1. ./hack/dev is a script that builds dev dagger using the bootstrap sdk
    • If you provide no args, all it does is build dev dagger, run it in docker and print out the DAGGER_HOST to use to connect to it
    • If you do provide args it executes whatever they are with the DAGGER_HOST env set
      • ./hack/dev make ... would let you run any other automation with your dev engine (this might also require that ./hack/dev adds the hack dir to $PATH before exec'ing the args)
      • ./hack/dev bash would open a shell where all your commands run against the dev engine
  2. Rather than symlinking go.mod, ./hack/dev just does a cd into the directory where the other go.mod exists and does go run from there to build and deploy the dev engine.
    • I just suspect this might be simpler and possible in this different approach, but could be missing something

I think that might be more flexible than the original idea of fork/exec'ing mage.

The biggest downside I can think of is that it still means users would have to remember to do ./hack/dev ... in order to run against the dev engine rather than it being the default (and running against not dev being the exception), which I think was one of @aluzzardi's general concerns around all this stuff too.

  • ./hack/dev bash helps with this I suppose. Maybe we could view ./hack/dev as the general entrypoint to doing any sort of local dev? Or maybe that's really limiting, not sure.

Just curious to get your initial thoughts on this, not so strongly opinionated that I think it's worth blocking this for a long time bikeshedding the DX of our ./hack dir :-)

@vito

vito commented Nov 10, 2022

Copy link
Copy Markdown
Contributor Author

@sipsma Makes sense, yeah this feels like a good time for just a bit of Bash glue instead. I'll play around with the new setup.

I'll likely introduce a hack/test since I find this to be a good sane default entrypoint; it'll probably just be shorthand for hack/dev hack/make engine:test or something.

@aluzzardi

Copy link
Copy Markdown
Contributor

Something is bothering me, but I can't quite put my finger on it. Maybe that's just the price to pay to test dagger with dagger, I can't find a simpler approach.

My main concern is our tests go through 3 different DAGGER_HOST -- 1) bootstrap with release engine 2) run pipeline with bootstrapped engine 3) dagger-in-dagger for tests running in a ctr.Exec()

Even though I know how this works, I keep getting confused ("where am I?")

Trying to find an alternative but I'm empty handed for now

@vito

vito commented Nov 10, 2022

Copy link
Copy Markdown
Contributor Author

@aluzzardi I hear ya. It does get really confusing really quickly.

@sipsma I wonder if we should just bootstrap with a plain old Dockerfile instead? Then we can just do a regular docker build for dev/test, and for building the production multi-platform image then we can re-use the Dockerfile with Dagger?

@aluzzardi

Copy link
Copy Markdown
Contributor

@sipsma I wonder if we should just bootstrap with a plain old Dockerfile instead? Then we can just do a regular docker build for dev/test, and for building the production multi-platform image then we can re-use the Dockerfile with Dagger?

Yeah, I was thinking something along those lines.

  • Bootstrap using a Dockerfile
  • CI code assumes there's a fresh docker-image://dagger-engine-dev
    • Therefore CI code always targets main -- no need to use release dagger to build main dagger
  • Perhaps, with unix socket passthrough, no need for dagger-in-dagger -- SDKs can bootstrap a proper engine, for real

That would get the number of layers down to 1.

Downsides:

  • Development becomes a 2 step process: 1) bootstrap 2) do whatever
  • Dockerfile bootstrapping means we don't get to use dagger anymore
    • What if our build process becomes more complex (e.g. networking), won't we miss being able to generate the image in Go?
  • No dagger-in-dagger
    • Will tests get insanely slow?
    • No cache re-use between tests
    • Need to handle concurrency

@sipsma

sipsma commented Nov 10, 2022

Copy link
Copy Markdown
Contributor

My main concern is our tests go through 3 different DAGGER_HOST -- 1) bootstrap with release engine 2) run pipeline with bootstrapped engine 3) dagger-in-dagger for tests running in a ctr.Exec()

@aluzzardi I think this is just what happens when you dogfood and bootstrap though. In my experience, the bootstrap builds for compilers and distributions is always confusing+mindblowing. Honestly even more so than what we're doing since it'll often involve even more stages. So while yeah I agree that building dagger w/ dagger and testing dagger in dagger gets meta+confusing at times, it's not necessarily indicative that we're doing anything wrong. I think it's par for the course.

The alternative is what @vito said, use something else to build and test dagger. We can of course, I just personally think we'd be resigning to that too soon and missing out on great dogfooding opportunities.

  • E.g. the engine image is going to grow in complexity more and more as time goes on I think. So we'll need to rely on a progressively more and more complicated Dockerfile or will have to break up the bootstrap into multiple stages where you first build a simple engine w/ Dockerfile and use that to build the more complicated one, which is just as complex if not worse than just building with dagger in the first place.

In terms of the build, I really think we can isolate the complication entirely to just the first part where you build a dev dagger. The ./hack/dev (or ./hack/test) approach seems like it accomplishes that. It can also just consist of a single isolated directory where there's a go.mod and you just run the build from there, done. After that you have your dev dagger running and usable by future builds+tests.

@vito

vito commented Nov 10, 2022

Copy link
Copy Markdown
Contributor Author

@sipsma @aluzzardi I'm good to carry on with the dogfooding. I went through similar confusion with Bass (see buildkit.bass for my own image building code) and it definitely became handy to have a programmable builder for the Buildkit image.

The part where it was most helpful was being able to configure IP address ranges for CNI so the inner test Buildkit instance didn't conflict with the outer one (here).

My case was a little different since I'm actually running another Buildkit as a service with Bass and then running tests against it, whereas our Dagger-in-Dagger just forwards the outer engine through. But the principle still stands that our engine image will likely beef up someday and it'll be nice to have Dagger at that point.


Latest changes should be ready to review. I ended up having to keep the restructure (internal/mage/* => ./magefiles) and go.mod symlink dance for the same reason as before: once Go sees a go.mod exists it treats it as a module boundary regardless of where you run it from. So the root magefile.go has to move elsewhere, and luckily Mage also respects magefiles/, which is a convenient place to install the temporary go.mod.

Happy to take another swing at it though if I missed something.

@vito
vito marked this pull request as ready for review November 10, 2022 20:06
@vito vito changed the title mage: support using bootstrap SDK add hack/ helpers for bootstrapping engine image using stable SDK Nov 10, 2022
Comment thread .github/workflows/lint.yml 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.

Revisiting this now, one last tweak I think might make sense is something like this:

  • ./hack/make engine:test - results in the dev engine being bootstrapped and running the provided target
    • basically, ./hack/make always implicitly wraps itself with ./hack/dev (or the end effect of doing that)
  • ./hack/make-prod engine:test results in the tests running against the actual prod release, basically just skip the dev bootstrap

We can still retain ./hack/dev too to support wrapping arbitrary commands.

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.

Ran into a bunch of problems recently (catch 22): #3928 (comment)

Brainstorming here, but would this be simpler:

  • We write our pipelines targeting stable entirely
    • separate go.mod, there's no multiple version of dagger in the same codebase competing against each other
  • In our pipelines, we bootstrap dagger and run tests with dagger-in-dagger-main
    • internal/mage -> stable
    • core/integration, sdk/*/*test -> main

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.

While we're talking about this, I'd also really like the ability to run the tests against a native local engine. Having to rebuild the image all the time for engine changes (even just log.Println) is a pretty gnarly hit to the feedback loop. 😕

I'm still for testing against the engine image since that's what actually ships now, and maybe we do that in CI where the feedback loop doesn't matter, but at least for local dev it'd be awesome to optimize.

vito and others added 8 commits November 23, 2022 17:09
otherwise Dockerfile was hitting skipped errors

Signed-off-by: Alex Suraci <[email protected]>
* track bootstrap SDK version in separate Go module
* move mage code under magefiles/ so it can be optionally run with its
  own go.mod/go.sum without clashing with the root go.mod
* run mage boot:run TARGET to run a target using bootstrap SDK version
* run mage boot:dev engine:test to run engine:test target against
  dev engine built using bootstrap SDK version

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

sipsma commented Nov 24, 2022 •

Copy link
Copy Markdown
Contributor

@vito @aluzzardi I got hard blocked by a need for bootstrapping in this PR: #3983

So I went ahead and rebased this and applied the simplifications discussed in comments:

  1. All mage steps use the separate go.mod now, no script for symlinking them around or anything
    • EDIT: see my latest comment, this is actually simpler now by using go.work instead, now there's not even a need for a separate go.mod for the mage steps.
  2. ./hack/make-prod runs the mage steps using the stable dagger
  3. ./hack/make bootstraps docker-container://test-dagger-engine from local code and then runs mage steps
    • It's thus the same as before, but no need for our mage code to wrap everything in WithDaggerEngine, that util is gone entirely
  4. ./hack/dev bootstraps docker-container://test-dagger-engine and then runs any command you want with the DAGGER_HOST and other env vars set to point to it
    • @vito I think that ./hack/dev bash (or replace bash with whatever shell) enables the "run the tests against a native local engine" use case you mentioned above. Basically, run that command and now in your shell you'll be able to just run tests natively without rebuilding the engine every time. Let me know if that's what you had in mind or if it was something else.

There's also docs for all this in hack/README.md now. cc @marcosnils since you mentioned writing docs for building the CLI locally recently, it may fit in that file.

Open to bikeshedding on the various name choices, I have no attachment to the choices I went with.

@sipsma

sipsma commented Nov 24, 2022 •

Copy link
Copy Markdown
Contributor

Okay after writing my last comment I went back to use this on #3983 and realized there was still a catch 22.

Fortunately I found a fix that's relatively painless thanks to go workspaces (arguably simpler than before), pushed as a separate commit. Explanation below.


The problem is that PR changes engine session to communicate over unix sockets, which means that engine-session binary (and thus the engine image) changes AND the Go SDK needs to change in order communicate with it.

With the change I made here earlier, I could successfully bootstrap the dev engine despite those backwards incompatible changes because we now used stable engine to build dev engine. But then when I went to actually run the tests it failed because it was trying to use the old go sdk with the new dev engine-session.

Basically, this situation called for something like the original approach of symlinking in go.mod opportunistically so that we could switch between running the CI first with stable go sdk and then later with dev go sdk. However, I found a pretty nice hack to avoid that: go.work. Go workspaces have two extremely convenient properties:

  1. They enable you to override a dep with a local module
  2. They can be easily disabled by just setting GOWORK=off

The above gives us the ability to switch back and forth between use of the stable go sdk and the local dev go sdk by just switching GOWORK on and off. To be specific:

  1. Our go.mod specifies the stable version of the go sdk to use (this updates it to be v0.4.1)
  2. Our new go.work overrides go commands to use the local go sdk instead
  3. Thus GOWORK=on (the default) results in use of latest dev go sdk, GOWORK=off results in use of the stable go sdk.

So if you look at the ./hack scripts now, when we need to do something that requires use of the stable engine, we just prefix the command with GOWORK=off.


@vito @aluzzardi Provided there isn't any functional issue with this approach I think we should merge it as an incremental improvement:

  1. It solves the bootstrapping problem; Serve engine session over unix sock/named pipe #3983 is now buildable+testable whereas before it wasn't
  2. I don't think it's actually more complicated to think about than what we had going on previously. I think it may actually be a legit use of go workspaces rather than a complete and utter hack.

@marcosnils

Copy link
Copy Markdown
Contributor

There's also docs for all this in hack/README.md now. cc @marcosnils since you mentioned writing docs for building the CLI locally recently, it may fit in that file.

👍 I'll write something and send a PR targeting this branch as base.

@vito

vito commented Nov 24, 2022

Copy link
Copy Markdown
Contributor Author

Nice, I like the use of Go workspaces for this, the GOWORK=off toggle makes it much less awkward than the symlink dance.

  1. ./hack/dev bootstraps docker-container://test-dagger-engine and then runs any command you want with the DAGGER_HOST and other env vars set to point to it

    • @vito I think that ./hack/dev bash (or replace bash with whatever shell) enables the "run the tests against a native local engine" use case you mentioned above. Basically, run that command and now in your shell you'll be able to just run tests natively without rebuilding the engine every time. Let me know if that's what you had in mind or if it was something else.

Not quite, I still have to run ./hack/dev to start the new engine image if I make changes to e.g. core/*.go, unless I'm missing something. This definitely isn't a blocker though. Happy to merge your changes since this ship has already sailed, though it looks like it needs rebase.

@sipsma

sipsma commented Nov 28, 2022

Copy link
Copy Markdown
Contributor

@vito thanks for the rebase, was just about to do that and saw it was already done :-)

@aluzzardi let me know if you have any concerns with this approach

@aluzzardi

Copy link
Copy Markdown
Contributor

I think I suggested this in another PR but can't find the link: Wouldn't it be simpler and solve all the issues if we just always write our internal pipelines using stable?

As in, we put a go.mod in internal/mage and the pipelines are always targeting a stable release. Whereas our tests always target main (root go.mod has a replace)

  • ./hack/make-prod runs the mage steps using the stable dagger
  • ./hack/make bootstraps docker-container://test-dagger-engine from local code and then runs mage steps

I don't fully understand when we'd use one rather than the other. For instance, I got recently stuck with Stdout returning string rather than a File. In that case, either the pipelines assume a string is returned then only ./hack/make works, or they assume a File is returned and only ./hack/make-prod works, but they're not interchangeable right?

@sipsma

sipsma commented Nov 28, 2022

Copy link
Copy Markdown
Contributor

As in, we put a go.mod in internal/mage and the pipelines are always targeting a stable release. Whereas our tests always target main (root go.mod has a replace)

That's what I did at first too in this commit but realized it doesn't work in every case.

Using the example of my other PR where I am updating SDKs to communicate with engine session over unix sockets, this is what happens if you just have a separate internal/mage/go.mod where a stable go sdk is specified and then try to run engine:test:

  1. You successfully use stable Go SDK + stable engine to bootstrap dev engine
  2. You then try to run the engine tests against it, which requires dagger-in-dagger and thus setting up an exec
  3. You are now trying to use the stable Go SDK to run an exec with the dev engine. Stable Go SDK tries speaking over localhost to engine-session using unix sockets and you get an error.

The go.work approach fixes this by allowing us to use stable sdk + stable engine to bootstrap and then switch to dev sdk + dev engine after the bootstrap.

There are other possible fixes but they are much more involved:

  1. Try to update my PR to support both localhost and unix socket connections and make it configurable; but that would be a pretty big change and require weird hacks in our actual code solely for the purpose of working around this bootstrapping issue.
    • Also will need to repeat this approach for any similar changes in the future
  2. Support mounting docker.sock into the test container and then mount in the dev engine-session binary instead of using the PrivilegedNesting flag.

For instance, I got recently stuck with Stdout returning string rather than a File. In that case, either the pipelines assume a string is returned then only ./hack/make works, or they assume a File is returned and only ./hack/make-prod works, but they're not interchangeable right?

Yeah true, this PR fixes the problem where we have an internal backwards incompatible change (unix sockets in my case) but it won't work if we ever need to make another fully backwards-incompatible API change like that (though @deprecated APIs should still be fine).

@aluzzardi What do you think of this plan:

  1. Use go.work for now so the other PR is unblocked
    • I don't think we have any fully backwards incompatible changes in the API planned, so this could be okay on a very temporary basis?
  2. Once we have docker socket mount support then switch our engine tests to use that
  3. At that time, also change back to my previous commit where there is a separate go.mod for internal/mage, no more switching back and forth with go.work.

If there's other approaches for unblocking my PR I'm all ears. I just really want to avoid that PR needing to introduce a way of supporting both localhost and unix sockets at the same time, but if others think making that change is better than the go.work approach (and associated api backcompat issues) then I can go that route instead.

@aluzzardi

Copy link
Copy Markdown
Contributor

@aluzzardi What do you think of this plan:

  1. Use go.work for now so the other PR is unblocked

    • I don't think we have any fully backwards incompatible changes in the API planned, so this could be okay on a very temporary basis?
  2. Once we have docker socket mount support then switch our engine tests to use that

  3. At that time, also change back to my previous commit where there is a separate go.mod for internal/mage, no more switching back and forth with go.work.

Yes, that makes sense! Whatever unblocks us right away.

Long term, I think a separate go.mod is the "safe" plan since there's still chicken-and-egg problems (e.g. stdout)

Yeah true, this PR fixes the problem where we have an internal backwards incompatible change (unix sockets in my case) but it won't work if we ever need to make another fully backwards-incompatible API change like that (though @deprecated APIs should still be fine).

One cool but annoying side effect of @deprecated is the linter fails when using @deprecated functions. I don't know whether linters honor go.work or not, but either way a @deprecated usage is still a "breaking" change for the CI

  • You then try to run the engine tests against it, which requires dagger-in-dagger and thus setting up an exec
  • You are now trying to use the stable Go SDK to run an exec with the dev engine. Stable Go SDK tries speaking over localhost to engine-session using unix sockets and you get an error.

Not sure I fully understand this part. I would have assumed that mage uses stable (sdk and engine) all the way up until exec(go test) (and mounting the dev session binary), at which point we'd "switch" to dev for the duration of the tests. The problem though is the session binary is talking to non-dev engine and that's where it falls apart. Is that correct?

@sipsma

sipsma commented Nov 28, 2022

Copy link
Copy Markdown
Contributor

I would have assumed that mage uses stable (sdk and engine) all the way up until exec(go test) (and mounting the dev session binary), at which point we'd "switch" to dev for the duration of the tests.

That's what would be ideal, but in practice we can't do that yet. In order to test against the dev engine we have to set DAGGER_HOST=docker-container://test-dagger-engine, but if we do that then the stable SDK is trying to setup the dagger-in-dagger exec using the dev session binary and it fails. So in this case it's that we have a backwards incompatible change between SDK <-> session bin.

That's why support for mounting docker.sock should fix it; then we don't have to set DAGGER_HOST on the host; we only have to set it in the exec and can just use stable sdk on the host the whole time.

@aluzzardi

Copy link
Copy Markdown
Contributor

That's what would be ideal, but in practice we can't do that yet. In order to test against the dev engine we have to set DAGGER_HOST=docker-container://test-dagger-engine, but if we do that then the stable SDK is trying to setup the dagger-in-dagger exec using the dev session binary and it fails. So in this case it's that we have a backwards incompatible change between SDK <-> session bin.

In the "new world" approach (#3830 (comment)), we could just set _EXPERIMENTAL_DAGGER_CLI_BIN and mount the binary, right? The only problem though is it would be talking to "stable" engine, assuming the shim sets up _EXPERIMENTAL_DAGGER_RUNNER_HOST

@sipsma

sipsma commented Nov 28, 2022

Copy link
Copy Markdown
Contributor

The only problem though is it would be talking to "stable" engine, assuming the shim sets up _EXPERIMENTAL_DAGGER_RUNNER_HOST

Yeah exactly, we wouldn't actually be testing the engine we want to test.

Once all the changes in #3830 are through and docker.sock mounting is available, engine tests can be updated to do this:

  1. Use stable sdk+engine to bootstrap test-dagger-engine container (same as today)
  2. Instead of setting ExperimentalPrivilegedNesting to true in the engine test exec, mount docker.sock in there along with the dev engine-session bin
  3. Explicitly set _EXPERIMENTAL_DAGGER_CLI_BIN=/path/to/dev/engine-session
  4. Explicitly set _EXPERIMENTAL_DAGGER_RUNNER_HOST=docker-container://test-dagger-engine

@aluzzardi

Copy link
Copy Markdown
Contributor

Makes sense! :shipit:

@sipsma

sipsma commented Nov 28, 2022 •

Copy link
Copy Markdown
Contributor

(I just saw that the merge commit from before seems to have undone some new logic in the nodejs publish step, fixing, then will merge once I have an approval)

EDIT: nevermind, was getting confused by rebase diffs, this should be good to go

@sipsma
sipsma merged commit 58c782e into dagger:main Nov 28, 2022
@sipsma

sipsma commented Nov 28, 2022

Copy link
Copy Markdown
Contributor

I used the squash and merge button based on recent advice, for some reason that resulted in undefined being the commit message? 58c782e

Sorry that looks weird @vito, I didn't fully realize the implications of squash+merge.

@vito

vito commented Nov 28, 2022

Copy link
Copy Markdown
Contributor Author

lol, weird. np!

there must be some JS in there somewhere. 😄

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.

Update engine:dev to be more of a bootstrap

4 participants