compute

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 51 Imported by: ? 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 compute emulates the parts of compute.googleapis.com v1 the other services build on (SRS 1.2 supporting resources, 3.3, 5.10): regions and zones, projects.get, VPC networks, subnetworks, firewalls, routes, addresses, Cloud Routers with Cloud NAT (FR-NAT-001), network endpoint groups (zonal for FR-GKE-008, regional and global), VPC Network Peering (recorded) and compute Operations (FR-CORE-023), plus the minimal servicenetworking.googleapis.com API that binds private services access ranges to a VPC.

It also implements emu.VPC: subnetworks are realised as container networks so workload addresses are real, and a per-VPC NAT egress proxy container forwards traffic to the internet only while a Cloud NAT covers the source subnetwork (FR-NAT-002..005).

Index

Constants

This section is empty.

Variables

View Source
var Images = map[string]string{
	"nat-egress-proxy": natImage,
	"alias-probe":      aliasProbeImage,
}

Images lists the container images the compute service launches, by role (pinned by tag; digests are pinned at release).

Functions

func Fingerprint

func Fingerprint(v any) string

Fingerprint returns a GCP-style content fingerprint of v (ignoring its fingerprint fields); LabelFingerprint that of a label set.

func LabelFingerprint

func LabelFingerprint(labels map[string]string) string

LabelFingerprint returns the labelFingerprint of a label set.

func MergePatch

func MergePatch(cur any, patch []byte, out any, keep ...string) error

MergePatch applies a JSON merge patch to cur's JSON form and decodes the result into out; fields in keep retain cur's value (compute PATCH semantics).

func New

func New(env *emu.Env) emu.Service

New returns the service.

func SelfLink(path string) string

SelfLink returns the compute selfLink of a relative resource path.

func WriteAggregated

func WriteAggregated(w http.ResponseWriter, r *http.Request, kind, id, field string, scoped map[string][]ListItem)

WriteAggregated writes an aggregatedList response; scoped maps a scope ("global", "regions/R") to its items and field names the per-scope array.

func WriteList

func WriteList(w http.ResponseWriter, r *http.Request, kind, id string, items []ListItem)

WriteList applies filter, maxResults and pageToken exactly like this module's own list methods and writes a compute list response (kind "compute#xxxList", id the collection path).

Types

type InstanceGroup

type InstanceGroup struct {
	Instances  []emu.NEGEndpoint // Port is unset
	NamedPorts map[string]int
}

InstanceGroup is an instance group as load balancer backends see it: the primary internal addresses of its instances and its named ports.

type InstanceGroupResolver

type InstanceGroupResolver func(ctx context.Context, path string) (ig InstanceGroup, ok bool)

InstanceGroupResolver returns the instance group at path ("projects/P/zones/Z/instanceGroups/N"), or ok=false when it doesn't own one by that name. The compute API has no instances of its own; GKE registers a resolver for its node pool groups.

type ListItem

type ListItem struct {
	Key   string
	Value any
}

ListItem is one element of a list response with its page key (the resource name), for modules serving further collections (WriteList).

type Service

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

Service is the compute service module. It implements emu.VPC.

func (*Service) AddInstanceGroupResolver

func (s *Service) AddInstanceGroupResolver(r InstanceGroupResolver)

AddInstanceGroupResolver registers a source of instance groups.

func (*Service) AddUsageChecker

func (s *Service) AddUsageChecker(c UsageChecker)

AddUsageChecker registers a resource-in-use checker.

func (*Service) Address

func (s *Service) Address(ctx context.Context, path string) (*computev1.Address, error)

Address returns a regional or global address by path or URL.

func (*Service) AddressByIP

func (s *Service) AddressByIP(ctx context.Context, project, region, ip string) (string, bool)

AddressByIP finds the reserved address holding ip: a global one when region is "", else one in region. It returns the address path.

func (*Service) AllocateIP

func (s *Service) AllocateIP(ctx context.Context, net emu.SubnetNet, owner string) (string, error)

