controllers

package
v0.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// LeaseNamePrefix is prepended to a relay's name to form its Lease name.
	// Relays are cluster-scoped but Leases are namespaced and shared with other
	// coordination.apoxy.dev consumers (e.g. leader election), so the prefix
	// keeps relay leases in their own namespace and avoids collisions. The lease
	// watcher strips it to resolve the owning Relay.
	LeaseNamePrefix = "relay-"

	// DefaultLeaseNamespace is where relay Leases live when none is configured.
	DefaultLeaseNamespace = "default"
)
View Source
const LabelRelay = "vpc.apoxy.dev/relay"

LabelRelay is stamped by the relay on every Tunnel with its own name, so the relay-lease-gone orphan GC (and `kubectl get tunnels -l vpc.apoxy.dev/relay=x`) can select a relay's connections.

Variables

This section is empty.

Functions

func LeaseName

func LeaseName(relayName string) string

LeaseName returns the Lease name for a relay of the given name.

Types

type Connection

type Connection interface {
	io.Closer
	// ID is the unique identifier of the connection.
	ID() string
	// Set the overlay address/prefix assigned to this connection.
	SetOverlayAddress(addr string) error
	// Set the VNI assigned to this connection.
	SetVNI(ctx context.Context, vni uint) error
	// Stats returns a snapshot of connection statistics.
	Stats() (ConnectionStats, bool)
	// Network is the name of the VPCNetwork this connection is bound to (the
	// credential-authorized network, resolved by the relay at connect).
	Network() string
	// Scope is the opaque tenant scope the connection's credential resolved to
	// (AuthzResult.Scope); empty on single-tenant relays. Multi-tenant relay
	// operators use it to route per-connection callbacks to the right tenant.
	Scope() string
	// Labels are the agent-declared labels, already validated against the
	// credential's bounds. VPCService selectors match on these.
	Labels() map[string]string
	// AdvertisedRoutes are the agent-declared prefixes reachable behind this
	// connection, already validated against the credential's bounds.
	AdvertisedRoutes() []netip.Prefix
	// AgentInstance is the agent process's stable instance UUID, if declared.
	AgentInstance() string
	// SetAddresses reconciles the connection's programmed overlay address set
	// (an IPv6 /96 plus a best-effort IPv4 /32) against addrs: non-primary
	// prefixes are programmed onto the router and into the VNI's allowed
	// routes, and prefixes that fell out of the set are unprogrammed
	// (SetOverlayAddress covers the primary IPv6 address).
	SetAddresses(addrs []string) error
	// Addresses returns the programmed dual-stack overlay address set (primary
	// first), or nil if nothing is programmed. It is the set surfaced in the
	// connect response and the Tunnel object.
	Addresses() []string
}

Connection is a simple abstraction representing a connection from a TunnelAgent to a Relay.

type ConnectionStats

type ConnectionStats struct {
	// RXBytes is the total number of bytes received on this connection.
	RXBytes int64
	// TXBytes is the total number of bytes transmitted on this connection.
	TXBytes int64
	// LastRX is the last time a packet was received on this connection.
	// The zero value indicates that no packets have been received.
	LastRX time.Time
}

ConnectionStats is a lightweight snapshot of connection counters.

type RegistrarOption

type RegistrarOption func(*RelayRegistrar)

RegistrarOption configures a RelayRegistrar.

func WithLeaseDuration

func WithLeaseDuration(d time.Duration) RegistrarOption

WithLeaseDuration overrides the advertised lease duration.

func WithLeaseNamespace

func WithLeaseNamespace(ns string) RegistrarOption

WithLeaseNamespace overrides the namespace relay Leases are written to.

func WithRenewInterval

func WithRenewInterval(d time.Duration) RegistrarOption

WithRenewInterval overrides the lease renewal cadence.

type Relay

type Relay interface {
	// Name is the name of the relay.
	Name() string
	// Address is the underlay address of the relay.
	Address() netip.AddrPort
	// SetCredentials sets the authentication token used by agents to authenticate with the relay.
	SetCredentials(tunnelName, token string)
	// RemoveCredentials revokes a tunnel's authentication token so new connects to it fail closed.
	RemoveCredentials(tunnelName string)
	// SetEgressGateway enables or disables internet egress for the tunnel agents.
	SetEgressGateway(enabled bool)
	// SetOnConnect sets a callback that is invoked when a new connection is established to the relay.
	SetOnConnect(onConnect func(ctx context.Context, tunnelName, agentName string, conn Connection) error)
	// SetOnDisconnect sets a callback that is invoked when a connection is closed.
	SetOnDisconnect(onDisconnect func(ctx context.Context, agentName, id string) error)
	// SetOnShutdown sets a callback that is invoked when the relay is shutting down.
	SetOnShutdown(onShutdown func(ctx context.Context))
}

Relay is a simple abstraction representing a relay server that TunnelAgents connect to.

type RelayRegistrar

type RelayRegistrar struct {
	// contains filtered or unexported fields
}

RelayRegistrar owns a relay's presence in the control plane: it creates the write-once Relay object on start and renews a coordination.apoxy.dev Lease on a fixed cadence so the lease watcher can flip Relay readiness on crash. On drain it flips readiness off and deletes both objects (§2.3/§5).

