mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
chore: use original service spec instead of deriving from container, fixes diff for scale
This commit is contained in:
@@ -369,6 +369,75 @@ func (c *Client) CreateServiceContainer(
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// InspectServiceContainer returns the container information and service specification that was used to create the
|
||||
// container with the given ID.
|
||||
func (c *Client) InspectServiceContainer(ctx context.Context, id string) (api.ServiceContainer, error) {
|
||||
var resp api.ServiceContainer
|
||||
|
||||
grpcResp, err := c.grpcClient.InspectServiceContainer(ctx, &pb.InspectContainerRequest{Id: id})
|
||||
if err != nil {
|
||||
if status.Convert(err).Code() == codes.NotFound {
|
||||
return resp, errdefs.NotFound(err)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(grpcResp.Container, &resp.Container); err != nil {
|
||||
return resp, fmt.Errorf("unmarshal container: %w", err)
|
||||
}
|
||||
if err = json.Unmarshal(grpcResp.ServiceSpec, &resp.ServiceSpec); err != nil {
|
||||
return resp, fmt.Errorf("unmarshal service spec: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
type MachineServiceContainers struct {
|
||||
Metadata *pb.Metadata
|
||||
Containers []api.ServiceContainer
|
||||
}
|
||||
|
||||
// ListServiceContainers returns all containers on requested machines that belong to the service with the given
|
||||
// name or ID. If serviceNameOrID is empty, all service containers are returned.
|
||||
func (c *Client) ListServiceContainers(
|
||||
ctx context.Context, serviceNameOrID string, opts container.ListOptions,
|
||||
) ([]MachineServiceContainers, error) {
|
||||
optsBytes, err := json.Marshal(opts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal options: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.grpcClient.ListServiceContainers(ctx, &pb.ListServiceContainersRequest{
|
||||
ServiceId: serviceNameOrID,
|
||||
Options: optsBytes,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
machineContainers := make([]MachineServiceContainers, len(resp.Messages))
|
||||
for i, msg := range resp.Messages {
|
||||
machineContainers[i].Metadata = msg.Metadata
|
||||
if msg.Metadata != nil && msg.Metadata.Error != "" {
|
||||
continue
|
||||
}
|
||||
|
||||
containers := make([]api.ServiceContainer, len(msg.Containers))
|
||||
for j, sc := range msg.Containers {
|
||||
if err = json.Unmarshal(sc.Container, &containers[j].Container); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal container: %w", err)
|
||||
}
|
||||
if err = json.Unmarshal(sc.ServiceSpec, &containers[j].ServiceSpec); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal service spec: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
machineContainers[i].Containers = containers
|
||||
}
|
||||
|
||||
return machineContainers, nil
|
||||
}
|
||||
|
||||
// RemoveServiceContainer stops (kills after grace period) and removes a service container with the given ID.
|
||||
// A service container is a container that has been created with CreateServiceContainer.
|
||||
func (c *Client) RemoveServiceContainer(ctx context.Context, id string, opts container.RemoveOptions) error {
|
||||
|
||||
@@ -2,6 +2,7 @@ package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -455,6 +456,125 @@ func (s *Server) CreateServiceContainer(
|
||||
return &pb.CreateContainerResponse{Response: respBytes}, nil
|
||||
}
|
||||
|
||||
// InspectServiceContainer returns the container information and service specification that was used to create the
|
||||
// container with the given ID.
|
||||
func (s *Server) InspectServiceContainer(
|
||||
ctx context.Context, req *pb.InspectContainerRequest,
|
||||
) (*pb.ServiceContainer, error) {
|
||||
ctr, err := s.client.ContainerInspect(ctx, req.Id)
|
||||
if err != nil {
|
||||
if client.IsErrNotFound(err) {
|
||||
return nil, status.Errorf(codes.NotFound, err.Error())
|
||||
}
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
ctrBytes, err := json.Marshal(ctr)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "marshal response: %v", err)
|
||||
}
|
||||
|
||||
var specBytes []byte
|
||||
err = s.db.QueryRowContext(ctx, `SELECT service_spec FROM containers WHERE id = $1`, ctr.ID).Scan(&specBytes)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, status.Errorf(codes.NotFound, "service spec not found for container: '%s'", ctr.ID)
|
||||
}
|
||||
return nil, status.Errorf(codes.Internal, "get service spec for container '%s' from machine database: %v",
|
||||
ctr.ID, err)
|
||||
}
|
||||
|
||||
return &pb.ServiceContainer{
|
||||
Container: ctrBytes,
|
||||
ServiceSpec: specBytes,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListServiceContainers returns all containers that belong to the service with the given name or ID.
|
||||
// If req.ServiceId is empty, all service containers are returned.
|
||||
func (s *Server) ListServiceContainers(
|
||||
ctx context.Context, req *pb.ListServiceContainersRequest,
|
||||
) (*pb.ListServiceContainersResponse, error) {
|
||||
var opts container.ListOptions
|
||||
if len(req.Options) > 0 {
|
||||
if err := json.Unmarshal(req.Options, &opts); err != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "unmarshal options: %v", err)
|
||||
}
|
||||
|
||||
// Handle filters separately because they implement custom JSON unmarshalling.
|
||||
var raw map[string]json.RawMessage
|
||||
if err := json.Unmarshal(req.Options, &raw); err != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "unmarshal options to raw map: %v", err)
|
||||
}
|
||||
|
||||
if filtersBytes, ok := raw["Filters"]; ok {
|
||||
args, err := filters.FromJSON(string(filtersBytes))
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "unmarshal filters: %v", err)
|
||||
}
|
||||
opts.Filters = args
|
||||
} else {
|
||||
opts.Filters = filters.NewArgs()
|
||||
}
|
||||
}
|
||||
// Only uncloud-managed containers that belong to some service.
|
||||
opts.Filters.Add("label", api.LabelServiceID)
|
||||
opts.Filters.Add("label", api.LabelManaged)
|
||||
|
||||
containerSummaries, err := s.client.ContainerList(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
containers := make([]*pb.ServiceContainer, 0, len(containerSummaries))
|
||||
for _, cs := range containerSummaries {
|
||||
if req.ServiceId != "" &&
|
||||
cs.Labels[api.LabelServiceID] != req.ServiceId && cs.Labels[api.LabelServiceName] != req.ServiceId {
|
||||
continue
|
||||
}
|
||||
|
||||
ctr, err := s.client.ContainerInspect(ctx, cs.ID)
|
||||
if err != nil {
|
||||
if client.IsErrNotFound(err) {
|
||||
// The listed container may have been removed while we were inspecting other containers.
|
||||
continue
|
||||
}
|
||||
return nil, status.Errorf(codes.Internal, "inspect container %s: %v", cs.ID, err)
|
||||
}
|
||||
ctrBytes, err := json.Marshal(ctr)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "marshal container: %v", err)
|
||||
}
|
||||
|
||||
var specBytes []byte
|
||||
err = s.db.QueryRowContext(ctx, `SELECT service_spec FROM containers WHERE id = $1`, ctr.ID).Scan(&specBytes)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
// If this happens, there is a bug in the code, or someone manually removed the container from the DB,
|
||||
// or created a managed container out of band.
|
||||
slog.Error("Service container not found in machine database.", "id", ctr.ID)
|
||||
// Just ignore such a container to not fail the list operation as it's not easily recoverable.
|
||||
continue
|
||||
}
|
||||
return nil, status.Errorf(codes.Internal, "get service spec for container '%s' from machine database: %v",
|
||||
ctr.ID, err)
|
||||
}
|
||||
|
||||
containers = append(containers, &pb.ServiceContainer{
|
||||
Container: ctrBytes,
|
||||
ServiceSpec: specBytes,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.ListServiceContainersResponse{
|
||||
Messages: []*pb.MachineServiceContainers{
|
||||
{
|
||||
Containers: containers,
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// RemoveServiceContainer stops (kills after grace period) and removes a service container with the given ID.
|
||||
// The difference between this method and RemoveContainer is that it also removes the container from the machine
|
||||
// database.
|
||||
|
||||
Reference in New Issue
Block a user