refactor: client StartContainer pass serviceID

This commit is contained in:
Pavel Sviderski
2025-02-11 16:47:52 +10:00
parent a477db6b16
commit 76a9f8e8b6
3 changed files with 27 additions and 13 deletions
+21 -7
View File
@@ -17,6 +17,7 @@ import (
"uncloud/internal/secret"
)
// CreateContainer creates a new container for the given service on the specified machine.
func (cli *Client) CreateContainer(
ctx context.Context, serviceID string, spec api.ServiceSpec, machineID string,
) (container.CreateResponse, error) {
@@ -218,20 +219,33 @@ func toPullProgressEvent(jm jsonmessage.JSONMessage) *progress.Event {
}
}
func (cli *Client) StartContainer(ctx context.Context, id string, machineID string) error {
// StartContainer starts the specified container within the service.
func (cli *Client) StartContainer(ctx context.Context, serviceID, containerID string) error {
svc, err := cli.InspectService(ctx, serviceID)
if err != nil {
return fmt.Errorf("inspect service: %w", err)
}
var ctr api.Container
var machineID string
for _, c := range svc.Containers {
if c.Container.ID == containerID || c.Container.Names[0] == containerID {
ctr = c.Container
machineID = c.MachineID
}
}
if ctr.ID == "" {
return ErrNotFound
}
machine, err := cli.InspectMachine(ctx, machineID)
if err != nil {
return fmt.Errorf("inspect machine '%s': %w", machineID, err)
}
ctx = proxyToMachine(ctx, machine.Machine)
ctr, err := cli.Docker.InspectContainer(ctx, id)
if err != nil {
return err
}
pw := progress.ContextWriter(ctx)
eventID := fmt.Sprintf("Container %s on %s", ctr.Name, machine.Machine.Name)
eventID := fmt.Sprintf("Container %s on %s", ctr.Names[0], machine.Machine.Name)
pw.Event(progress.StartingEvent(eventID))
if err = cli.Docker.StartContainer(ctx, ctr.ID, container.StartOptions{}); err != nil {
+2 -2
View File
@@ -214,7 +214,7 @@ func (cli *Client) runContainer(
return resp, fmt.Errorf("create container: %w", err)
}
if err = cli.StartContainer(ctx, resp.ID, machine.Name); err != nil {
if err = cli.StartContainer(ctx, serviceID, resp.ID); err != nil {
return resp, fmt.Errorf("start container: %w", err)
}
@@ -319,7 +319,7 @@ func (cli *Client) InspectService(ctx context.Context, id string) (api.Service,
serviceID := containers[0].Container.ServiceID()
for _, mc := range containers[1:] {
if mc.Container.ServiceID() != serviceID {
return svc, fmt.Errorf("multiple services found with name: %s", id)
return svc, fmt.Errorf("multiple services found with name '%s', use the service ID instead", id)
}
}
}
+4 -4
View File
@@ -24,9 +24,9 @@ func TestService(t *testing.T) {
t.Run("container lifecycle", func(t *testing.T) {
t.Parallel()
name := "busybox-container-lifecycle"
svcName := "busybox-container-lifecycle"
spec := api.ServiceSpec{
Name: name,
Name: svcName,
Container: api.ContainerSpec{
Command: []string{"sleep", "infinity"},
Image: "busybox:latest",
@@ -34,11 +34,11 @@ func TestService(t *testing.T) {
}
machineID := c.Machines[0].Name
ctr, err := cli.CreateContainer(ctx, name, spec, machineID)
ctr, err := cli.CreateContainer(ctx, svcName, spec, machineID)
require.NoError(t, err)
assert.NotEmpty(t, ctr.ID)
err = cli.StartContainer(ctx, ctr.ID, machineID)
err = cli.StartContainer(ctx, svcName, ctr.ID)
require.NoError(t, err)
})
}