mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
start WG network when the machine is initialised via RPC call
This commit is contained in:
+61
-36
@@ -36,11 +36,14 @@ type Machine struct {
|
|||||||
|
|
||||||
config Config
|
config Config
|
||||||
state *State
|
state *State
|
||||||
|
// initialised is closed when the machine is initialised as a member of a cluster.
|
||||||
|
initialised chan struct{}
|
||||||
|
|
||||||
localServer *grpc.Server
|
localServer *grpc.Server
|
||||||
networkServer *grpc.Server
|
networkServer *grpc.Server
|
||||||
clusterState *cluster.State
|
|
||||||
cluster *cluster.Server
|
clusterState *cluster.State
|
||||||
|
cluster *cluster.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMachine(config *Config) (*Machine, error) {
|
func NewMachine(config *Config) (*Machine, error) {
|
||||||
@@ -72,8 +75,10 @@ func NewMachine(config *Config) (*Machine, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m := &Machine{
|
m := &Machine{
|
||||||
config: *config,
|
config: *config,
|
||||||
state: state,
|
state: state,
|
||||||
|
initialised: make(chan struct{}),
|
||||||
|
|
||||||
localServer: grpc.NewServer(),
|
localServer: grpc.NewServer(),
|
||||||
networkServer: grpc.NewServer(),
|
networkServer: grpc.NewServer(),
|
||||||
}
|
}
|
||||||
@@ -93,44 +98,23 @@ func NewMachine(config *Config) (*Machine, error) {
|
|||||||
pb.RegisterClusterServer(m.networkServer, m.cluster)
|
pb.RegisterClusterServer(m.networkServer, m.cluster)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if m.IsInitialised() {
|
||||||
|
close(m.initialised)
|
||||||
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsInitialised returns true if the machine has been configured as a member of a cluster,
|
||||||
|
// either by initialising a new cluster on it or joining an existing one.
|
||||||
|
func (m *Machine) IsInitialised() bool {
|
||||||
|
return m.state.ID != ""
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Machine) Run(ctx context.Context) error {
|
func (m *Machine) Run(ctx context.Context) error {
|
||||||
// Use an errgroup to coordinate error handling and graceful shutdown of multiple machine components.
|
// Use an errgroup to coordinate error handling and graceful shutdown of multiple machine components.
|
||||||
errGroup, ctx := errgroup.WithContext(ctx)
|
errGroup, ctx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
// Start the network only if it is configured.
|
|
||||||
if m.state.Network.IsConfigured() {
|
|
||||||
wgnet, err := network.NewWireGuardNetwork()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("create WireGuard network: %w", err)
|
|
||||||
}
|
|
||||||
if err = wgnet.Configure(*m.state.Network); err != nil {
|
|
||||||
return fmt.Errorf("configure WireGuard network: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
//go wgnet.WatchEndpoints(ctx, peerEndpointChangeNotifier)
|
|
||||||
|
|
||||||
//addrs, err := network.ListRoutableIPs()
|
|
||||||
//if err != nil {
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
//fmt.Println("Addresses:", addrs)
|
|
||||||
|
|
||||||
errGroup.Go(
|
|
||||||
func() error {
|
|
||||||
if err = wgnet.Run(ctx); err != nil {
|
|
||||||
return fmt.Errorf("WireGuard network failed: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
slog.Info("Waiting for network configuration to start WireGuard network.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start the machine local API server.
|
// Start the machine local API server.
|
||||||
apiSockPath := DefaultAPISockPath
|
apiSockPath := DefaultAPISockPath
|
||||||
if m.config.APISockPath != "" {
|
if m.config.APISockPath != "" {
|
||||||
@@ -143,7 +127,7 @@ func (m *Machine) Run(ctx context.Context) error {
|
|||||||
errGroup.Go(
|
errGroup.Go(
|
||||||
func() error {
|
func() error {
|
||||||
slog.Info("Starting local API server.", "path", apiSockPath)
|
slog.Info("Starting local API server.", "path", apiSockPath)
|
||||||
if err = m.localServer.Serve(localListener); err != nil {
|
if err := m.localServer.Serve(localListener); err != nil {
|
||||||
return fmt.Errorf("local API server failed: %w", err)
|
return fmt.Errorf("local API server failed: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -169,6 +153,45 @@ func (m *Machine) Run(ctx context.Context) error {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start the WireGuard network after the machine is initialised as a member of a cluster.
|
||||||
|
errGroup.Go(
|
||||||
|
func() error {
|
||||||
|
if !m.IsInitialised() {
|
||||||
|
slog.Info(
|
||||||
|
"Waiting for the machine to be initialised as a member of a cluster to start WireGuard network.",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-m.initialised:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("Starting WireGuard network.")
|
||||||
|
wgnet, err := network.NewWireGuardNetwork()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create WireGuard network: %w", err)
|
||||||
|
}
|
||||||
|
if err = wgnet.Configure(*m.state.Network); err != nil {
|
||||||
|
return fmt.Errorf("configure WireGuard network: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
//go wgnet.WatchEndpoints(ctx, peerEndpointChangeNotifier)
|
||||||
|
|
||||||
|
//addrs, err := network.ListRoutableIPs()
|
||||||
|
//if err != nil {
|
||||||
|
// return err
|
||||||
|
//}
|
||||||
|
//fmt.Println("Addresses:", addrs)
|
||||||
|
|
||||||
|
if err = wgnet.Run(ctx); err != nil {
|
||||||
|
return fmt.Errorf("WireGuard network failed: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
// Shutdown goroutine.
|
// Shutdown goroutine.
|
||||||
errGroup.Go(
|
errGroup.Go(
|
||||||
func() error {
|
func() error {
|
||||||
@@ -307,6 +330,8 @@ func (m *Machine) InitCluster(ctx context.Context, req *pb.InitClusterRequest) (
|
|||||||
return nil, status.Errorf(codes.Internal, "save machine state: %v", err)
|
return nil, status.Errorf(codes.Internal, "save machine state: %v", err)
|
||||||
}
|
}
|
||||||
slog.Info("Cluster initialised.", "machine", m.state.Name)
|
slog.Info("Cluster initialised.", "machine", m.state.Name)
|
||||||
|
// Signal that the machine is initialised as a member of a cluster.
|
||||||
|
close(m.initialised)
|
||||||
|
|
||||||
resp := &pb.InitClusterResponse{
|
resp := &pb.InitClusterResponse{
|
||||||
Machine: addResp.Machine,
|
Machine: addResp.Machine,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ type PeerConfig struct {
|
|||||||
PublicKey secret.Secret
|
PublicKey secret.Secret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsConfigured returns true if the configuration is complete to establish a WireGuard network.
|
||||||
func (c Config) IsConfigured() bool {
|
func (c Config) IsConfigured() bool {
|
||||||
return c.Subnet != (netip.Prefix{}) && c.ManagementIP != (netip.Addr{}) &&
|
return c.Subnet != (netip.Prefix{}) && c.ManagementIP != (netip.Addr{}) &&
|
||||||
c.PrivateKey != nil && c.PublicKey != nil
|
c.PrivateKey != nil && c.PublicKey != nil
|
||||||
|
|||||||
Reference in New Issue
Block a user