runtime

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: Apache-2.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package runtime is the container runtime driver (Section 3.1): a small client for the Docker Engine API, which Docker Engine and Podman (its Docker-compatible socket) both serve. It creates, labels, starts, stops and reclaims the node and PostgreSQL containers the emulator runs, plus the networks and volumes they use.

Index

Constants

View Source
const (
	LabelInstance = "gcpemu.instance" // instance ID
	LabelService  = "gcpemu.service"  // owning service, e.g. "sql"
	LabelResource = "gcpemu.resource" // owning resource name
	LabelRole     = "gcpemu.role"     // e.g. "server", "node", "nat"
)

Labels applied to everything the emulator creates.

View Source
const SelfEnv = "GCPEMU_SELF_CONTAINER"

SelfEnv names the emulator's own container explicitly.

Variables

View Source
var ErrNotFound = errors.New("runtime: not found")

ErrNotFound is returned when a container, network, volume or image is absent.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Network string
	IP      string
	Aliases []string
}

Attachment connects the container to a network at creation.

type Client

type Client struct {

	// Endpoint is the socket or URL in use.
	Endpoint string
	// contains filtered or unexported fields
}

Client talks to a Docker-compatible Engine API endpoint.

func Detect

func Detect() (*Client, error)

Detect finds a runtime socket: $DOCKER_HOST, then the default Docker socket, then rootless and rootful Podman sockets.

func New

func New(endpoint string) (*Client, error)

New returns a client for endpoint ("unix:///path" or "tcp://host:port").

func (*Client) AttachSelf added in v0.3.0

func (c *Client) AttachSelf(ctx context.Context, network string) error

AttachSelf connects the emulator's container to network; it is a no-op on the host and when already connected.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, network, container, ip string, aliases ...string) error

Connect attaches a container to a network with an optional static IP.

func (*Client) CopyFrom

func (c *Client) CopyFrom(ctx context.Context, id, file string) ([]byte, error)

CopyFrom reads a single file from the container.

func (*Client) CopyTo

func (c *Client) CopyTo(ctx context.Context, id, dir string, files []File) error

CopyTo writes files into dir inside the container (which may be created but not started).

func (*Client) CreateContainer

func (c *Client) CreateContainer(ctx context.Context, s ContainerSpec) (string, error)

CreateContainer creates (but does not start) a container.

func (*Client) CreateNetwork

func (c *Client) CreateNetwork(ctx context.Context, s NetworkSpec) (string, error)

CreateNetwork creates a bridge network and returns its ID. Inside a container the emulator joins it (AttachSelf).

func (*Client) CreateVolume

func (c *Client) CreateVolume(ctx context.Context, name string, labels map[string]string) error

CreateVolume creates (or returns the existing) named volume.

func (*Client) Disconnect

func (c *Client) Disconnect(ctx context.Context, network, container string) error

Disconnect detaches a container from a network.

func (*Client) EnsureImage

func (c *Client) EnsureImage(ctx context.Context, ref string, offline bool) error

EnsureImage pulls ref unless it is already present.

func (*Client) Exec

func (c *Client) Exec(ctx context.Context, id string, cmd []string, stdin []byte) (ExecResult, error)

Exec runs cmd in a running container and waits for it.

func (*Client) ImageExists

func (c *Client) ImageExists(ctx context.Context, ref string) (bool, error)

ImageExists reports whether ref is present locally.

func (*Client) Info

func (c *Client) Info(ctx context.Context) (Info, error)

Info returns runtime identification.

func (*Client) InspectContainer

func (c *Client) InspectContainer(ctx context.Context, id string) (Container, error)

InspectContainer returns container state.

func (*Client) InspectNetwork

func (c *Client) InspectNetwork(ctx context.Context, name string) (Network, error)

InspectNetwork returns a network by name or ID.

func (*Client) ListContainers

func (c *Client) ListContainers(ctx context.Context, labels map[string]string) ([]Container, error)

ListContainers returns all containers (running or not) carrying all labels.

func (*Client) ListNetworks

func (c *Client) ListNetworks(ctx context.Context, labels map[string]string) ([]Network, error)

ListNetworks returns networks carrying all the given labels.

func (*Client) ListVolumes

func (c *Client) ListVolumes(ctx context.Context, labels map[string]string) ([]string, error)

ListVolumes returns names of volumes carrying all the given labels.

func (*Client) Logs

func (c *Client) Logs(ctx context.Context, id string, tail int) (string, error)

Logs returns the last tail lines of a container's combined output.

func (*Client) RemoveContainer

func (c *Client) RemoveContainer(ctx context.Context, id string, force bool) error

RemoveContainer deletes a container (with anonymous volumes); a missing container is not an error.

func (*Client) RemoveNetwork

func (c *Client) RemoveNetwork(ctx context.Context, name string) error

RemoveNetwork deletes a network; a missing network is not an error.

func (*Client) RemoveVolume

func (c *Client) RemoveVolume(ctx context.Context, name string) error

RemoveVolume deletes a volume; a missing volume is not an error.

func (*Client) RestartContainer

