Skip to content

WIP feat: Bundling buildkit - #3187

Closed
grouville wants to merge 2 commits into
dagger:mainfrom
grouville:bundling_buildkit
Closed

grouville wants to merge 2 commits into
dagger:mainfrom
grouville:bundling_buildkit

Conversation

@grouville

@grouville grouville commented Sep 29, 2022 •

Copy link
Copy Markdown
Member

This is the first advanced draft regarding the bundling of buildkit as a daemon. Almost ready to be merged with current state of cloak codebase.

2 new commands have been added

  • cloak serve
  • cloak stdio (for buildkit to be able to run)

Cloak stdio

Cloak stdio is the port of the stdio command that Buildkit relies on

Cloak serve

Cloak serve embeds Buildkit's main function, currently only implemented on linux architectures. It also contains all the dependencies/tweaks/utilities necessary to make it work.

A builder/provisioner architecture has been implemented, with an interface permitting to add any new provisioner / builder in the codebase. The only provisioner / builder currently implemented is Docker.

WARNING: The current implementation rebuilds everytime the image. A new way to handle that part could be added as a future improvement.

UPDATE: Will fix the rebuild part, as it currently leads to race conditions

@samalba Fixes at the same time #3015. May conflict with your PR #3185
cc @sipsma

@grouville
grouville requested a review from sipsma September 29, 2022 22:37
@netlify

netlify Bot commented Sep 29, 2022 •

Copy link
Copy Markdown

👷 Deploy request for cloak-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit e9297eaf87e82ac0ecf771c723cec5fd05188b83

@grouville grouville changed the title WIP: Bundling buildkit (still test case not working) WIP: Bundling buildkit (still one test case not working) Sep 29, 2022
@grouville grouville changed the title WIP: Bundling buildkit (still one test case not working) WIP: feat: Bundling buildkit (still one test case not working) Sep 29, 2022
@grouville grouville changed the title WIP: feat: Bundling buildkit (still one test case not working) feat: Bundling buildkit Sep 29, 2022
@grouville grouville changed the title feat: Bundling buildkit WIP: feat: Bundling buildkit Sep 30, 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.

It looks really good so far! I just left a first pass of comments, mostly little things or follow-ups. I will take a look again after the update to check version before replacement

Comment thread embeding.go Resolved
Comment thread Dockerfile.daggerd Resolved
Comment thread cmd/cloak/serve.go Resolved
Comment thread go.mod Resolved
Comment thread internal/buildkitd/buildkitd.go Resolved
Comment thread internal/buildkitd/bundling/LICENSE 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 the buildkitd license? Makes sense if so, just checking. cc @aluzzardi, we have large chunks of code from buildkitd's main func here now, just want to double-check that including the license is all that's needed

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is, yes

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

Looks good, I will try it out locally tomorrow. Few more minor comments in the mean time. Test failure is the only important one (seems like an easy fix).

BTW the platform issue you mentioned earlier should be fixed by this: #3226

Comment thread internal/buildkitd/buildkitd.go Resolved
Comment thread internal/buildkitd/buildkitd.go Resolved
Comment thread cmd/cloak/serve.go Resolved
@grouville
grouville force-pushed the bundling_buildkit branch 2 times, most recently from 3280acf to 0ee9151 Compare October 4, 2022 15:18
@grouville

grouville commented Oct 4, 2022 •

Copy link
Copy Markdown
Member Author

@sipsma. Got it into a mergeable state.

@grouville grouville changed the title WIP: feat: Bundling buildkit feat: Bundling buildkit Oct 4, 2022
@grouville

grouville commented Oct 4, 2022 •

Copy link
Copy Markdown
Member Author

I might have found decent workaround to my truly hackish StartGoModDaggerd implem: rely on ftok, whose sole purpose is to generate a consistent key for inter-process communication + generate a hash from the go test path binary. Needs testing tomorrow, but getting closer to an implem with a truly nice DX (nothing to do for the dev)

@shykes

shykes commented Oct 4, 2022

Copy link
Copy Markdown
Contributor

Cloak stdio

Cloak stdio is the port of the stdio command that Buildkit relies on

