mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
chore: populate ucind cluster machines with IDs
This commit is contained in:
@@ -32,6 +32,29 @@ type CreateClusterOptions struct {
|
|||||||
Machines int
|
Machines int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Cluster) PopulateMachineIDs(ctx context.Context) error {
|
||||||
|
for i := range c.Machines {
|
||||||
|
if c.Machines[i].ID != "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
cli, err := c.Machines[i].Connect(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connect to machine '%s': %w", c.Machines[i].Name, err)
|
||||||
|
}
|
||||||
|
//goland:noinspection GoDeferInLoop
|
||||||
|
defer cli.Close()
|
||||||
|
|
||||||
|
m, err := cli.Inspect(ctx, &emptypb.Empty{})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("inspect machine '%s': %w", c.Machines[i].Name, err)
|
||||||
|
}
|
||||||
|
c.Machines[i].ID = m.Id
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts CreateClusterOptions) (Cluster, error) {
|
func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts CreateClusterOptions) (Cluster, error) {
|
||||||
var c Cluster
|
var c Cluster
|
||||||
|
|
||||||
@@ -75,6 +98,9 @@ func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts Creat
|
|||||||
if err = p.initCluster(ctx, c.Machines); err != nil {
|
if err = p.initCluster(ctx, c.Machines); err != nil {
|
||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
|
if err = c.PopulateMachineIDs(ctx); err != nil {
|
||||||
|
return c, fmt.Errorf("populate machine IDs: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if p.configUpdater != nil {
|
if p.configUpdater != nil {
|
||||||
if err = p.configUpdater.AddCluster(c); err != nil {
|
if err = p.configUpdater.AddCluster(c); err != nil {
|
||||||
@@ -90,7 +116,7 @@ func (p *Provisioner) initCluster(ctx context.Context, machines []Machine) error
|
|||||||
// Init a new cluster on the first machine.
|
// Init a new cluster on the first machine.
|
||||||
initMachine := machines[0]
|
initMachine := machines[0]
|
||||||
|
|
||||||
if err := p.WaitMachineReady(ctx, initMachine, 30*time.Second); err != nil {
|
if err := WaitMachineReady(ctx, initMachine, 30*time.Second); err != nil {
|
||||||
return fmt.Errorf("wait for machine %q to be ready: %w", initMachine.Name, err)
|
return fmt.Errorf("wait for machine %q to be ready: %w", initMachine.Name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +138,7 @@ func (p *Provisioner) initCluster(ctx context.Context, machines []Machine) error
|
|||||||
|
|
||||||
// Join the rest of the machines to the cluster.
|
// Join the rest of the machines to the cluster.
|
||||||
for _, m := range machines[1:] {
|
for _, m := range machines[1:] {
|
||||||
if err = p.WaitMachineReady(ctx, m, 5*time.Second); err != nil {
|
if err = WaitMachineReady(ctx, m, 5*time.Second); err != nil {
|
||||||
return fmt.Errorf("wait for machine %q to be ready: %w", m.Name, err)
|
return fmt.Errorf("wait for machine %q to be ready: %w", m.Name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,13 +242,17 @@ func (p *Provisioner) InspectCluster(ctx context.Context, name string) (Cluster,
|
|||||||
c.Machines = append(c.Machines, m)
|
c.Machines = append(c.Machines, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = c.PopulateMachineIDs(ctx); err != nil {
|
||||||
|
return c, fmt.Errorf("populate machine IDs: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitClusterReady waits for all machines in the cluster to be ready and UP.
|
// WaitClusterReady waits for all machines in the cluster to be ready and UP.
|
||||||
func (p *Provisioner) WaitClusterReady(ctx context.Context, c Cluster, timeout time.Duration) error {
|
func (p *Provisioner) WaitClusterReady(ctx context.Context, c Cluster, timeout time.Duration) error {
|
||||||
firstMachine := c.Machines[0]
|
firstMachine := c.Machines[0]
|
||||||
if err := p.WaitMachineReady(ctx, firstMachine, timeout); err != nil {
|
if err := WaitMachineReady(ctx, firstMachine, timeout); err != nil {
|
||||||
return fmt.Errorf("wait for machine '%s' to be ready: %w", firstMachine.Name, err)
|
return fmt.Errorf("wait for machine '%s' to be ready: %w", firstMachine.Name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/cenkalti/backoff/v4"
|
"github.com/cenkalti/backoff/v4"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/api/types/image"
|
"github.com/docker/docker/api/types/image"
|
||||||
@@ -13,10 +18,6 @@ import (
|
|||||||
"github.com/psviderski/uncloud/pkg/client"
|
"github.com/psviderski/uncloud/pkg/client"
|
||||||
"github.com/psviderski/uncloud/pkg/client/connector"
|
"github.com/psviderski/uncloud/pkg/client/connector"
|
||||||
"google.golang.org/protobuf/types/known/emptypb"
|
"google.golang.org/protobuf/types/known/emptypb"
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"net/netip"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -29,6 +30,7 @@ const (
|
|||||||
type Machine struct {
|
type Machine struct {
|
||||||
ClusterName string
|
ClusterName string
|
||||||
ContainerName string
|
ContainerName string
|
||||||
|
ID string
|
||||||
Name string
|
Name string
|
||||||
APIAddress netip.AddrPort
|
APIAddress netip.AddrPort
|
||||||
}
|
}
|
||||||
@@ -185,7 +187,7 @@ func randomMachineName() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WaitMachineReady waits for the machine API to respond.
|
// WaitMachineReady waits for the machine API to respond.
|
||||||
func (p *Provisioner) WaitMachineReady(ctx context.Context, m Machine, timeout time.Duration) error {
|
func WaitMachineReady(ctx context.Context, m Machine, timeout time.Duration) error {
|
||||||
cli, err := m.Connect(ctx)
|
cli, err := m.Connect(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("connect to machine over TCP '%s': %w", m.APIAddress, err)
|
return fmt.Errorf("connect to machine over TCP '%s': %w", m.APIAddress, err)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -45,6 +46,9 @@ func createTestCluster(
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, name, c.Name)
|
assert.Equal(t, name, c.Name)
|
||||||
assert.Len(t, c.Machines, opts.Machines)
|
assert.Len(t, c.Machines, opts.Machines)
|
||||||
|
for _, m := range c.Machines {
|
||||||
|
assert.NotEmpty(t, m.ID)
|
||||||
|
}
|
||||||
|
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
require.NoError(t, p.RemoveCluster(ctx, name))
|
require.NoError(t, p.RemoveCluster(ctx, name))
|
||||||
@@ -69,7 +73,7 @@ func TestClusterLifecycle(t *testing.T) {
|
|||||||
// Create a client for each machine and wait for it to be ready.
|
// Create a client for each machine and wait for it to be ready.
|
||||||
clients := make([]*client.Client, len(c.Machines))
|
clients := make([]*client.Client, len(c.Machines))
|
||||||
for i, m := range c.Machines {
|
for i, m := range c.Machines {
|
||||||
require.NoError(t, p.WaitMachineReady(ctx, m, 5*time.Second))
|
require.NoError(t, ucind.WaitMachineReady(ctx, m, 5*time.Second))
|
||||||
clients[i], err = m.Connect(ctx)
|
clients[i], err = m.Connect(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
//goland:noinspection GoDeferInLoop
|
//goland:noinspection GoDeferInLoop
|
||||||
@@ -106,6 +110,18 @@ func TestClusterLifecycle(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("inspect", func(t *testing.T) {
|
||||||
|
cluster, err := p.InspectCluster(ctx, name)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, name, cluster.Name)
|
||||||
|
assert.Len(t, cluster.Machines, 3)
|
||||||
|
for _, m := range cluster.Machines {
|
||||||
|
assert.NotEmpty(t, m.ID)
|
||||||
|
assert.True(t, strings.HasPrefix(m.Name, "machine-"))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("remove", func(t *testing.T) {
|
t.Run("remove", func(t *testing.T) {
|
||||||
err := p.RemoveCluster(ctx, name)
|
err := p.RemoveCluster(ctx, name)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user