Skip to content

core: #Mount supports inline file - #2290

Merged
helderco merged 1 commit into
dagger:mainfrom
TomChv:core/exec-mounts-file
Jun 17, 2022
Merged

helderco merged 1 commit into
dagger:mainfrom
TomChv:core/exec-mounts-file

Conversation

@TomChv

@TomChv TomChv commented Apr 23, 2022

Copy link
Copy Markdown
Member

It was really painful to mount a simple file using type: fs because it creates some verbose intermediate step.
With this update, it is not possible to directly mount a file as string.

Signed-off-by: Vasek - Tom C [email protected]

@TomChv TomChv added the kind/dx label Apr 23, 2022
@TomChv TomChv self-assigned this Apr 23, 2022
@TomChv
TomChv requested review from a team, helderco and shykes as code owners April 23, 2022 14:28
@aluzzardi

Copy link
Copy Markdown
Contributor

/cc @helderco @sipsma

This is not really "mounting", but I agree it's a major convenience. IIRC @jpadams was helping someone affected by this very issue (attempting to mount a file)

@shykes

shykes commented Apr 25, 2022

Copy link
Copy Markdown
Contributor
  1. It looks like this does follow the semantics of mounting, eg. the mounted file will no longer exist after exec. Is this correct?
  2. Why not do this in CUE, in docker.#Run?

@sipsma

sipsma commented Apr 26, 2022

Copy link
Copy Markdown
Contributor

I like the convenience of this, but agree that this seems like something we should be able to do in Cue without having to make a separate task for it. We already have a way of turning a string into core.#FS via core.#WriteFile, so it feels like there should be a way to create a cue definition that adapts #WriteFile to a #Mount, but in trying quickly just now I couldn't find a way to make it work. Maybe someone else has ideas though.

If there's no way to do that right now then LGTM, I'd rather users have the convenience of this than block it because it feels like there should be a different way.

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

Some comments on the code if we end up going this route

Comment thread plan/task/exec.go Resolved
Comment thread plan/task/exec.go Resolved
@helderco

Copy link
Copy Markdown
Contributor

Since the mounts are set in core.#Exec we'd have to nest a core action inside another which I think we can't do. Even if we could, the mounts disjunction seems impossible because you want the user to set

type: "file"
contents: string

But in Go, it needs to be

type: "fs"
contents: dagger.#FS

Even doing it in docker.#Run doesn't make it easier. So there's a conflict that's easier solved by just doing this in Go.

@TomChv

TomChv commented Apr 26, 2022

Copy link
Copy Markdown
Member Author

there should be a way to create a cue definition that adapts #WriteFile to a #Mount, but in trying quickly just now I couldn't find a way to make it work. Maybe someone else has ideas though.

I tried a lot of combinaison but it was not possible, as Helder said :

Since the mounts are set in core.#Exec we'd have to nest a core action inside another which I think we can't do. Even if we could, the mounts disjunction seems impossible because you want the user to set

@TomChv

TomChv commented Apr 26, 2022 •

Copy link
Copy Markdown
Member Author

It looks like this does follow the semantics of mounting, eg. the mounted file will no longer exist after exec. Is this correct?

Since it uses llb.AddMount, yes, the logic is the same. Would you want a test to be sure?

EDIT: Here's a test

			verify: core.#ReadFile & {
				input: write.output
				path:  "/test.txt" // Use original file instead of `result.txt`
			} & {
				contents: "hello world"
			}
		}
	}

Result

dagger do -p ./mount_file.cue test --log-format plain
1:54PM INF actions.image | computing
1:54PM INF actions.image | completed    duration=0s
1:54PM INF actions.test.write | computing
1:54PM INF actions.test.write | completed    duration=0s
1:54PM INF actions.test.verify | computing
1:54PM ERR actions.test.verify | failed: ReadFile /test.txt: open /tmp/buildkit-mount2779228987/test.txt: no such file or directory
    duration=0s
1:54PM FTL system | failed to execute plan: task failed: actions.test.verify: ReadFile /test.txt: open /tmp/buildkit-mount2779228987/test.txt: no such file or directory

@shykes

shykes commented Apr 27, 2022

Copy link
Copy Markdown
Contributor

there should be a way to create a cue definition that adapts #WriteFile to a #Mount, but in trying quickly just now I couldn't find a way to make it work. Maybe someone else has ideas though.

I tried a lot of combinaison but it was not possible, as Helder said :

Since the mounts are set in core.#Exec we'd have to nest a core action inside another which I think we can't do. Even if we could, the mounts disjunction seems impossible because you want the user to set

I don't think we need to do it in core. As I said above, we can start by doing it in docker.#Run (similar to export which has no core implementation). The door is still open to a core implementation later, but we don't have to go through it right away IMO.

@TomChv

TomChv commented Apr 27, 2022

Copy link
Copy Markdown
Member Author

I don't think we need to do it in core. As I said above, we can start by doing it in docker.#Run (similar to export which has no core implementation). The door is still open to a core implementation later, but we don't have to go through it right away IMO

Okay but then how would you do that? Adding a field files in docker Run to specially mount a file? Or Is it possible to extends #Mount in docker.#Run without complexity?
My concerns is that if we add a new field now, it will be hard to remove it later. On the other side extending #Mount is quite simple.

Tbh, if it requires a new field in docker.#Run, I'm against.

I just fear that it become complex for nothing, export has no core implementation but the internal is ugly and we had to find workarounds to make it usable.

I'll open a PR tomorrow to mounts file from docker.#Run and we'll see what is possible, maybe we can just extends #Mounts

@helderco

helderco commented Apr 27, 2022 •

Copy link
Copy Markdown
Contributor

I'll open a PR tomorrow to mounts file from docker.#Run and we'll see what is possible, maybe we can just extends #Mounts

I don't think you'll be able to extend the mount from there. For one, how do you add to the disjunction? Secondly, you still have the same conflict with type/contents fields.

The only thing I can think of right now would be another field that then passes the correct mount to #Exec, but this doesn't sound like a clean API even though it would be a better reflection of what's actually going on.

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@TomChv
TomChv force-pushed the core/exec-mounts-file branch from 37460d8 to e212686 Compare May 13, 2022 13:55
@TomChv TomChv removed the kind/stale label May 16, 2022
@TomChv
TomChv force-pushed the core/exec-mounts-file branch from e212686 to 9e74e2a Compare May 16, 2022 11:32
It was really painful to mount a simple file using `type: fs` because it
creates some verbose intermediate step.
With this update, it is not possible to directly mount a file as string.

Signed-off-by: Vasek - Tom C <[email protected]>
@TomChv
TomChv force-pushed the core/exec-mounts-file branch from 9e74e2a to 209f6d5 Compare May 16, 2022 16:48
@TomChv

TomChv commented May 20, 2022

Copy link
Copy Markdown
Member Author

I have updated the PR to latest change, what we decide with this one?

There is no clean way to add it to docker.#Run, that can be done by adding a new field but IMO it will be harder to change the API after that.

/cc @aluzzardi @sipsma @shykes @helderco

@github-actions

github-actions Bot commented Jun 4, 2022

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@TomChv TomChv removed the kind/stale label Jun 6, 2022
@mircubed

Copy link
Copy Markdown
Contributor

@helderco @sipsma @shykes can one of you do the review and approve? Thanks!

@shykes

shykes commented Jun 10, 2022

Copy link
Copy Markdown
Contributor

I defer to @helderco

@helderco

helderco commented Jun 14, 2022 •

Copy link
Copy Markdown
Contributor

I agree that it would be better to abstract this in docker.#Run. I've tried a quick POC for a solution for adding to the existing mounts. Basically we'd have to redefine a new docker.#Mount with supported mount types, but we can refactor core.#Mount to reduce duplication.

//
// CORE
//

#Mount: {
	dest: string
	type: string
	
	#CacheMount | #TmpMount
}

#CacheMount: {
    type: "cache"
    contents: #CacheDir
}

#TmpMount: {
    type: "tmp"
    contents: #TempDir
}

#CacheDir: {
	id:          string
	concurrency: *"shared" | "private" | "locked"
}

#TempDir: {
	size: int64 | *0
}

#Exec: {
    mounts: [string]: #Mount
}


// 
// DOCKER
//

#RunMount: {
    dest: string
    type: string
    
    #CacheMount | #TmpMount | #FileMount
}


#FileMount: {
    type: "file"
    contents: string
}

#Run: {
    mounts: [string]: #RunMount
    
    _fileMounts: {
        for mount, cfg in mounts {
            if cfg.type == "file" {
                (mount): {
                    dest: cfg.dest
                    contents: #CacheDir & {
                        id: "imagine-fs-here--\(cfg.contents)"
                    }
                }
            }
        }
    }

    exec: #Exec & {
        "mounts": {
            for mount, cfg in mounts {
                if cfg.type != "file" {
                    (mount): cfg
                }
            }
            _fileMounts
        }
    }
}

run: #Run & {
    mounts: wat: {
        dest: "/file"
        contents: "wat"
    }
}
// OUTPUT

run: {
	mounts: {
		wat: {
			dest:     "/file"
			contents: "wat"
			type:     "file"
		}
	}
	exec: {
		mounts: {
			wat: {
				dest: "/file"
				contents: {
					id:          "imagine-fs-here--wat"
					concurrency: "shared"
				}
				type: "cache"
			}
		}
	}
}

https://cuelang.org/play/?id=B6QTb2zpG2_L#cue@export@cue

Granted it's much uglier to resort to these loops and conditions, but has a better separation of concerns.

A positive aspect is that the API is the same from docker.#Run point of view, just done in CUE.

But is it worth it?

@TomChv

TomChv commented Jun 14, 2022

Copy link
Copy Markdown
Member Author

But is it worth it?

I think the best way to make a decision is a benchmark, I'll test both solution and see which is the most effective :D

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

Even though this can be done entirely in CUE in docker.#Run I wonder if the added complexity is worth it for the separation of concerns. API can be the same (implementation detail), except for core.#Exec supporting it as well.

@TomChv

TomChv commented Jun 17, 2022

Copy link
Copy Markdown
Member Author

@helderco Thanks for your follow up!

Even though this can be done entirely in CUE in docker.#Run I wonder if the added complexity is worth it for the separation of concerns. API can be the same (implementation detail), except for core.#Exec supporting it as well.

Totally agree, IMO it's not worth and can lead to performance issue / misunderstanding from user because API will be much complex.

I let you merge this one if you think we should.

I will indeed do a follow up PR to implement this new mount where it can be useful.

@helderco
helderco merged commit 2b6fd2b into dagger:main Jun 17, 2022
helderco added a commit to helderco/dagger that referenced this pull request Jun 17, 2022
This broke github workflows, from dagger#2290.

Signed-off-by: Helder Correia <[email protected]>
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.

6 participants