cloudrun

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package cloudrun runs Cloud Run services locally.

An image runs as a container, through Docker, because that is what Cloud Run deploys. This is the path to use when the container is the thing being tested: its base image, its entrypoint, what it bundles.

A source directory instead runs as a process. Cloud Run's contract with your code is small — an HTTP server on $PORT — and a process honours it without a container build, which is faster and needs no daemon. It is a convenience, not an emulation of Cloud Run: nothing about the container is exercised.

The rest of cloudrig needs no Docker. This service does, for images.

Index

Constants

View Source
const (
	DefaultProject  = "cloudrig-local"
	DefaultLocation = "us-central1"
)

Defaults for a deploy that does not say.

View Source
const ContainerPort = 8080

ContainerPort is what a Cloud Run container is told to listen on. Cloud Run sets $PORT to 8080 unless the service says otherwise, and the container publishes that port to one the host picks.

View Source
const SourcePrefix = "source:"

SourcePrefix marks an image field that names a source directory rather than a container image. Cloud Run's API has nowhere to say "run this directory", so this is cloudrig's own spelling of it.

Variables

View Source
var ErrSourceOverNetwork = errors.New(
	"a source directory cannot be deployed over the API, because it runs a program on the " +
		"emulator's machine: deploy an image, or use cloudrun.Registry.Deploy from your own code")

ErrSourceOverNetwork is returned when a request asks the emulator to run a directory from this machine.

View Source
var Prefixes = []string{"/apis/serving.knative.dev/"}

Prefixes are the path prefixes this service claims. The Knative one is its own; /v1/ is shared with Cloud Functions and Pub/Sub, so the front door asks which service has a route rather than splitting on the prefix.

View Source
var StartupTimeout = 10 * time.Second

StartupTimeout bounds how long a container may take to listen. Cloud Run's own default is four minutes; a local process that has not listened in ten seconds has failed, and waiting longer only delays the log that says why. It is a var, not a const, so the test suite can raise it: under `go test -race` with every package running at once the machine is saturated and a freshly-forked process is starved well past ten seconds.

Functions

func DockerAvailable

func DockerAvailable(ctx context.Context) bool

DockerAvailable reports whether a container can be run here. Deploying an image without a daemon is a clear error rather than a confusing one.

Types

type API

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

API serves the Cloud Run Admin API.

Two surfaces, because gcloud uses both: the regional one is Knative's serving API under /apis/serving.knative.dev/v1, and the global one is /v1/projects/{project}/locations/{location}/services, which is what `gcloud run services list` without a --region reaches.

func NewAPI

func NewAPI(reg *Registry) *API

NewAPI wires the routes over a registry.

func (*API) Matches

func (a *API) Matches(method, escapedPath string) bool

Matches reports whether a route here claims the request, so /v1/ can be shared with the other services mounted on it.

func (*API) ServeHTTP

func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Instance

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

Instance is a running service.

func (*Instance) ContainerID

func (i *Instance) ContainerID() string

ContainerID is the container behind a service, or empty for one running as a process.

func (*Instance) Location

func (i *Instance) Location() string

func (*Instance) LogSnapshot

func (i *Instance) LogSnapshot() []string

func (*Instance) Name

func (i *Instance) Name() string

func (*Instance) Project

func (i *Instance) Project() string

func (*Instance) Revision

func (i *Instance) Revision() string

func (*Instance) ServeHTTP

func (i *Instance) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Instance) Stop

func (i *Instance) Stop() error

Stop kills the service and removes what building it left behind.

func (*Instance) URL

func (i *Instance) URL() string

type Options

type Options struct {
	// Env is added to the child's environment, as KEY=VALUE.
	Env []string

	// Stderr receives the child's output as well as the log ring.
	Stderr io.Writer
}

Options configure a run.

type Registry

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

Registry holds the running services. It is the single source of truth: the API is a view over this, never a second store, so a deploy that the API reports cannot be one that nothing is serving.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) Delete

func (r *Registry) Delete(project, location, name string) error

Delete stops a service and forgets it.

func (*Registry) Deploy

func (r *Registry) Deploy(ctx context.Context, svc Service, o Options) (Service, error)

Deploy builds and starts a service, replacing any earlier revision.

func (*Registry) Describe

func (r *Registry) Describe(project, location, name string) (Service, bool)

Describe returns what was deployed.

func (*Registry) Instance

func (r *Registry) Instance(project, location, name string) (*Instance, bool)

Instance returns a running service.

func (*Registry) List

func (r *Registry) List(project, location string) []Service

List returns every deployed service, in name order.

func (*Registry) Route

func (r *Registry) Route(escapedPath string) (http.Handler, string, bool)

Route resolves a request path to a service. The emulator addresses one as /{location}-{project}/{service}/..., mirroring the functions layout.

The prefix is matched against what is deployed rather than parsed: both a location and a project contain hyphens, so "us-central1-cloudrig-local" cannot be split by looking at it. Comparing against known services has no such ambiguity, and there are never many.

func (*Registry) StopAll

func (r *Registry) StopAll()

StopAll shuts every service down.

type Service

type Service struct {
	Project  string
	Location string
	Name     string

	// Image is a container image to run, which is what Cloud Run deploys. It
	// needs a container runtime.
	Image string

	// Source is a directory to build and run as a process instead. It is the
	// faster path and needs no Docker, but it is not what Cloud Run does: it
	// honours the contract — an HTTP server on $PORT — without the container.
	Source string

	// Env is what the service sees, as KEY=VALUE.
	Env []string

	// Memory and CPU are Kubernetes quantities, as gcloud sends them:
	// "512Mi", "1Gi", "1", "500m". They are applied to the container, so a
	// service that would be killed for exceeding its memory is killed here
	// too. They mean nothing to a source deploy, which is not a container.
	Memory string
	CPU    string

	// Generation counts deploys, so each one names a new revision the way
	// Cloud Run does.
	Generation int
}

Service is a deployed Cloud Run service.

func (Service) ResourceName

func (s Service) ResourceName() string

ResourceName is the v2 API's name for the service.

func (Service) Revision

func (s Service) Revision() string

Revision names the current deploy, as Cloud Run spells it: service-00001-abc.

Jump to

Keyboard shortcuts

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