Skip to content

Simplify writing to host filesystem + export container to OCI archive - #3401

Closed
shykes wants to merge 2 commits into
dagger:mainfrom
shykes:api-tweaks
Closed

shykes wants to merge 2 commits into
dagger:mainfrom
shykes:api-tweaks

Conversation

@shykes

@shykes shykes commented Oct 15, 2022 •

Copy link
Copy Markdown
Contributor

This is a proposal for improving the Graphql API, following up on this discussion. It only modifies graphql schema files without consideration for implementation.

Write to host filesystem

  • Proposal for a simpler and more dev-friendly API
  • Also allow writing a file to the host filesystem

Export container to OCI archive

  • Short-term implementation built on simpler host filesystem API
  • Longer-term implementation to make things even simpler

* Short-term: `Container { export }`
* Long-term: `Container { archive }`

Signed-off-by: Solomon Hykes <[email protected]>
@shykes shykes changed the title Api tweaks and improvements Simplify writing to host filesystem + export container to OCI archive Oct 15, 2022
Comment thread core/schema/host.graphqls

"Write the contents of another directory to the directory"
write(contents: DirectoryID!, path: String): Boolean!
}

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.

Being restricted to a single workdir will mean I can't do a Dagger runtime for Bass. 😢 That's not a blocker for us obviously, but to me Bass is a good litmus test for our API flexibility since it targets the exact same problem space and I've only added capabilities to it as I've needed them. So to me this feels like a step backwards.

Just to be clear about the extent of my use case: for Bass I only actually need read access to multiple host dirs. I don't need to write to the host at all, but it's the only way to get data out of the API at the moment. I would much prefer a streaming tar export API.

We're pretty gleefully exposing all the magical powers of LLB everywhere else, but here we're sticking to a relic of docker build instead and I don't understand why. Are there downsides to supporting multiple host dirs I'm missing besides not being sure we'll need it? Because I'm pretty sure! 😆

There are times where I don't need a workdir at all, and there are times where I need to access multiple host dirs. From that perspective, having a required workdir is what feels like a trapdoor. If it were up to me I'd just remove it and have users explicitly configure the directories they want to access. It feels less confusing.

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.

I feel like I'm misunderstanding how the current API works... In my mind this proposal doesn't change what's possible at all, only the cosmetics of how to do it. So, it's definitely the intent that you can read and write to multiple host dirs.

If it were up to me I'd just remove it and have users explicitly configure the directories they want to access. It feels less confusing.

Are you talking about literally exposing the low-level buildkit API for localdirs, ie. "give me a map of abstract IDs and underlying paths at solve, and then you can reference those abstractIDs in the llb"?

My thinking is: we should allow our devs to use the power of this feature (ie "access arbitrary host directories"), but present the API in a different form, because I find that form really awkward. Happy to go into why, but first I want to make sure we're even talking about the same thing :)

@vito vito Oct 15, 2022 •

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.

I feel like I'm misunderstanding how the current API works... In my mind this proposal doesn't change what's possible at all, only the cosmetics of how to do it. So, it's definitely the intent that you can read and write to multiple host dirs.

Hmm I'll start with my understanding so we can build a bridge. Sorry for not starting with that. :) In previous discussions it sounded like we didn't want multi-dir read/write because it was a trapdoor decision, so I assumed that was the motivation.

Host is currently a singleton in the API, accessed via host {} with no params, and it reflects the engine's state + configuration. It exposes a workdir (from engine config) and a set of local dirs with user-provided IDs (also via engine config). The workdir is just a predefined local dir with whose ID is hidden from the user.

