start WG network when the machine is initialised via RPC call

This commit is contained in:
Pavel Sviderski
2024-09-10 18:23:55 +10:00
parent 4e2640bbd2
commit 676f22a935
2 changed files with 62 additions and 36 deletions
+57 -32
View File
@@ -36,9 +36,12 @@ type Machine struct {
config Config
state *State
// initialised is closed when the machine is initialised as a member of a cluster.
initialised chan struct{}
localServer *grpc.Server
networkServer *grpc.Server
clusterState *cluster.State
cluster *cluster.Server
}
@@ -74,6 +77,8 @@ func NewMachine(config *Config) (*Machine, error) {
m := &Machine{
config: *config,
state: state,
initialised: make(chan struct{}),
localServer: grpc.NewServer(),
networkServer: grpc.NewServer(),
}
@@ -93,44 +98,23 @@ func NewMachine(config *Config) (*Machine, error) {
pb.RegisterClusterServer(m.networkServer, m.cluster)
}
if m.IsInitialised() {
close(m.initialised)
}
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 {
// Use an errgroup to coordinate error handling and graceful shutdown of multiple machine components.
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.
apiSockPath := DefaultAPISockPath
if m.config.APISockPath != "" {
@@ -143,7 +127,7 @@ func (m *Machine) Run(ctx context.Context) error {
errGroup.Go(
func() error {
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 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.
errGroup.Go(
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)
}
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{
Machine: addResp.Machine,
+1
View File
@@ -35,6 +35,7 @@ type PeerConfig struct {
PublicKey secret.Secret
}
// IsConfigured returns true if the configuration is complete to establish a WireGuard network.
func (c Config) IsConfigured() bool {
return c.Subnet != (netip.Prefix{}) && c.ManagementIP != (netip.Addr{}) &&
c.PrivateKey != nil && c.PublicKey != nil