What does this command do exactly? Do we want to show it to users, or hide it?

Cloak serve

Cloak serve embeds Buildkit's main function, currently only implemented on linux architectures. It also contains all the dependencies/tweaks/utilities necessary to make it work.

Does it run the graphql server also? If not, what command does that?

A builder/provisioner architecture has been implemented, with an interface permitting to add any new provisioner / builder in the codebase. The only provisioner / builder currently implemented is Docker.

I know what a provisioner does, but what is the job of the "builder"? Is it to build a dagger binary, a dagger container image, or both? Why does it need to be pluggable?

@grouville grouville changed the title feat: Bundling buildkit WIP feat: Bundling buildkit Oct 4, 2022
@grouville
grouville force-pushed the bundling_buildkit branch 3 times, most recently from 706dfb7 to f72b558 Compare October 5, 2022 14:07
@grouville

Copy link
Copy Markdown
Member Author

Cloak stdio

Cloak stdio is the port of the stdio command that Buildkit relies on
What does this command do exactly? Do we want to show it to users, or hide it?

It proxies the stdio stream to the daemon connection. It should not be invoked manually and only exists on linux builds. It is a hidden subcommand:

Hidden: true,

Cloak serve

Does it run the graphql server also? If not, what command does that?

Not at the moment. It is, as a first step, a port of Buildkit. The aim is to have a solid, minimal integration of Buildkit, with a minimal build-serve implementation on which we could iterate and expand (add graphQL server, for example)

I know what a provisioner does, but what is the job of the "builder"? Is it to build a dagger binary, a dagger container image, or both? Why does it need to be pluggable?

