Skip to content

[Draft Design] Prototype cue-based cache config - #2519

Closed
sipsma wants to merge 1 commit into
dagger:mainfrom
sipsma:config-cache
Closed

sipsma wants to merge 1 commit into
dagger:mainfrom
sipsma:config-cache

Conversation

@sipsma

@sipsma sipsma commented May 26, 2022

Copy link
Copy Markdown
Contributor

This is a very rough prototype of the updates I'm proposing to enable better ways of configuring cache. I'm opening this to get feedback on the design; the code here is just serving as a proof-of-concept to give everyone a rough idea of what this could look like.


Problems Being Solved

This is mentioned in the Seamless Configuration section of the CI caching epic, but to summarize the problems being solved here:

  1. Configuring cache today involves constructing complex command line arguments to dagger do. See our CI for example where we setup a complex system of imports/exports based on whether the run is for a pull request or a push to main.
    • There's no way to create abstractions over these patterns or to make them re-usable (users will just have to figure them out on their own for each CI system+cache-backend or copy-paste from each other)
  2. There's no way to provide credentials for registry cache import/export. While we could bolt on this ability, it would not integrate with our existing system for providing secrets (similar to how credentials are provided for image push/pull today)

Proposed Solution

  1. Cache can now be configured using cue, specifically by setting a cache field under dagger.#Plan (sibling to client and actions).
  2. This allows for creation of high-level, reusable configuration patterns. For instance, the complex imports/exports based on the Github Action event type that we use in our CI can be abstracted into a cue definition that accepts simple, high-level inputs and sets up the cache configuration for them
    • This has been roughly implemented as the dagger.#GHAConfig definition in the code here, but note that even the interface there has more inputs than necessary. The final cleaned-up interface can likely just accept a single input (the whole github event object), from which all the existing fields are parsed out of.
  3. Though not put in the prototype yet as only GHA configs have been tested, it will now be possible to set registry credentials with #Secret now too. This means we have the flexibility to provide creds from anything: env vars, files, universe packages like the new 1password one, etc.
  4. The existing ability to pass "ambient" cache configuration through environment variables is still possible thanks to the fact that we can configure cache to be read through client.env. I added a rough #StringConfig definition to assist this, which parses cache configuration from a string that matches the format we used for env var based config before this.

Implementation Details

  1. Integrating this with our cue/flow setup is a bit tricky. The cache configuration needs to be evaluated before any actions (as we have to provide it at the beginning of the buildkit session), but it may still require fields from client or even other imported packages (i.e. to get credentials)
    • I tried a bunch of approaches here, but ended up with the simple/dumb approach of just evaluating the cache field in the same way the action is, just first. This is fine for the prototype, but has some issues in that it can mean that client fields get evaluated twice if referenced in both cache and the action and a few other related side effects like that. I think we can address those by either adding some de-duplication ability or maybe there's a different approach that will avoid them.
  2. The deleted GH workflows in the PR diff are just to make my test runs in CI faster; they will all work with this design too. I also focused the bats run to just a single integ test for the same reason.

Alternatives

Some other possible solutions instead of this one:

  1. Leave cache configuration as is; just bolt on ability to provide registry credentials through env vars
    • We technically don't have to solve this at all; I just think it makes caching features much more usable across all the different CIs+cache-backends we want to support in addition to being a great foundation for plugging in more caching-related features in the future. New and improved caching features can be exposed through cue packages rather than updating command line arg configs. This is just an opinion though.
  2. Leave as is but solve through more advanced, long-term features
    • There are some ideas about creating pluggable cache backends that just automatically configure cache such that the end user never has to think about it or configure anything.
    • While I think that is a great very long-term goal, it is quite a ways away. It also may end up needing some sort of minor configuration anyways, in which case we still may benefit from having a flexible configuration interface (even if it becomes less important).

@sipsma sipsma mentioned this pull request May 26, 2022
5 of 14 tasks
@sipsma
sipsma requested review from TomChv and aluzzardi May 26, 2022 18:53

@TomChv TomChv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You did an amazing works, it's really awesome!
Some hot reaction now, I think I'll need multiple read to get all the content.

