feat: switch agent to a periodic dump rather than long running process

This commit is contained in:
Aarnav Tale
2026-03-27 13:19:16 -04:00
parent 2c57187628
commit ee59a2d06d
17 changed files with 521 additions and 464 deletions
+14 -5
View File
@@ -3,10 +3,13 @@ package config
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
)
// Checks to make sure all required environment variables are set
// Checks to make sure all required environment variables are set.
// TSAuthKey is only required when there is no existing tsnet state.
func validateRequired(config *Config) error {
if config.Hostname == "" {
return fmt.Errorf("%s is required", HostnameEnv)
@@ -16,17 +19,23 @@ func validateRequired(config *Config) error {
return fmt.Errorf("%s is required", TSControlURLEnv)
}
if config.TSAuthKey == "" {
return fmt.Errorf("%s is required", TSAuthKeyEnv)
}
if config.WorkDir == "" {
return fmt.Errorf("%s is required", WorkDirEnv)
}
if config.TSAuthKey == "" && !hasExistingState(config.WorkDir) {
return fmt.Errorf("%s is required for first run (no existing state in %s)", TSAuthKeyEnv, config.WorkDir)
}
return nil
}
// hasExistingState checks if tsnet has previously stored identity state.
func hasExistingState(workDir string) bool {
_, err := os.Stat(filepath.Join(workDir, "tailscaled.state"))
return err == nil
}
// Pings the Tailscale control server to make sure it's up and running
func validateTSReady(config *Config) error {
testURL := config.TSControlURL
+8 -6
View File
@@ -69,17 +69,17 @@ func (s *TSAgent) GetStatusForPeer(id string) (*tailcfg.HostinfoView, error) {
return &whois.Node.Hostinfo, nil
}
// Dispatches ALL the HostInfo entries in our Tailnet to the master
func (s *TSAgent) DispatchHostInfo(ctx context.Context) error {
// FetchAllHostInfo fetches hostinfo for all peers and returns them as a map
// keyed by node public key (e.g., "nodekey:abc123...").
func (s *TSAgent) FetchAllHostInfo(ctx context.Context) (map[string]json.RawMessage, 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)
return nil, 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
@@ -96,6 +96,8 @@ func (s *TSAgent) DispatchHostInfo(ctx context.Context) error {
nodeMap[nodeKey] = peer
}
result := make(map[string]json.RawMessage)
for nodeKey, peer := range nodeMap {
idBytes, err := nodeKey.MarshalText()
if err != nil {
@@ -138,11 +140,11 @@ func (s *TSAgent) DispatchHostInfo(ctx context.Context) error {
}
mu.Lock()
fmt.Println("HOSTINFO " + nodeID + " " + string(data))
result[nodeID] = json.RawMessage(data)
mu.Unlock()
}()
}
wg.Wait()
return nil
return result, nil
}
+1 -1
View File
@@ -59,7 +59,7 @@ func (l *Logger) SetDebug(enabled bool) {
func (l *Logger) log(level LogLevel, format string, v ...any) {
msg := fmt.Sprintf(format, v...)
fmt.Printf("LOG %s %s\n", level, msg)
fmt.Fprintf(os.Stderr, "LOG %s %s\n", level, msg)
if level == LevelFatal {
os.Exit(1)
}