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.
51 lines
2.0 KiB
Go
51 lines
2.0 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
|
|
)
|
|
|
|
// setupModelEgress starts the egress proxy task containers send their model
|
|
// calls through, on the worker bridge's host gateway. It degrades to no
|
|
// proxy rather than failing boot; a task that needs a keyed provider then
|
|
// reports why it cannot start.
|
|
func (b *boot) setupModelEgress(ctx context.Context) {
|
|
pool, log := b.containerPool, b.log
|
|
if pool == nil || pool.HostGateway() == "" {
|
|
log.InfoContext(ctx, "model egress disabled: it needs the managed container pool on a bridge with a host gateway")
|
|
return
|
|
}
|
|
caDir := filepath.Join(b.cfg.StateDir, "egress")
|
|
ca, err := egress.LoadOrCreateCA(caDir)
|
|
if err != nil {
|
|
log.WarnContext(ctx, "model egress disabled: egress CA", "err", err)
|
|
return
|
|
}
|
|
ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", net.JoinHostPort(pool.HostGateway(), "0"))
|
|
if err != nil {
|
|
log.WarnContext(ctx, "model egress disabled: egress proxy listener", "err", err)
|
|
return
|
|
}
|
|
resolver := runCredentialResolver{runs: b.stateStoreGrants.Client, models: b.taskModels, config: b.cfgHolder, secrets: b.secrets, credentials: b.credentialWatch}
|
|
proxy := egress.NewProxy(ca, egress.ProxyOptions{Resolver: resolver})
|
|
srv := &http.Server{Handler: proxy, ReadHeaderTimeout: 30 * time.Second}
|
|
go func() {
|
|
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.ErrorContext(ctx, "egress proxy stopped", "err", err)
|
|
}
|
|
}()
|
|
b.addCleanup(func() { _ = srv.Close() })
|
|
// Task containers reach the proxy only through the egress relay, which
|
|
// forwards its proxy port here.
|
|
b.egressProxyAddr = ln.Addr().String()
|
|
b.modelEgress = modelEgress{proxy: proxy, addr: net.JoinHostPort(egress.RelayAlias, strconv.Itoa(egress.RelayPort)), caFile: egress.CACertPath(caDir), config: b.cfgHolder, secrets: b.secrets}
|
|
log.InfoContext(ctx, "model egress enabled", "proxy", ln.Addr().String())
|
|
}
|