ucind: create machines in containers with published API ports

This commit is contained in:
Pavel Sviderski
2024-11-28 16:15:57 +10:00
parent 7aa7a9fea1
commit cd60d5178f
3 changed files with 237 additions and 23 deletions
+52 -18
View File
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
)
@@ -14,20 +16,23 @@ const (
)
type Cluster struct {
Name string
Name string
Machines []Machine
}
type CreateClusterOptions struct {
Machines int
}
func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts CreateClusterOptions) error {
func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts CreateClusterOptions) (Cluster, error) {
var c Cluster
_, err := p.InspectCluster(ctx, name)
if err == nil {
return fmt.Errorf("cluster with name '%s' already exists", name)
return c, fmt.Errorf("cluster with name '%s' already exists", name)
}
if !errors.Is(err, ErrNotFound) {
return fmt.Errorf("inspect cluster '%s': %w", name, err)
return c, fmt.Errorf("inspect cluster '%s': %w", name, err)
}
netOpts := network.CreateOptions{
@@ -36,36 +41,49 @@ func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts Creat
ManagedLabel: "",
},
}
// Docker network name is the same as the cluster name.
// Create a Docker network with the same as the cluster name.
if _, err = p.client.NetworkCreate(ctx, name, netOpts); err != nil {
return fmt.Errorf("create Docker network '%s': %w", name, err)
return c, fmt.Errorf("create Docker network '%s': %w", name, err)
}
c.Name = name
// Create machines (containers) in the created cluster network.
for i := 1; i < opts.Machines+1; i++ {
mopts := CreateMachineOptions{
Name: fmt.Sprintf("machine-%d", i),
}
m, err := p.CreateMachine(ctx, name, mopts)
if err != nil {
return c, fmt.Errorf("create machine '%s': %w", mopts.Name, err)
}
c.Machines = append(c.Machines, m)
}
// TODO: create machines (containers) with the cluster name label.
return nil
return c, nil
}
func (p *Provisioner) InspectCluster(ctx context.Context, name string) (*Cluster, error) {
func (p *Provisioner) InspectCluster(ctx context.Context, name string) (Cluster, error) {
var c Cluster
// Docker network name is the same as the cluster name.
net, err := p.client.NetworkInspect(ctx, name, network.InspectOptions{})
if err != nil {
if client.IsErrNotFound(err) {
return nil, ErrNotFound
return c, ErrNotFound
}
return nil, fmt.Errorf("inspect Docker network '%s': %w", name, err)
return c, fmt.Errorf("inspect Docker network '%s': %w", name, err)
}
if _, ok := net.Labels[ManagedLabel]; !ok {
// The network with the cluster name exists, but it's not managed by ucind.
return nil, ErrNotFound
return c, ErrNotFound
}
c.Name = name
// TODO: list containers (machines) with the cluster name label and include them in the cluster struct.
return &Cluster{
Name: name,
}, nil
return c, nil
}
func (p *Provisioner) RemoveCluster(ctx context.Context, name string) error {
@@ -73,9 +91,25 @@ func (p *Provisioner) RemoveCluster(ctx context.Context, name string) error {
return err
}
// TODO: remove machines (containers) with the cluster name label.
// Remove all containers (machines) with the cluster name label.
opts := container.ListOptions{
All: true,
Filters: filters.NewArgs(
filters.Arg("label", ClusterNameLabel+"="+name),
filters.Arg("label", ManagedLabel),
),
}
containers, err := p.client.ContainerList(ctx, opts)
if err != nil {
return fmt.Errorf("list Docker containers with cluster name '%s': %w", name, err)
}
for _, c := range containers {
if err = p.client.ContainerRemove(ctx, c.ID, container.RemoveOptions{Force: true}); err != nil {
return fmt.Errorf("remove Docker container '%s': %w", c.ID, err)
}
}
if err := p.client.NetworkRemove(ctx, name); err != nil {
if err = p.client.NetworkRemove(ctx, name); err != nil {
return fmt.Errorf("remove Docker network '%s': %w", name, err)
}
return nil