Skip to content

feat: Use client: filesystem path as a default - #2385

Merged
helderco merged 1 commit into
dagger:mainfrom
helderco:client-fs-race
May 12, 2022
Merged

helderco merged 1 commit into
dagger:mainfrom
helderco:client-fs-race

Conversation

@helderco

@helderco helderco commented May 2, 2022 •

Copy link
Copy Markdown
Contributor

Resolves #2196

Why

The ability to write multiple times to the same path has come up a few times. Reading as well. We ourselves have resorted to the hack of reading from . and ./ in the same plan (in CI), to allow different include/exclude patterns. Using core.#Copy to filter down a bigger dagger.#FS from the host isn't always appropriate because you'll load more than you need, depending on your target action.

Also, the fact that you can't use a dynamic path in client: filesystem has come up a lot too. Users want to use paths from environment variables for better portability and reusability.

What

Allow overriding client: filesystem path in the action's field, using the CUE path as a default.

How

Simply using the CUE path as a default, allowing to override explicitly. This is backwards compatible.

Added a file lock to avoid race conditions to allow multiple actions to the same path, which gives more protection than previous behavior (e.g., . vs ./).

For dynamic paths, reading directories aren't supported but everything else is. It also improves the error message if a user attempts this.

Reading multiple times from the same dir

client: filesystem: {
	goFiles: read: {
		path: "." // path override here. goFiles becomes a reference, not the path
		contents: dagger.#FS
		include: ["**/*.go"]
	}
	cueFiles: read: {
		path: "." // allows using the same path multiple times
		contents: dagger.#FS
		include: ["**/*.cue"]
	}
}

Writing multiple times to the same dir

// writes are protected by a lock
client: filesystem: {
	lintedGo: write: {
		path: "."
		contents: actions.lint.go.output
	}
	lintedCue: write: {
		path: "."
		contents: actions.lint.cue.output
	}
}

Reading a file from a dynamic path (output of an action)

client: {
	env: KUBECONFIG: string
	filesystem: kubeconfig: read: {
		path: env.KUBECONFIG // dynamic path; value not known during load
		contents: dagger.#Secret
	}
}

Related

Signed-off-by: Helder Correia

@helderco

helderco commented May 2, 2022 •

Copy link
Copy Markdown
Contributor Author

@aluzzardi can you review the locking part? Not sure how to test it. I tested manually with a loop. Thought about writing a big enough file in parallel and doing a checksum after.

@helderco

helderco commented May 2, 2022 •

Copy link
Copy Markdown
Contributor Author

@aluzzardi How about moving pctx.LocalDirs.Add(path) from PreRun to Run (clientfilesystemread.go)?

The reason is that with dynamic paths, you don't know the path at load time.

EDIT: I see that it doesn't work, what can we do here?

@aluzzardi

aluzzardi commented May 2, 2022 •

Copy link
Copy Markdown
Contributor

@aluzzardi How about moving pctx.LocalDirs.Add(path) from PreRun to Run (clientfilesystemread.go)?

Unfortunately, we can't. Buildkit requires the list of localdirs before starting the Build session. That's the only reason we have a PreRun to begin with

/cc @sipsma, maybe there's a workaround but I'm not aware of that

@sipsma

sipsma commented May 2, 2022

Copy link
Copy Markdown
Contributor

maybe there's a workaround but I'm not aware of that

Yeah you can't dynamically add more local dirs to the session after it has started. I guess this could in theory be added to BuildKit but we'd need to think about the security implications, how to make sure we aren't creating a footgun where a client can easily be tricked into providing arbitrary paths based on the output of possibly untrusted code, etc.

I don't have any good suggestions for immediate solutions other than what @aluzzardi suggested on Discord.

I will throw out an idea that only requires moderate upstream changes though: even though we can't add brand new dirs to a session, we can dynamically choose which paths we want to use under the local dirs after the session starts. The idea here is that we could have clients just put the list of dirs they are okay with exposing to the build as their local dirs (i.e. /home) and then as long as the env var references a path under that dir, it's allowed to use it.

The main thing stopping that from being a good experience today is that BuildKit local refs aren't lazy. They will just load the whole dir into the daemon rather than only loading files in as needed (i.e. you allow all of /dir but then KUBECONFIG only requests /dir/subdir/ and thus you only load /dir/subdir in). That would require upstream changes, though nothing too awful.

I do like this idea in that it retains a fair bit of client control over what they want to allow access too while still keeping the convenience of dynamic paths.

Comment thread plan/task/clientfilesystemread.go Resolved
@helderco helderco mentioned this pull request May 2, 2022
2 tasks done
@helderco

helderco commented May 3, 2022

Copy link
Copy Markdown
Contributor Author

Context from Discord:

The only workaround I see -- and there's a ton of implications -- would be to process ENV before starting buildkit
That requires tweaking the running architecture quite a bit. Basically, we'd run the CUE tree twice: once before buildkit session started and then once more after
This could work but I don't know how much effort it is
Basically, in PreRun we would do whatever we can do without buildkit
things like client.env would be computed in PreRun
but then we would need to keep track of a computed layer, merge it on top of source, then call Run again

Originally posted by @aluzzardi

@jlongtine

Copy link
Copy Markdown
Contributor

Hopefully some of the issues we're having will be fixed by newer versions of CUE (v0.4.4 or v0.5.X), and we could potentially use flow.Value() to run subsections of the task tree prior to initializing the buildkit client. I think that'd help solve this problem.

@helderco helderco changed the title POC: Allow overriding client: filesystem path in action Allow overriding client: filesystem path May 9, 2022
@helderco
helderco marked this pull request as ready for review May 9, 2022 21:37
@helderco
helderco requested review from a team and shykes as code owners May 9, 2022 21:37
@helderco
helderco requested a review from jlongtine May 9, 2022 21:37
@helderco

helderco commented May 9, 2022

Copy link
Copy Markdown
Contributor Author

Tested the lock manually with a loop. Not sure how to test the race condition without wasting resources.

@helderco
helderco requested a review from aluzzardi May 9, 2022 22:01
@helderco helderco changed the title Allow overriding client: filesystem path feat: Use client: filesystem path as a default May 9, 2022

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

Few minor comments.

/cc @shykes for API review

Comment thread plan/task/clientfilesystem.go Resolved
Comment thread plan/task/clientfilesystem.go Resolved
Comment thread plan/task/clientfilesystem.go Resolved
Comment thread plan/task/clientfilesystem.go Resolved
Comment thread plan/task/clientfilesystem.go Resolved
Comment thread plan/task/clientfilesystem.go Resolved
@TomChv TomChv added area/engine About dagger core engine area/core labels May 10, 2022

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

LGTM!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client API dynamic paths

6 participants