Cache can now be configured using cue, specifically by setting a cache field under dagger.#Plan (sibling to client and actions).

That's really good but how we will handle new buildkit option? By adding a new field in cue?
It will make our core cue API having recurrent change, cannot we find a way more maintainable?

For instance, Buildkit can now export cache to s3, how would you support it in dagger? By adding another definition?

Though not put in the prototype yet as only GHA configs have been tested, it will now be possible to set registry credentials with #Secret now too. This means we have the flexibility to provide creds from anything: env vars, files, universe packages like the new 1password one, etc

That's exactly why I think we would not need CLI options anymore, those capabilities is 100% better (and secured) that flags.

Leave cache configuration as is; just bolt on ability to provide registry credentials through env vars

It's not a bad solution but we do not use the full power of Buildkit cache with it. More than that, it makes mandatory to run a sidecar registry to have cache, which can certainly be a limitation for some users.

Leave as is but solve through more advanced, long-term features

I think we should let time do his job, your proposal is already an amazing improvement and as you said, this concept of cache configuration directly from plan will certainly be reused for long term features.

Comment thread cmd/dagger/cmd/common/common.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If our cue implementation is strong enough, I think it is no longer useful to keep those flag.
Cache management will be part of the plan as env input is now.

I think it's better to keep one source of truth (that one of the reason why we switched from dagger project with a values.yaml to dagger plan).

Comment thread cmd/dagger/cmd/do.go Outdated
Comment on lines 153 to 160

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Loading plan 2 times like this will not create logging issues and log duplication?
It's just a question, I'm not sure about this.

The cache configuration needs to be evaluated before any actions (as we have to provide it at the beginning of the buildkit session), but it may still require fields from client or even other imported packages (i.e. to get credentials)

It is not possible to evaluate both cache and client at the same time and let Buildkit/cue do the correct dependency arrangement of the DAG?

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.

Loading plan 2 times like this will not create logging issues and log duplication?

Yeah it does, that's one of the issues I mentioned in the description, I just accepted it for the proof-of-concept for now.

It is not possible to evaluate both cache and client at the same time and let Buildkit/cue do the correct dependency arrangement of the DAG?

Well that's what we do, we evaluate cache and client together first, then we evaluate the action and client together. What would be nice is if we could just evaluate all of them at the same time.

Part of the problem is that the action may not have an explicit reference to anything in cache, in which case cache will be ignored. I'm sure there's some cue magic I could do to overlay a forced dependency on cache or something to solve this.

The other part of the problem though is that we have to specify the cache settings when we start the buildkit session. The only clean fix I can think of here is to add upstream support for specifying cache imports/exports to the gateway api, but that's obviously something that will take a while. In the short-term I can't really think of any clean way of fixing this besides the approach here of constructing the session twice. I'll think about it more though and am open to suggestions of course.

Comment thread pkg/dagger.io/dagger/plan.cue Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You could put all those definition in a cache.cue file :D

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.

Agreed, put them here for convenience right now but there should be a separate package for this

Comment thread plan/task/cacheconfig.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is already done in ParseImportCache and ParseExportCache functions of buildkit with loadGitHubEnv so I'm not even sure you need this function.

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.

Those functions in buildctl have some behavior we don't want (like writing logs and including error messages that refer to buildctl flags), so I don't think we should use them. Either way, on revisiting this TODO I think we should probably deprecate this behavior just so we can keep everything in cue and thus simple.

Comment thread plan/task/cacheconfig.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it a typo or just to avoid conflict with type? You could name it exportType too?

Comment thread plan/task/cacheconfig.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That would be better to put case function into sub function for clarity

@TomChv TomChv added area/engine About dagger core engine area/core labels May 27, 2022
@sipsma

sipsma commented May 31, 2022

Copy link
Copy Markdown
Contributor Author

Thanks @TomChv !

For instance, Buildkit can now export cache to s3, how would you support it in dagger? By adding another definition?

Yep exactly, we can just add the new cache config type to the disjunctions in #CacheConfig and then create higher-level wrappers for configuring it. I think this makes it much easier to add the support and much easier for users to discover the new features.

That's exactly why I think we would not need CLI options anymore, those capabilities is 100% better (and secured) that flags.

