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
+26 -58
View File
@@ -2,26 +2,15 @@ package hpagent
import (
"bufio"
"context"
"fmt"
"encoding/json"
"os"
"sync"
"github.com/tale/headplane/internal/tsnet"
"github.com/tale/headplane/internal/util"
"tailscale.com/tailcfg"
)
// Represents messages from the Headplane master
type RecvMessage struct {
NodeIDs []string
}
type SendMessage struct {
Type string
Data any
}
// Starts listening for messages from stdin
func FollowMaster(agent *tsnet.TSAgent) {
log := util.GetLogger()
@@ -30,55 +19,34 @@ func FollowMaster(agent *tsnet.TSAgent) {
for scanner.Scan() {
line := scanner.Bytes()
var msg RecvMessage
err := json.Unmarshal(line, &msg)
if err != nil {
log.Error("Unable to decode message from master: %s", err)
directive := string(line)
log.Debug("Received directive from master: %s", directive)
switch directive {
case "SHUTDOWN":
log.Debug("Received SHUTDOWN directive from master, shutting down agent")
agent.Shutdown()
return
case "START":
log.Debug("Received START directive from master, starting agent")
// TODO: Start the agent here instead of in main
fmt.Println("READY " + agent.ID)
continue
}
log.Debug("Recieved message from master: %v", line)
if len(msg.NodeIDs) == 0 {
log.Debug("Message recieved had no node IDs")
log.Debug("Full message: %s", line)
case "PING":
log.Debug("Received PING directive from master, responding with PONG")
fmt.Println("PONG " + agent.ID)
continue
case "REFRESH":
log.Debug("Received REFRESH directive from master, refreshing status for all nodes")
err := agent.DispatchHostInfo(context.Background())
if err != nil {
log.Error("Error refreshing host info: %s", err)
fmt.Println("ERR " + err.Error())
}
}
// Accumulate the results since we invoke via gofunc
results := make(map[string]*tailcfg.HostinfoView)
mu := sync.Mutex{}
wg := sync.WaitGroup{}
for _, nodeID := range msg.NodeIDs {
wg.Add(1)
go func(nodeID string) {
defer wg.Done()
result, err := agent.GetStatusForPeer(nodeID)
if err != nil {
log.Error("Unable to get status for node %s: %s", nodeID, err)
return
}
if result == nil {
log.Debug("No status for node %s", nodeID)
return
}
mu.Lock()
results[nodeID] = result
mu.Unlock()
}(nodeID)
}
wg.Wait()
// Send the results back to the Headplane master
log.Debug("Sending status back to master: %v", results)
log.Msg(&SendMessage{
Type: "status",
Data: results,
})
}
if err := scanner.Err(); err != nil {