If we remove Host { directory } and only have the one workdir it'll only be possible to interact with one host directory at a time per engine session. This means I can't run a command that reads from two host dirs, or reads from one and writes to another, etc. (that's not possible either way)

Are you talking about literally exposing the low-level buildkit API for localdirs, ie. "give me a map of abstract IDs and underlying paths at solve, and then you can reference those abstractIDs in the llb"?

My thinking is: we should allow our devs to use the power of this feature (ie "access arbitrary host directories"), but present the API in a different form, because I find that form really awkward. Happy to go into why, but first I want to make sure we're even talking about the same thing :)

Totally agree that it's awkward, particularly the 'random abstract IDs' part. We only need it ahead of time because we call Build at engine start time, so we need LocalDirs to be in SolveOpts immediately. I wonder if that's really necessary. Maybe we can just use real paths and call Build with a dynamic LocalDirs? We could just re-use the path (or a hash of it) as the ID so we can still build LLB (e.g. DirectoryID) ahead of time before putting it in SolveOpts.

Another motivation for taking an explicit list of dirs to expose is to prevent unfettered access to the host filesystem, but in #3401 (comment) you suggest extensions wouldn't have that anyway.


To make things concrete, I think something like this would work for me:

type Host {
  directory(path: String!): Directory!
}

# writing is as you proposed, e.g.:

type Container {
  export(path: String!): Boolean!
}

@shykes shykes Oct 17, 2022 •

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.

In previous discussions it sounded like we didn't want multi-dir read/write because it was a trapdoor decision, so I assumed that was the motivation.

As far as I know that discussion has been resolved: @jlongtine pointed out that Classic requires access to the host filesystem for core.#Source and client: filesystem to work. We looked for a way to avoid crossing that one-way door, but couldn't find one. Therefore allowing host filesystem access in the GraphQL API is the only viable option.

That is the assumption in this proposal.

Maybe we can just use real paths and call Build with a dynamic LocalDirs? We could just re-use the path (or a hash of it) as the ID so we can still build LLB (e.g. DirectoryID) ahead of time before putting it in SolveOpts.

This is how Dagger 0.2 does it :) The path is the ID. My assumption is that we could, and should, do the same in 0.3.

To make things concrete, I think something like this would work for me:

type Host {
  directory(path: String!): Directory!
}

# writing is as you proposed, e.g.:

type Container {
  export(path: String!): Boolean!
}

Yes, that works for me. I actually almost added it, but felt removing the first-class concept of workdir in the API would draw bikeshedding and distract from the rest. Ultimately, the concept of workdir remains anyway, as long as we support relative paths. This is also what we do in 0.2. It's universally familiar and I think we should keep it.

One important detail in path handling: we should forbid relative paths from leaving the workdir, eg. .. should be forbidden. This costs us nothing and keeps our options open in terms of handling workdir behind the scenes: unlike in the unix convention, workdir can exist outside the rootfs. I don't know how and when this will be prove useful, but I bet the day will come when we are glad we did it :)

TLDR: I like your version, ship it!

@vito vito Oct 17, 2022 •

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.

Ultimately, the concept of workdir remains anyway, as long as we support relative paths. This is also what we do in 0.2. It's universally familiar and I think we should keep it.

👍 Makes sense to me as long as our interface for host paths is literal path strings. It'll always raise the question of what relative paths are relative to, and it's nice to have it mean something.

Followup question: if I know I don't need a workdir, I want to be able to skip uploading it, and maybe skip configuring one. If it's not configured and I pass a relative path maybe it could error?

I think we could skip uploading if we only configure LocalDirs for paths that are actually referenced, in the same dynamic way I mentioned before.

One important detail in path handling: we should forbid relative paths from leaving the workdir, eg. .. should be forbidden. This costs us nothing and keeps our options open in terms of handling workdir behind the scenes: unlike in the unix convention, workdir can exist outside the rootfs. I don't know how and when this will be prove useful, but I bet the day will come when we are glad we did it :)

I think I see what you're saying, but want to be sure: we want to forbid ../ to maintain a pure concept of a workdir decoupled from the host filesystem, not to prevent access to arbitrary host directories. So we can still allow absolute paths outside of the workdir? That's my ultimate goal since my whole issue that is my starting point is always going to be specific paths, without the concept of workdir to restrict them to.

TLDR: I like your version, ship it!

Starting on it now just in case it turns into a whole big refactor. 😄 There might be a very good reason we establish a Buildkit session so early. Most of the other core API work is done/scoped out so I'd like to have an answer to that even though we haven't 100% reached consensus here.

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.

Followup question: if I know I don't need a workdir, I want to be able to skip uploading it, and maybe skip configuring one. If it's not configured and I pass a relative path maybe it could error?

I think we could skip uploading if we only configure LocalDirs for paths that are actually referenced, in the same dynamic way I mentioned before.

Yeah I think so.

I don't know for sure what the limitations are here. Ideally A) there would be no scenario where referencing a valid DirectoryID would result in a weird error, and B) host files that aren't needed aren't uploaded.

