Add [master] config section to agent for registration
Heartbeat client now reads master connection settings from the agent config ([master] section) with env var fallback. Includes address, ca_cert, token_path, and role fields. Agent's Run() creates and starts the heartbeat client automatically when [master] is configured. Tested on all three nodes: rift (master), svc (edge), orion (worker) all registered with the master and sending heartbeats every 30s. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -131,6 +131,15 @@ func Run(cfg *config.AgentConfig, version string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start heartbeat client (registers with master and sends heartbeats).
|
||||||
|
hbClient, hbErr := NewHeartbeatClient(*cfg, logger)
|
||||||
|
if hbErr != nil {
|
||||||
|
logger.Warn("heartbeat client failed to start", "err", hbErr)
|
||||||
|
} else if hbClient != nil {
|
||||||
|
hbClient.Start()
|
||||||
|
logger.Info("heartbeat client started", "master", cfg.Master.Address)
|
||||||
|
}
|
||||||
|
|
||||||
mon.Start()
|
mon.Start()
|
||||||
|
|
||||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||||
@@ -144,6 +153,9 @@ func Run(cfg *config.AgentConfig, version string) error {
|
|||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
logger.Info("shutting down")
|
logger.Info("shutting down")
|
||||||
|
if hbClient != nil {
|
||||||
|
hbClient.Stop()
|
||||||
|
}
|
||||||
mon.Stop()
|
mon.Stop()
|
||||||
server.GracefulStop()
|
server.GracefulStop()
|
||||||
_ = proxy.Close()
|
_ = proxy.Close()
|
||||||
|
|||||||
@@ -44,17 +44,31 @@ type HeartbeatClient struct {
|
|||||||
// NewHeartbeatClient creates a client that registers with the master and
|
// NewHeartbeatClient creates a client that registers with the master and
|
||||||
// sends periodic heartbeats. Returns nil if master address is not configured.
|
// sends periodic heartbeats. Returns nil if master address is not configured.
|
||||||
func NewHeartbeatClient(cfg config.AgentConfig, logger interface{ Info(string, ...any); Warn(string, ...any); Error(string, ...any) }) (*HeartbeatClient, error) {
|
func NewHeartbeatClient(cfg config.AgentConfig, logger interface{ Info(string, ...any); Warn(string, ...any); Error(string, ...any) }) (*HeartbeatClient, error) {
|
||||||
masterAddr := os.Getenv("MCP_MASTER_ADDRESS")
|
// Config takes precedence, env vars as fallback.
|
||||||
masterCACert := os.Getenv("MCP_MASTER_CA_CERT")
|
masterAddr := cfg.Master.Address
|
||||||
masterToken := os.Getenv("MCP_MASTER_TOKEN_PATH")
|
if masterAddr == "" {
|
||||||
|
masterAddr = os.Getenv("MCP_MASTER_ADDRESS")
|
||||||
|
}
|
||||||
|
masterCACert := cfg.Master.CACert
|
||||||
|
if masterCACert == "" {
|
||||||
|
masterCACert = os.Getenv("MCP_MASTER_CA_CERT")
|
||||||
|
}
|
||||||
|
masterTokenPath := cfg.Master.TokenPath
|
||||||
|
if masterTokenPath == "" {
|
||||||
|
masterTokenPath = os.Getenv("MCP_MASTER_TOKEN_PATH")
|
||||||
|
}
|
||||||
|
role := cfg.Master.Role
|
||||||
|
if role == "" {
|
||||||
|
role = "worker"
|
||||||
|
}
|
||||||
|
|
||||||
if masterAddr == "" {
|
if masterAddr == "" {
|
||||||
return nil, nil // master not configured
|
return nil, nil // master not configured
|
||||||
}
|
}
|
||||||
|
|
||||||
token := ""
|
token := ""
|
||||||
if masterToken != "" {
|
if masterTokenPath != "" {
|
||||||
data, err := os.ReadFile(masterToken) //nolint:gosec // trusted config
|
data, err := os.ReadFile(masterTokenPath) //nolint:gosec // trusted config
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("read master token: %w", err)
|
return nil, fmt.Errorf("read master token: %w", err)
|
||||||
}
|
}
|
||||||
@@ -92,7 +106,7 @@ func NewHeartbeatClient(cfg config.AgentConfig, logger interface{ Info(string, .
|
|||||||
client: mcpv1.NewMcpMasterServiceClient(conn),
|
client: mcpv1.NewMcpMasterServiceClient(conn),
|
||||||
conn: conn,
|
conn: conn,
|
||||||
nodeName: cfg.Agent.NodeName,
|
nodeName: cfg.Agent.NodeName,
|
||||||
role: "worker", // default; master node sets this via config
|
role: role,
|
||||||
address: cfg.Server.GRPCAddr,
|
address: cfg.Server.GRPCAddr,
|
||||||
arch: runtime.GOARCH,
|
arch: runtime.GOARCH,
|
||||||
interval: 30 * time.Second,
|
interval: 30 * time.Second,
|
||||||
|
|||||||
@@ -20,6 +20,16 @@ type AgentConfig struct {
|
|||||||
Monitor MonitorConfig `toml:"monitor"`
|
Monitor MonitorConfig `toml:"monitor"`
|
||||||
Log LogConfig `toml:"log"`
|
Log LogConfig `toml:"log"`
|
||||||
Boot BootConfig `toml:"boot"`
|
Boot BootConfig `toml:"boot"`
|
||||||
|
Master AgentMasterConfig `toml:"master"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AgentMasterConfig holds the optional master connection settings.
|
||||||
|
// When configured, the agent self-registers and sends heartbeats.
|
||||||
|
type AgentMasterConfig struct {
|
||||||
|
Address string `toml:"address"` // master gRPC address
|
||||||
|
CACert string `toml:"ca_cert"` // CA cert to verify master's TLS
|
||||||
|
TokenPath string `toml:"token_path"` // MCIAS service token for auth
|
||||||
|
Role string `toml:"role"` // "worker", "edge", or "master"
|
||||||
}
|
}
|
||||||
|
|
||||||
// BootConfig holds the boot sequence for the master node.
|
// BootConfig holds the boot sequence for the master node.
|
||||||
|
|||||||
Reference in New Issue
Block a user