mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-28 10:51:44 +01:00 
			
		
		
		
	populate serving from primary routes
Depends on #2464 Fixes #2480 Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
		
							parent
							
								
									603f3ad490
								
							
						
					
					
						commit
						5a464036f0
					
				| @ -729,7 +729,7 @@ func nodeRoutesToPtables( | |||||||
| 		"Hostname", | 		"Hostname", | ||||||
| 		"Approved", | 		"Approved", | ||||||
| 		"Available", | 		"Available", | ||||||
| 		"Serving", | 		"Serving (Primary)", | ||||||
| 	} | 	} | ||||||
| 	tableData := pterm.TableData{tableHeader} | 	tableData := pterm.TableData{tableHeader} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -27,6 +27,7 @@ import ( | |||||||
| 	v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | 	v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | ||||||
| 	"github.com/juanfont/headscale/hscontrol/db" | 	"github.com/juanfont/headscale/hscontrol/db" | ||||||
| 	"github.com/juanfont/headscale/hscontrol/policy" | 	"github.com/juanfont/headscale/hscontrol/policy" | ||||||
|  | 	"github.com/juanfont/headscale/hscontrol/routes" | ||||||
| 	"github.com/juanfont/headscale/hscontrol/types" | 	"github.com/juanfont/headscale/hscontrol/types" | ||||||
| 	"github.com/juanfont/headscale/hscontrol/util" | 	"github.com/juanfont/headscale/hscontrol/util" | ||||||
| ) | ) | ||||||
| @ -349,7 +350,7 @@ func (api headscaleV1APIServer) SetApprovedRoutes( | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	tsaddr.SortPrefixes(routes) | 	tsaddr.SortPrefixes(routes) | ||||||
| 	slices.Compact(routes) | 	routes = slices.Compact(routes) | ||||||
| 
 | 
 | ||||||
| 	node, err := db.Write(api.h.db.DB, func(tx *gorm.DB) (*types.Node, error) { | 	node, err := db.Write(api.h.db.DB, func(tx *gorm.DB) (*types.Node, error) { | ||||||
| 		err := db.SetApprovedRoutes(tx, types.NodeID(request.GetNodeId()), routes) | 		err := db.SetApprovedRoutes(tx, types.NodeID(request.GetNodeId()), routes) | ||||||
| @ -371,7 +372,10 @@ func (api headscaleV1APIServer) SetApprovedRoutes( | |||||||
| 		api.h.nodeNotifier.NotifyWithIgnore(ctx, types.UpdatePeerChanged(node.ID), node.ID) | 		api.h.nodeNotifier.NotifyWithIgnore(ctx, types.UpdatePeerChanged(node.ID), node.ID) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return &v1.SetApprovedRoutesResponse{Node: node.Proto()}, nil | 	proto := node.Proto() | ||||||
|  | 	proto.SubnetRoutes = util.PrefixesToString(api.h.primaryRoutes.PrimaryRoutes(node.ID)) | ||||||
|  | 
 | ||||||
|  | 	return &v1.SetApprovedRoutesResponse{Node: proto}, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func validateTag(tag string) error { | func validateTag(tag string) error { | ||||||
| @ -497,7 +501,7 @@ func (api headscaleV1APIServer) ListNodes( | |||||||
| 			return nil, err | 			return nil, err | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		response := nodesToProto(api.h.polMan, isLikelyConnected, nodes) | 		response := nodesToProto(api.h.polMan, isLikelyConnected, api.h.primaryRoutes, nodes) | ||||||
| 		return &v1.ListNodesResponse{Nodes: response}, nil | 		return &v1.ListNodesResponse{Nodes: response}, nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @ -510,11 +514,11 @@ func (api headscaleV1APIServer) ListNodes( | |||||||
| 		return nodes[i].ID < nodes[j].ID | 		return nodes[i].ID < nodes[j].ID | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
| 	response := nodesToProto(api.h.polMan, isLikelyConnected, nodes) | 	response := nodesToProto(api.h.polMan, isLikelyConnected, api.h.primaryRoutes, nodes) | ||||||
| 	return &v1.ListNodesResponse{Nodes: response}, nil | 	return &v1.ListNodesResponse{Nodes: response}, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func nodesToProto(polMan policy.PolicyManager, isLikelyConnected *xsync.MapOf[types.NodeID, bool], nodes types.Nodes) []*v1.Node { | func nodesToProto(polMan policy.PolicyManager, isLikelyConnected *xsync.MapOf[types.NodeID, bool], pr *routes.PrimaryRoutes, nodes types.Nodes) []*v1.Node { | ||||||
| 	response := make([]*v1.Node, len(nodes)) | 	response := make([]*v1.Node, len(nodes)) | ||||||
| 	for index, node := range nodes { | 	for index, node := range nodes { | ||||||
| 		resp := node.Proto() | 		resp := node.Proto() | ||||||
| @ -532,6 +536,7 @@ func nodesToProto(polMan policy.PolicyManager, isLikelyConnected *xsync.MapOf[ty | |||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		resp.ValidTags = lo.Uniq(append(tags, node.ForcedTags...)) | 		resp.ValidTags = lo.Uniq(append(tags, node.ForcedTags...)) | ||||||
|  | 		resp.SubnetRoutes = util.PrefixesToString(pr.PrimaryRoutes(node.ID)) | ||||||
| 		response[index] = resp | 		response[index] = resp | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -247,13 +247,7 @@ func (node *Node) IPsAsString() []string { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (node *Node) InIPSet(set *netipx.IPSet) bool { | func (node *Node) InIPSet(set *netipx.IPSet) bool { | ||||||
| 	for _, nodeAddr := range node.IPs() { | 	return slices.ContainsFunc(node.IPs(), set.Contains) | ||||||
| 		if set.Contains(nodeAddr) { |  | ||||||
| 			return true |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return false |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // AppendToIPSet adds the individual ips in NodeAddresses to a
 | // AppendToIPSet adds the individual ips in NodeAddresses to a
 | ||||||
| @ -334,9 +328,12 @@ func (node *Node) Proto() *v1.Node { | |||||||
| 		GivenName:   node.GivenName, | 		GivenName:   node.GivenName, | ||||||
| 		User:        node.User.Proto(), | 		User:        node.User.Proto(), | ||||||
| 		ForcedTags:  node.ForcedTags, | 		ForcedTags:  node.ForcedTags, | ||||||
|  | 
 | ||||||
|  | 		// Only ApprovedRoutes and AvailableRoutes is set here. SubnetRoutes has
 | ||||||
|  | 		// to be populated manually with PrimaryRoute, to ensure it includes the
 | ||||||
|  | 		// routes that are actively served from the node.
 | ||||||
| 		ApprovedRoutes:  util.PrefixesToString(node.ApprovedRoutes), | 		ApprovedRoutes:  util.PrefixesToString(node.ApprovedRoutes), | ||||||
| 		AvailableRoutes: util.PrefixesToString(node.AnnouncedRoutes()), | 		AvailableRoutes: util.PrefixesToString(node.AnnouncedRoutes()), | ||||||
| 		SubnetRoutes:    util.PrefixesToString(node.SubnetRoutes()), |  | ||||||
| 
 | 
 | ||||||
| 		RegisterMethod: node.RegisterMethodToV1Enum(), | 		RegisterMethod: node.RegisterMethodToV1Enum(), | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user