Skip to content

container: Support WithDirectory, WithFile & WithNewFile - #3827

Merged
vito merged 6 commits into
dagger:mainfrom
aluzzardi:container-with-file-directory
Dec 1, 2022
Merged

vito merged 6 commits into
dagger:mainfrom
aluzzardi:container-with-file-directory

Conversation

@aluzzardi

@aluzzardi aluzzardi commented Nov 15, 2022 •

Copy link
Copy Markdown
Contributor

See #3673

/cc @shykes @helderco @dolanor

Behaves like COPY in Dockerfile -- workdir is honored.

Comment thread core/container.go Outdated

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.

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

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.

You mean writing in the mount if it's a mounted directory?

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.

Yes, similar to the behavior we have for Directory and File where the path you specify takes mounts into account

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.

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?

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.

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 "/".

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.

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.

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.

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.

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

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.

Comment thread core/container.go Outdated

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

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.

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.

// 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"))?

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'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

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

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?

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.

That's good, right?

Yeah that looks correct to me!

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

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

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

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.

Updated PR. I honestly don't know why CI is passing, but it is.

Comment thread core/container.go Outdated

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

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.

@aluzzardi
aluzzardi force-pushed the container-with-file-directory branch 2 times, most recently from 0e78f61 to f8f230c Compare November 17, 2022 20:17
Comment thread core/container.go Outdated

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

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

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.

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

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.

@vito May I have some help with this PR when you have some time?

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.

Taking a look!

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 seems to fix it:

Suggested change
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:

dagger/core/container.go

Lines 577 to 584 in eb9ef56

sub := mnt.SourcePath
if containerPath != mnt.Target {
// make relative portion relative to the source path
dirSub := strings.TrimPrefix(containerPath, mnt.Target+"/")
if dirSub != "" {
sub = path.Join(sub, dirSub)
}
}

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

Looking great! Just a few small things

Comment thread core/container.go Outdated

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.

These empty strings feel spooky to me for some reason, this feels more clear:

Suggested change
return dir.WithDirectory(ctx, "", src, filter)
return dir.WithDirectory(ctx, ".", src, filter)

Comment thread core/container.go Outdated

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.

Same here:

Suggested change
return dir.WithFile(ctx, "", src)
return dir.WithFile(ctx, ".", src)

Comment thread core/container.go Outdated
Comment on lines 355 to 358

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 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:

Suggested change
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)
})

@vito
vito force-pushed the container-with-file-directory branch from 0ff977d to b82e28f Compare November 24, 2022 20:15
@vito

vito commented Nov 24, 2022

Copy link
Copy Markdown
Contributor

Realized I can just push to your branch, so went ahead and rebased + applied the suggested changes.

@vito
vito force-pushed the container-with-file-directory branch from ff1df45 to 65be90f Compare November 24, 2022 20:49
@wxdao

wxdao commented Dec 1, 2022

Copy link
Copy Markdown

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

@aluzzardi

Copy link
Copy Markdown
Contributor Author

Realized I can just push to your branch, so went ahead and rebased + applied the suggested changes.

Thank you and LGTM!

I can't LGTM my own PR so if you're good with it @vito can you LGTM and merge?

@vito

vito commented Dec 1, 2022

Copy link
Copy Markdown
Contributor

Sure thing, just had to resolve some conflicts, will merge when it's green

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 2bd84ee into dagger:main Dec 1, 2022
@aluzzardi

Copy link
Copy Markdown
Contributor Author

@vito 🙏

@wxdao It's in -- we're about to publish a new engine today, this should make it as well

/cc @sipsma

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.

4 participants