diff --git a/cmd/uncloud/machine/init.go b/cmd/uncloud/machine/init.go index a92f7206..d6c8d317 100644 --- a/cmd/uncloud/machine/init.go +++ b/cmd/uncloud/machine/init.go @@ -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( diff --git a/internal/machine/cluster/cluster.go b/internal/machine/cluster/cluster.go index 7ad4d035..75bfe459 100644 --- a/internal/machine/cluster/cluster.go +++ b/internal/machine/cluster/cluster.go @@ -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) } diff --git a/internal/machine/cluster/ipam.go b/internal/machine/cluster/ipam.go index 3f9617ac..7e1adbf1 100644 --- a/internal/machine/cluster/ipam.go +++ b/internal/machine/cluster/ipam.go @@ -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 diff --git a/internal/machine/network/config.go b/internal/machine/network/config.go index e58dd4f5..7ecb2184 100644 --- a/internal/machine/network/config.go +++ b/internal/machine/network/config.go @@ -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. diff --git a/internal/machine/state.go b/internal/machine/state.go index f4dc09bf..d5e9ca1e 100644 --- a/internal/machine/state.go +++ b/internal/machine/state.go @@ -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 -}