Skip to content

replace up command with chainable core api - #6407

Merged
sipsma merged 3 commits into
dagger:mainfrom
sipsma:chainable-up
Jan 23, 2024
Merged

sipsma merged 3 commits into
dagger:mainfrom
sipsma:chainable-up

Conversation

@sipsma

@sipsma sipsma commented Jan 12, 2024 •

Copy link
Copy Markdown
Contributor

This is one part of our efforts to consolidate dagger CLI commands, along side #6390. It removes dagger up in favor of using a up api chained from the core Service type.

For example, previously you'd do:
dagger up my-svc --port 8080:80 --port 2222:22

Now you do:
dagger call my-svc up --ports 8080:80 --ports 2222:22
or
dagger call my-svc up --native
or just
dagger call my-svc up (use any available local port)

This change seems to have also fixed #6303 along the way for whatever reason, at least based on testing so far 🤷‍♂️🎉?

This one was somewhat less obvious than the equivalent for dagger shell, so looking for feedback on some of the high level decisions here:

up API on Service

Name

For now I just went with a field on the Service type literally called up. Whether the name up is best seems like prime bikeshedding material, so have at it 😄

Other possibilities that came to mind so far include:

  • proxy
  • localProxy (local-proxy in the CLI command)
  • serveLocal (serve-local in the CLI command)

Overall none of them struck me personally as clearly better enough than up to switch right away, so left up to get thoughts from others. If I was forced to choose, I kind of like local-proxy, with only downside of being more verbose than up.

Behavior + Implementation

up is implemented such that it creates and starts the host service and then waits for the context to be cancelled (i.e. the cli user ctr-c's or otherwise disconnects). The CLI thus has no special handling of this type or Service in general, it's all handled server-side.

The only non-obvious part of the implementation was how to display the port mappings back to the client since the CLI can't directly anymore.

Handling graphql Input types as TypeDefs

The PortForward type in our core API is a graphql input type, which meant that before this PR it wasn't visible to the CLI because graphql input types were ignored when converting to TypeDef.

The first commit here fixes that by adding very limited support to TypeDef for input types. It's explicitly doc'd as being only for compatibility w/ the core API and not for use w/ user mods and has the bare minimum fields required to get this functionality w/ the CLI working.

I don't love that at all but it's low impact and the alternative was to change PortForward to be an id-able object, which was much higher impact and a breaking change.

I went down that path anyways for a bit but realized that besides being a breaking change, it actually makes the DX, at least in the Go SDK, somewhat worse since you can't just provide e.g. &dagger.PortForward{Frontend: 123, Backend: 234} and instead have to contend with optional args (very verbose in Go) or use something we add like dagger.PortForward().WithFrontend(123).WithBackend(234).

If others think that dagger.PortForward().WithFrontend(123).WithBackend(234) is okay enough and the backwards incompatible change is not a big deal, then I'd be happy to switch to that. In general there's appeal in the ide of our core api moving entirely away from input types and exclusively using id-able objects, but not sure if it's worth it now.

@sipsma
sipsma requested review from helderco and vito January 12, 2024 00:35
@sipsma
sipsma requested review from a team as code owners January 12, 2024 00:35
@helderco

Copy link
Copy Markdown
Contributor

On the other hand, I could see this being a gotcha when users forget to chain up ("why can't I reach my service??").

I'm thinking that we can leave this behavior as is but if the user doesn't chain up, print a line to the progress telling them that it's running without any host proxying.

Yeah, since it can be useful to execute without needing to proxy I'm leaning towards just warning as well. Including mentioning up if that's what the user intends.

Overall none of them struck me personally as clearly better enough than up to switch right away, so left up to get thoughts from others.

Same. up is fine by me.

I don't love that at all but it's low impact and the alternative was to change PortForward to be an id-able object, which was much higher impact and a breaking change.

👍

From dagql:

  • All Objects are Nodes, i.e. all objects have an id.
  • All Objects have their own ID type, e.g. PointID.

Does that include input object types @vito?

@vito

vito commented Jan 12, 2024

Copy link
Copy Markdown
Contributor

Does that include input object types @vito?

@helderco Those rules only apply to regular Objects; input objects are handled separately, with similar complexity to handling input arrays. They're not too difficult to support at that layer, so I'd be OK with keeping them; they're definitely more expressive for simple cases like this, and we already need to support them in a bunch of places (container build args, pipeline labels) where forcing an ID-able object feels overkill.

I think I'd be OK with doubling-down and supporting input objects all the way into modules/SDKs if we can find a nice way, but I don't completely remember what biased against them in the first place. I suppose it's just awkward having two ways to do something that conflict at the GraphQL type layer (can't have one type be both input and output). Maybe this comes down to figuring out the right patterns?


re: up, my biggest gripe would be potential overlap with start - it's not super clear that it's starting a tunnel/proxy. It makes sense in the CLI because it mirrors docker-compose up but I don't think that notion really carries over to the API. I'd be in favor of localProxy out of the given options. Or maybe something like listen? 🤔 Will mosey back with more ideas if they come to me...

Also poking @shykes since I remember talking about this general idea of whether Host-effect-ful things should live on Host or just be chained from objects, and at the time we said they should live on Host and perhaps Directory.export and friends are wrong, but it seems like we now have a decent reason to go back on that idea (CLI chaining). Which is all well and good, just connecting dots for continuity.

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

LGTM 🚀, just missing the "no proxy" message when not using up.

Comment thread cmd/dagger/flags.go Resolved
@shykes

shykes commented Jan 12, 2024

Copy link
Copy Markdown
Contributor

The implementation here updates the CLI to handle any Service return type by calling Start and waiting for the user to ctrl-c and then exiting (same as dagger up's previous behavior on service types).

Doesn't this contradict the current consensus reached in #6229, of not doing "magical" things with return types? For example, don't exec a returned Container, which seems to me it would apply to services too?

@sipsma

sipsma commented Jan 12, 2024 •

Copy link
Copy Markdown
Contributor Author

The implementation here updates the CLI to handle any Service return type by calling Start and waiting for the user to ctrl-c and then exiting (same as dagger up's previous behavior on service types).

Doesn't this contradict the current consensus reached in #6229, of not doing "magical" things with return types? For example, don't exec a returned Container, which seems to me it would apply to services too?

@shykes That was the first idea yes but after more thinking around the fact that we don't need to strictly enforce the rule around the return type always being scalar I also realized that there's no harm in having handling of core types in the CLI. I was trying to get at this in my subsequent comment ("Loosening restrictions on return type being a scalar"), but hadn't made a decision yet on Service specifically. This PR is the decision I went with :-)

The general principle in my mind is that we shouldn't have the CLI give users a bunch of different ways of doing the same thing. For Service specifically, the way the API is designed such that start itself returns a Service means that if a CLI command ends in a Service type, it's not like there's some other API available that would also result in the service running. So we can enforce in the CLI that the only way to execute a service is by ending your CLI command in a Service.

This is as opposed to Container where I no longer think it makes sense to have the behavior of just selecting stdout on the Container if your CLI command ends in it since you can just call stdout yourself from the CLI (or chain any number of other APIs that would implicitly result in the container executing)


That all being said, on a second thought yeah I suppose I can see some confusion arising from the fact that dagger call my-service runs the service but dagger call my-container doesn't run the container. There's theoretical backing to the idea but I guess it's probably not super apparent in the end result 🤔

We can fairly easily add another field to Service that does "start and then wait until it stops or the client disconnects (i.e. ctrl-c)". So if we called that field startAndWait (strawman), the CLI handling could be updated to:

dagger call my-service up --native start-and-wait

Let me know what you think.

@shykes

shykes commented Jan 13, 2024

Copy link
Copy Markdown
Contributor

dagger call my-service up --native start-and-wait

What if those two were one and the same? So it would become dagger call my-service up --native

And taking it one step further, shouldn't start itself be what you call start-and-wait? Is there really a need for start to be chainable?

In the end, these three calls would become perfectly equivalent:

  • dagger call up --native
  • dagger call start --native
  • dagger call start-and-wait --native

Which leaves us the job of choosing one name and deprecating the other two :)

What am I missing?

@sipsma

sipsma commented Jan 13, 2024

Copy link
Copy Markdown
Contributor Author

dagger call my-service up --native start-and-wait

What if those two were one and the same? So it would become dagger call my-service up --native

Yeah you're right that's totally possible; with no other changes we would sacrifice the ability to run a service without any host tunneling but that was A) just a theoretically maybe nice feature, not that important and B) something we could probably still support too on up (with slight api implementation updates).

And taking it one step further, shouldn't start itself be what you call start-and-wait? Is there really a need for start to be chainable?

This in particular is just how the start API already works today (returning a Service), changing it would require rethinking the existing Service API because that chainability is what currently allows you to query which ports were setup after the service started running (in the case where frontend port is 0 and got randomly selected), among other things.

So I'm inclined to leave that as is for now.


But either way you're totally right we can just make up have the start-and-wait behavior and problem solved. Got a little lost in overthinking here, thanks :-) I'll make that update

@helderco helderco added this to the v0.9.7 milestone Jan 16, 2024
@shykes

shykes commented Jan 18, 2024 •

Copy link
Copy Markdown
Contributor

How about serve? :) Benefits:

  • It's a nice familiar hint at the forever-blocking nature of it (it evokes Go's ListenAndServe for example).
  • It doesn't clash with start in the way up does (those two kind of sound like they do the same thing, which to be honest is partly true)

EDIT: I'm not really convinced myself, I think I still prefer up.

Either way, I do think we should consider deprecating the chainable start in the future, to me it feels redundant to sync, but I might be missing something. In any case that can be a separate, future discussion.

@helderco helderco removed this from the v0.9.7 milestone Jan 18, 2024
@sipsma
sipsma force-pushed the chainable-up branch 5 times, most recently from 1edf5c6 to 776c742 Compare January 23, 2024 03:44
@sipsma

sipsma commented Jan 23, 2024

Copy link
Copy Markdown
Contributor Author

Updated w/ up to have the full blown "serve-and-wait" behavior (so no more special handling of Service) and checked w/ Alex out-of-band about the progress thing mentioned in the description (made a tiny tweak to be forward compatible w/ upcoming new UI: 3efdd69).

So gonna merge in the interest of reducing number of pending PRs, can open follow ups if anything else comes up!

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.

Bug: bind: address already in use when using dagger up --port

5 participants