Skip to content

Basic namespacing in API #3345

Closed as not planned
Closed as not planned

Description

@shykes

Overview

In the new cloak core API, cache volumes are associated with a key provided by the client. Buildkit does not provide a facility for namespacing these keys, so by default every client that looks up the cache volume foo on the same engine, will use the same cache volume. This is not practical, and will probably lead to ad-hoc namespacing solutions. It would be better for Dagger itself to handle namespacing. But how?

As explained by @vito (see original discussion below), the answer is probably namespacing by something roughly equivalent to a "project". This issue is to discuss whether this feature is actually needed, and if so how to design it.

Original discussion

As discussed in #3287 (comment)_ :

I agree that we'll need scoping really soon if not on day 1. I wonder if it's closer to "project" than "user" though. I may be using the same machine/user to work on multiple projects that all use Dagger and it'd be awkward if they collided with one another.

You are right. I see two possible paths here:

  1. We introduce the smallest possible concept of namespace, just enough for have basic scoping, and improve later.
  2. We defer namespacing entirely. We take the leap of faith that we can introduce it later and not break the UX (similar to extensions).

I'm leaning towards option 1. Mostly because it seems really hard to ship a UX with global scoping, then change it to support namespacing that can support something like a project. I could be wrong though.

Note: I think @vito 's use of "project" is roughly equivalent to @sipsma 's use of "session" in this discussion, is this accurate?

In some cases you might want to share caches across projects though, for example it should be safe to share one big Go build/module cache.

This part I'm OK with deferring to later. I think if we have a good project scoping primitive, we can find a way to add this feature later.

Also one thing worth clarifying: we're aiming to prevent cache collisions just from a usability perspective, not a security perspective, right? So it should be OK to let the user provide the scope key. Multiple untrusted tenants on the same Buildkit is never a safe thing to do anyway.

IMO we need a UX primitive that works in a single-tenant and multi-tenant context. Totally agree that we don't want to actually make a buildkit instance multi-tenant (at least not with untrusted tenants) but we might want to implement multi-tenancy at a higher level (with cloud etc). So if the UX has a hook for that, it would be ideal.

