mirror of
https://github.com/tale/headplane.git
synced 2026-08-11 22:36:53 +00:00
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:
+26
-58
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user