Skip to content

Commit

Permalink
fix save node when routes/services
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Dec 2, 2023
1 parent 495ff53 commit 979569c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
5 changes: 0 additions & 5 deletions hscontrol/db/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ func (hsdb *HSDatabase) listPeers(node *types.Node) (types.Nodes, error) {

sort.Slice(nodes, func(i, j int) bool { return nodes[i].ID < nodes[j].ID })

log.Trace().
Caller().
Str("node", node.Hostname).
Msgf("Found peers: %s", nodes.String())

return nodes, nil
}

Expand Down
55 changes: 44 additions & 11 deletions hscontrol/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (h *Headscale) handlePoll(
Str("node_key", node.NodeKey.ShortString()).
Str("node", node.Hostname).
Int("cap_ver", int(mapRequest.Version)).
Msg("Received endpoint update")
Msg("Received update")

change := node.PeerChangeFromMapRequest(mapRequest)

Expand All @@ -89,6 +89,10 @@ func (h *Headscale) handlePoll(

node.ApplyPeerChange(&change)

hostInfoChange := node.Hostinfo.Equal(mapRequest.Hostinfo)

logTracePeerChange(node.Hostname, hostInfoChange, &change)

// Check if the Hostinfo of the node has changed.
// If it has changed, check if there has been a change tod
// the routable IPs of the host and update update them in
Expand All @@ -97,7 +101,7 @@ func (h *Headscale) handlePoll(
// the route change.
// If the hostinfo has changed, but not the routes, just update
// hostinfo and let the function continue.
if !node.Hostinfo.Equal(mapRequest.Hostinfo) {
if !hostInfoChange {
oldRoutes := node.Hostinfo.RoutableIPs
newRoutes := mapRequest.Hostinfo.RoutableIPs

Expand All @@ -121,13 +125,6 @@ func (h *Headscale) handlePoll(
return
}

if err := h.db.NodeSave(node); err != nil {
logErr(err, "Failed to persist/update node in the database")
http.Error(writer, "", http.StatusInternalServerError)

return
}

sendUpdate = true
}

Expand All @@ -142,6 +139,13 @@ func (h *Headscale) handlePoll(
}

if sendUpdate {
if err := h.db.NodeSave(node); err != nil {
logErr(err, "Failed to persist/update node in the database")
http.Error(writer, "", http.StatusInternalServerError)

return
}

stateUpdate := types.StateUpdate{
Type: types.StatePeerChanged,
ChangeNodes: types.Nodes{node},
Expand Down Expand Up @@ -269,15 +273,13 @@ func (h *Headscale) handlePoll(

logInfo("Sending initial map")

log.Trace().Msgf("NODE FULLMAP BEGIN %s", node.Hostname)
mapResp, err := mapp.FullMapResponse(mapRequest, node, h.ACLPolicy)
if err != nil {
logErr(err, "Failed to create MapResponse")
http.Error(writer, "", http.StatusInternalServerError)

return
}
log.Trace().Msgf("NODE FULLMAP END %s", node.Hostname)

// Send the client an update to make sure we send an initial mapresponse
_, err = writer.Write(mapResp)
Expand Down Expand Up @@ -532,3 +534,34 @@ func (h *Headscale) handleLiteRequest(
logErr(err, "Failed to write response")
}
}

func logTracePeerChange(hostname string, hostinfoChange bool, change *tailcfg.PeerChange) {
trace := log.Trace().Str("node_id", change.NodeID.String()).Str("hostname", hostname)

if change.Key != nil {
trace = trace.Str("node_key", change.Key.ShortString())
}

if change.DiscoKey != nil {
trace = trace.Str("disco_key", change.DiscoKey.ShortString())
}

if change.Online != nil {
trace = trace.Bool("online", *change.Online)
}

if change.Endpoints != nil {
eps := make([]string, len(change.Endpoints))
for idx, ep := range change.Endpoints {
eps[idx] = ep.String()
}

trace = trace.Strs("endpoints", eps)
}

if hostinfoChange {
trace = trace.Bool("hostinfo_changed", hostinfoChange)
}

trace.Time("last_seen", *change.LastSeen).Msg("PeerChange received")
}
4 changes: 2 additions & 2 deletions integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ func assertContains(t *testing.T, str, subStr string) {
}
}

func pingAllHelper(t *testing.T, clients []TailscaleClient, addrs []string) int {
func pingAllHelper(t *testing.T, clients []TailscaleClient, addrs []string, opts ...tsic.PingOption) int {
t.Helper()
success := 0

for _, client := range clients {
for _, addr := range addrs {
err := client.Ping(addr)
err := client.Ping(addr, opts...)
if err != nil {
t.Fatalf("failed to ping %s from %s: %s", addr, client.Hostname(), err)
} else {
Expand Down

0 comments on commit 979569c

Please sign in to comment.