Files
archie/internal/daemon/statestore_target.go
sam e7d3ed72e4 fix(container): task containers run on their own sandbox network behind the egress relay (#1867)
Each task container now gets an internal bridge with no host address,
shared only with the egress relay. NATS, the State Store and the model
proxy are reached through the relay's ports; a loopback State Store is
forwarded to the host gateway. The network is removed with the task, and
boot sweeps any a previous daemon left. The per-task State Store relay on
the host bridge is gone.
2026-11-14 00:14:21 +11:00

57 lines
1.7 KiB
Go

package daemon
import (
"fmt"
"net"
"strings"
)
// StateStoreNeedsRelay reports whether target is bound to this host's
// loopback, which the egress relay can reach only through the host bridge's
// gateway address.
func StateStoreNeedsRelay(target string) bool {
_, host, ok := stateStoreTargetAddr(target)
return ok && loopbackHost(host)
}
// RelayStateStoreTarget is the host:port the egress relay forwards task State
// Store traffic to. The relay has its own network namespace, so a loopback
// target would name the relay itself: it is reached at gateway, the host's
// address on the relay's bridge, on the same port.
func RelayStateStoreTarget(target, gateway string) (string, error) {
addr, host, ok := stateStoreTargetAddr(target)
if !ok {
return "", fmt.Errorf("state store target %q: want host:port", target)
}
if !loopbackHost(host) {
return addr, nil
}
if gateway == "" {
return "", fmt.Errorf("state store target %q is loopback and the container network has no host gateway", target)
}
_, port, _ := net.SplitHostPort(addr)
return net.JoinHostPort(gateway, port), nil
}
// stateStoreTargetAddr is target as host:port, accepting the gRPC scheme form
// ("dns:///host:port") the configuration allows.
func stateStoreTargetAddr(target string) (addr, host string, ok bool) {
target = strings.TrimSpace(target)
if _, rest, found := strings.Cut(target, ":///"); found {
target = rest
}
host, _, err := net.SplitHostPort(target)
if err != nil {
return "", "", false
}
return target, host, true
}
func loopbackHost(host string) bool {
if strings.EqualFold(host, "localhost") {
return true
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
}