Kubernetes manifests for a public node, and --announce

k8s/base (kustomize): StatefulSet with a volume, p2p LoadBalancer for 4001
TCP+UDP, ClusterIP for the UI, optional ingress; k8s/overlays/nodeport for
clusters without a load balancer. The pod waits for 'allmende init' so the
recovery phrase never goes to a log.

--announce makes a node that sits behind a router or in a container announce
the address others can dial (e.g. /dns4/name/tcp/4001), so a bootstrap node
behind a port forward works, and the DNS records it prints name the host.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-18 11:40:25 +01:00
co-authored by Claude Sonnet 5.5
parent dd229895f4
commit 819df6d512
15 changed files with 431 additions and 1 deletions
+17
View File
@@ -31,6 +31,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
"git.stinnesbeck.com/nils/allmende/internal/identity"
@@ -69,6 +70,11 @@ type Config struct {
// address or a forwarded port). It then serves the DHT and acts as a relay
// for nodes behind a NAT, which is what a bootstrap node does.
Public bool
// Announce lists the addresses other nodes should dial, without /p2p/<id>,
// e.g. /dns4/node.example.org/tcp/4001. A node behind a router or in a
// container does not know its public address; when this is set, only these
// addresses are announced.
Announce []string
// PeerCache is a file in which the node remembers its peers between runs;
// empty turns that off.
PeerCache string
@@ -167,6 +173,17 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
autorelay.WithMinCandidates(1),
autorelay.WithNumRelays(2)),
}
if len(cfg.Announce) > 0 {
announce := make([]ma.Multiaddr, 0, len(cfg.Announce))
for _, a := range cfg.Announce {
m, err := ma.NewMultiaddr(a)
if err != nil {
return nil, fmt.Errorf("announce address %q: %w", a, err)
}
announce = append(announce, m)
}
opts = append(opts, libp2p.AddrsFactory(func([]ma.Multiaddr) []ma.Multiaddr { return announce }))
}
if cfg.Public {
opts = append(opts, libp2p.ForceReachabilityPublic(), libp2p.EnableRelayService(), libp2p.EnableNATService())
}
+32
View File
@@ -7,6 +7,7 @@ import (
"io"
"log/slog"
"os"
"strings"
"testing"
"time"
@@ -406,3 +407,34 @@ func TestANodeWithoutRegionsFollowsTheFirstOneWhenChosen(t *testing.T) {
}
eventually(t, "the first region is followed and caught up", func() bool { return count(b) == 1 })
}
func TestAnnouncedAddressesReplaceTheLocalOnes(t *testing.T) {
const want = "/dns4/node.example.org/tcp/4001"
n := startNodeWith(t, nil, func(c *Config) { c.Announce = []string{want} }, "u1x")
addrs := n.Addrs()
if len(addrs) != 1 || !strings.HasPrefix(addrs[0], want+"/p2p/") {
t.Fatalf("a node behind a router announces only what it was told: %v", addrs)
}
}
func TestABadAnnounceAddressIsRefused(t *testing.T) {
dev, _ := identity.Enroll(mustRoot(t), time.Now().Add(-time.Hour), 90*24*time.Hour)
s, err := store.Open(":memory:", store.Options{})
if err != nil {
t.Fatal(err)
}
defer s.Close()
_, err = New(context.Background(), Config{Device: dev, Store: s, ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0"}, Announce: []string{"not an address"}})
if err == nil || !strings.Contains(err.Error(), "announce address") {
t.Fatalf("err = %v", err)
}
}
func mustRoot(t *testing.T) *identity.Identity {
t.Helper()
root, _, err := identity.NewRoot()
if err != nil {
t.Fatal(err)
}
return root
}