Skip to content

sdk: go: dagger.Connect - #3349

Merged
aluzzardi merged 6 commits into
dagger:mainfrom
aluzzardi:go-sdk-connect
Oct 14, 2022
Merged

aluzzardi merged 6 commits into
dagger:mainfrom
aluzzardi:go-sdk-connect

Conversation

@aluzzardi

@aluzzardi aluzzardi commented Oct 13, 2022 •

Copy link
Copy Markdown
Contributor
c, err := dagger.Connect(ctx)
require.NoError(t, err)
defer c.Close()

tree := c.Core().Git("github.com/dagger/dagger").
	Branch("cloak").
	Tree()
  • I put everything in dagger/client since there's a bunch of crap in there, didn't want to grow the dagger package unchecked (which also contains the server).
    • Q However it might make sense to make dagger/client just dagger, and potentially move the serve part to a sub-package if needs be?
    • Done: move everything to dagger
  • There's a connhelper concept, half baked
    • Right now it's hardcoded to use engineconn to use the built-in engine
    • Planning to eventually have unixconn etc, use DAGGER_HOST or something to automatically select which conn to use (e.g. core could set DAGGER_HOST=unix:///cloak.sock and dagger.Connect() would work out of the box)
  • Lots of breakages: there's a ton of dependencies around directly to the engine package
    • Planning to do something like dagger.Connect(ctx, dagger.WithLocalDir(...), dagger.WithWorkdir(...)) to maintain compat?
    • Still, lots of other intricate dependencies (project config, etc). Unsure if we can get rid of them?
  • Returned client abstracts github.com/Khan/genqlient/graphql.Client away

Interface:

  • client.Do() --> send a graphql request (e.g. equivalent of graphql.Client.MakeRequest)
  • client.Core() --> return the codegen'd API (e.g. c.Core().Container().From(...))
  • Codegen'd API lives in client/api
    • e.g. client.Query() returns an api.Query
    • This is to avoid mixing the codegen'd stuff with the API
    • Once we switch to "everyone runs their own codegen", simply updating the import path will fix it all. If we mix codegen inside the dagger package, then it'll get tricky
    • Q: however, not 100% convinced

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

Looks good overall!

Still, lots of other intricate dependencies (project config, etc). Unsure if we can get rid of them?

Assuming you're talking about the stuff in engine.Context

  • Project config is good to remove afaict (was needed for older codegen, not needed right now).
  • Localdirs can be removed (that was just a convenience to save an extra call to retrieve the name->id mapping

Planning to eventually have unixconn etc, use DAGGER_HOST or something to automatically select which conn to use (e.g. core could set DAGGER_HOST=unix:///cloak.sock and dagger.Connect() would work out of the box)

👍

Q However it might make sense to make dagger/client just dagger, and potentially move the serve part to a sub-package if needs be?

Yeah moving it like that would make more sense to me. Client is going to be imported more often than the server stuff (which honestly needs to be renamed anyways as part of fleshing out the code-first stuff). dagger.Connect has a nice ring to it.

Comment thread sdk/go/dagger/client/client.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.

Once this PR is merged: #3342 (comment)

We should also update this func to wrap errors with that message I think.

Also, looking at this code now, we could consider just wrapping the error in Do instead of where I inserted it in that PR. Probably makes no functional difference, may just be cleaner.

@aluzzardi
aluzzardi force-pushed the go-sdk-connect branch 11 times, most recently from 0435cdb to 80be96c Compare October 14, 2022 02:37
@aluzzardi

Copy link
Copy Markdown
Contributor Author

/cc @kpenfound -- one of the last big breaking changes

@aluzzardi
aluzzardi marked this pull request as ready for review October 14, 2022 02:45

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

Couple comments but nothing blocking, awesome to see extensions and other clients more unified! 🎉

Comment thread sdk/go/dagger/client.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.

nit, not blocking, just if you agree

type ClienOpt interface {
  SetClientOpt(cfg *engineconn.Config)
}

...

func (cfg *engineconn.Config) SetClientOpt(other *engineconn.Config) {
  // set each other field from fields set in cfg
}

Is nice as it addresses some people's concerns about optional pattern not being discoverable as you can just pass a struct instead. Can still have all the individual With* methods too for those who like those.

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.

Yes, I remember your suggestion and was planning to do that.

However I got a bit stuck. I don't want to expose engineconn to the user and originally intended for this to be dagger.Config{}, however got into a circular dependency problem (dagger -> engineconn -> dagger).

Do you see an easy way out of this?

(ps: I wish to move engineconn and whatever else is not part of the public API under internal/ so we're free to change without breaking anyone)

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 guess the generic fallback option w/ circular deps is to split out into a separate package, like:

  • dagger -> engineconfig
  • engineconn -> engineconfig

engineconfig is just a placeholder name, but it would just hold the common (public) interface/struct definitions needed to configure the engine. engineconn can remain internal too then I think.

I'd be fine with that. Intermediate option is to just change ClientOpt to be an interface rather than direct func so that we can retain the flexibility to do something like this in the future.

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'd be fine with that. Intermediate option is to just change ClientOpt to be an interface rather than direct func so that we can retain the flexibility to do something like this in the future.

Done.

Might find a better way than a separate package at some point, just don't want to it in this PR for now

Comment thread cmd/dagger/project.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.

I think we dropped the SkipInstall here unless I missed something. If we can keep it that would be preferable because the only thing that's needed here is to load project metadata; there's no need to actually compile the extension (and fail if it has an error).

The only other possibility would be to move loading of project out from engine, but that's the whole other can of worms we talked about before, not worth it yet probably.

This isn't blocking if it's time consuming, but worth an issue if not fixed here.

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.

Yep, I was actually puzzled by this.

If I understand correctly, setting SkipInstall would only load the project under ctx.Project, which is not exposed anymore anyway, correct?

So a similar behavior could be achieved with a WithConfigPath(""), or to make it more explicit a better name would be WithNoExtensions()?

I see this code is querying for its own loadExtension so it doesn't care that the engine is actually loading the extensions (and skipping install)

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.

Ah yeah, I forgot the code that I wrote several days ago already 😵‍💫😵‍💫😵‍💫 This file actually already is doing the project load itself (line 192), so the only thing needed is for the engine to not load anything, which will be the implicit behavior if you don't set ConfigPath, so actually the only change here should be to just not call WithConfigPath at all.

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 pushed a small update in a separate commit engine: remove redundant functionality -- helps clarify the load project stuff in engine (much smaller surface area).

How does that look? Can easily revert

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.

(also can add an explicit dagger.WithNoExtensions())

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 commit looks good overall, but I don't think you need NoExtensions even. AFAICT if you don't provide a ConfigPath, that should just be the same thing. I think that makes sense: no extension config, no extensions loaded.

Then special cases (like this project command) that need different extension loading logic (like skipping install) can just do the call themselves.

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.

Ah, actually we call NormalizePaths in engine.Start, so this doesn't quite work as is (it will see empty ConfigPath and then default it to $(pwd)/dagger.json). I actually think that in and of itself should be refactored, but that's getting way out of scope here.

How about we just leave NoExtensions for now (it's an obscure thing, only needed by this project command) and then we can refactor this a bit more in the future. I think I see a path for how to do it cleanly, I'll make an issue.

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.

Made an issue here: #3375

@aluzzardi aluzzardi Oct 14, 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.

Done in last commit

Actually, even client-gen needs this for now

Comment thread internal/testutil/query.go Resolved
Comment thread cmd/dagger/clientgen.go Resolved
Comment thread cmd/dagger/dev.go Resolved
Comment thread internal/testutil/query.go Outdated

@vito vito Oct 14, 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.

nit, not a blocker: maybe this could become QueryWithClientOpts(..., clientOpts ...dagger.ClientOpt)?

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.

Done!

@aluzzardi
aluzzardi changed the base branch from cloak to main October 14, 2022 18:22
@aluzzardi
aluzzardi force-pushed the go-sdk-connect branch 4 times, most recently from 703a997 to dfcdfd4 Compare October 14, 2022 20:21
Signed-off-by: Andrea Luzzardi <[email protected]>
@aluzzardi
aluzzardi merged commit 5b77fba into dagger:main Oct 14, 2022
@aluzzardi
aluzzardi deleted the go-sdk-connect branch October 14, 2022 20:53
@aluzzardi aluzzardi mentioned this pull request Oct 15, 2022
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