It can be for any of those. It needs to be pluggable, because the other options we tried seemed more complex:

  1. Import the go compiler as lib (doesn't exist)
  2. Embed the go binary (100mb) and compile on-the-fly
  3. Embed the daggerd binary, for each arch
  4. Embed the source code, and build an image (easier as a first step), and a provisioner that will run it.

We could also assemble images, by directly embedding the layers, but this could fit one of the option of the "builder" available. Some other context: #3250 (comment)

@shykes

shykes commented Oct 5, 2022

Copy link
Copy Markdown
Contributor

Cloak stdio

It proxies the stdio stream to the daemon connection. It should not be invoked manually and only exists on linux builds. It is a hidden subcommand:

👍 thanks

Cloak serve

It is, as a first step, a port of Buildkit. The aim is to have a solid, minimal integration of Buildkit, with a minimal build-serve implementation on which we could iterate and expand (add graphQL server, for example)

👍 thanks

I know what a provisioner does, but what is the job of the "builder"? Is it to build a dagger binary, a dagger container image, or both? Why does it need to be pluggable?

It can be for any of those. It needs to be pluggable, because the other options we tried seemed more complex:

  1. Import the go compiler as lib (doesn't exist)
  2. Embed the go binary (100mb) and compile on-the-fly
  3. Embed the daggerd binary, for each arch
  4. Embed the source code, and build an image (easier as a first step), and a provisioner that will run it.

We could also assemble images, by directly embedding the layers, but this could fit one of the option of the "builder" available. Some other context: #3250 (comment)

I have several questions here:

  1. What's the best option for building the dagger binary?
  2. Why not just choose that best option and implement it? What's the benefit of letting the user choose?
  3. What's the best option for packaging the dagger binary into a docker image?
  4. Why not just choose that best option and implement it? What's the benefit of letting the user choose?

I understand the value of pluggable backends for provisioning the dagger image. I don't understand the value of pluggable backends for building the binary or the image. Sorry if I'm missing something obvious.

@sipsma

sipsma commented Oct 5, 2022

Copy link
Copy Markdown
Contributor

I understand the value of pluggable backends for provisioning the dagger image. I don't understand the value of pluggable backends for building the binary or the image. Sorry if I'm missing something obvious.

The intention is not to expose this to users in the same way we plan to expose the provisioner concept to users (sorry this probably wasn't clear); we are modelling this "builder" as an interface in our own internal code so we can retain flexibility in terms of switching from docker build to newer and better implementations in the long-run. It's really just meant to ensure we don't hardcode+entangle this first implementation, which likely has a limited lifespan.

What's the best option for building the dagger binary?

There's tons of tradeoffs and branch points here, so it's really hard to say which one is "best". Some previous discussion here. Happy to discuss more on the individual possibilities too, but I guess the main point is that we will be choosing on behalf of users, not asking them.

@shykes

shykes commented Oct 5, 2022

Copy link
Copy Markdown
Contributor

The intention is not to expose this to users in the same way we plan to expose the provisioner concept to users

Ah! Now I understand. Thanks!

@aluzzardi

Copy link
Copy Markdown
Contributor

[Opening up a discussion: I think there's something a bit funky going on with the architecture, it's worth to talk it through. Nothing to do with the PR itself]

Cloak stdio
Cloak serve

Note: this is clashing with cloak's own commands (especially with #3265 which adds stdio support as well). cloak dev might become cloak serve someday, since we're removing graphiql.

The fact the commands are clashing is a symptom of some weirdness, I think. Basically we have:

[client using cloak SDK] --(stdio)---> [cloak binary] --(stdio)--- > [cloak binary]

Even though there's two [cloak binary] boxes in there, in reality they use two different protocols, with two different "main" functions (it's the same one, but they don't share a single line of code).

I see buildkitd is rebranded as "daggerd", but in practice we end up with two daemons I suppose? From the user's point of view, this is the chain of processes going on:

[SDK] --> [dagger] -- > [daggerd]

I don't have really sketched out answers for this (and maybe @sipsma you already do?), but I was thinking:

  1. It doesn't matter, it's a temporary problem because [insert thing here that we'll do in the future].
  • Maybe dagger and daggerd (I'm confusing myself here with the naming point above -- I mean cloak engine and buildkitd) will be one and the same, don't communicate over network, and the combined thing will run in a single daggerd container?
    • But I don't think so, since we have the constraint of one daemon running on the host (local dirs etc) and one running in a linux container (need cgroups), so the two will always clash
    • Unless on the host side, we have a stripped out binary used by the SDKs that only takes care of local dirs etc?
  • lima/colima solves everything because [reasons]
  • dagger/daggerd become the same, serving the same protocol. When running on Linux, it doesn't matter. When running somewhere else, daggerd somehow magically "fork execs" itself into a container, and the one on the host becomes a proxy-ish?
  1. Or, option 2, it does matter and there's no long term solution. In which case the nomenclature as is doesn't pass the "draw it on a whiteboard" test, IMHO
  • Maybe "daggerd" (ex buildkitd) needs a proper name. Making things up to give an example but dagger-engine vs dagger-builder, whatever.
    • In which case if they have proper names distinct to themselves, I think it would make more sense to have them as separate binaries to avoid a flags soup (and to avoid having the whole dagger build itself, it could just build the "dagger-builder", just like we build the "dagger-shim")
  1. Anything else?

@sipsma

sipsma commented Oct 6, 2022

Copy link
Copy Markdown
Contributor

@aluzzardi There's no goal to make any actual architecture changes in this PR, it's the same fundamental architecture before+after. The only goal here is to just swap out what used to be buildkitd and replace it with our own wrapper that is otherwise the same besides the fact that we control+build it. That's it though. I agree we have two daemons and that having two daemon is weird, but that's the exact same situation as before this PR. It's just that before the two daemons were dagger and buildkitd and now it's dagger and daggerd.

Making this swap is the pre-req for any possible changes we may want to make in the future, which is why we're starting here. I completely agree we need to sort out all the weird overlapping commands, combine various servers, etc. It's just far too much to swap buildkitd with a wrapper (this PR) and also change a bunch of other things.

I also agree though that the naming of dagger and daggerd makes the pre-existing situation of having multiple daemons more obvious in its confusion. I'd be fine with renaming daggerd to be dagger-buildkitd and change the commands from dagger serve -> dagger buildkitd and dagger stdio -> dagger buildkitd-stdio if that helps in the short term before we start doing actual rearchitecting.


For what we want to do in terms of next steps, it needs to be designed+discussed still but the general idea in my head has been that there's only one "daemon" component which includes both the router+buildkitd functionality. Let's say that's called dagger serve.

SDKs would continue to work the same as today in that they rely on invoking the dagger serve command to run (except Go which is a special case and can optionally just import this as a library). This is when the "provisioner" functionality kicks in. If dagger serve is unable to actually perform builds in its current environment (i.e. it lacks privilege, it needs to build for Linux but is on macos, etc.) then it will do something to provision itself somewhere it can perform builds. After that it will serve as a proxy more or less (in addition to handling local dirs).

Our current "docker run" implementation is essentially a very primitive version of this much more general concept.

The "provisioner" part will have reasonable defaults but also be configurable by users too. So, for instance, it will often be possible to use the "docker run" provisioner and the "fork/exec + ask for sudo" provisioner. We'll need to choose a default on behalf of the user (probably a list in order of preference) but it should be possible for users to override that choice too.

I think something like this will give us the same base functionality as we have today but in a much more flexible+generic way. It also avoids the need to do things like "rewrite support for buildkit's localdir syncing in every language".

There's no clear answer here of course and infinite variations on the above. But again this PR in particular is just the step needed to unlock all the other future possibilities.

Comment thread .gitignore Resolved
@sipsma

sipsma commented Oct 6, 2022

Copy link
Copy Markdown
Contributor

@grouville I gave it a try locally, looks great! I tried combining the Dockerfiles too and was able to get it to work with this commit: sipsma@22ebec4

The changes involving Stdout/Stderr are unrelated. The changes to the Dockerfile are also rough (should be less duplication between stages), but I think it should be possible unless I'm missing something.

grouville added a commit to grouville/dagger that referenced this pull request Oct 7, 2022
Following's dagger#3187 (comment) and to avoid confusion in the naming of CLI subcommands, all the references of daggerd in the context of dagger-buildkitd have been changed

Signed-off-by: Guillaume de Rouville <[email protected]>
@grouville

grouville commented Oct 7, 2022 •

Copy link
Copy Markdown
Member Author

@grouville I gave it a try locally, looks great! I tried combining the Dockerfiles too and was able to get it to work with this commit: sipsma@22ebec4

The changes involving Stdout/Stderr are unrelated. The changes to the Dockerfile are also rough (should be less duplication between stages), but I think it should be possible unless I'm missing something.

Thanks. All of above is now fixed.

FYI, to follow your naming convention, I had to import the docker connhelper package. Adding the podman one also won't be that hard

I did not rebase with latest cloak yet, will do on Monday.

Regarding the StartGoModDaggerBuildkitd, after a lot of thought/experimentation, the easier would be to rely on an env var generating a random number: $RANDOM, and fail if this env var is not set (when running go tests).

In our new docs regarding tests, we would show an example for each major shells/distributions.

So, instead of doing go tests ..., it would become DAGGER_SALT=$RANDOM go tests .... This would help generating new container images everytime people run go tests with few collision probabilities. This has the best tradeoffs, as it could also be easily sustained in the future dogfooding of dagger

@grouville

Copy link
Copy Markdown
Member Author

Had to squash all the commits, for easier rebase

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

Really minor things at this point, @aluzzardi please take a look if you'd like, otherwise I think this is pretty much mergeable!

Comment thread internal/buildkitd/buildkitd.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 we keep this, would need to update this and all the other cloak occurrences to dagger. But in reference to your other comment:

Regarding the StartGoModDaggerBuildkitd, after a lot of thought/experimentation, the easier would be to rely on an env var generating a random number: $RANDOM, and fail if this env var is not set (when running go tests).
In our new docs regarding tests, we would show an example for each major shells/distributions.
So, instead of doing go tests ..., it would become DAGGER_SALT=$RANDOM go tests .... This would help generating new container images everytime people run go tests with few collision probabilities. This has the best tradeoffs, as it could also be easily sustained in the future dogfooding of dagger

I agree something along these lines might be a better approach. What about a variation on that idea that allows callers to continue just using go test as before:

  1. When the tests start, generate a single global random ID in go code (probably in the init func in suite_test.go)
  2. Use that ID to identify buildkitd (and thus replace it every time the tests run)

Like you were getting at, this is probably a better approach in general since it reduces the chance of tests being impacted by cached results of previous runs+tests.

If possible, let's try to do that before merging (just realized the tests currently don't run for me locally from this branch due to the fact that I set dagger to be a wrapper script). But if it's a ton of effort then it can be a follow up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I implemented something as above, however, there is still one downside: go test ./... seems to build a dagger binary for each subpackage's test, and run all those tests in parallel. Meaning that the init() function doesn't share the same state between test runs, builds dagger images and run them in parallel --> leads to unwanted stop of the container

Given this constraint, queries tests need to be excluded from this recursive/parallel invocation (for now):
Until we dogfood and run those tests in a controlled environment, this is the least constraining solution. However, the env var with the random key still looks like the simplest implementation: generate a random key (exterior to dagger's execution). This random key will only live during the execution of all go tests: SALT=$RANDOM go tests ./.... We have a shared token state between all runs, and it's easy to implement. In multiplatform though, it might be trickier

Comment thread internal/stdio/dial.go Resolved
Comment thread internal/stdio/dial.go Resolved
@grouville
grouville force-pushed the bundling_buildkit branch 3 times, most recently from 47febe4 to e9297ea Compare October 12, 2022 14:50
@aluzzardi
aluzzardi changed the base branch from cloak to main October 14, 2022 18:26
@grouville
grouville force-pushed the bundling_buildkit branch 2 times, most recently from d681b92 to 8b31b7a Compare October 18, 2022 23:35
First baby-steps towards the buildkit bundling:
1. Integrate Buildkit's main function. It currently builds only on Linux (GOOS=linux)
2. Integrate it into Dagger's CLI

fix: update NewDockerAuthProvider according to Buildkit's new API

CI always starts a new build environment. To run the go tests in CI, we do not need to be sure that the version of daggerd is different from  previous one.
It will always build a new one between PRs

fix: add comment, implement DaggerdState, fix host bug when container not set

- Comment vcs.revision logic for the compilation of daggerd
- Regroup GetBuildkitInformation and GetHost under DaggerdState
- Remove last buidlkit naming references
- Fix host return value bug, from DaggerdState, when container not set yet

test: Add Integration tests to CheckDaggerd

- Protect core/integration tests by buildtags to avoid race with internal/buildkitd test
- Add a step to integration test in Github CI, with dedicated tag
- Refactor revision function in its own package
- Integration tests of CheckDaggerd

Signed-off-by: Guillaume de Rouville <[email protected]>
Co-authored-by: Tanguy ⧓ Herrmann <[email protected]>

Bump all source code to official buildkit v0.10.4 release
Comment added to `engine/engine.go`, for a fix to be implemented when bumping to buildkit's next release

- create  dagger buildkitd subcommand
- create dagger buildkitd-stdio
- Add dockercontainer package to not rely on `buildctl serve` but `dagger buildkitd-stdio`

chore: remove buildkit progress debug

chore: rename cloak invocation to dagger

Rebase cloak to dagger CLI

Signed-off-by: Guillaume de Rouville <[email protected]>

feat(goModDaggerBuildkitd): change go test version logic

- Change version check of `dagger-buildkitd` to rely on `identity.NewID()`. Not perfect, as `go test ./...` generates go binaries for each test / package and runs them in parallel. `queries` test suite might conflict with some others.
- Change Action() to ProxyIO()
- Move ProxyIO addr as const

chore(rebase): update embeding + rebase StartGoModDaggerBuildkitd

- update embeding to include new pkg
- use StartGoModDaggerBuildkitd and SetupDaggerBuildkitd

Signed-off-by: Guillaume de Rouville <[email protected]>
- print errors on stderr
- rely on git to retrieve latest dagger version

Signed-off-by: Guillaume de Rouville <[email protected]>
@grouville

Copy link
Copy Markdown
Member Author

Closing PR, as #3058 got closed and a new architecture is yet to be defined

@grouville grouville closed this Oct 20, 2022
@gerhard gerhard mentioned this pull request Aug 7, 2023
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.

4 participants