mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
add service inspect command, introduce squirrel for sql query building
This commit is contained in:
@@ -10,8 +10,8 @@ import (
|
||||
"github.com/docker/docker/client"
|
||||
"log/slog"
|
||||
"time"
|
||||
"uncloud/internal/docker"
|
||||
"uncloud/internal/machine/store"
|
||||
"uncloud/internal/service"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -152,8 +152,8 @@ func (m *Manager) syncContainersToStore(ctx context.Context) error {
|
||||
// List only Uncloud service containers identified by their labels.
|
||||
containers, err := m.client.ContainerList(ctx, dockercontainer.ListOptions{
|
||||
Filters: filters.NewArgs(
|
||||
filters.Arg("label", docker.LabelServiceID),
|
||||
filters.Arg("label", docker.LabelServiceName),
|
||||
filters.Arg("label", service.LabelServiceID),
|
||||
filters.Arg("label", service.LabelServiceName),
|
||||
),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -185,7 +185,7 @@ func (m *Manager) syncContainersToStore(ctx context.Context) error {
|
||||
|
||||
// Create or update the current Docker containers in the store.
|
||||
for _, dc := range containers {
|
||||
c := &docker.Container{Container: dc}
|
||||
c := &service.Container{Container: dc}
|
||||
if err = m.store.CreateOrUpdateContainer(ctx, c, m.machineID); err != nil {
|
||||
storeErr = errors.Join(storeErr, fmt.Errorf("create or update container %q: %w", c.ID, err))
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package machine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/docker/docker/client"
|
||||
@@ -686,6 +687,7 @@ func (m *Machine) Inspect(_ context.Context, _ *emptypb.Empty) (*pb.MachineInfo,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// InspectService returns detailed information about a service and its containers.
|
||||
func (m *Machine) InspectService(
|
||||
ctx context.Context, req *pb.InspectServiceRequest,
|
||||
) (*pb.InspectServiceResponse, error) {
|
||||
@@ -698,11 +700,26 @@ func (m *Machine) InspectService(
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "list containers: %v", err)
|
||||
}
|
||||
if len(records) == 0 {
|
||||
return nil, status.Error(codes.NotFound, "service not found")
|
||||
}
|
||||
|
||||
fmt.Println("## records: ", records)
|
||||
return &pb.InspectServiceResponse{
|
||||
Service: &pb.Service{
|
||||
Id: req.Id,
|
||||
},
|
||||
}, nil
|
||||
containers := make([]*pb.Service_Container, len(records))
|
||||
for i, r := range records {
|
||||
containerJSON, err := json.Marshal(r.Container)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "marshal container: %v", err)
|
||||
}
|
||||
containers[i] = &pb.Service_Container{
|
||||
MachineId: r.MachineID,
|
||||
Container: containerJSON,
|
||||
}
|
||||
}
|
||||
|
||||
svc := &pb.Service{
|
||||
Id: records[0].Container.ServiceID(),
|
||||
Name: records[0].Container.ServiceName(),
|
||||
Containers: containers,
|
||||
}
|
||||
return &pb.InspectServiceResponse{Service: svc}, nil
|
||||
}
|
||||
|
||||
@@ -4,10 +4,11 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
"uncloud/internal/docker"
|
||||
"uncloud/internal/service"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -21,7 +22,7 @@ const (
|
||||
)
|
||||
|
||||
type ContainerRecord struct {
|
||||
Container *docker.Container
|
||||
Container *service.Container
|
||||
MachineID string
|
||||
SyncStatus string
|
||||
UpdatedAt time.Time
|
||||
@@ -46,7 +47,7 @@ type DeleteOptions struct {
|
||||
|
||||
// CreateOrUpdateContainer creates a new container record or updates an existing one in the store database.
|
||||
// The container is associated with the given machine ID that indicates which machine the container is running on.
|
||||
func (s *Store) CreateOrUpdateContainer(ctx context.Context, c *docker.Container, machineID string) error {
|
||||
func (s *Store) CreateOrUpdateContainer(ctx context.Context, c *service.Container, machineID string) error {
|
||||
cJSON, err := json.Marshal(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal container: %w", err)
|
||||
@@ -75,34 +76,27 @@ func (s *Store) CreateOrUpdateContainer(ctx context.Context, c *docker.Container
|
||||
|
||||
// ListContainers returns a list of container records from the store database that match the given options.
|
||||
func (s *Store) ListContainers(ctx context.Context, opts ListOptions) ([]*ContainerRecord, error) {
|
||||
query := "SELECT container, machine_id, sync_status, updated_at FROM containers"
|
||||
var args []any
|
||||
q := sq.Select("container", "machine_id", "sync_status", "updated_at").From("containers")
|
||||
|
||||
var whereConditions []string
|
||||
if len(opts.MachineIDs) > 0 {
|
||||
whereConditions = append(whereConditions, "machine_id IN (?"+strings.Repeat(", ?", len(opts.MachineIDs)-1)+")")
|
||||
args = make([]any, len(opts.MachineIDs))
|
||||
for i, id := range opts.MachineIDs {
|
||||
args[i] = id
|
||||
q = q.Where(sq.Eq{"machine_id": opts.MachineIDs})
|
||||
}
|
||||
|
||||
if opts.ServiceIDOrName.ID != "" || opts.ServiceIDOrName.Name != "" {
|
||||
var conditions []sq.Sqlizer
|
||||
if opts.ServiceIDOrName.ID != "" {
|
||||
conditions = append(conditions, sq.Eq{"service_id": opts.ServiceIDOrName.ID})
|
||||
}
|
||||
if opts.ServiceIDOrName.Name != "" {
|
||||
conditions = append(conditions, sq.Eq{"service_name": opts.ServiceIDOrName.Name})
|
||||
}
|
||||
q = q.Where(sq.Or(conditions))
|
||||
}
|
||||
|
||||
var serviceConditions []string
|
||||
var serviceArgs []any
|
||||
if opts.ServiceIDOrName.ID != "" {
|
||||
serviceConditions = append(serviceConditions, "service_id = ?")
|
||||
serviceArgs = append(serviceArgs, opts.ServiceIDOrName.ID)
|
||||
query, args, err := q.ToSql()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build query: %w", err)
|
||||
}
|
||||
if opts.ServiceIDOrName.Name != "" {
|
||||
serviceConditions = append(serviceConditions, "service_name = ?")
|
||||
serviceArgs = append(serviceArgs, opts.ServiceIDOrName.Name)
|
||||
}
|
||||
if len(serviceConditions) > 0 {
|
||||
whereConditions = append(whereConditions, "("+strings.Join(serviceConditions, " OR ")+")")
|
||||
args = append(args, serviceArgs...)
|
||||
}
|
||||
|
||||
query += " WHERE " + strings.Join(whereConditions, " AND ")
|
||||
|
||||
rows, err := s.corro.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
@@ -119,7 +113,7 @@ func (s *Store) ListContainers(ctx context.Context, opts ListOptions) ([]*Contai
|
||||
return nil, fmt.Errorf("scan container record: %w", err)
|
||||
}
|
||||
|
||||
var c docker.Container
|
||||
var c service.Container
|
||||
if err = json.Unmarshal([]byte(cJSON), &c); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal container: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user