mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
refactor cluster network related constants
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"uncloud/internal/cli"
|
"uncloud/internal/cli"
|
||||||
"uncloud/internal/cli/config"
|
"uncloud/internal/cli/config"
|
||||||
"uncloud/internal/machine/network"
|
"uncloud/internal/machine/cluster"
|
||||||
)
|
)
|
||||||
|
|
||||||
type initOptions struct {
|
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().StringVarP(&opts.name, "name", "n", "", "Assign a name to the machine.")
|
||||||
cmd.Flags().StringVar(
|
cmd.Flags().StringVar(
|
||||||
&opts.network, "network", network.DefaultNetwork.String(),
|
&opts.network, "network", cluster.DefaultNetwork.String(),
|
||||||
"IPv4 network CIDR to use for machines and services.",
|
"IPv4 network CIDR to use for machines and services.",
|
||||||
)
|
)
|
||||||
cmd.Flags().StringVarP(
|
cmd.Flags().StringVarP(
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ func (c *Cluster) AddMachine(ctx context.Context, req *pb.AddMachineRequest) (*p
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, "create IPAM manager: %v", err)
|
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 {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, "allocate subnet for machine: %v", err)
|
return nil, status.Errorf(codes.Internal, "allocate subnet for machine: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ import (
|
|||||||
"net/netip"
|
"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.
|
// IPAM is an in-memory IP address manager for allocating and releasing subnets for machines from a cluster network.
|
||||||
type IPAM struct {
|
type IPAM struct {
|
||||||
network netip.Prefix
|
network netip.Prefix
|
||||||
|
|||||||
@@ -8,11 +8,6 @@ import (
|
|||||||
"uncloud/internal/secret"
|
"uncloud/internal/secret"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
DefaultNetwork = netip.MustParsePrefix("10.210.0.0/16")
|
|
||||||
DefaultSubnetBits = 24
|
|
||||||
)
|
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
// Subnet is the IPv4 address range allocated to the machine. The machine's IP address is the first address
|
// 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.
|
// in the subnet. Other IP addresses are allocated to containers running on the machine.
|
||||||
|
|||||||
@@ -3,11 +3,9 @@ package machine
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"uncloud/internal/machine/cluster"
|
|
||||||
"uncloud/internal/machine/network"
|
"uncloud/internal/machine/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -83,37 +81,3 @@ func (c *State) Save() error {
|
|||||||
}
|
}
|
||||||
return os.WriteFile(c.path, data, 0600)
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user