Help us shape the future of Go! Take the 2026 Go Developer Survey

gcpserve

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package gcpserve holds the pieces GCP emulators share to serve user workloads from containers.

Index

Constants

View Source
const (
	StreamStdout = "stdout"
	StreamStderr = "stderr"
)

Container output streams, named as in the Cloud Run log ids.

View Source
const ProbeUserAgent = "gopherstack-readiness-probe"

ProbeUserAgent identifies readiness probes sent to workload containers.

Variables

This section is empty.

Functions

func AcquirePort

func AcquirePort(ctx context.Context, pool PortPool, label string) (int, error)

AcquirePort takes a host port from pool, or a free loopback port when pool is nil.

func CloudRunJobResource

func CloudRunJobResource(project, job, location string) *monitoredres.MonitoredResource

CloudRunJobResource returns the cloud_run_job resource that Cloud Run job tasks log under.

func CloudRunResource

func CloudRunResource(project, service, revision, location string) *monitoredres.MonitoredResource

CloudRunResource returns the cloud_run_revision resource that Cloud Run and 2nd-gen functions log under.

func LogName

func LogName(project, id string) string

LogName returns projects/{project}/logs/{id} with id URL-escaped, as the Logging API expects.

func Proxy

func Proxy(port int, rt http.RoundTripper) *httputil.ReverseProxy

Proxy reverse-proxies to the container published on the loopback port, preserving the inbound Host.

func ReleasePort

func ReleasePort(pool PortPool, port int)

ReleasePort returns port to pool; a nil pool or zero port is a no-op.

func StreamLogs

func StreamLogs(ctx context.Context, rt any, id string, t LogTarget, sink LogSink) error

StreamLogs follows container id's output and writes each line to sink until the container goes away or ctx ends. rt must implement container.LogStreamer.

func WaitReady

func WaitReady(ctx context.Context, port int) error

WaitReady blocks until the loopback port accepts TCP and answers an HTTP request (any status), or ctx ends. The TCP check alone is not enough: a published container port accepts connections before the app listens.

Types

type LazyRuntime

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

LazyRuntime connects to the container runtime on first use and retries while it is unreachable.

func NewLazyRuntime

func NewLazyRuntime(log *slog.Logger, lockName string) *LazyRuntime

NewLazyRuntime returns a LazyRuntime whose lock is named lockName.

func (*LazyRuntime) Close

func (l *LazyRuntime) Close() error

Close releases the runtime client.

func (*LazyRuntime) Get

func (l *LazyRuntime) Get() (Runner, error)

Get returns the connected runtime, dialing it if needed.

type LogCapture

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

LogCapture forwards workload output and request logs to a LogSink once one is wired.

func NewLogCapture

func NewLogCapture(lockName string) *LogCapture

NewLogCapture returns a LogCapture whose lock is named lockName.

func (*LogCapture) Follow

func (c *LogCapture) Follow(ctx context.Context, rt any, id string, t LogTarget) StopFunc

Follow is Start for a workload whose output must be complete before it is removed: the returned stop waits up to drain for the stream to end on its own (the container has exited), then cancels it. It is a no-op without a sink.

func (*LogCapture) Request

func (c *LogCapture) Request(
	ctx context.Context,
	t LogTarget,
	r *http.Request,
	rec *RequestRecorder,
	took time.Duration,
)

Request writes the request log for a finished request; a no-op without a sink.

func (*LogCapture) SetSink

func (c *LogCapture) SetSink(s LogSink)

SetSink wires the destination; nil disables capture.

func (*LogCapture) Start

func (c *LogCapture) Start(ctx context.Context, rt any, id string, t LogTarget)

Start follows container id's output in the background until the container is removed or ctx ends.

func (*LogCapture) Wait

func (c *LogCapture) Wait()

Wait blocks until every capture goroutine has finished; call after the containers are removed.

type LogSink

type LogSink interface {
	WriteLogEntries(ctx context.Context, req *loggingpb.WriteLogEntriesRequest) error
}

LogSink receives log entries; *gcplogging.InMemoryBackend satisfies it.

type LogTarget

type LogTarget struct {
	Resource *monitoredres.MonitoredResource
	Labels   map[string]string
	Project  string
}

LogTarget says which monitored resource a workload's log entries belong to.

func (LogTarget) LineEntry

func (t LogTarget) LineEntry(stream, line string) *loggingpb.LogEntry

LineEntry builds the entry for one output line: textPayload for plain text, jsonPayload for a JSON object.

func (LogTarget) LogRequest

func (t LogTarget) LogRequest(
	ctx context.Context,
	sink LogSink,
	r *http.Request,
	rec *RequestRecorder,
	took time.Duration,
)

LogRequest writes the run.googleapis.com/requests entry for a finished request.

func (LogTarget) RequestEntry

func (t LogTarget) RequestEntry(r *http.Request, status int, respSize int64, took time.Duration) *loggingpb.LogEntry

RequestEntry builds the request-log entry with its httpRequest fields.

type PortPool

type PortPool interface {
	Acquire(label string) (int, error)
	Release(port int) error
}

PortPool hands out host ports for published containers.

type RequestRecorder

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

RequestRecorder remembers the status and response size of a proxied request without wrapping the ResponseWriter.

func NewRequestRecorder

func NewRequestRecorder() *RequestRecorder

NewRequestRecorder returns an empty recorder; Attach it to the proxy before serving.

func (*RequestRecorder) Attach

func (r *RequestRecorder) Attach(p *httputil.ReverseProxy)

Attach records p's upstream status and body size and its error responses.

func (*RequestRecorder) Size

func (r *RequestRecorder) Size() int64

Size returns the response bytes read from the upstream.

func (*RequestRecorder) Status

func (r *RequestRecorder) Status() int

Status returns the recorded status, 200 when the handler wrote nothing.

type Runner

type Runner interface {
	CreateAndStart(ctx context.Context, spec container.Spec) (string, error)
	StopAndRemove(ctx context.Context, containerID string) error
}

Runner is the slice of the container runtime that served workloads need.

type StopFunc

type StopFunc func(drain time.Duration)

StopFunc ends a followed log stream after waiting up to drain for it to finish.

Jump to

Keyboard shortcuts

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