Documentation
¶
Overview ¶
Package frontend is the emulator's "Google front end": a TLS terminator that lets unmodified clients reach emulated services by their real hostnames (storage.googleapis.com, pubsub.googleapis.com, LOCATION-docker.pkg.dev, ...) over HTTPS, HTTP/2 and gRPC (FR-INT-007, FR-CORE-043).
Design ¶
The Google front end is a TCP listener in the emulator process (Service endpoint "frontend", dynamic port unless --port frontend=N). For each connection it completes a TLS handshake with a certificate issued by the instance's local CA (env.CA) for the requested SNI, offering ALPN "h2" and "http/1.1", then passes the decrypted byte stream unchanged to:
- the Artifact Registry listener for LOCATION-docker.pkg.dev, or
- the API gateway for every other name, in process (SetGateway), so the API gateway knows the client used HTTPS and services render https links (#440).
Both backends speak HTTP/1.1 and prior-knowledge h2c, so whichever protocol the client negotiated works end to end, including gRPC, streaming and trailers, and the Host / :authority header arrives untouched; the gateway's host routing (emu.Router Mount hosts) then picks the service. Only names the emulator serves get a certificate: the gateway's mounted hosts and LOCATION-docker.pkg.dev. A connection without SNI (a client dialing an IP address, such as kube-apiserver calling an admission webhook) gets a certificate for "localhost" and every IP address of the host, which includes the container networks' gateway addresses.
Port 443 is reached from containers through small TCP relays rather than by binding it on the host (which needs root): the GKE node agent listens on 169.254.169.254:443 in every node, and the host-mode container (internal/hostmode) listens on its own address. Both resolve the served names to themselves with the DNS Relay in this package.
A second listener, the workload Service endpoint ("frontend-workload"), serves the same names to the Workload front-end relays of GKE nodes and the Cloud Build agent (ADR 0092). Every connection on it is a Workload caller's: the API gateway marks its requests so, and registry hostnames go to the registry's workload Service endpoint ("ar-workload"). Host mode and kube-apiserver's webhook calls keep the "frontend" endpoint, which carries Host clients' traffic.
Index ¶
- Constants
- func IsRegistryHost(name string) bool
- func ListenDNS(addr string, h mdns.Handler) (stop func(), err error)
- func Network(m *mdns.Msg) string
- func NewReadThrough(resolvers []string) http.Handler
- func Normalize(name string) string
- func Pipe(client, server net.Conn)
- func Relay(l net.Listener, target string, opts ...RelayOption) error
- func Serves(hosts []string, name string) bool
- func SetNetwork(m *mdns.Msg, network string) *mdns.Msg
- func WorkloadRelay(ctx context.Context, addr func(context.Context, string) (string, error), ...) (string, []string)
- type DNS
- type Frontend
- func (f *Frontend) Addr() string
- func (f *Frontend) Close() error
- func (f *Frontend) Hosts() []string
- func (f *Frontend) Serves(name string) bool
- func (f *Frontend) SetGateway(serve func(ctx context.Context, c net.Conn, workload bool) error)
- func (f *Frontend) Start() error
- func (f *Frontend) StartLAN(lan LAN)
- func (f *Frontend) StopLAN(ctx context.Context)
- func (f *Frontend) TLSConfig() *tls.Config
- type LAN
- type RelayOption
- type Storage
- type UpstreamLog
Constants ¶
const Endpoint = "frontend"
Endpoint is the Google front end's name in emu.Endpoints (and netplane.Addr).
const LANEndpoint = "gfe-lan"
LANEndpoint is the --gfe-bind listeners' Service endpoint name in emu.Endpoints.
const LANRelayName = "gfe-lan"
LANRelayName names the LAN front-end relay's container, gcpemu-<id>-gfe-lan.
const NetworkOption = 65400
NetworkOption is the EDNS0 local option (RFC 6891 private range) with which a VPC's DNS relay tells the emulated Cloud DNS the VPC network a query comes from ("projects/P/global/networks/N"), so that private zones are answered only on the networks they are bound to (FR-DNS-004).
const RegistryWorkloadEndpoint = "ar-workload"
RegistryWorkloadEndpoint is the name of Artifact Registry's workload Service endpoint, to which WorkloadEndpoint sends registry hostnames.
const UpstreamLogEvery = 30 * time.Second
UpstreamLogEvery is the least time between two lines while problems continue.
const UpstreamSlow = 2 * time.Second
UpstreamSlow is how long an upstream call may take before it is a problem.
const WorkloadEndpoint = "frontend-workload"
WorkloadEndpoint is the name of the Google front end's workload Service endpoint, which marks every connection as a Workload caller's (ADR 0092).
Variables ¶
This section is empty.
Functions ¶
func IsRegistryHost ¶
IsRegistryHost reports whether name is an Artifact Registry Docker host, LOCATION-docker.pkg.dev.
func ListenDNS ¶
ListenDNS serves h on addr over UDP and TCP and returns once both are listening; stop shuts them down.
func NewReadThrough ¶ added in v0.9.0
NewReadThrough returns the handler that sends requests on to real GCS, resolving storage.googleapis.com with resolvers (host:port), the host's real DNS servers, since the host's own resolver may send it back here in Host mode. It honours HTTPS_PROXY. With no resolvers it returns nil.
func Pipe ¶
Pipe copies between client and server in both directions. When the server side ends, the client gets a half-close and the connection is torn down shortly after; a client half-close is passed on and the server may keep answering.
func Relay ¶
func Relay(l net.Listener, target string, opts ...RelayOption) error
Relay accepts TCP connections on l and pipes each to target (used by the in-container 443 relays). A dial to target that fails or is slow is an upstream problem (UpstreamLog). It returns when l is closed.
func Serves ¶
Serves reports whether name is served by the frontend given the gateway's mounted hosts.
func SetNetwork ¶
SetNetwork returns a copy of m that carries network in NetworkOption.
func WorkloadRelay ¶ added in v0.18.0
func WorkloadRelay(ctx context.Context, addr func(context.Context, string) (string, error), hosts func() []string, log *slog.Logger, prefix string) (string, []string)
WorkloadRelay returns what a GKE node's or Cloud Build worker's config needs for its Workload front-end relay: the address of WorkloadEndpoint on the services network (from addr, such as netplane.Plane.Addr) and the Google hostnames hosts lists, which its DNS relay answers with the metadata address. It returns "" and nil when the front end is not configured (hosts is nil) or its address fails. addr is called either way, as the configs did before (#1276).
When the address fails while the front end is configured, the node or worker still starts, but without the relay, so its workloads' Google API calls become Escaped requests. WorkloadRelay warns about that on log, which names the node or worker (log.With), under the calling service's prefix, such as "gke".
Types ¶
type DNS ¶
type DNS struct {
// Answer returns the IPv4 address for name (normalized, no trailing
// dot), or nil when the name is relayed.
Answer func(name string) net.IP
// Upstreams returns the servers (host:port) a name is relayed to;
// none yields SERVFAIL.
Upstreams func(name string) []string
// TTL of synthesized answers (default 60 s).
TTL uint32
// Network, when set, tags relayed queries with the VPC network they
// come from (NetworkOption).
Network string
// Log, when set, gets each upstream lookup: one that fails or is
// slow is an upstream problem (UpstreamLog).
Log *UpstreamLog
}
DNS is a small resolver that answers the names a relay serves with its own address and relays everything else upstream. The GKE node agent (cluster DNS upstream) and the host-mode container use it so that *.googleapis.com names the emulator serves resolve to their 443 relay.
type Frontend ¶
type Frontend struct {
// contains filtered or unexported fields
}
Frontend is the TLS-terminating front end.
func New ¶
New returns a frontend serving the names hosts returns (the gateway's mounted hosts) plus LOCATION-docker.pkg.dev.
func (*Frontend) Close ¶
Close stops the listener and open connections, and the --gfe-bind listeners (StopLAN), allowing a relay left up to 30s to be removed. The Instance calls StopLAN first, with its Teardown's ctx.
func (*Frontend) Hosts ¶
Hosts returns the exact hostnames served (registry hosts match by pattern; see IsRegistryHost).
func (*Frontend) SetGateway ¶ added in v0.10.0
SetGateway makes the frontend hand API gateway connections to serve (Gateway.ServeFrontend) instead of dialing the "gateway" endpoint, where they look like direct plain-HTTP clients. Without it (tests building a Frontend alone) the frontend dials the endpoint. Call it before Start.
func (*Frontend) Start ¶
Start opens the listeners on the configured bind address and records the "frontend" and "frontend-workload" endpoints. A dynamic port comes from --port-range, except in a Containerized Instance, whose published range ports forwarding rules and LoadBalancer Services need (ADR 0012). The port is settled before the endpoint is recorded, so it never moves.
func (*Frontend) StartLAN ¶ added in v0.9.0
StartLAN serves lan on every --gfe-bind address. An address whose port 443 the emulator may not bind is relayed by the LAN front-end relay when it can be (lanrelay.go); any other failed address is logged and skipped.
func (*Frontend) StopLAN ¶ added in v0.10.0
StopLAN stops the --gfe-bind listeners and removes the LAN front-end relay, one still starting included, for as long as ctx allows: a relay ctx cuts short is left to the runtime's Cleanup, which finds it by the Instance's label. The Instance calls it before it removes its containers, so that no relay starts after.
type LAN ¶ added in v0.9.0
type LAN struct {
// Gateway serves every real hostname but the registry's.
Gateway http.Handler
// Registry returns Artifact Registry's authenticating handler, or nil
// while it isn't running.
Registry func() http.Handler
// Storage classifies storage.googleapis.com requests; nil without
// Cloud Storage.
Storage Storage
// ReadThrough serves reads of buckets the Instance doesn't have
// (NewReadThrough); nil sends them through the credentials check like
// any other.
ReadThrough http.Handler
}
LAN is where the --gfe-bind listeners send the requests they admit.
type RelayOption ¶ added in v0.18.0
type RelayOption func(*relayOptions)
RelayOption changes how Relay logs or dials.
func RelayLog ¶ added in v0.18.0
func RelayLog(u *UpstreamLog) RelayOption
RelayLog makes Relay write its upstream problems to u, whose Prefix names the relay in the convention of the binary it runs in. Without it they go to stderr, prefixed "relay".
type Storage ¶ added in v0.9.0
type Storage interface {
// ForeignBucket reports whether r reads a bucket the Instance doesn't have.
ForeignBucket(r *http.Request) bool
// Signed reports whether r carries a signature Cloud Storage verifies.
Signed(r *http.Request) bool
}
Storage is what the LAN listeners ask Cloud Storage.
type UpstreamLog ¶ added in v0.18.0
type UpstreamLog struct {
// Prefix starts each line and names the relay, in the convention of
// the binary it runs in (for example "gke-node: DNS relay").
Prefix string
// Out receives the lines; nil is os.Stderr.
Out io.Writer
// Now is the clock; nil is time.Now.
Now func() time.Time
// contains filtered or unexported fields
}
UpstreamLog writes a relay's upstream problems to stderr without flooding it (#1245). A call to the upstream that fails, or that takes longer than UpstreamSlow, is an upstream problem: the first one writes one full line; while problems continue at most one line every UpstreamLogEvery gives how many failed and how many were slow since the last line; the first fast success after them writes that the upstream is reachable again and how long the problems lasted. A nil *UpstreamLog logs nothing.
func (*UpstreamLog) Done ¶ added in v0.18.0
func (u *UpstreamLog) Done(start time.Time, upstream, name string, err error)
Done records an upstream call that started at start and ended with err. name is what was asked for (a DNS name), or "".
func (*UpstreamLog) Start ¶ added in v0.18.0
func (u *UpstreamLog) Start() time.Time
Start returns the time an upstream call starts, to pass to Done.