Skip to content

Agent Mesh

expose register and fix bootstrap race

Antonio Ojea63eebe090858
diff --git a/.github/k8s/sam-hub-template.yaml b/.github/k8s/sam-hub-template.yaml
index cd910ada..40f0476e 100644
--- a/.github/k8s/sam-hub-template.yaml
+++ b/.github/k8s/sam-hub-template.yaml
@@ -17,6 +17,10 @@ spec:
targetPort: 4001
protocol: UDP
name: libp2p-udp
+ - port: 80
+ targetPort: 9090
+ protocol: TCP
+ name: register-http
selector:
app: sam-hub-${ENV_NAME}
---
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.go
index b5eff599..bef96814 100644
--- a/cmd/sam-node/node.go
+++ b/cmd/sam-node/node.go
@@ -240,6 +240,28 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P
}
func (n *SamNode) RegisterStaticServices(ctx context.Context, services []api.ServiceConfig) error {
+ // Wait for DHT to be ready (size > 0)
+ // This avoids failure if we try to register immediately after enrollment
+ // before the DHT has discovered peers.
+ ticker := time.NewTicker(100 * time.Millisecond)
+ defer ticker.Stop()
+
+ timeout := time.After(10 * time.Second) // 10 seconds timeout for DHT readiness
+
+dhtLoop:
+ for {
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case <-timeout:
+ return fmt.Errorf("timeout waiting for DHT to be ready before registering static services")
+ case <-ticker.C:
+ if n.DHT.RoutingTable().Size() > 0 {
+ break dhtLoop
+ }
+ }
+ }
+
var errs []error
for _, sCfg := range services {
sType, err := parseServiceType(sCfg.Type)
diff --git a/cmd/sam-node/static_service_test.go b/cmd/sam-node/static_service_test.go
index 4c0b851b..de4d84c8 100644
--- a/cmd/sam-node/static_service_test.go
+++ b/cmd/sam-node/static_service_test.go
@@ -119,8 +119,8 @@ func TestStaticServiceRegistrationRequiresConnection(t *testing.T) {
if err == nil {
t.Fatal("Expected RegisterStaticServices to fail before connecting to hub, but it succeeded")
}
- if !strings.Contains(err.Error(), "failed to find any peer in table") {
- t.Fatalf("Expected error to contain 'failed to find any peer in table', got: %v", err)
+ if !strings.Contains(err.Error(), "timeout waiting for DHT to be ready") {
+ t.Fatalf("Expected error to contain 'timeout waiting for DHT to be ready', got: %v", err)
}
// 6. Now enroll and connect to hub (which sets up DHT and hub connection)