mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
refactor: api.Container to use the detailed ContainerJSON struct instead of Container (Summary)
This commit is contained in:
@@ -150,7 +150,7 @@ func (c *Client) StopContainer(ctx context.Context, id string, opts container.St
|
||||
|
||||
type MachineContainers struct {
|
||||
Metadata *pb.Metadata
|
||||
Containers []types.Container
|
||||
Containers []types.ContainerJSON
|
||||
}
|
||||
|
||||
func (c *Client) ListContainers(ctx context.Context, opts container.ListOptions) ([]MachineContainers, error) {
|
||||
|
||||
@@ -149,8 +149,9 @@ func (m *Manager) syncContainersToStore(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("list containers from store: %w", err)
|
||||
}
|
||||
|
||||
// List only Uncloud service containers identified by their labels.
|
||||
containers, err := m.client.ContainerList(ctx, dockercontainer.ListOptions{
|
||||
containerSummaries, err := m.client.ContainerList(ctx, dockercontainer.ListOptions{
|
||||
Filters: filters.NewArgs(
|
||||
filters.Arg("label", api.LabelServiceID),
|
||||
filters.Arg("label", api.LabelServiceName),
|
||||
@@ -161,11 +162,21 @@ func (m *Manager) syncContainersToStore(ctx context.Context) error {
|
||||
return fmt.Errorf("list Docker containers: %w", err)
|
||||
}
|
||||
|
||||
// Delete containers that are not present in the Docker daemon from the store.
|
||||
// Inspect each container to get the full container details.
|
||||
containers := make([]api.Container, len(containerSummaries))
|
||||
for i, cs := range containerSummaries {
|
||||
ctr, err := m.client.ContainerInspect(ctx, cs.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("inspect container '%s': %w", cs.ID, err)
|
||||
}
|
||||
containers[i] = api.Container{ContainerJSON: ctr}
|
||||
}
|
||||
|
||||
// Delete containers from the store that are no longer present in the Docker daemon.
|
||||
var deleteIDs []string
|
||||
for _, sc := range storeContainers {
|
||||
found := false
|
||||
for i, _ := range containers {
|
||||
for i := range containers {
|
||||
if containers[i].ID == sc.Container.ID {
|
||||
found = true
|
||||
break
|
||||
@@ -184,8 +195,7 @@ func (m *Manager) syncContainersToStore(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// Create or update the current Docker containers in the store.
|
||||
for _, dc := range containers {
|
||||
c := &api.Container{Container: dc}
|
||||
for _, c := range containers {
|
||||
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))
|
||||
}
|
||||
|
||||
@@ -9,6 +9,6 @@ import (
|
||||
)
|
||||
|
||||
// EnsureUncloudNetwork is a stub for darwin.
|
||||
func (d *Manager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix) error {
|
||||
func (m *Manager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix) error {
|
||||
return fmt.Errorf("not supported on darwin")
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@ import (
|
||||
// EnsureUncloudNetwork creates the Docker bridge network NetworkName with the provided machine subnet
|
||||
// if it doesn't exist. If the network exists but has a different subnet, it removes and recreates the network.
|
||||
// It also configures iptables to allow container access from the WireGuard network.
|
||||
func (d *Manager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix) error {
|
||||
func (m *Manager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix) error {
|
||||
// Ensure the Docker network 'uncloud' is created with the correct subnet.
|
||||
needsCreation := false
|
||||
nw, err := d.client.NetworkInspect(ctx, NetworkName, dnetwork.InspectOptions{})
|
||||
nw, err := m.client.NetworkInspect(ctx, NetworkName, dnetwork.InspectOptions{})
|
||||
if err != nil {
|
||||
if !client.IsErrNotFound(err) {
|
||||
return fmt.Errorf("inspect Docker network %q: %w", NetworkName, err)
|
||||
@@ -29,7 +29,7 @@ func (d *Manager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix)
|
||||
slog.Info(
|
||||
"Removing Docker network with old subnet.", "name", NetworkName, "subnet", nw.IPAM.Config[0].Subnet,
|
||||
)
|
||||
if err = d.client.NetworkRemove(ctx, NetworkName); err != nil {
|
||||
if err = m.client.NetworkRemove(ctx, NetworkName); err != nil {
|
||||
// It can still fail if the network is in use by a container. Leave it to the user to resolve the issue.
|
||||
return fmt.Errorf("remove Docker network %q: %w", NetworkName, err)
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func (d *Manager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix)
|
||||
}
|
||||
|
||||
if needsCreation {
|
||||
if _, err = d.client.NetworkCreate(
|
||||
if _, err = m.client.NetworkCreate(
|
||||
ctx, NetworkName, dnetwork.CreateOptions{
|
||||
Driver: "bridge",
|
||||
Scope: "local",
|
||||
@@ -54,7 +54,7 @@ func (d *Manager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix)
|
||||
}
|
||||
slog.Info("Docker network created.", "name", NetworkName, "subnet", subnet.String())
|
||||
|
||||
if nw, err = d.client.NetworkInspect(ctx, NetworkName, dnetwork.InspectOptions{}); err != nil {
|
||||
if nw, err = m.client.NetworkInspect(ctx, NetworkName, dnetwork.InspectOptions{}); err != nil {
|
||||
return fmt.Errorf("inspect Docker network %q: %w", NetworkName, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/image"
|
||||
@@ -144,10 +145,18 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque
|
||||
}
|
||||
}
|
||||
|
||||
containers, err := s.client.ContainerList(ctx, opts)
|
||||
containerSummaries, err := s.client.ContainerList(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
}
|
||||
containers := make([]types.ContainerJSON, len(containerSummaries))
|
||||
for i, cs := range containerSummaries {
|
||||
c, err := s.client.ContainerInspect(ctx, cs.ID)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "inspect container %s: %v", cs.ID, err)
|
||||
}
|
||||
containers[i] = c
|
||||
}
|
||||
|
||||
containersBytes, err := json.Marshal(containers)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user