1
0
mirror of https://github.com/juanfont/headscale.git synced 2025-05-14 01:17:07 +02:00

populate serving from primary routes

Depends on #2464
Fixes #2480

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2025-03-19 10:21:47 +01:00
parent 603f3ad490
commit 5a464036f0
No known key found for this signature in database
3 changed files with 21 additions and 19 deletions

View File

@ -729,7 +729,7 @@ func nodeRoutesToPtables(
"Hostname",
"Approved",
"Available",
"Serving",
"Serving (Primary)",
}
tableData := pterm.TableData{tableHeader}

View File

@ -27,6 +27,7 @@ import (
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol/db"
"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/hscontrol/routes"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
)
@ -349,7 +350,7 @@ func (api headscaleV1APIServer) SetApprovedRoutes(
}
}
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) {
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)
}
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 {
@ -497,7 +501,7 @@ func (api headscaleV1APIServer) ListNodes(
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
}
@ -510,11 +514,11 @@ func (api headscaleV1APIServer) ListNodes(
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
}
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))
for index, node := range nodes {
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.SubnetRoutes = util.PrefixesToString(pr.PrimaryRoutes(node.ID))
response[index] = resp
}

View File

@ -247,13 +247,7 @@ func (node *Node) IPsAsString() []string {
}
func (node *Node) InIPSet(set *netipx.IPSet) bool {
for _, nodeAddr := range node.IPs() {
if set.Contains(nodeAddr) {
return true
}
}
return false
return slices.ContainsFunc(node.IPs(), set.Contains)
}
// AppendToIPSet adds the individual ips in NodeAddresses to a
@ -329,14 +323,17 @@ func (node *Node) Proto() *v1.Node {
DiscoKey: node.DiscoKey.String(),
// TODO(kradalby): replace list with v4, v6 field?
IpAddresses: node.IPsAsString(),
Name: node.Hostname,
GivenName: node.GivenName,
User: node.User.Proto(),
ForcedTags: node.ForcedTags,
IpAddresses: node.IPsAsString(),
Name: node.Hostname,
GivenName: node.GivenName,
User: node.User.Proto(),
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),
AvailableRoutes: util.PrefixesToString(node.AnnouncedRoutes()),
SubnetRoutes: util.PrefixesToString(node.SubnetRoutes()),
RegisterMethod: node.RegisterMethodToV1Enum(),