@@ -420,34 +420,6 @@ func (c *Client) RemoveVolume(ctx context.Context, id string, force bool) error
420420 return err
421421}
422422
423- // CreateServiceContainer creates a new container for the service with the given specifications.
424- func (c * Client ) CreateServiceContainer (
425- ctx context.Context , serviceID string , spec api.ServiceSpec , containerName string ,
426- ) (container.CreateResponse , error ) {
427- var resp container.CreateResponse
428-
429- specBytes , err := json .Marshal (spec )
430- if err != nil {
431- return resp , fmt .Errorf ("marshal service spec: %w" , err )
432- }
433- grpcResp , err := c .GRPCClient .CreateServiceContainer (ctx , & pb.CreateServiceContainerRequest {
434- ServiceId : serviceID ,
435- ServiceSpec : specBytes ,
436- ContainerName : containerName ,
437- })
438- if err != nil {
439- if status .Convert (err ).Code () == codes .NotFound {
440- return resp , errdefs .NotFound (err )
441- }
442- return resp , err
443- }
444-
445- if err = json .Unmarshal (grpcResp .Response , & resp ); err != nil {
446- return resp , fmt .Errorf ("unmarshal gRPC response: %w" , err )
447- }
448- return resp , nil
449- }
450-
451423// InspectServiceContainer returns the container information and service specification that was used to create the
452424// container with the given ID.
453425func (c * Client ) InspectServiceContainer (ctx context.Context , id string ) (api.ServiceContainer , error ) {
@@ -474,10 +446,13 @@ func (c *Client) InspectServiceContainer(ctx context.Context, id string) (api.Se
474446type MachineServiceContainers struct {
475447 Metadata * pb.Metadata
476448 Containers []api.ServiceContainer
449+ // HookContainers are one-shot containers for deployment hooks (e.g. pre-deploy).
450+ HookContainers []api.ServiceContainer
477451}
478452
479453// ListServiceContainers returns all containers on requested machines that belong to the service with the given
480454// name or ID. If serviceNameOrID is empty, all service containers are returned.
455+ // Set opts.All to true to include hook containers.
481456func (c * Client ) ListServiceContainers (
482457 ctx context.Context , serviceNameOrID string , opts container.ListOptions ,
483458) ([]MachineServiceContainers , error ) {
@@ -501,22 +476,33 @@ func (c *Client) ListServiceContainers(
501476 continue
502477 }
503478
504- containers := make ([]api.ServiceContainer , len (msg .Containers ))
505- for j , sc := range msg .Containers {
506- if err = json .Unmarshal (sc .Container , & containers [j ].Container ); err != nil {
507- return nil , fmt .Errorf ("unmarshal container: %w" , err )
508- }
509- if err = json .Unmarshal (sc .ServiceSpec , & containers [j ].ServiceSpec ); err != nil {
510- return nil , fmt .Errorf ("unmarshal service spec: %w" , err )
511- }
479+ machineContainers [i ].Containers , err = serviceContainersFromProto (msg .Containers )
480+ if err != nil {
481+ return nil , err
482+ }
483+ machineContainers [i ].HookContainers , err = serviceContainersFromProto (msg .HookContainers )
484+ if err != nil {
485+ return nil , err
512486 }
513-
514- machineContainers [i ].Containers = containers
515487 }
516488
517489 return machineContainers , nil
518490}
519491
492+ // serviceContainersFromProto converts a slice of protobuf service containers to api.ServiceContainer.
493+ func serviceContainersFromProto (pbContainers []* pb.ServiceContainer ) ([]api.ServiceContainer , error ) {
494+ containers := make ([]api.ServiceContainer , len (pbContainers ))
495+ for i , sc := range pbContainers {
496+ if err := json .Unmarshal (sc .Container , & containers [i ].Container ); err != nil {
497+ return nil , fmt .Errorf ("unmarshal container: %w" , err )
498+ }
499+ if err := json .Unmarshal (sc .ServiceSpec , & containers [i ].ServiceSpec ); err != nil {
500+ return nil , fmt .Errorf ("unmarshal service spec: %w" , err )
501+ }
502+ }
503+ return containers , nil
504+ }
505+
520506// RemoveServiceContainer stops (kills after grace period) and removes a service container with the given ID.
521507// A service container is a container that has been created with CreateServiceContainer.
522508func (c * Client ) RemoveServiceContainer (ctx context.Context , id string , opts container.RemoveOptions ) error {
0 commit comments