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 egress gateway container forwards traffic to the internet only while a Cloud NAT covers the source subnetwork (FR-NAT-002..005).
Index ¶
- Variables
- func Fingerprint(v any) string
- func LabelFingerprint(labels map[string]string) string
- func MergePatch(cur any, patch []byte, out any, keep ...string) error
- func New(env *emu.Env) emu.Service
- func SelfLink(path string) string
- func WriteAggregated(w http.ResponseWriter, r *http.Request, kind, id, field string, ...)
- func WriteList(w http.ResponseWriter, r *http.Request, kind, id string, items []ListItem)
- type InstanceGroup
- type InstanceGroupResolver
- type ListItem
- type Service
- func (s *Service) AddInstanceGroupResolver(r InstanceGroupResolver)
- func (s *Service) AddUsageChecker(c UsageChecker)
- func (s *Service) Address(ctx context.Context, path string) (*computev1.Address, error)
- func (s *Service) AddressByIP(ctx context.Context, project, region, ip string) (string, bool)
- func (s *Service) AllocateIP(ctx context.Context, net emu.SubnetNet, owner string) (string, error)
- func (s *Service) ApplySeed(ctx context.Context, section *yaml.Node, baseDir string) error
- func (s *Service) DeleteNEG(ctx context.Context, project, zone, name string) error
- func (s *Service) EgressGateway(ctx context.Context, subnetwork string) (string, error)
- func (s *Service) EnvVars(gateway string, endpoints map[string]string) map[string]string
- func (s *Service) EphemeralIP(ctx context.Context, project, region string, internal bool, subnetwork string, ...) (string, error)
- func (s *Service) HandleFunc(pattern string, h http.HandlerFunc)
- func (s *Service) InstanceGroup(ctx context.Context, group string) (InstanceGroup, error)
- func (s *Service) InstanceGroupEndpoints(ctx context.Context, group, portName string) ([]emu.NEGEndpoint, error)
- func (s *Service) NEGEndpoints(ctx context.Context, neg string) ([]emu.NEGEndpoint, error)
- func (s *Service) Name() string
- func (s *Service) Operations() []emu.OperationInfo
- func (s *Service) PrivateServicesNetwork(ctx context.Context, network string) (emu.SubnetNet, error)
- func (s *Service) Ready() error
- func (s *Service) Register(r emu.Router) error
- func (s *Service) ReleaseIP(ctx context.Context, net emu.SubnetNet, owner string) error
- func (s *Service) Reset(ctx context.Context) error
- func (s *Service) ResolveSubnetwork(ctx context.Context, project, region, network, subnetwork string) (string, error)
- func (s *Service) ResourceCounts() map[string]int
- func (s *Service) SetAddressUsers(ctx context.Context, path string, users func(tx store.Tx) []string) error
- func (s *Service) SetNEGEndpoints(ctx context.Context, project, zone, name string, eps []emu.NEGEndpoint) error
- func (s *Service) Start(ctx context.Context) error
- func (s *Service) StartOperation(ctx context.Context, project, scope, opType, target string, targetID uint64, ...) (*computev1.Operation, error)
- func (s *Service) Stop(ctx context.Context) error
- func (s *Service) SubnetNetwork(ctx context.Context, subnetwork string) (emu.SubnetNet, error)
- func (s *Service) Timestamp() string
- func (s *Service) UpsertNEG(ctx context.Context, project, zone, name, network, subnetwork string, ...) error
- type UsageChecker
Constants ¶
This section is empty.
Variables ¶
var Images = map[string]string{
"nat-gateway": natImage,
}
Images lists the container images the compute service launches, by role (pinned by tag; digests are pinned at release).
Functions ¶
func Fingerprint ¶
Fingerprint returns a GCP-style content fingerprint of v (ignoring its fingerprint fields); LabelFingerprint that of a label set.
func LabelFingerprint ¶
LabelFingerprint returns the labelFingerprint of a label set.
func MergePatch ¶
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 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.
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 ¶
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) AddressByIP ¶
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 ¶
AllocateIP implements emu.VPC: it reserves the lowest free address of the container network for owner, skipping the network and gateway addresses, GCP's reserved second-to-last address (where the egress gateway lives) and the broadcast address, plus internal addresses reserved through the compute API.
func (*Service) ApplySeed ¶
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 ¶
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) EgressGateway ¶
EgressGateway implements emu.VPC.
func (*Service) EnvVars ¶
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) 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 ¶
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) NEGEndpoints ¶
NEGEndpoints returns the endpoints of a NEG given by path or URL (used by the M3 load balancer).
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) Register ¶
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) Reset ¶
Reset removes the NAT egress gateways (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
ResourceCounts implements emu.ResourceCounter.
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) SetNEGEndpoints ¶
func (s *Service) SetNEGEndpoints(ctx context.Context, project, zone, name string, eps []emu.NEGEndpoint) error
SetNEGEndpoints replaces a NEG's endpoints.
func (*Service) Start ¶
Start fails operations interrupted by a previous run. Container networks and the egress gateway are (re)created lazily when workloads ask for them.
func (*Service) StartOperation ¶
func (s *Service) StartOperation(ctx context.Context, 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 configured LRO latency (FR-CORE-023). Validation should happen before; errors from fn are reported in the operation.
func (*Service) SubnetNetwork ¶
SubnetNetwork implements emu.VPC: it realises the subnetwork as a container network on first use.
type UsageChecker ¶
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).