AllocateIP implements emu.VPC: it reserves the lowest free address of the network's address range (the Declared range unless the network fell back without translation) for owner, skipping the network and gateway addresses, GCP's reserved second-to-last address (where the NAT egress proxy lives) and the broadcast address, plus internal addresses reserved through the compute API, and addresses held on the container network by containers compute did not allocate for (#146), such as ones the emulator does not manage, which join with an address the runtime picks.

func (*Service) ApplySeed

func (s *Service) ApplySeed(ctx context.Context, section *yaml.Node, baseDir string) error

ApplySeed implements emu.Seeder by replaying the entries through the REST handlers, so they get exactly the API's validation and defaults.

func (*Service) DeleteNEG

func (s *Service) DeleteNEG(ctx context.Context, project, zone, name string) error

DeleteNEG removes a NEG and its endpoints; a missing NEG is not an error. It fails while a load balancer backend references the NEG.

func (*Service) EgressProxy added in v0.10.0

func (s *Service) EgressProxy(ctx context.Context, subnetwork string) (string, error)

EgressProxy implements emu.VPC.

func (*Service) EnvVars

func (s *Service) EnvVars(gateway string, endpoints map[string]string) map[string]string

EnvVars implements emu.EnvVarer. gcloud's compute client has BASE_URL https://compute.googleapis.com/compute/v1/ and uses an override with a non-root path verbatim. gcloud defines no api_endpoint_overrides property for servicenetworking (`gcloud services vpc-peerings` always calls servicenetworking.googleapis.com), so none is exported for it; OpenTofu (service_networking_custom_endpoint, see `gcpemu tofu-provider`) and the Go client (/servicenetworking/ base path) can be pointed at it.

func (*Service) EphemeralIP

func (s *Service) EphemeralIP(ctx context.Context, project, region string, internal bool, subnetwork string, inUse func(ip string) bool) (string, error)

EphemeralIP allocates an ephemeral address for a load balancer forwarding rule: external addresses from the global or regional pool (region == "" means global), internal ones from subnetwork's primary range, skipping reserved addresses and those for which inUse is true.

func (*Service) ExportSeed added in v0.6.0

func (s *Service) ExportSeed(ctx context.Context) (any, error)

ExportSeed implements emu.SeedExporter (FR-CORE-013). Resources a network creates itself (an auto-mode network's subnetworks, its default route) are left out; reserved addresses keep their IP. VPC Network Peerings and network endpoint groups have no seed entries and are not exported.

func (*Service) HandleFunc

func (s *Service) HandleFunc(pattern string, h http.HandlerFunc)

HandleFunc adds a route to the compute v1 mux (patterns are rooted at "/compute/v1/..."), so other modules (the M3 load balancer) can serve further compute collections with the same mount and host routing.

func (*Service) InstanceGroup

func (s *Service) InstanceGroup(ctx context.Context, group string) (InstanceGroup, error)

InstanceGroup returns the instance group at path or URL.

func (*Service) InstanceGroupEndpoints

func (s *Service) InstanceGroupEndpoints(ctx context.Context, group, portName string) ([]emu.NEGEndpoint, error)

InstanceGroupEndpoints returns the endpoints a backend service with portName reaches in an instance group: every instance at the group's named port. A group without that named port has no endpoints, as on GCP.

func (*Service) NATSink added in v0.6.0

func (s *Service) NATSink() (on bool, status int, body string)

NATSink reports whether NAT-allowed egress goes to the offline sink (FR-NAT-005), and the HTTP status and body the sink answers with.

func (*Service) NEGEndpoints

func (s *Service) NEGEndpoints(ctx context.Context, neg string) ([]emu.NEGEndpoint, error)

NEGEndpoints returns the endpoints of a NEG given by path or URL (used by the M3 load balancer).

func (*Service) Name

func (s *Service) Name() string

func (*Service) Operations added in v0.2.0

func (s *Service) Operations() []emu.OperationInfo

Operations implements emu.OperationLister: the compute#operations of every scope, and the google.longrunning Operations of Service Networking.

func (*Service) PrivateServicesNetwork

func (s *Service) PrivateServicesNetwork(ctx context.Context, network string) (emu.SubnetNet, error)

PrivateServicesNetwork implements emu.VPC: it realises the first reserved range of the network's servicenetworking connection.

func (*Service) Ready

func (s *Service) Ready() error

Ready: the control plane needs nothing external.

func (*Service) Register

func (s *Service) Register(r emu.Router) error

Register mounts compute v1 at /compute/v1/ (the Go client and gcloud base URL path), under the /compute/ gateway prefix and for host compute.googleapis.com; servicenetworking under /servicenetworking/. Requests are first checked against the pinned discovery document's required parameters and fields (NFR-MNT-001).

func (*Service) RelayAddr added in v0.7.0

func (s *Service) RelayAddr(network string) string

RelayAddr implements emu.RelayAddresser: the VM relay takes the Host address again on the network it was recorded on.

func (*Service) ReleaseIP

func (s *Service) ReleaseIP(ctx context.Context, net emu.SubnetNet, owner string) error

ReleaseIP implements emu.VPC.

func (*Service) ReservedAddrs added in v0.7.0

func (s *Service) ReservedAddrs() []string

ReservedAddrs implements emu.AddressReserver: no container started without a fixed address takes a Host address the relay holds.

func (*Service) Reset

func (s *Service) Reset(ctx context.Context) error

Reset drops pending operations and waits, bounded by ctx, for those already running (#301). It then removes the NAT egress proxies (their state is wiped with the store); workload containers and networks belong to their owners and the core.

func (*Service) ResolveSubnetwork

func (s *Service) ResolveSubnetwork(ctx context.Context, project, region, network, subnetwork string) (string, error)

ResolveSubnetwork implements emu.VPC.

func (*Service) ResourceCounts added in v0.2.0

func (s *Service) ResourceCounts() map[string]int

ResourceCounts implements emu.ResourceCounter.

func (*Service) Restore added in v0.7.0

func (s *Service) Restore(ctx context.Context, dir string) error

Restore implements emu.Snapshotter: Reset withdrew the Offered ports, and those of the snapshot are served again.

func (*Service) RoutePublic added in v0.6.0

func (s *Service) RoutePublic(ctx context.Context, via string, ips []string) error

RoutePublic implements emu.PublicRouter: every proxy routes the load balancer's forwarding rule addresses via the Edge relay, so private workloads reach them through Cloud NAT as on GCP, with the NAT's address as the client (#187).

func (*Service) SetAddressUsers

func (s *Service) SetAddressUsers(ctx context.Context, path string, users func(tx store.Tx) []string) error

SetAddressUsers records which resources use an address (status IN_USE while there are any). path is the address path or URL. users computes them inside the same store transaction, so that concurrent changes to the users cannot leave a stale list behind.

func (*Service) SetNATSink added in v0.6.0

func (s *Service) SetNATSink(on bool)

SetNATSink switches the offline sink on or off until the Instance stops (the Web console Cloud NAT view; --offline sets it at start) and re-programs every running NAT egress proxy.

func (*Service) SetNEGEndpoints

func (s *Service) SetNEGEndpoints(ctx context.Context, project, zone, name string, eps []emu.NEGEndpoint) error

SetNEGEndpoints replaces a NEG's endpoints.

func (*Service) Snapshot added in v0.7.0

func (s *Service) Snapshot(ctx context.Context, dir string) error

Snapshot implements emu.Snapshotter: the Host attachment is all in the Store.

func (*Service) Start

func (s *Service) Start(ctx context.Context) error

Start fails operations interrupted by a previous run. Container networks and the NAT egress proxy are (re)created lazily when workloads ask for them, except those of subnetworks with Offered ports, which are served again in the background.

func (*Service) StartOperation

func (s *Service) StartOperation(ctx context.Context, service, project, scope, opType, target string, targetID uint64, fn func(ctx context.Context) error) (*computev1.Operation, error)

StartOperation records a compute Operation (global, "regions/R" or "zones/Z" scope) whose work is fn, honouring the LRO latency configured for the Service ID service (FR-CORE-023). Validation should happen before; errors from fn are reported in the operation.

func (*Service) Stop

func (s *Service) Stop(ctx context.Context) error

Stop cancels pending operations and stops the NAT sink.

func (*Service) SubnetNetwork

func (s *Service) SubnetNetwork(ctx context.Context, subnetwork string) (emu.SubnetNet, error)

SubnetNetwork implements emu.VPC: it realises the subnetwork as a container network on first use.

func (*Service) Timestamp

func (s *Service) Timestamp() string

Timestamp renders the current emulator time as a compute creationTimestamp.

func (*Service) UpsertNEG

func (s *Service) UpsertNEG(ctx context.Context, project, zone, name, network, subnetwork string, defaultPort int, description string) error

UpsertNEG creates a zonal GCE_VM_IP_PORT NEG if absent.

type UsageChecker

type UsageChecker func(ctx context.Context, path string) string

UsageChecker reports a resource that uses the compute resource at path ("projects/P/global/networks/N", ...), or "" when none does. Other services (the load balancer in M3) register checkers so deletes honour their references (FR-CORE-026).

Jump to

Keyboard shortcuts

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