Activity

  1. added this to the 0.3.0-alpha.1 milestone on Oct 12, 2022
  2. changed the title [-]Basic project management[/-] [+]Basic project scoping[/+] on Oct 12, 2022
  3. sipsma commented on Oct 12, 2022

    @sipsma
    Contributor

    "Session" and the use of "project" here are related but not entirely the same.

    Session is an already existing concept today. It's how buildkit scopes certain resources (localdirs, gateway containers, among others). Sessions have a random ID (they also have a name you can set, but that's different than the ID) and are tied to a single client connection. Once that connection is closed, the session is gone.

    • Confusingly some resources associated with it are freed (gateway containers) but others have more complicated stories (local dirs)

    Project as it's used here (and as I understand it, correct me if wrong) would be more persistent than a session because it would be scoping cache dirs. If you use a cache dir, disconnect and then reconnect, it should be possible for the cache dir to still exist (as contrasted with e.g. gateway containers where they are gone when the session ends).

    The direction I've been leaning more and more towards for a while is that the session concept we get from Buildkit is really hard to work with and often not what we want. What we really want is more like the "project" concept above where resources are less ephemeral. You should be able to let a service keep running even if you disconnect.

    • I actually asked Buildkit maintainers about whether they'd want persistent gateway containers specifically a few months ago, they said no.

    The vague, incomplete idea that's been knocking around my head is that we maybe should invent our own "dagger session" concept which encompasses not only services and local dirs (like Buildkit sessions) but probably also the loaded schema. Can add cache dirs to that too. Dagger sessions would have some degree of persistence.

    If we wanted to call "dagger session" a project instead that'd make sense to me in isolation, we just need to sort of the conflicts with the existing concept called project. The two concepts have overlap in that they may both be associated with a schema, but in other ways they are not really the same. I'd actually be in favor of calling the "persistent-session thing" a "project" and the "config for an extension" (i.e. what's currently called "project") something else.

    Hard parts:

    1. Garbage collection - does project state live forever? Get pruned after some time? etc.
    2. Sharing between projects - what if a service should be accessible in multiple projects? Or a cache volume? Or a synced dir? etc.
      • Support for tiered namespacing is a possibility, but complicated
  4. shykes commented on Oct 12, 2022

    @shykes
    ContributorAuthor

    In order to avoid confusion with existing definitions of "session" and "project", for the purpose of this discussion I propose using the more neutral and functional "namespace". Analogy is eg. to kubernetes namespaces, containerd namespaces, and perhaps docker context?

    Doesn't have to be the actual name we use, it just seems useful to have our own name for now, to avoid interference.

    EDIT: add comparison to containerd namespace

  5. changed the title [-]Basic project scoping[/-] [+]Basic namespacing in API[/+] on Oct 12, 2022
  6. sipsma commented on Oct 12, 2022

    @sipsma
    Contributor

    kubernetes namespaces

    containerd has namespaces too, which could be especially relevant to us if we decide to use the containerd worker

  7. added
    area/engine
    and removed on Dec 5, 2022
  8. shykes commented on Jul 7, 2023

    @shykes
    ContributorAuthor

    I see two possible paths here:

    1. We introduce the smallest possible concept of namespace, just enough for have basic scoping, and improve later.
    2. We defer namespacing entirely. We take the leap of faith that we can introduce it later and not break the UX (similar to extensions).

    Update: we have gone with option 2 (defer namespace entirely). Now we are facing an urgent need to introduce namespacing, in order to implement persistence of cache volumes in Dagger Cloud.

    I am resurrecting this thread to make sure we avoid breaking the UX.

    cc @sipsma @vito @marcosnils

  9. 8 remaining items

  10. shykes commented on Jul 10, 2023

    @shykes
    ContributorAuthor
    • Cache volumes can be defined in a Zenith env and vendored out to users in your org from there

    I didn't understand this part, what does "vendored out to users in your org" mean?

  11. modified the milestones: 0.3.0-alpha.1, on Jul 10, 2023
  12. sipsma commented on Jul 10, 2023

    @sipsma
    Contributor

    I didn't understand this part, what does "vendored out to users in your org" mean?

    I'm imagining a top-hat for an org creating their org's standalone environment and including CacheVolume types as part of that environment's API. The low-level plumbing for this would be easy to support, but this doesn't clearly fit into any of the existing entrypoint types (commands, artifacts, shells, etc.), so I suppose we could consider adding a Caches entrypoint type?

    The DX would be that when users in that org need to use a cache volume, they just import their org's environment and then can choose cache volumes from it. So if top-hat wants their support pip_cache and gradle_cache, the user would use them in their pipelines either like:

    1. Type-unsafe version: client.Environment().Load("my-org").Cache("pip_cache")
    2. Codegen version: client.MyOrg().PipCache()

    Either one of those above would be returning a CacheVolume type, so it would just plug into the rest of the APIs seamlessly. You could also imagine APIs for querying what caches exist, etc.

    The overall idea is that Zenith gives orgs a way of cataloguing cache volumes for end-users, which solves the problem of "when I write code describing my pipeline logic (which includes creating and naming my cache volumes). I shouldn't have to know everything else that runs, or will run in the future, in the same Dagger Cloud org as my pipeline." You know what to use because your org's environment tells you what is available to use.

    There's actually a bunch more interesting features we could layer on top of something like this, but I'll try to not derail the conversation too much yet :-)

  13. shykes commented on Jul 11, 2023

    @shykes
    ContributorAuthor
    1. Type-unsafe version: client.Environment().Load("my-org").Cache("pip_cache")
    2. Codegen version: client.MyOrg().PipCache()

    Either one of those above would be returning a CacheVolume type, so it would just plug into the rest of the APIs seamlessly. You could also imagine APIs for querying what caches exist, etc.

    There's actually a bunch more interesting features we could layer on top of something like this, but I'll try to not derail the conversation too much yet :-)

    OK that's pretty cool - perhaps even mindblowing ;) This gives me a glimpse of a deeper integration with Dagger Cloud, where devs can take advantage of its features programmatically. Which I love.

    BUT it also scares me a little bit, wouldn't this cause cloud-specific, and org-specific details to leak all over the place into otherwise perfectly portable code?

    I feel like we could have the best of both worlds, if we could make this similar to the dynamic secrets API: 1) fully programmatic; but also 2) traces a path to keeping the code portable in the future, with "secret providers".

    I feel like your idea for cache volumes isn't quite there, but perhaps it could be?

  14. removed this from the milestone on Jul 18, 2023
  15. sipsma commented on Jul 19, 2023

    @sipsma
    Contributor

    @shykes Sorry I just remembered this whole thread and realized I never got back

    BUT it also scares me a little bit, wouldn't this cause cloud-specific, and org-specific details to leak all over the place into otherwise perfectly portable code?

    I'd argue that "cloud-specific, and org-specific details" are not something to be avoided. We absolutely 100% want portable, re-usable environments, but ultimately end-users need to take those environments and use them to their precise needs, and it's great if that's just code too. I guess the analogies would be:

    1. There's lots of re-usable/generic libraries out there for every language, but you still ultimately have to actually import and use those for your specific use case. All of that, both the libraries and the end users code, get committed/published somewhere
    2. There's plenty of shared utils and abstractions in various IaC frameworks, but it's still beneficial to actually publish the code for your specific infra somewhere too that uses those utils.

    I feel like we could have the best of both worlds, if we could make this similar to the dynamic secrets API: 1) fully programmatic; but also 2) traces a path to keeping the code portable in the future, with "secret providers".

    I can't totally imagine what this would look like in a way that's different than just having the ability to import a cache mount definition that's defined in an external environment, might need clarification on what you are imagining for something like this.

  16. mircubed commented on Sep 12, 2023

    @mircubed
    Contributor

    @marcosnils @aluzzardi can you provide additional context on the current status of this?

  17. sagikazarmark commented on Dec 19, 2023

    @sagikazarmark
    Contributor

    This is what I ran into recently:

    I have a project with two Go binary builds and an e2e test run (not to mention a golangci-lint module that uses the same Go module under the hood).
    The binary builds run in one GHA workflow, the e2e test in another (although I don't think that really matters).

    All three steps use the same Go module that define the following cache volumes:

            WithMountedCache("/root/.cache/go-build", dag.CacheVolume("go-build")).
            WithMountedCache("/go/pkg/mod", dag.CacheVolume("go-mod"))

    That results in the behavior outlined above: essentially the last one to complete wins and overwrites the cache of the other two.

    In this case each three step would need their own "namespace" to work.

    I also wonder if the namespace should include other information, like branch or commit hash (similarly to how caching works on GHA and other SaaS CI providers). Let's say I have two PRs running the same builds in parallel. They would also overwrite the cache.

    One potential solution I can imagine is initializing a new module with some context accessible from the module:

    // When calling the module
    dag.Go(/*add some context here*/).FromVersion("1.21.5)
    
    // In module
    dag.Context()

    I considered adding a cache key argument to my Go module (I may still do it), but considering the rather complex API (partly due to the method chaining) it wouldn't be trivial. (For reference: here is the module)

  18. shykes commented on Dec 19, 2023

    @shykes
    ContributorAuthor

    @sagikazarmark in your case I think you are running into 3 distinct (but related) consequences of lack of namespacing :

    1. Global Go cache. All your invocations of go share the same Go cache because they use the same cache volume name. This is actually OK (I think) because Go supports sharing the same cache across projects.

    2. Concurrent access. In the default sharing configuration, cache volumes allow concurrent writers. So if you invoke go multiple times concurrently, they will hit the same cache volume at the same time. This is also OK (I think) because Go also supports concurrent writers to its global cache.

    3. Last run wins. You are seeing some runs overwrite the result of other runs. I think this is an artifact of Dagger Cloud’s distributed caching implementation. At the moment cache volumes are shared between all engines connected to the same Dagger Cloud organization. The Dagger Cloud organization is a global namespace for cache volumes. Synchronization is done by uploading a snapshot of the entire volume. The last upload wins. There is no merge mechanism at the moment.

    In summary: the core namespacing problem is not specific to Dagger Cloud, but it can manifest itself in more visible ways when using Dagger Cloud with cache volumes, and that is what you are encountering.

    If I’m right, then grouping all three steps in the same Dagger session (ie. in the same CI job) should solve the “last writer wins” problem.

  19. sagikazarmark commented on Dec 19, 2023

    @sagikazarmark
    Contributor

    @shykes makes sense, I'll try that, thanks.

    TBH, I'm not that comfortable with bundling all CI steps into a single run though.

    For one, I don't get the same feedback on GH right now with checks if I do that.

    Also, in some projects it's just not possible to scale the runner vertically (eg. OSS projects), so splitting CI runs is often for performance reasons (I don't have the numbers to back that up though...yet).

  20. shykes commented on Dec 19, 2023

    @shykes
    ContributorAuthor

    @shykes makes sense, I'll try that, thanks.

    TBH, I'm not that comfortable with bundling all CI steps into a single run though.

    For one, I don't get the same feedback on GH right now with checks if I do that.

    Also, in some projects it's just not possible to scale the runner vertically (eg. OSS projects), so splitting CI runs is often for performance reasons (I don't have the numbers to back that up though...yet).

    I completely understand. To be clear I only suggested this as a stopgap to alleviate the immediate pain. I absolutely agree that you shouldn’t have to combine runs in order to get the cache volume sharing semantics you expect.

  21. shykes commented on Apr 26, 2024

    @shykes
    ContributorAuthor

    I am deprecating this issue in favor of #7211 , which is more up-to-date and more narrowly focused on cache volumes.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions