Skip to content

Commit 448f1d5

Browse files
committed
feat: add custom announce addresses support for Kubernetes deployments
1 parent 3cda16f commit 448f1d5

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.history/
12
p2p_poc
23
peer_cache.json

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.PHONY: lint test build clean
2+
3+
lint:
4+
@echo "Running linters..."
5+
@go vet ./...
6+
@gofmt -l -s .
7+
@if command -v golangci-lint >/dev/null 2>&1; then \
8+
golangci-lint run ./...; \
9+
else \
10+
echo "golangci-lint not installed, skipping"; \
11+
echo "Install with: brew install golangci-lint"; \
12+
fi
13+
14+
test:
15+
@echo "Running tests..."
16+
@go test -v -race -coverprofile=coverage.out ./...
17+
@go tool cover -func=coverage.out
18+
19+
clean:
20+
@echo "Cleaning..."
21+
@rm -f p2p_poc example/example coverage.out

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type Config struct {
8282
Logger Logger // Optional: custom logger (uses DefaultLogger if not provided)
8383
PrivateKey crypto.PrivKey // Required: private key for persistent peer ID
8484
PeerCacheFile string // Optional: file path for peer persistence
85+
AnnounceAddrs []string // Optional: addresses to advertise to peers (for K8s)
8586
}
8687
```
8788

@@ -143,6 +144,31 @@ When enabled:
143144
- This significantly speeds up network reconnection
144145
- If not provided, peer caching is disabled
145146

147+
**Kubernetes Support:**
148+
149+
The `AnnounceAddrs` field allows you to specify the external addresses that your peer should advertise. This is essential in Kubernetes where the pod's internal IP differs from the externally accessible address:
150+
151+
```go
152+
// Get external address from environment or K8s service
153+
externalIP := os.Getenv("EXTERNAL_IP") // e.g., "203.0.113.1"
154+
externalPort := os.Getenv("EXTERNAL_PORT") // e.g., "30001"
155+
156+
client, err := p2p.NewPeer(p2p.Config{
157+
Name: "node1",
158+
PrivateKey: privKey,
159+
AnnounceAddrs: []string{
160+
fmt.Sprintf("/ip4/%s/tcp/%s", externalIP, externalPort),
161+
},
162+
})
163+
```
164+
165+
Common Kubernetes scenarios:
166+
- **LoadBalancer Service**: Use the external IP of the LoadBalancer
167+
- **NodePort Service**: Use the node's external IP and the NodePort
168+
- **Ingress with TCP**: Use the ingress external IP and configured port
169+
170+
Without `AnnounceAddrs`, libp2p will announce the pod's internal IP, which won't be reachable from outside the cluster.
171+
146172
### Client
147173

148174
#### GeneratePrivateKeyHex

client.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ func NewClient(config Config) (*Client, error) {
6464
var hostOpts []libp2p.Option
6565
hostOpts = append(hostOpts, libp2p.Identity(config.PrivateKey))
6666

67+
// Configure announce addresses if provided (useful for K8s)
68+
if len(config.AnnounceAddrs) > 0 {
69+
var announceAddrs []multiaddr.Multiaddr
70+
for _, addrStr := range config.AnnounceAddrs {
71+
maddr, err := multiaddr.NewMultiaddr(addrStr)
72+
if err != nil {
73+
cancel()
74+
return nil, fmt.Errorf("invalid announce address %s: %w", addrStr, err)
75+
}
76+
announceAddrs = append(announceAddrs, maddr)
77+
}
78+
79+
hostOpts = append(hostOpts, libp2p.AddrsFactory(func([]multiaddr.Multiaddr) []multiaddr.Multiaddr {
80+
return announceAddrs
81+
}))
82+
logger.Infof("Using custom announce addresses: %v", config.AnnounceAddrs)
83+
}
84+
6785
// Create libp2p host
6886
hostOpts = append(hostOpts,
6987
libp2p.ListenAddrStrings(

config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@ type Config struct {
4646
// If provided, the client will save connected peers to this file and reload them
4747
// on restart for faster reconnection. If not provided, peer caching is disabled.
4848
PeerCacheFile string
49+
50+
// AnnounceAddrs is an optional list of multiaddr strings that this peer should
51+
// advertise to other peers. This is useful in Kubernetes or other environments
52+
// where the local address differs from the externally reachable address.
53+
// Example: []string{"/ip4/203.0.113.1/tcp/4001"}
54+
// If not provided, libp2p will automatically detect and announce local addresses.
55+
AnnounceAddrs []string
4956
}

0 commit comments

Comments
 (0)