If we have to choose between A and B, I expect that would lead to a follow-up design discussion.

I think I see what you're saying, but want to be sure: we want to forbid ../ to maintain a pure concept of a workdir decoupled from the host filesystem, not to prevent access to arbitrary host directories. So we can still allow absolute paths outside of the workdir? That's my ultimate goal since my whole issue that is my starting point is always going to be specific paths, without the concept of workdir to restrict them to.

Yes, correct. We are on the same page.

Starting on it now just in case it turns into a whole big refactor. 😄 There might be a very good reason we establish a Buildkit session so early. Most of the other core API work is done/scoped out so I'd like to have an answer to that even though we haven't 100% reached consensus here.

Sounds good. Definitely make sure you're up to speed on how 0.2 handles dynamic localdirs. Whether we do it the same way, or differently, that seems like an important baseline to be aware of.

Comment thread core/schema/host.graphqls
"Access a directory on the host"
directory(id: HostDirectoryID!): HostDirectory!
"The root filesystem on the host"
fs: Directory!

@vito vito Oct 15, 2022 •

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.

This feels like a major security risk; I'm not sure I'd trust any extensions at this point. Also wouldn't it mean uploading their entire hard drive?

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.

Extensions would definitely not have access to the real host... Each extension would have its own sandboxed "host" with its own content and nothing else. Either that, or privileged host access would simply return an error when an extension calls it. Otherwise, yeah, 100% agree it would be a major problem. It's very important that extensions can be safely shared and reused.

Comment thread core/schema/host.graphqls
@@ -6,32 +6,19 @@ extend type Query {
"Information about the host execution environment"
type Host {

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.

Side question: how will Host even work when we have a long-lived remote API server? Per option 2 in #3347 it'll live in the OCI image, which might even be in a separate VM/machine, up to two "hosts" away from the client. With multiple clients, what does workdir even mean? How are client-side env vars accessed through the API?

Do we need to get started on Session or something that holds exposed env/secrets + filesync'd dirs?

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.

Side question: how will Host even work when we have a long-lived remote API server? Per option 2 in #3347 it'll live in the OCI image, which might even be in a separate VM/machine, up to two "hosts" away from the client.

I assumed the plan was to implement it on top of llb.Local. Once the engine is fully remote, that would require exposing some of the underlying buildkit API (or something equivalent) to support filestreaming etc.

With multiple clients, what does workdir even mean?

Same as "context" in docker build. Each client has its own workdir, and streams its content. Under the hood it's all llb.Local.

How are client-side env vars accessed through the API?

I'm guessing some sort of callback? Once engine is remote, I guess we could piggyback on secret callbacks?

Note: I think this point is orthogonal to my proposal, as it applies equally to the current API no?

Do we need to get started on Session or something that holds exposed env/secrets + filesync'd dirs?

I think a regular buildkit session handles it just fine, but I might be missing something.

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.

Note: I think this point is orthogonal to my proposal, as it applies equally to the current API no?

Agree, I wanted to raise it because it started to feel like we were rearranging deck chairs on the Titanic. 😄

I think a regular buildkit session handles it just fine, but I might be missing something.

I think what's throwing me off is there's currently only one session and it wraps the entire engine API. I guess we'll be changing that?

dagger/engine/engine.go

Lines 110 to 118 in 76e4482

_, err = c.Build(ctx, solveOpts, "", func(ctx context.Context, gw bkgw.Client) (*bkgw.Result, error) {
// Secret store is a circular dependency, since it needs to resolve
// SecretIDs using the gateway, we don't have a gateway until we call
// Build, which needs SolveOpts, which needs to contain the secret store.
//
// Thankfully we can just yeet the gateway into the store.
secretStore.SetGateway(gw)
coreAPI, err := schema.New(schema.InitializeArgs{

Related to my previous comment. Maybe I'm just getting caught up. :)

@shykes

shykes commented Oct 15, 2022

Copy link
Copy Markdown
Contributor Author

@vito do you like the writing part though? :)

@vito vito mentioned this pull request Oct 18, 2022
1 of 2 tasks
@shykes

shykes commented Oct 20, 2022

Copy link
Copy Markdown
Contributor Author

I believe this PR has served its purpose as a conversation starter, closing in favor of @vito 's actual implementation work.

@shykes shykes closed this Oct 20, 2022
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.

2 participants