It takes two clients so the same implementation serves OSS (both point at the standalone apiserver) and cloud cmd/relay (leaseClient -> infra-apiz, relayClient -> shard fan-out). This is the cmd/relay seam.

func NewRelayRegistrar

func NewRelayRegistrar(
	leaseClient, relayClient client.Client,
	relay Relay,
	addresses []string,
	networkSelector *metav1.LabelSelector,
	opts ...RegistrarOption,
) *RelayRegistrar

NewRelayRegistrar creates a RelayRegistrar. addresses are the underlay endpoints agents dial; networkSelector scopes which networks the relay serves (nil selects all).

func (*RelayRegistrar) Drain

func (r *RelayRegistrar) Drain(ctx context.Context)

Drain tears down the relay's control-plane presence by deleting its Lease and Relay objects (§5). Deletion is the terminal signal consumers act on; there is no separate ready=false write, since Drain runs synchronously at shutdown with no settle window in which an intermediate not-ready state could be observed. It is meant to be wired to Relay.SetOnShutdown by the caller; it does not stop the renewal loop itself (canceling Start's ctx does that).

func (*RelayRegistrar) Start

func (r *RelayRegistrar) Start(ctx context.Context) error

Start registers the Relay (write-once) then renews the lease until ctx is canceled. It implements manager.Runnable so it can be added to a manager.

type TunnelPublisher

type TunnelPublisher struct {
	// contains filtered or unexported fields
}

TunnelPublisher owns the relay side of a connection's control-plane presence. It is wired to Relay.SetOnConnect/SetOnDisconnect and, on connect, allocates the connection's overlay addresses in-process from a leased slot (§2.8) plus a relay-local VNI (§2.5), assigns them onto the connection synchronously, and creates the single-writer Tunnel object (§2.4). On disconnect it deletes the Tunnel and returns the addresses and VNI to their pools. It makes zero apiserver round-trips for addressing; the one write is the Tunnel object.

Network name -> NetworkID resolution is fed by the relay-side VPCNetwork watcher via SetNetworkID; a connect to an unresolved network fails until the network is provisioned and observed.

func NewTunnelPublisher

func NewTunnelPublisher(c client.Client, relay Relay, leaser ipalloc.SlotLeaser, vnis vniAllocator) *TunnelPublisher

NewTunnelPublisher creates a TunnelPublisher and wires it to the relay's connect/disconnect callbacks.

func (*TunnelPublisher) InvalidateSlot added in v0.22.0

func (p *TunnelPublisher) InvalidateSlot(s ipalloc.Slot)

InvalidateSlot drops a slot the leaser lost so no new connections allocate from it. Wired to the leaser's slot-lost notification where one exists.

func (*TunnelPublisher) OnConnect

func (p *TunnelPublisher) OnConnect(ctx context.Context, tunnelName, agentName string, conn Connection) error

OnConnect allocates addresses + a VNI for the connection, assigns them, and creates the Tunnel object. It is called synchronously from handleConnect.

func (*TunnelPublisher) OnDisconnect

func (p *TunnelPublisher) OnDisconnect(ctx context.Context, agentName, id string) error

OnDisconnect deletes the connection's Tunnel object and returns its addresses and VNI to their pools. It is idempotent: a connection with no allocation record (e.g. an orphan from a prior relay incarnation) still has its Tunnel object deleted by ID.

func (*TunnelPublisher) ReleaseAll

func (p *TunnelPublisher) ReleaseAll(ctx context.Context)

ReleaseAll returns every leased slot to the leaser. Called at drain.

func (*TunnelPublisher) RemoveNetwork added in v0.22.0

func (p *TunnelPublisher) RemoveNetwork(ctx context.Context, name string)

RemoveNetwork forgets a deleted VPCNetwork: connects to it fail closed again and every slot leased for it is returned, so its identifiers stop being renewed against a network that no longer exists.

func (*TunnelPublisher) SetNetworkID

func (p *TunnelPublisher) SetNetworkID(name string, id tunnet.NetworkID)

SetNetworkID records the NetworkID a VPCNetwork name resolves to. Fed by the relay-side VPCNetwork watcher as networks are observed.

type VPCNetworkReconciler

type VPCNetworkReconciler struct {
	client.Client
	// contains filtered or unexported fields
}

VPCNetworkReconciler is the relay-side consumer of VPCNetwork objects. It feeds the relay the network's connect credential (for the static token validator), tracks its egress-gateway setting, and resolves the network's name to a NetworkID for the TunnelPublisher's in-process slot allocation. It never writes VPCNetworks - the apiserver-side provisioner owns their identity and credentials.

func NewVPCNetworkReconciler

func NewVPCNetworkReconciler(c client.Client, relay Relay, publisher *TunnelPublisher) *VPCNetworkReconciler

NewVPCNetworkReconciler creates a relay-side VPCNetwork watcher.

func (*VPCNetworkReconciler) Reconcile

Reconcile propagates a VPCNetwork's credential, egress setting, and resolved NetworkID to the relay and publisher.

func (*VPCNetworkReconciler) SetupWithManager

func (r *VPCNetworkReconciler) SetupWithManager(mgr ctrl.Manager) error

SetupWithManager wires the watcher to VPCNetwork objects.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL