-
Notifications
You must be signed in to change notification settings - Fork 72
feat(cli): add kind and endpointHost to docker model status --json #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,11 +7,12 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/docker/model-runner/cmd/cli/pkg/types" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/docker/cli/cli-plugins/hooks" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/docker/model-runner/cmd/cli/commands/completion" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/docker/model-runner/cmd/cli/desktop" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/docker/model-runner/cmd/cli/pkg/standalone" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/docker/model-runner/cmd/cli/pkg/types" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/docker/model-runner/pkg/inference" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -21,7 +22,7 @@ func newStatusCmd() *cobra.Command { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Use: "status", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Short: "Check if the Docker Model Runner is running", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| standalone, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| runner, err := ensureStandaloneRunnerAvailable(cmd.Context(), asPrinter(cmd), false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("unable to initialize standalone model runner: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,7 +41,7 @@ func newStatusCmd() *cobra.Command { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if formatJson { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return jsonStatus(standalone, status, backendStatus) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return jsonStatus(asPrinter(cmd), runner, status, backendStatus) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| textStatus(cmd, status, backendStatus) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -69,44 +70,54 @@ func textStatus(cmd *cobra.Command, status desktop.Status, backendStatus map[str | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func jsonStatus(standalone *standaloneRunner, status desktop.Status, backendStatus map[string]string) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func makeEndpoint(host string, port int) string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "http://" + net.JoinHostPort(host, strconv.Itoa(port)) + "/v1/" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func jsonStatus(printer standalone.StatusPrinter, runner *standaloneRunner, status desktop.Status, backendStatus map[string]string) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type Status struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Running bool `json:"running"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Backends map[string]string `json:"backends"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Endpoint string `json:"endpoint"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Running bool `json:"running"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Backends map[string]string `json:"backends"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Kind string `json:"kind"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Endpoint string `json:"endpoint"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EndpointHost string `json:"endpointHost"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var endpoint string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var endpoint, endpointHost string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kind := modelRunner.EngineKind() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch kind { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case types.ModelRunnerEngineKindDesktop: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint = "http://model-runner.docker.internal/engines/v1/" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint = "http://model-runner.docker.internal/v1/" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpointHost = "http://localhost" + inference.ExperimentalEndpointsPrefix + "/v1/" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case types.ModelRunnerEngineKindMobyManual: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint = modelRunner.URL("/engines/v1/") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint = modelRunner.URL("/v1/") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpointHost = endpoint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case types.ModelRunnerEngineKindCloud: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fallthrough | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case types.ModelRunnerEngineKindMoby: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if standalone.gatewayIP == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| standalone.gatewayIP = "127.0.0.1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if runner.gatewayIP == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| runner.gatewayIP = "127.0.0.1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if standalone.gatewayPort == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| standalone.gatewayPort = 12434 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if runner.gatewayPort == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| runner.gatewayPort = standalone.DefaultControllerPortCloud | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint = "http://" + net.JoinHostPort(standalone.gatewayIP, strconv.Itoa(int(standalone.gatewayPort))) + "/engines/v1/" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint = makeEndpoint(runner.gatewayIP, int(runner.gatewayPort)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpointHost = makeEndpoint("127.0.0.1", standalone.DefaultControllerPortCloud) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
94
to
+102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a potential nil pointer dereference here. The
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh that's true in case of setting |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case types.ModelRunnerEngineKindMoby: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint = makeEndpoint("host.docker.internal", standalone.DefaultControllerPortMoby) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpointHost = makeEndpoint("127.0.0.1", standalone.DefaultControllerPortMoby) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("unhandled engine kind: %v", kind) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s := Status{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Running: status.Running, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Backends: backendStatus, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Endpoint: endpoint, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Running: status.Running, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Backends: backendStatus, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Kind: kind.String(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Endpoint: endpoint, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EndpointHost: endpointHost, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| marshal, err := json.Marshal(s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println(string(marshal)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printer.Println(string(marshal)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,6 +113,20 @@ func NewContextForMock(client DockerHttpClient) *ModelRunnerContext { | |
| } | ||
| } | ||
|
|
||
| // NewContextForMockWithKind is a ModelRunnerContext constructor exposed only for the | ||
| // purposes of mock testing with a specific engine kind. | ||
| func NewContextForMockWithKind(client DockerHttpClient, kind types.ModelRunnerEngineKind, rawURLPrefix string) *ModelRunnerContext { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we could update |
||
| urlPrefix, err := url.Parse(rawURLPrefix) | ||
| if err != nil { | ||
| panic("error occurred while parsing URL: " + err.Error()) | ||
| } | ||
| return &ModelRunnerContext{ | ||
| kind: kind, | ||
| urlPrefix: urlPrefix, | ||
| client: client, | ||
| } | ||
| } | ||
|
|
||
| // NewContextForTest creates a ModelRunnerContext for integration testing | ||
| // with a custom URL endpoint. This is intended for use in integration tests | ||
| // where the Model Runner endpoint is dynamically created (e.g., testcontainers). | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with other cases like
ModelRunnerEngineKindMobyManualand to avoid re-implementing the URL construction logic, it's better to usemodelRunner.URL("/v1/")to get the host endpoint URL. ThemodelRunnercontext is already configured with the correct base URL for host-to-model-runner communication in theDesktopcase. This change improves maintainability by centralizing URL construction.