Repository navigation
container: Support WithDirectory, WithFile & WithNewFile - #3827
Conversation
548938c to
8699ca4
Compare
There was a problem hiding this comment.
For this and the other new APIs, I don't think this will account for mounts. The locatePath func in this file has the logic for finding the right mount
There was a problem hiding this comment.
You mean writing in the mount if it's a mounted directory?
There was a problem hiding this comment.
Yes, similar to the behavior we have for Directory and File where the path you specify takes mounts into account
There was a problem hiding this comment.
Not sure I see how that would work. Directory and File are read operations, here it's a write operation. How can we update the *Directory returned from locatePath? If a mount is returned what happens?
There was a problem hiding this comment.
In the current change we get the rootfs, add the directory to it and then return the container with that new rootfs set. To handle mounts, we determine which mount the path points to, add the directory to it, and then return the container with that new mount at its mount point.
From the LLB perspective, both the rootfs and any other normal (i.e. not cache/tmpfs) mounts are more or less the same, they are just states. So updating the rootfs isn't really much different than updating a mount at somewhere besides "/".
There was a problem hiding this comment.
To be clear, I think locatePath might need some minor refactoring to support this, but we need essentially the same logic as is present in there today for mapping a path -> llb state for mount at that path.
There was a problem hiding this comment.
Just pushed an update, updated only WithDirectory to give it a try.
I'm like 70% confident at most that I'm doing the right thing. There's so many paths involved: the workdir, the mount point, the relative path of the directory within the mount point, etc.
And the directory itself has a path.
For instance I tried to use container.WithMountedDirectory but that uses payload.Dir which is set by locatePath to be a relative path within the mount.
There was a problem hiding this comment.
I 100% agree this is all extremely confusing. For better or worse though, I think it's all just inherent to the problem rather than indicative of us doing something wrong. IME basically anything involving filepath calculations ends up being 100x more complicated than you'd ever think.
For the merge+diff stuff in buildkit (which also fell into complicated filepath logic) I just compensated by having enormous numbers of test cases. I think that's probably the best approach here too (and we have been doing that so far).
If there was a library that could do all of this for us, that'd be great, but I suspect the particulars of our use case are too specific (would be delighted to be wrong though).
Either way, commenting on the code with one possible bug I noticed.
There was a problem hiding this comment.
Not 100% sure but think there might be a case needing handling still. Either way should have a test case for this if it's not already in there (didn't see it on a quick glance)
mntDir := Directory().WithNewDirectory("/a/b/c")
ctr := Container().WithMountedDirectory("/mnt", mntDir)
newDir := Directory().WithNewDirectory("/foo")
ctr = ctr.WithDirectory("/mnt/a/b/foo", newDir)
// expected: "/mnt/a/b/foo"
// actual: "/mnt/foo" ??Basically, I think when you grab dir from locatePath you end up getting a dir whose root is / (with the /a/b hidden), so when you remount it you end up dropping the preceding /a/b.
Again, not at all sure this is the case, just worth double checking in a test case as it's what I noticed reading the code.
There was a problem hiding this comment.
// expected: "/mnt/a/b/foo"
I'm confused -- isn't this supposed to be /a/b/foo/foo given ctr.WithDirectory("/a/b/foo", Directory().WithNewDirectory("/foo"))?
There was a problem hiding this comment.
I'm confused -- isn't this supposed to be /a/b/foo/foo given ctr.WithDirectory("/a/b/foo", Directory().WithNewDirectory("/foo"))?
Sorry, I had a typo in my example (even trying to write out an example is confusing 😞), I fixed it now, I meant to put it at /mnt/a/b/foo, not /a/b/foo
There was a problem hiding this comment.
Updated (local code, not in this PR)
ctr.Exec("ls", "-al", "/mnt/a/b/foo") yields:
+total 12
+drwxr-xr-x 3 root root 4096 Nov 17 19:40 .
+drwxr-xr-x 1 root root 4096 Nov 17 19:40 ..
+drwxr-xr-x 2 root root 4096 Nov 17 01:49 foo
That's good, right?
There was a problem hiding this comment.
That's good, right?
Yeah that looks correct to me!
There was a problem hiding this comment.
The test is this (passes):
mnt := c.Directory().WithNewDirectory("/a/b/c")
ctr = c.Container().
From("alpine:3.16.2").
WithMountedDirectory("/mnt", mnt)
dir = c.Directory().
WithNewDirectory("/foo").
WithNewFile("/foo/some-file", dagger.DirectoryWithNewFileOpts{
Contents: "some-content",
})
ctr = ctr.WithDirectory("/mnt/a/b/foo", dir)
contents, err = ctr.Exec(dagger.ContainerExecOpts{
Args: []string{"cat", "/mnt/a/b/foo/foo/some-file"},
}).Stdout().Contents(ctx)
require.NoError(t, err)
require.Equal(t, "some-content", contents)I noticed that replacing WithNewFile("/foo/some-file" by WithNewFile("some-file" fails. I don't know if it's related to this PR or if relative Directory.WithNewFile are broken
There was a problem hiding this comment.
I noticed that replacing WithNewFile("/foo/some-file" by WithNewFile("some-file" fails. I don't know if it's related to this PR or if relative Directory.WithNewFile are broken
Confirmed unrelated. Opened #3903
There was a problem hiding this comment.
Updated PR. I honestly don't know why CI is passing, but it is.
There was a problem hiding this comment.
I 100% agree this is all extremely confusing. For better or worse though, I think it's all just inherent to the problem rather than indicative of us doing something wrong. IME basically anything involving filepath calculations ends up being 100x more complicated than you'd ever think.
For the merge+diff stuff in buildkit (which also fell into complicated filepath logic) I just compensated by having enormous numbers of test cases. I think that's probably the best approach here too (and we have been doing that so far).
If there was a library that could do all of this for us, that'd be great, but I suspect the particulars of our use case are too specific (would be delighted to be wrong though).
Either way, commenting on the code with one possible bug I noticed.
0e78f61 to
f8f230c
Compare
There was a problem hiding this comment.
Noticed something else, verified locally this time it was real:
mnt := c.Directory().
WithNewDirectory("/a/b/c").
Directory("/a") // <-- the important part!
ctr = c.Container().
From("alpine:3.16.2").
WithMountedDirectory("/mnt", mnt)
dir = c.Directory().
WithNewDirectory("/foo").
WithNewFile("/foo/some-file", dagger.DirectoryWithNewFileOpts{
Contents: "some-content",
})
contents, err = ctr.Exec(dagger.ContainerExecOpts{
Args: []string{"ls", "-la", "/mnt"},
}).Stdout().Contents(ctx)
require.NoError(t, err)
t.Log(contents)
/*
container_test.go:1557: total 12
drwxr-xr-x 3 root root 4096 Nov 17 21:38 .
drwxr-xr-x 1 root root 4096 Nov 17 21:39 ..
drwxr-xr-x 3 root root 4096 Nov 17 21:38 b
*/
ctr = ctr.WithDirectory("/mnt/b/foo", dir)
contents, err = ctr.Exec(dagger.ContainerExecOpts{
Args: []string{"ls", "-la", "/mnt"},
}).Stdout().Contents(ctx)
require.NoError(t, err)
t.Log(contents)
/*
container_test.go:1563: total 16
drwxr-xr-x 1 root root 4096 Nov 17 21:39 .
drwxr-xr-x 1 root root 4096 Nov 17 21:39 ..
drwxr-xr-x 1 root root 4096 Nov 17 21:38 a
*/Calling WithDirectoy unexpectedly reveals the root of the directory (containing a), whereas before we saw the subselection (containing b).
I think it's because of the / provided to withMounted here. It should be the subselection of the original dir rather than / (I think...)
There was a problem hiding this comment.
Hey, thanks for this!
I actually am not sure what I'm breaking vs what's already broken. I opened this earlier: #3903
There's something not right with path chaining in the API
There was a problem hiding this comment.
@vito May I have some help with this PR when you have some time?
There was a problem hiding this comment.
This seems to fix it:
| return container.withMounted(mount.Target, dirPayload.LLB, "/") | |
| return container.withMounted(mount.Target, dirPayload.LLB, mount.SourcePath) |
It should be the subselection of the original dir rather than / (I think...)
The dirPayload.Dir being mounted is /a/b/foo; locatePath makes it relative to the mount's SourcePath so we just need to continue respecting it with the new mount:
Lines 577 to 584 in eb9ef56
Admittedly this is all pretty confusing coming back after a week of vacation, but the fix at least looks straightforward. 😛
Should definitely have a test for this though.
Here's one:
func TestContainerWithDirectoryToMount(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
require.NoError(t, err)
defer c.Close()
mnt := c.Directory().
WithNewDirectory("/top/sub-dir/sub-file").
Directory("/top") // <-- the important part!
ctr := c.Container().
From("alpine:3.16.2").
WithMountedDirectory("/mnt", mnt)
dir := c.Directory().
WithNewFile("/copied-file", dagger.DirectoryWithNewFileOpts{
Contents: "some-content",
})
ctr = ctr.WithDirectory("/mnt/sub-dir/copied-dir", dir)
contents, err := ctr.Exec(dagger.ContainerExecOpts{
Args: []string{"find", "/mnt"},
}).Stdout().Contents(ctx)
require.NoError(t, err)
require.Equal(t, []string{
"/mnt",
"/mnt/sub-dir",
"/mnt/sub-dir/sub-file",
"/mnt/sub-dir/copied-dir",
"/mnt/sub-dir/copied-dir/copied-file",
}, strings.Split(strings.Trim(contents, "\n"), "\n"))
}Without the fix, it fails like so:
Error: Not equal:
expected: []string{"/mnt", "/mnt/sub-dir", "/mnt/sub-dir/sub-file", "/mnt/sub-dir/copied-dir", "/mnt/sub-dir/copied-dir/copied-file"}
actual : []string{"/mnt", "/mnt/top", "/mnt/top/sub-dir", "/mnt/top/sub-dir/sub-file", "/mnt/top/sub-dir/copied-dir", "/mnt/top/sub-dir/copied-dir/copied-fi
le"}
vito
left a comment
There was a problem hiding this comment.
Looking great! Just a few small things
There was a problem hiding this comment.
These empty strings feel spooky to me for some reason, this feels more clear:
| return dir.WithDirectory(ctx, "", src, filter) | |
| return dir.WithDirectory(ctx, ".", src, filter) |
There was a problem hiding this comment.
Same here:
| return dir.WithFile(ctx, "", src) | |
| return dir.WithFile(ctx, ".", src) |
There was a problem hiding this comment.
This one spooks me out the most; isn't this value supposed to be a filename? Not sure how this is working at the moment. 🤔
This seems to work:
| return container.updateRootFS(ctx, gw, dest, func(dir *Directory) (*Directory, error) { | |
| return dir.WithNewFile(ctx, gw, "", content) | |
| }) | |
| dir, name := filepath.Split(dest) | |
| return container.updateRootFS(ctx, gw, dir, func(dir *Directory) (*Directory, error) { | |
| return dir.WithNewFile(ctx, gw, name, content) | |
| }) |
See dagger#3673 Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Andrea Luzzardi <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
0ff977d to
b82e28f
Compare
Signed-off-by: Alex Suraci <[email protected]>
|
Realized I can just push to your branch, so went ahead and rebased + applied the suggested changes. |
ff1df45 to
65be90f
Compare
|
Can this get a boost? Fiddling around with the new cli it’s frustrating to write raw graphql query for this (having to write nearly identical query twice one to modify fs one to withfs). |
Thank you and LGTM! I can't LGTM my own PR so if you're good with it @vito can you LGTM and merge? |
Signed-off-by: Alex Suraci <[email protected]>
|
Sure thing, just had to resolve some conflicts, will merge when it's green |
Signed-off-by: Alex Suraci <[email protected]>
See #3673
/cc @shykes @helderco @dolanor
Behaves like
COPYin Dockerfile -- workdir is honored.