refactor cluster network related constants

This commit is contained in:
Pavel Sviderski
2024-11-28 19:57:26 +10:00
parent 3d5fe0a824
commit 035f44896d
5 changed files with 7 additions and 44 deletions
+2 -2
View File
@@ -6,7 +6,7 @@ import (
"net/netip"
"uncloud/internal/cli"
"uncloud/internal/cli/config"
"uncloud/internal/machine/network"
"uncloud/internal/machine/cluster"
)
type initOptions struct {
@@ -49,7 +49,7 @@ func NewInitCommand() *cobra.Command {
}
cmd.Flags().StringVarP(&opts.name, "name", "n", "", "Assign a name to the machine.")
cmd.Flags().StringVar(
&opts.network, "network", network.DefaultNetwork.String(),
&opts.network, "network", cluster.DefaultNetwork.String(),
"IPv4 network CIDR to use for machines and services.",
)
cmd.Flags().StringVarP(
+1 -1
View File
@@ -160,7 +160,7 @@ func (c *Cluster) AddMachine(ctx context.Context, req *pb.AddMachineRequest) (*p
if err != nil {
return nil, status.Errorf(codes.Internal, "create IPAM manager: %v", err)
}
subnet, err := ipam.AllocateSubnetLen(network.DefaultSubnetBits)
subnet, err := ipam.AllocateSubnetLen(DefaultSubnetBits)
if err != nil {
return nil, status.Errorf(codes.Internal, "allocate subnet for machine: %v", err)
}
+4
View File
@@ -7,6 +7,10 @@ import (
"net/netip"
)
const DefaultSubnetBits = 24
var DefaultNetwork = netip.MustParsePrefix("10.210.0.0/16")
// IPAM is an in-memory IP address manager for allocating and releasing subnets for machines from a cluster network.
type IPAM struct {
network netip.Prefix
-5
View File
@@ -8,11 +8,6 @@ import (
"uncloud/internal/secret"
)
var (
DefaultNetwork = netip.MustParsePrefix("10.210.0.0/16")
DefaultSubnetBits = 24
)
type Config struct {
// Subnet is the IPv4 address range allocated to the machine. The machine's IP address is the first address
// in the subnet. Other IP addresses are allocated to containers running on the machine.
-36
View File
@@ -3,11 +3,9 @@ package machine
import (
"encoding/json"
"fmt"
"net/netip"
"os"
"path/filepath"
"sync"
"uncloud/internal/machine/cluster"
"uncloud/internal/machine/network"
)
@@ -83,37 +81,3 @@ func (c *State) Save() error {
}
return os.WriteFile(c.path, data, 0600)
}
// NewBootstrapConfig returns a new machine configuration that should be applied to the first machine in a cluster.
func NewBootstrapConfig(name string, subnet netip.Prefix, peers ...network.PeerConfig) (*State, error) {
mid, err := cluster.NewMachineID()
if err != nil {
return nil, fmt.Errorf("generate machine ID: %w", err)
}
if name == "" {
name, err = cluster.NewRandomMachineName()
if err != nil {
return nil, fmt.Errorf("generate machine name: %w", err)
}
}
privKey, pubKey, err := network.NewMachineKeys()
if err != nil {
return nil, fmt.Errorf("generate machine keys: %w", err)
}
if subnet == (netip.Prefix{}) {
// Use the first /24 subnet in the default network.
subnet = netip.PrefixFrom(network.DefaultNetwork.Addr(), network.DefaultSubnetBits)
}
return &State{
ID: mid,
Name: name,
Network: &network.Config{
Subnet: subnet,
ManagementIP: network.ManagementIP(pubKey),
PrivateKey: privKey,
PublicKey: pubKey,
Peers: peers,
},
}, nil
}