feat: overhaul hp_agent lifecycle handling

* Added backoff and liveness probes for better management
* Switched IPC to a simple text based system
* Lookups don't directly touch the agent now
* Use the database as a source of truth
This commit is contained in:
Aarnav Tale
2025-08-19 00:10:06 -04:00
parent a4a037ed68
commit 8cb91cd45b
12 changed files with 619 additions and 512 deletions
+82
View File
@@ -3,10 +3,14 @@ package tsnet
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"github.com/tale/headplane/internal/util"
"tailscale.com/ipn/ipnstate"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
@@ -64,3 +68,81 @@ func (s *TSAgent) GetStatusForPeer(id string) (*tailcfg.HostinfoView, error) {
log.Debug("Got whois for peer %s: %v", id, whois)
return &whois.Node.Hostinfo, nil
}
// Dispatches ALL the HostInfo entries in our Tailnet to the master
func (s *TSAgent) DispatchHostInfo(ctx context.Context) error {
log := util.GetLogger()
stat, err := s.Lc.Status(ctx)
if err != nil {
log.Debug("Failed to get status: %s", err)
return fmt.Errorf("failed to get status: %w", err)
}
// Do lookups for all peers with a hint of parallelism for speed!
const maxParallel = 8
sema := make(chan struct{}, maxParallel)
var wg sync.WaitGroup
var mu sync.Mutex
nodeMap := make(map[key.NodePublic]*ipnstate.PeerStatus)
nodeMap[stat.Self.PublicKey] = stat.Self
for nodeKey, peer := range stat.Peer {
if peer == nil {
log.Debug("Skipping nil peer for node key: %s", nodeKey)
continue
}
nodeMap[nodeKey] = peer
}
for nodeKey, peer := range nodeMap {
idBytes, err := nodeKey.MarshalText()
if err != nil {
log.Debug("Failed to marshal node key: %s", err)
continue
}
nodeID := string(idBytes)
wg.Add(1)
sema <- struct{}{}
go func() {
defer wg.Done()
defer func() { <-sema }()
wctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
ip := peer.TailscaleIPs[0].String()
if len(ip) == 0 {
log.Debug("Peer %s has no Tailscale IPs", nodeID)
return
}
whois, err := s.Lc.WhoIs(wctx, ip)
if err != nil {
log.Debug("WhoIs failed for %s (%s): %s", nodeID, ip, err)
return
}
if whois == nil || whois.Node == nil {
log.Debug("WhoIs returned nil node for %s (%s)", nodeID, ip)
return
}
data, err := json.Marshal(whois.Node.Hostinfo)
if err != nil {
log.Debug("Failed to marshal hostinfo for %s (%s): %s", nodeID, ip, err)
return
}
mu.Lock()
fmt.Println("HOSTINFO " + nodeID + " " + string(data))
mu.Unlock()
}()
}
wg.Wait()
return nil
}