mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
move Docker manager to docker package
This commit is contained in:
@@ -11,8 +11,8 @@ import (
|
|||||||
"google.golang.org/protobuf/types/known/emptypb"
|
"google.golang.org/protobuf/types/known/emptypb"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
machinecore "uncloud/internal/machine"
|
|
||||||
"uncloud/internal/machine/api/pb"
|
"uncloud/internal/machine/api/pb"
|
||||||
|
"uncloud/internal/machine/docker"
|
||||||
"uncloud/internal/secret"
|
"uncloud/internal/secret"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ func (c *Client) RunService(ctx context.Context, opts *ServiceOptions) (RunServi
|
|||||||
}
|
}
|
||||||
netConfig := &network.NetworkingConfig{
|
netConfig := &network.NetworkingConfig{
|
||||||
EndpointsConfig: map[string]*network.EndpointSettings{
|
EndpointsConfig: map[string]*network.EndpointSettings{
|
||||||
machinecore.DockerNetworkName: {},
|
docker.NetworkName: {},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// TODO: pull image if it doesn't exist on the machine.
|
// TODO: pull image if it doesn't exist on the machine.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package machine
|
package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -10,24 +10,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DockerNetworkName = "uncloud"
|
NetworkName = "uncloud"
|
||||||
DockerUserChain = "DOCKER-USER"
|
UserChain = "DOCKER-USER"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DockerManager struct {
|
type Manager struct {
|
||||||
client *client.Client
|
client *client.Client
|
||||||
store *store.Store
|
store *store.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerManager(client *client.Client, store *store.Store) *DockerManager {
|
func NewManager(client *client.Client, store *store.Store) *Manager {
|
||||||
return &DockerManager{
|
return &Manager{
|
||||||
client: client,
|
client: client,
|
||||||
store: store,
|
store: store,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitDaemonReady waits for the Docker daemon to start and be ready to serve requests.
|
// WaitDaemonReady waits for the Docker daemon to start and be ready to serve requests.
|
||||||
func (d *DockerManager) WaitDaemonReady(ctx context.Context) error {
|
func (d *Manager) WaitDaemonReady(ctx context.Context) error {
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
//go:build darwin
|
//go:build darwin
|
||||||
|
|
||||||
package machine
|
package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -9,6 +9,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// EnsureUncloudNetwork is a stub for darwin.
|
// EnsureUncloudNetwork is a stub for darwin.
|
||||||
func (d *DockerManager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix) error {
|
func (d *Manager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix) error {
|
||||||
return fmt.Errorf("not supported on darwin")
|
return fmt.Errorf("not supported on darwin")
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package machine
|
package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -11,34 +11,34 @@ import (
|
|||||||
"uncloud/internal/machine/network"
|
"uncloud/internal/machine/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EnsureUncloudNetwork creates the Docker bridge network DockerNetworkName with the provided machine subnet
|
// 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.
|
// 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.
|
// It also configures iptables to allow container access from the WireGuard network.
|
||||||
func (d *DockerManager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix) error {
|
func (d *Manager) EnsureUncloudNetwork(ctx context.Context, subnet netip.Prefix) error {
|
||||||
// Ensure the Docker network 'uncloud' is created with the correct subnet.
|
// Ensure the Docker network 'uncloud' is created with the correct subnet.
|
||||||
needsCreation := false
|
needsCreation := false
|
||||||
nw, err := d.client.NetworkInspect(ctx, DockerNetworkName, dnetwork.InspectOptions{})
|
nw, err := d.client.NetworkInspect(ctx, NetworkName, dnetwork.InspectOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !client.IsErrNotFound(err) {
|
if !client.IsErrNotFound(err) {
|
||||||
return fmt.Errorf("inspect Docker network %q: %w", DockerNetworkName, err)
|
return fmt.Errorf("inspect Docker network %q: %w", NetworkName, err)
|
||||||
}
|
}
|
||||||
needsCreation = true
|
needsCreation = true
|
||||||
} else if nw.IPAM.Config[0].Subnet != subnet.String() {
|
} else if nw.IPAM.Config[0].Subnet != subnet.String() {
|
||||||
// Remove the Docker network if the subnet is different.
|
// Remove the Docker network if the subnet is different.
|
||||||
// It could be a leftover from a previous incomplete cleanup.
|
// It could be a leftover from a previous incomplete cleanup.
|
||||||
slog.Info(
|
slog.Info(
|
||||||
"Removing Docker network with old subnet.", "name", DockerNetworkName, "subnet", nw.IPAM.Config[0].Subnet,
|
"Removing Docker network with old subnet.", "name", NetworkName, "subnet", nw.IPAM.Config[0].Subnet,
|
||||||
)
|
)
|
||||||
if err = d.client.NetworkRemove(ctx, DockerNetworkName); err != nil {
|
if err = d.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.
|
// 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", DockerNetworkName, err)
|
return fmt.Errorf("remove Docker network %q: %w", NetworkName, err)
|
||||||
}
|
}
|
||||||
needsCreation = true
|
needsCreation = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if needsCreation {
|
if needsCreation {
|
||||||
if _, err = d.client.NetworkCreate(
|
if _, err = d.client.NetworkCreate(
|
||||||
ctx, DockerNetworkName, dnetwork.CreateOptions{
|
ctx, NetworkName, dnetwork.CreateOptions{
|
||||||
Driver: "bridge",
|
Driver: "bridge",
|
||||||
Scope: "local",
|
Scope: "local",
|
||||||
IPAM: &dnetwork.IPAM{
|
IPAM: &dnetwork.IPAM{
|
||||||
@@ -50,12 +50,12 @@ func (d *DockerManager) EnsureUncloudNetwork(ctx context.Context, subnet netip.P
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return fmt.Errorf("create Docker network %q: %w", DockerNetworkName, err)
|
return fmt.Errorf("create Docker network %q: %w", NetworkName, err)
|
||||||
}
|
}
|
||||||
slog.Info("Docker network created.", "name", DockerNetworkName, "subnet", subnet.String())
|
slog.Info("Docker network created.", "name", NetworkName, "subnet", subnet.String())
|
||||||
|
|
||||||
if nw, err = d.client.NetworkInspect(ctx, DockerNetworkName, dnetwork.InspectOptions{}); err != nil {
|
if nw, err = d.client.NetworkInspect(ctx, NetworkName, dnetwork.InspectOptions{}); err != nil {
|
||||||
return fmt.Errorf("inspect Docker network %q: %w", DockerNetworkName, err)
|
return fmt.Errorf("inspect Docker network %q: %w", NetworkName, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ func (d *DockerManager) EnsureUncloudNetwork(ctx context.Context, subnet netip.P
|
|||||||
bridgeName := "br-" + nw.ID[:12]
|
bridgeName := "br-" + nw.ID[:12]
|
||||||
ipt := iptables.GetIptable(iptables.IPv4)
|
ipt := iptables.GetIptable(iptables.IPv4)
|
||||||
rule := []string{"--in-interface", network.WireGuardInterfaceName, "--out-interface", bridgeName, "-j", "ACCEPT"}
|
rule := []string{"--in-interface", network.WireGuardInterfaceName, "--out-interface", bridgeName, "-j", "ACCEPT"}
|
||||||
if err = ipt.ProgramRule(iptables.Filter, DockerUserChain, iptables.Insert, rule); err != nil {
|
if err = ipt.ProgramRule(iptables.Filter, UserChain, iptables.Insert, rule); err != nil {
|
||||||
return fmt.Errorf("insert iptables rule: %w", err)
|
return fmt.Errorf("insert iptables rule: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
"uncloud/internal/machine/api/pb"
|
"uncloud/internal/machine/api/pb"
|
||||||
"uncloud/internal/machine/corroservice"
|
"uncloud/internal/machine/corroservice"
|
||||||
|
"uncloud/internal/machine/docker"
|
||||||
"uncloud/internal/machine/network"
|
"uncloud/internal/machine/network"
|
||||||
"uncloud/internal/machine/store"
|
"uncloud/internal/machine/store"
|
||||||
)
|
)
|
||||||
@@ -106,12 +107,12 @@ func (nc *networkController) Run(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
docker := NewDockerManager(cli, nc.store)
|
manager := docker.NewManager(cli, nc.store)
|
||||||
if err := docker.WaitDaemonReady(ctx); err != nil {
|
if err := manager.WaitDaemonReady(ctx); err != nil {
|
||||||
return fmt.Errorf("wait for Docker daemon: %w", err)
|
return fmt.Errorf("wait for Docker daemon: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := docker.EnsureUncloudNetwork(ctx, nc.state.Network.Subnet); err != nil {
|
if err := manager.EnsureUncloudNetwork(ctx, nc.state.Network.Subnet); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
slog.Info("Docker network configured.")
|
slog.Info("Docker network configured.")
|
||||||
|
|||||||
Reference in New Issue
Block a user