Repository navigation
Description
Continuing the discussion here: #3421 (comment)
This is all just my interpretation and summary of that discussion, @vito please correct me if I misrepresent anything.
Problem
Buildkit sessions are hard to work with as is. They result in certain state being sticky for the duration of a client connection. But once that connection is closed, you can't resume the session or otherwise reobtain that sticky state.
Examples of state associated with sessions:
- Resolution of container image tags to digest (i.e. mapping
:latestto an image SHA) - Resolution of git refs to commit SHAs
- Synchronization of HTTP sources
- Synchronization of local directories
- Blocking of cache reference pruning (i.e. anything in buildkit's cache that you have touched during your session won't be able to be pruned until the session has ended)
- Gateway containers (i.e. what we use to implement services)
- (Maybe) Temporary cache of "no-cache" vertices
Forcing all the above to be intricately tied to a single client connection is limiting to our architecture and makes it much harder to enable "seamless reproducibility" on behalf of our users.
For example, in the current state of things if a user submits a query that references the main branch of a git repo, they can only be guaranteed to be using a consistent mapping of main->commit sha for the duration of their client object due to the fact that the client object is one-to-one with buildkit sessions.
- This means that we are completely tied to always having client objects maintain a single continuous connection with a single remote engine and that connection must be one-to-one with a buildkit session in order to guarantee source consistency. This rules out a lot of possible architectures and other changes.
Possible Solutions
One possible solution to items 1 through 4 above (image, git, http and local sources) is to enable "source pinning" as a separate feature to replace session stickiness.
- There's also some overlap w/ 5 above. 6 is a different problem probably.
Two variations (among many more possibilities) on how this could work are:
A) Pin sources in returned IDs
When an ID of an object is resolved, all sources that use non-pinned references (i.e. latest, main, etc.) are replaced with pinned versions. So if you do
Container().From("alpine:latest").ID()The ID that is returned to you won't contain latest, it will contain the SHA that latest resolved to.
Same would be true for all the other source types (how this could work for local dirs is mentioned below).
Pros:
- Pretty easy to understand both for us and for users (also probably relatively easy to implement)
Cons:
- We need to think through cases where a single client could end up resolving the same reference to different pinned versions, could result in surprising behavior at times.
- Wanting to retain pins across different client instances becomes a matter of re-using IDs, so we'll be committing to making IDs re-usable in this case (I think, could be wrong). That itself has a lot of implications that need thinking through.
B) Support source pin policies attached to queries
Buildkit will likely soon have support for attaching policies that enable not only pinning but also more complex rules for where to obtain a source from: moby/buildkit#2943
Buildkit also already returns "BuildInfo" metadata from solves that can be used to determine which specific sources were resolved in the solve. Apparently support for SLSA provenance info is in the works too: moby/buildkit#3240
We could use these features in combination with one another to support highly configurable policies that would be capable of modeling both the current behavior where pins last the duration of a session but also quite a bit more.
e.g.
// Clients are now internally initialized with a pinning policy. By **default**, it's initially empty
// You can also optionally provide something like `WithSourcePolicy` to Connect
client, err := dagger.Connect()
// When you use the client, it sends the pinning policy alongside each request.
// Here's it's empty, so "alpine:latest" will be resolved to a SHA by the builder.
// That SHA gets returned to you w/ the query result.
// Client will then (by default) update its own pinning policy to now use that SHA
_, err := client.Container().From("alpine:latest").Exec(...).Stdout()
// So then future queries from the same client will use that new pinning policy
// e.g. this will tell buildkit to use the same SHA rather than resolving latest
_, err = client.Container().From("alpine:latest").Exec(...).Stdout()
// The above is just the default behavior for users who don't care and want the same degree
// of stability that sessions previously provided (sources are sticky for the duration of
// the `client` object).
//
// There'd also be interfaces on `client` for reading+modifying the pinning policy for users who care about that
// (either because they want to or their company forces them to, etc.).Pros:
- May be more aligned with the features buildkit is starting to provide, could make life easier for us in the long run.
- Source policies are detached from IDs, so we don't have to tie ourselves into making IDs reusable necessarily. More flexible in that respect.
Cons:
- Pretty complicated. If we can get the "default path" easy so users don't need to understand all of this right away, then maybe it's okay. But if every users needs to learn about all this to use Dagger then it's probably not a good path.
Pinning Local Dirs and HTTP Sources
Container image and Git sources are nice in that when you resolve a non-pinned reference to a SHA, you can still obtain the source from that SHA. I.e. if I resolve latest->sha:1234 I can pull the image using just sha:1234 later. Basically, they come from content addressable stores.
Local sources and HTTP sources are different in that arbitrary local filesystems and arbitrary http web servers are not content addressable stores. You can't sync a local dir, calculate its content checksum and then later ask the local filesystem for the contents based on that checksum (I think some filesystems have abilities similar to this, but it's far from universal).
I think this might be okay though. If we support pinning those resources based on their content checksum, that just means that buildkit needs to search its cache as the content addressable store. If it doesn't have it anymore (or never did), then an error is returned.
- Basically, if the user tries to reproduce something that can't be reproduced, error out.
- We'll need to think through how to make this a nice experience though not a frustrating one.
- This is where point 5 in the list of session state above becomes relevent. We may need to do things with time based containerd leases that buildkit uses internally. This is too much of a tangent for now, but needs more thought.
I'm just dumping this as is to start getting more thoughts. Next steps would be to write out in more detail specifically what this would look like in the context of the other changes we want to make in terms of the engine archictecture.
Activity
shykes commentedon Oct 31, 2022
Does this also include the problem of scoping cache volume names, or is that unrelated?
sipsma commentedon Oct 31, 2022
Good question, I think this isn't necessarily tied into that namespacing problem, but there might be potential for them to have overlapping or at least conceptually similar solutions.
Thinking out loud:
For example, if you submit a query that internally gets the ID of a cache volume, then maybe the ID returned to you can be namespaced somehow (very similar to what's described in Solution A above).
Or maybe there's a way of attaching namespacing information to queries that impact which cache volume is ultimately used when your query is executed (very similar to what's described in Solution B above).
So, probably something we need to design separately while keeping in mind the implementations could have meaningful overlap?
jlongtine commentedon Dec 16, 2022
@sipsma I'm guessing this change (moby/buildkit#3332) will be helpful for implementing source pinning? Looks like that dropped in the v0.11.0-rc2 buildkit release
sipsma commentedon Dec 16, 2022
@jlongtine
Yep! That's the follow up of the PR I linked in the description, glad to see it got in for v0.11. Still need to figure out whether+how we can integrate this w/ dagger, but it's pretty powerful and nice to have that flexibility when needed.
jlongtine commentedon Dec 16, 2022
That's awesome! I can see it coming in handy!