move generic docker types to top level docker package

This commit is contained in:
Pavel Sviderski
2024-11-23 15:35:26 +10:00
parent 6d39174b51
commit 5c1a3dd694
6 changed files with 24 additions and 18 deletions
+8 -8
View File
@@ -5,15 +5,15 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/distribution/reference" "github.com/distribution/reference"
dockercontainer "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/emptypb"
"slices" "slices"
"strings" "strings"
"uncloud/internal/docker"
"uncloud/internal/machine/api/pb" "uncloud/internal/machine/api/pb"
"uncloud/internal/machine/docker" machinedocker "uncloud/internal/machine/docker"
"uncloud/internal/machine/docker/container"
"uncloud/internal/secret" "uncloud/internal/secret"
) )
@@ -100,16 +100,16 @@ func (c *Client) RunService(ctx context.Context, opts *ServiceOptions) (RunServi
} }
containerName := fmt.Sprintf("%s-%s", serviceName, suffix) containerName := fmt.Sprintf("%s-%s", serviceName, suffix)
config := &dockercontainer.Config{ config := &container.Config{
Image: opts.Image, Image: opts.Image,
Labels: map[string]string{ Labels: map[string]string{
container.LabelServiceID: serviceID, docker.LabelServiceID: serviceID,
container.LabelServiceName: serviceName, docker.LabelServiceName: serviceName,
}, },
} }
netConfig := &network.NetworkingConfig{ netConfig := &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{ EndpointsConfig: map[string]*network.EndpointSettings{
docker.NetworkName: {}, machinedocker.NetworkName: {},
}, },
} }
// TODO: pull image if it doesn't exist on the machine. // TODO: pull image if it doesn't exist on the machine.
@@ -117,7 +117,7 @@ func (c *Client) RunService(ctx context.Context, opts *ServiceOptions) (RunServi
if err != nil { if err != nil {
return resp, fmt.Errorf("create container: %w", err) return resp, fmt.Errorf("create container: %w", err)
} }
if err = c.StartContainer(ctx, createResp.ID, dockercontainer.StartOptions{}); err != nil { if err = c.StartContainer(ctx, createResp.ID, container.StartOptions{}); err != nil {
return resp, fmt.Errorf("start container: %w", err) return resp, fmt.Errorf("start container: %w", err)
} }
@@ -1,4 +1,4 @@
package container package docker
import ( import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
@@ -1,4 +1,4 @@
package container package docker
import ( import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
+6
View File
@@ -0,0 +1,6 @@
package docker
const (
DeployModeReplicated = "replicated"
DeployModeGlobal = "global"
)
+4 -4
View File
@@ -10,7 +10,7 @@ import (
"github.com/docker/docker/client" "github.com/docker/docker/client"
"log/slog" "log/slog"
"time" "time"
"uncloud/internal/machine/docker/container" "uncloud/internal/docker"
"uncloud/internal/machine/store" "uncloud/internal/machine/store"
) )
@@ -152,8 +152,8 @@ func (m *Manager) syncContainersToStore(ctx context.Context) error {
// List only Uncloud service containers identified by their labels. // List only Uncloud service containers identified by their labels.
containers, err := m.client.ContainerList(ctx, dockercontainer.ListOptions{ containers, err := m.client.ContainerList(ctx, dockercontainer.ListOptions{
Filters: filters.NewArgs( Filters: filters.NewArgs(
filters.Arg("label", container.LabelServiceID), filters.Arg("label", docker.LabelServiceID),
filters.Arg("label", container.LabelServiceName), filters.Arg("label", docker.LabelServiceName),
), ),
}) })
if err != nil { 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. // Create or update the current Docker containers in the store.
for _, dc := range containers { for _, dc := range containers {
c := &container.Container{Container: dc} c := &docker.Container{Container: dc}
if err = m.store.CreateOrUpdateContainer(ctx, c, m.machineID); err != nil { 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)) storeErr = errors.Join(storeErr, fmt.Errorf("create or update container %q: %w", c.ID, err))
} }
+4 -4
View File
@@ -7,7 +7,7 @@ import (
"log/slog" "log/slog"
"strings" "strings"
"time" "time"
"uncloud/internal/machine/docker/container" "uncloud/internal/docker"
) )
const ( const (
@@ -21,7 +21,7 @@ const (
) )
type ContainerRecord struct { type ContainerRecord struct {
Container *container.Container Container *docker.Container
MachineID string MachineID string
SyncStatus string SyncStatus string
UpdatedAt time.Time UpdatedAt time.Time
@@ -38,7 +38,7 @@ type DeleteOptions struct {
// CreateOrUpdateContainer creates a new container record or updates an existing one in the store database. // 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. // 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 *container.Container, machineID string) error { func (s *Store) CreateOrUpdateContainer(ctx context.Context, c *docker.Container, machineID string) error {
cJSON, err := json.Marshal(c) cJSON, err := json.Marshal(c)
if err != nil { if err != nil {
return fmt.Errorf("marshal container: %w", err) return fmt.Errorf("marshal container: %w", err)
@@ -93,7 +93,7 @@ func (s *Store) ListContainers(ctx context.Context, opts ListOptions) ([]*Contai
return nil, fmt.Errorf("scan container record: %w", err) return nil, fmt.Errorf("scan container record: %w", err)
} }
var c container.Container var c docker.Container
if err = json.Unmarshal([]byte(cJSON), &c); err != nil { if err = json.Unmarshal([]byte(cJSON), &c); err != nil {
return nil, fmt.Errorf("unmarshal container: %w", err) return nil, fmt.Errorf("unmarshal container: %w", err)
} }