2023-10-26 17:53:12 +02:00
|
|
|
package mesh
|
|
|
|
|
|
|
|
import (
|
2023-10-27 18:49:18 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ip"
|
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
2023-10-26 17:53:12 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/route"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RouteManager interface {
|
|
|
|
UpdateRoutes() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type RouteManagerImpl struct {
|
|
|
|
meshManager *MeshManager
|
|
|
|
routeInstaller route.RouteInstaller
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RouteManagerImpl) UpdateRoutes() error {
|
2023-10-27 18:49:18 +02:00
|
|
|
meshes := r.meshManager.Meshes
|
|
|
|
ulaBuilder := new(ip.ULABuilder)
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
for _, mesh1 := range meshes {
|
|
|
|
for _, mesh2 := range meshes {
|
|
|
|
if mesh1 == mesh2 {
|
|
|
|
continue
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
ipNet, err := ulaBuilder.GetIPNet(mesh2.GetMeshId())
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
if err != nil {
|
|
|
|
logging.Log.WriteErrorf(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
err = mesh1.AddRoutes(r.meshManager.HostParameters.HostEndpoint, ipNet.String())
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRouteManager(m *MeshManager) RouteManager {
|
|
|
|
return &RouteManagerImpl{meshManager: m, routeInstaller: route.NewRouteInstaller()}
|
|
|
|
}
|