func (c *Client) RestartContainer(ctx context.Context, id string, timeout time.Duration) error

RestartContainer restarts a container.

func (*Client) SaveImages

func (c *Client) SaveImages(ctx context.Context, refs []string, w io.Writer) error

SaveImages writes a `docker save`-format tarball of the given local images to w (GKE preloads node images from the host cache with it).

func (*Client) Self added in v0.3.0

func (c *Client) Self() string

Self returns the emulator's own container ID, or "" when it runs on the host (or on the host network).

func (*Client) SelfIP added in v0.3.0

func (c *Client) SelfIP(ctx context.Context, network string) (string, error)

SelfIP returns the emulator's address on network ("" on the host).

func (*Client) StartContainer

func (c *Client) StartContainer(ctx context.Context, id string) error

StartContainer starts a created or stopped container.

func (*Client) StopContainer

func (c *Client) StopContainer(ctx context.Context, id string, timeout time.Duration) error

StopContainer stops a container, killing it after timeout.

type Container

type Container struct {
	ID      string
	Name    string
	Image   string
	Labels  map[string]string
	Running bool
	Status  string // created, running, exited, ...
	// ExitCode of the last run.
	ExitCode int
	// IPs maps network name to IPv4 address.
	IPs map[string]string
	// Ports maps "5432/tcp" to the published host address.
	Ports map[string]string
}

Container is an inspected container.

type ContainerSpec

type ContainerSpec struct {
	Name       string
	Image      string
	Entrypoint []string
	Cmd        []string
	Env        []string
	Labels     map[string]string
	Hostname   string
	Privileged bool
	CapAdd     []string
	Sysctls    map[string]string
	// Networks: the first is used at create time, the rest are connected
	// before start.
	Networks []Attachment
	Ports    []Port
	Mounts   []Mount
	// Tmpfs maps paths to mount options.
	Tmpfs map[string]string
	// CgroupnsHost runs in the host cgroup namespace (needed by some
	// nested-container setups).
	CgroupnsHost bool
	// Memory limit in bytes (0 = unlimited).
	Memory int64
	// Init runs a tiny init as PID 1 to reap zombies.
	Init bool
}

ContainerSpec describes a container to create.

type ExecResult

type ExecResult struct {
	ExitCode int
	Stdout   []byte
	Stderr   []byte
}

ExecResult is the outcome of Exec.

type File

type File struct {
	Name string // path relative to the destination directory
	Mode int64
	Data []byte
	// Path, if set, is read from the local filesystem instead of Data.
	Path string
}

File is a file to copy into a container.

type Info

type Info struct {
	Name     string // "docker" or "podman"
	Version  string
	Arch     string // GOARCH-style
	Rootless bool
}

Info describes the connected runtime.

type Manager

type Manager struct {
	*Client
	Info       Info
	InstanceID string
	Instance   string
	Offline    bool
	// Ephemeral instances also delete volumes and networks on cleanup.
	Ephemeral bool
	Log       *slog.Logger
}

Manager scopes runtime objects to one emulator instance: it names and labels them, and reclaims orphans left by a crashed run (FR-CORE-006).

func (*Manager) Cleanup

func (m *Manager) Cleanup(ctx context.Context, all bool) error

Cleanup removes all of the instance's containers; with all=true also its networks and volumes.

func (*Manager) Labels

func (m *Manager) Labels(service, resource, role string) map[string]string

Labels returns the standard label set for an object.

func (*Manager) Name

func (m *Manager) Name(parts ...string) string

Name builds a runtime object name "gcpemu-<id>-<parts...>".

func (*Manager) Reclaim

func (m *Manager) Reclaim(ctx context.Context) error

Reclaim removes every container this instance ID owns (orphans from a previous run). For ephemeral instances it also removes networks and volumes; inside a container the emulator rejoins the networks kept.

func (*Manager) WaitRunning

func (m *Manager) WaitRunning(ctx context.Context, id string) error

WaitRunning polls until the container is running or ctx ends. It fails early if the container exits.

type Mount

type Mount struct {
	Type     string // "volume", "bind" or "tmpfs"
	Source   string
	Target   string
	ReadOnly bool
}

Mount is a volume, bind or tmpfs mount.

type Network

type Network struct {
	ID       string
	Name     string
	Subnet   string
	Gateway  string
	Internal bool
	Labels   map[string]string
	// Bridge is the host interface name, when the runtime reports it.
	Bridge string
}

Network is an inspected network.

type NetworkSpec

type NetworkSpec struct {
	Name     string
	Subnet   string // CIDR
	Gateway  string // optional; defaults to the first host address
	Internal bool   // no external connectivity
	Labels   map[string]string
	// MTU sets com.docker.network.driver.mtu when non-zero.
	MTU int
	// Options are extra driver options (e.g. com.docker.network.bridge.*).
	Options map[string]string
}

NetworkSpec describes a bridge network.

type Port

type Port struct {
	HostIP        string
	HostPort      int // 0 = runtime picks
	ContainerPort int
	Proto         string // "tcp" (default) or "udp"
}

Port publishes a container port on the host.

Jump to

Keyboard shortcuts

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