Agreed, I wasn't sure where we stood on the backwards-compat concerns here. I'll see what others think too but agree that getting rid of those options results in the cleanest end state.

@aluzzardi

Copy link
Copy Markdown
Contributor

Haven't looked at the overall structure/design yet, but wanted to share a bit of info which I think will help:

Integrating this with our cue/flow setup is a bit tricky. The cache configuration needs to be evaluated before any actions (as we have to provide it at the beginning of the buildkit session), but it may still require fields from client or even other imported packages (i.e. to get credentials)

Loading plan 2 times like this will not create logging issues and log duplication?

We actually ran into this same exact issue for local directories and came up with a solution:

  • Tasks (e.g. CacheConfig) can implement a PreRun function. This will run BEFORE a buildkit session is running
  • Tasks can shove contextual information into plan context (which you're already doing)

So basically by renaming the Run function into PreRun, this should work pretty much out of the box. No need to call cl.Do twice, plan.Load will call all the PreRun hooks for you:

if err := r.PreRun(ctx, p.context, compiler.Wrap(t.Value())); err != nil {

That being said, while I'm typing this I realize this doesn't help with the second part of your statement, may still require fields from client or even other imported packages (i.e. to get credentials).

I believe @helderco ran into a similar issue with client: filesystem depending on client: env, not sure what the solution was for that one

@aluzzardi

Copy link
Copy Markdown
Contributor

Open question:

This effectively makes the cache configuration declarative from within the plan. If I look at ci.cue as an example though, this means it will NOT run on my laptop since it's now depending on GHA caching, correct?

Basically the open question is: Isn't the cache configuration environment dependent, and what happens when multiple environments share the same configuration?

@sipsma

sipsma commented Jun 1, 2022 •

Copy link
Copy Markdown
Contributor Author

This effectively makes the cache configuration declarative from within the plan. If I look at ci.cue as an example though, this means it will NOT run on my laptop since it's now depending on GHA caching, correct?

Basically the open question is: Isn't the cache configuration environment dependent, and what happens when multiple environments share the same configuration?

The draft here does hardcode the use of GHA in ci.cue but that was just for convenience while prototyping. In general, the final version of this should use optional fields and/or if statements to configure GHA if the relevant client.env vars are set and otherwise default to not using cache. Couple extra thoughts:

  • It's still possible to provide cache configuration directly through env vars; in plan.cue you'll see I added a #StringConfig type which accepts cache configuration strings in the same format we previously supported. You can then use client.env as an input to this (or anything else really, that's part of the flexibility this provides). This is helpful for our test setup where we want to pass the cache config to many individual bats tests.
  • We can also create abstractions in cue that implement something like "first check the DAGGER_CACHE_{FROM,TO} env, if those aren't set, check the GHA env vars, and finally default to not using any cache", or any variation on that. The fact that this is fully programmable now offers a lot more flexibility than users had before.

Overall, I guess the point is that when configuration is environment dependent, we can now use cue to control that behavior, which I think is preferable to the other method which is to have users write wrapper scripts (or GHA yaml) that implements this conditional behavior when invoking dagger. This is comparable to how the settings under client (like paths to read/write to/from) are often environment dependent, but it's still beneficial to be able to encode them in cue.

I will update this draft with the above conditional behavior implemented so we can get a better idea of what it will look like in practice.

That being said, while I'm typing this I realize this doesn't help with the second part of your statement, may still require fields from client or even other imported packages (i.e. to get credentials).

I believe @helderco ran into a similar issue with client: filesystem depending on client: env, not sure what the solution was for that one

Yeah I'm curious what we settled on there. As of right now, the only clean solution I can think of here is to add support to BuildKit's gateway API for specifying cache import/export settings as part of the individual solves. Otherwise I can't think of any way besides what the prototype here does. As long as performance isn't meaningfully compromised, then maybe this approach is fine for now? EDIT: to clarify, I meant "this approach with some additional fixes in place to not duplicate the execution of client tasks" (i.e. add some logic that ensures those tasks only run once, could be as simple as a sync.Once in the right places).

@helderco

helderco commented Jun 2, 2022 •

Copy link
Copy Markdown
Contributor

I believe @helderco ran into a similar issue with client: filesystem depending on client: env, not sure what the solution was for that one

In this case it was specifically client: filesystem: ...: read: contents: dagger.#FS (i.e., reading a directory). The quick solution was to just not support this. You can now read multiple times from the same path (with different include/export), but you can't use a dynamic path (i.e., from the output of an action).

Reading this felt like the same problem which is chicken/egg problem of needing the output of an action to bootstrap the buildkit session. Maybe the same solution can be used for both.

@sipsma

sipsma commented Jun 2, 2022

Copy link
Copy Markdown
Contributor Author

@aluzzardi Pushed an update that shows one example of how this can work across different environments. Now ci.cue works if you have no cache configuration, but also supports both GHA cache and any extra cache settings provided through env vars. This is just one example of many approaches that could be programmed.

Getting this to work required a lot of battling with CUE though, I was getting very strange errors about cycles where it did not seem like there could possibly be any cycles. Not sure yet, but I wonder if this is another side effect of evaluating the plan multiple times? Either way, I think my next step will be to investigate more ways to fix that whole issue cleaner. But let me know if the meantime if you think this addresses your question about environment-dependent config.

@sipsma

sipsma commented Jun 2, 2022

Copy link
Copy Markdown
Contributor Author

I think I figured out a way to cleanup the BuildKit part of the problems here:

  1. The gateway API already has support for specifying CacheImports in solves (not sure how I went this long without noticing that, but better late than never....). This means we can dynamically register imports after the client session has started (via the plan context)
  2. We can then implement cache exports the same way we implement image exports today, by just creating a separate solve session. This will happen once at the end of the build when there are cache exports specified.

The above means that we can manage the whole build with one instance of dagger's Client, which helps a lot.

The only remaining issues are the cue/flow aspects of the problem. We want the cache import tasks to be evaluated before the action; for the cache export tasks we only care about them being done eventually (it can be in parallel with the rest of the actions). The goal would be to achieve this all with a single plan evaluation so that this PR no longer needs to add an extra separate one and create associated problems.

Not totally sure how to achieve this quite yet, vague ideas:

  1. Somehow make it so every task under actions inherently has has a dependency on the cache.imports task. This wouldn't be user specified, it would just be kludge we add to our cue/flow code that somehow forces it
  2. Can overlays (that we generate in our go code) be used to create explicit dependencies between action tasks and the cache tasks?

Will continue investigating options there, but it feels like something must be possible, so there should be a route to getting this cleaned up.

@sipsma

sipsma commented Jun 4, 2022

Copy link
Copy Markdown
Contributor Author

I figured out a way to force action tasks to wait on cache import tasks before executing as described in my previous comment, but now having thought about it more I'm not sure this is the right approach:

  • It doesn't support cases where you want different cache configs for different actions in your plan (simplest example is wanting to disable it for certain actions)
  • The behavior in general is very "magical" (in a bad way). There's basically this huge set of dependencies implicitly created entirely outside of the actual CUE that gets evaluated. This type of magical behavior tends to become hard to understand and maintain over time, especially when it starts adding up with other magical behavior.

I'm wondering if what we really need here is the ability specify explicit dependencies. As described in my previous comment, I know now it's possible to basically dynamically register cache config from tasks and then have it take hold in the build. So I think what we really want here is the ability to say "run these cache config tasks first, then run this block of CUE", where "block of CUE" is whatever block the user wants the cache settings to apply to. Could be every action, could be one action, could be a subset of tasks within one action, etc.

I'm not sure of any way of accomplishing this without either explicit dependencies, "dynamic tasks", or very heavy refactorization of our CUE SDK (basically a v2), all of which are projects in and of themselves...

cc @aluzzardi Do you think I'm overthinking this and it would be okay to have the implicit behavior of forcing actions to depend on the cache field? Or is this a legitimate concern? I don't want to block this issue on other large ones because I think it makes a huge difference in terms of usability, but I also don't want to create one-way-doors and/or put in wasted effort if we end up making changes to how we use CUE anyways. Let me know if there's any other ideas on how to approach this.

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

@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions Bot closed this Jun 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/engine About dagger core engine kind/stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants