Skip to content

support dagger in dagger - #3674

Merged
aluzzardi merged 1 commit into
dagger:mainfrom
aluzzardi:dagger-in-dagger
Nov 4, 2022
Merged

aluzzardi merged 1 commit into
dagger:mainfrom
aluzzardi:dagger-in-dagger

Conversation

@aluzzardi

@aluzzardi aluzzardi commented Nov 4, 2022 •

Copy link
Copy Markdown
Contributor

Add the ExperimentalPrivilegedNesting flag to Exec() which sets a valid DAGGER_HOST inside the container, allowing to communicate back to the same engine instance.

In short, this means that it's possible to run a program that uses the Go SDK within Dagger itself -- dagger.Connect() will automagically work out of the box.

This PR also enables to to run dagger tests using dagger in our own CI.

/cc @helderco

@aluzzardi
aluzzardi requested review from shykes, sipsma and vito November 4, 2022 05:48
Comment thread sdk/go/examples_test.go Outdated

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 Wasn't sure what to do here.

This test is using dagger.Connect(ctx, dagger.WithWorkspace(".")).

Locally, that's the sdk/go directory. However when executed in a container, it will use the "parent"'s workdir (which is the root repo).

I was trying to find another way to make this test work that could run from either directory (either this, or remove the // Output at the end.

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 think we should just make this example self-contained. It's probably too implicit as it is. Something like:

dir, err := os.MkdirTemp("", "example*")
if err != nil {
  panic(err)
}

defer os.RemoveAll(dir)

err = os.WriteFile(filepath.Join(dir, "foo"), []byte("hello"), 0644)
if err != nil {
  panic(err)
}

client, err := dagger.Connect(ctx, dagger.WithWorkdir(dir))
// ...

It's more verbose but way more obvious what it'll do.

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.

client, err := dagger.Connect(ctx, dagger.WithWorkdir(dir))

The trick here is WithWorkdir() is a no op with dagger-in-dagger because it's only taken into account at engine.Start which already happened

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.

How about now? A bit hackish, but it's self contained

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.

Err -- I hit undo.

I was trying to write a "hello.txt" file and read it back -- however since there's no more "workdir write", os.WriteFile() and Workdir() didn't match

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.

OK with this for now, hopefully we can clean it up/make it clearer once we support remote engine.

Comment thread sdk/go/internal/engineconn/unix/unix.go Outdated

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 @sipsma Because we set a unix:// DAGGER_HOST when running dagger in dagger, I had a test panic'ing (ExampleHost_Workdir).

I commented this out -- any alternatives?

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.

Hm we could put the examples behind a build flag and skip them in CI. As long as they still run by default when I run go test on my machine that should be OK. So CI sets flag, examples_test.go skips with flag present.

https://stackoverflow.com/a/24036237

We should be able to remove it with #3671 but there's still work to do there.

@aluzzardi
aluzzardi force-pushed the dagger-in-dagger branch 4 times, most recently from 5cb6f8a to 466c460 Compare November 4, 2022 06:17
@dolanor

dolanor commented Nov 4, 2022

Copy link
Copy Markdown
Contributor

I was thinking about this this morning. I'm glad you already had a stab at it.

@aluzzardi
aluzzardi force-pushed the dagger-in-dagger branch 6 times, most recently from dc163fb to 0197794 Compare November 4, 2022 19:55
@aluzzardi

Copy link
Copy Markdown
Contributor Author

Added a bunch of include/exclude filters (thanks @vito!) -- cache is insanely better

@aluzzardi
aluzzardi force-pushed the dagger-in-dagger branch 4 times, most recently from 7884d80 to f1c3eb1 Compare November 4, 2022 21:13
Add the `ExperimentalPrivilegedNesting` flag to `Exec()` which sets a
valid `DAGGER_HOST` inside the container, allowing to communicate back
to the same engine instance.

In short, this means that it's possible to run a program that uses the
Go SDK within Dagger itself -- `dagger.Connect()` will automagically
work out of the box.

Signed-off-by: Andrea Luzzardi <[email protected]>
@aluzzardi
aluzzardi merged commit 4e694dd into dagger:main Nov 4, 2022
@aluzzardi
aluzzardi deleted the dagger-in-dagger branch November 4, 2022 21:43
@vito vito mentioned this pull request Nov 7, 2022
1 of 10 tasks
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.

3 participants