mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
update network peers when new machine added
This commit is contained in:
@@ -7,20 +7,26 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"log/slog"
|
||||
"net/netip"
|
||||
"uncloud/internal/machine/api/pb"
|
||||
"uncloud/internal/machine/network"
|
||||
"uncloud/internal/secret"
|
||||
)
|
||||
|
||||
type Cluster struct {
|
||||
pb.UnimplementedClusterServer
|
||||
|
||||
state *State
|
||||
|
||||
// TODO: temporary channel until the state is replaced with networkDB.
|
||||
newMachinesCh chan *pb.MachineInfo
|
||||
}
|
||||
|
||||
func NewCluster(state *State) *Cluster {
|
||||
return &Cluster{
|
||||
state: state,
|
||||
state: state,
|
||||
newMachinesCh: make(chan *pb.MachineInfo),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,15 +60,22 @@ func (c *Cluster) SetNetwork(network *pb.IPPrefix) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: this is a temporary watcher for PoC until the state is state is replaced with networkDB.
|
||||
func (c *Cluster) WatchNewMachines() <-chan *pb.MachineInfo {
|
||||
return c.newMachinesCh
|
||||
}
|
||||
|
||||
// AddMachine adds a machine to the cluster.
|
||||
func (c *Cluster) AddMachine(ctx context.Context, req *pb.AddMachineRequest) (*pb.AddMachineResponse, error) {
|
||||
if c.state == nil {
|
||||
return nil, status.Error(codes.FailedPrecondition, "cluster is not initialized")
|
||||
}
|
||||
|
||||
// TODO: replace errors with gRPC status.Error(f), e.g. status.Error(codes.InvalidArgument, "management IP not set")
|
||||
if req.Network.PublicKey == nil {
|
||||
return nil, fmt.Errorf("public key not set")
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(req.Network.Endpoints) == 0 {
|
||||
return nil, status.Error(codes.InvalidArgument, "endpoints not set")
|
||||
}
|
||||
|
||||
machines := c.state.State.Machines
|
||||
@@ -71,13 +84,16 @@ func (c *Cluster) AddMachine(ctx context.Context, req *pb.AddMachineRequest) (*p
|
||||
i := 0
|
||||
for _, m := range machines {
|
||||
if req.Name != "" && m.Name == req.Name {
|
||||
return nil, fmt.Errorf("machine with name %q already exists", req.Name)
|
||||
return nil, status.Errorf(codes.AlreadyExists, "machine with name %q already exists", req.Name)
|
||||
}
|
||||
if m.Network.ManagementIp != nil && m.Network.ManagementIp.Equal(req.Network.ManagementIp) {
|
||||
return nil, fmt.Errorf("machine with management IP %q already exists", req.Network.ManagementIp)
|
||||
if m.Network.ManagementIp != nil && req.Network.ManagementIp != nil &&
|
||||
m.Network.ManagementIp.Equal(req.Network.ManagementIp) {
|
||||
manageIP, _ := req.Network.ManagementIp.ToAddr()
|
||||
return nil, status.Errorf(codes.AlreadyExists, "machine with management IP %q already exists", manageIP)
|
||||
}
|
||||
if bytes.Equal(m.Network.PublicKey, req.Network.PublicKey) {
|
||||
return nil, fmt.Errorf("machine with public key %q already exists", req.Network.PublicKey)
|
||||
publicKey := secret.Secret(m.Network.PublicKey)
|
||||
return nil, status.Errorf(codes.AlreadyExists, "machine with public key %q already exists", publicKey)
|
||||
}
|
||||
|
||||
if allocatedSubnets[i], err = m.Network.Subnet.ToPrefix(); err != nil {
|
||||
@@ -88,7 +104,7 @@ func (c *Cluster) AddMachine(ctx context.Context, req *pb.AddMachineRequest) (*p
|
||||
|
||||
mid, err := NewMachineID()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("generate machine ID: %w", err)
|
||||
return nil, status.Errorf(codes.Internal, "generate machine ID: %v", err)
|
||||
}
|
||||
manageIP := req.Network.ManagementIp
|
||||
if manageIP == nil {
|
||||
@@ -105,39 +121,44 @@ func (c *Cluster) AddMachine(ctx context.Context, req *pb.AddMachineRequest) (*p
|
||||
if m.Name == "" {
|
||||
m.Name, err = NewRandomMachineName()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("generate machine name: %w", err)
|
||||
return nil, status.Errorf(codes.Internal, "generate machine name: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
clusterNetwork, err := c.Network()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get cluster network: %w", err)
|
||||
return nil, status.Errorf(codes.Internal, "get cluster network: %v", err)
|
||||
}
|
||||
ipam, err := NewIPAMWithAllocated(clusterNetwork, allocatedSubnets)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create IPAM manager: %w", err)
|
||||
return nil, status.Errorf(codes.Internal, "create IPAM manager: %v", err)
|
||||
}
|
||||
subnet, err := ipam.AllocateSubnetLen(network.DefaultSubnetBits)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("allocate subnet: %w", err)
|
||||
return nil, status.Errorf(codes.Internal, "allocate subnet for machine: %v", err)
|
||||
}
|
||||
m.Network.Subnet = pb.NewIPPrefix(subnet)
|
||||
|
||||
// TODO: announce the new machine to the cluster network using Serf and achieve consensus.
|
||||
mState := proto.Clone(m).(*pb.MachineInfo)
|
||||
c.state.State.Machines[mState.Id] = mState
|
||||
mCopy := proto.Clone(m).(*pb.MachineInfo)
|
||||
c.state.State.Machines[mCopy.Id] = mCopy
|
||||
// Store machine endpoints in a separate collection to allow modifying them with limited permissions.
|
||||
c.state.State.Endpoints[mState.Id] = &pb.MachineEndpoints{
|
||||
Id: mState.Id,
|
||||
c.state.State.Endpoints[mCopy.Id] = &pb.MachineEndpoints{
|
||||
Id: mCopy.Id,
|
||||
Endpoints: req.Network.Endpoints,
|
||||
}
|
||||
if err = c.state.Save(); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "save state: %v", err)
|
||||
}
|
||||
slog.Info("Machine added to the cluster.", "id", m.Id, "name", m.Name)
|
||||
|
||||
// Include the machine endpoints in the response.
|
||||
m.Network.Endpoints = req.Network.Endpoints
|
||||
|
||||
// TODO: notify all cluster machines about the new machine so they can update their peers config.
|
||||
// In PoC we just notify the local machine.
|
||||
c.newMachinesCh <- m
|
||||
|
||||
resp := &pb.AddMachineResponse{Machine: m}
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user