Documentation
¶
Overview ¶
Package gke emulates Google Kubernetes Engine (SRS 5.7): the container.googleapis.com v1 API over gRPC and REST on the gateway, backed by real k3s clusters running in the host's container runtime.
Control plane ¶
Clusters (Standard mode) and node pools are created, read, listed, updated, resized, upgraded and deleted through container Operations (the container API's own Operation resource, not google.longrunning). Autopilot clusters are rejected as UNIMPLEMENTED (FR-GKE-012). REST (gcloud, OpenTofu) is served by transcoding the google.api.http rules of the generated descriptors onto the gRPC implementation, including the legacy projects/P/zones/Z/... paths.
Data plane ¶
Each cluster runs one k3s server container (control plane only) and one privileged k3s agent container per node. Containers live on the cluster's VPC subnetwork (realised by compute's emu.VPC), on the instance's services network (to reach the emulator) and, unless the nodes are private, on the external network for egress. Private nodes' default route points at the VPC's NAT egress proxy, so they reach the internet only through Cloud NAT (FR-GKE-007, FR-INT-009). State lives in labelled volumes, so clusters survive emulator restarts with --data-dir. NetworkPolicy objects are enforced, by k3s's kube-router controller, only with Dataplane V2 or the networkPolicy setting, as on GKE (FR-GKE-011).
Every node runs the "gke-node" agent (the gcpemu binary) as its entrypoint. It serves the GKE metadata server on 169.254.169.254 (Workload Identity, FR-GKE-005), relays DNS to the emulated Cloud DNS (FR-INT-005), proxies Artifact Registry pulls with the node service account's token (FR-INT-006) and then runs the k3s agent.
Real hostnames in pods (FR-INT-007) ¶
Unmodified client libraries in pods reach emulated services by their real hostnames with no endpoint configuration:
- DNS: the node relay (CoreDNS's upstream) answers every hostname the gateway mounts (storage.googleapis.com, pubsub.googleapis.com, sqladmin.googleapis.com, ...) and LOCATION-docker.pkg.dev with 169.254.169.254; other names resolve through the emulated Cloud DNS.
- TLS: the node relays 169.254.169.254:443 to the emulator's Google frontend (internal/frontend), which terminates TLS with a certificate from the instance CA and passes HTTP/1.1, HTTP/2 and gRPC through to the gateway with Host / :authority intact.
- Trust: nodes add the CA to their system bundle, and a mutating admission webhook served by the emulator (cainject.go) mounts a bundle of the host's system roots plus the emulator CA into every pod at /etc/ssl/certs/ca-certificates.crt and sets SSL_CERT_FILE, except in kube-system and in namespaces or pods labelled gcpemu.dev/inject=disabled.
The API server authenticates bearer tokens through a TokenReview webhook served by the emulator (emulator IAM tokens, FR-GKE-004) and authorizes with Node,RBAC,Webhook where the webhook grants requests the caller's IAM roles (container.admin/developer/viewer) allow.
VM runtimes ¶
On a VM runtime (ADR 0005) the host cannot reach the cluster networks. The emulator then dials the API server and nodes through the VM relay, serves the API server on 127.0.0.1 and reports that as the cluster's endpoints, and serves LoadBalancer Services on host loopback addresses (lbservices.go) in place of k3s's servicelb.
Index ¶
- func New(env *emu.Env) emu.Service
- type PodRoute
- type Service
- func (s *Service) ApplySeed(ctx context.Context, section *yaml.Node, baseDir string) error
- func (s *Service) EnvVars(gw string, endpoints map[string]string) map[string]string
- func (s *Service) ExportSeed(ctx context.Context) (any, error)
- func (s *Service) LoopbackAddrs() []string
- func (s *Service) Name() string
- func (s *Service) Operations() []emu.OperationInfo
- func (s *Service) PodRoutes(ctx context.Context) []PodRoute
- func (s *Service) Ready() error
- func (s *Service) Register(r emu.Router) error
- func (s *Service) Reset(ctx context.Context) error
- func (s *Service) ResourceCounts() map[string]int
- func (s *Service) Restore(ctx context.Context, dir string) error
- func (s *Service) RoutePublic(ctx context.Context, via string, ips []string) error
- func (s *Service) Snapshot(ctx context.Context, dir string) error
- func (s *Service) Start(ctx context.Context) error
- func (s *Service) Stop(ctx context.Context) error
- func (s *Service) WaitConditions() map[string]emu.WaitCondition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type PodRoute ¶
type PodRoute struct {
Cluster string // projects/P/locations/L/clusters/C
Node string
CIDR string
Via string
Subnetwork string
}
PodRoute tells a peer data plane (the M3 load balancer) how to reach a node's pods: pod addresses in CIDR are routed via the node address Via on the VPC subnetwork Subnetwork ("projects/P/regions/R/subnetworks/S", realised by compute's emu.VPC). NEG endpoints (FR-GKE-008) are pod IPs, so a proxy container attached to that subnetwork needs `ip route add CIDR via Via` for each route to reach them.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the GKE service module.
func (*Service) EnvVars ¶
EnvVars implements emu.EnvVarer (FR-CORE-004): gcloud's container endpoint override, the kubeconfig the emulator maintains and, on a VM runtime, the host addresses of LoadBalancer Services.
func (*Service) ExportSeed ¶ added in v0.6.0
ExportSeed writes the clusters back as the "gke" section (FR-CORE-013), ordered by project, location and name, with the fields that differ from what the seed defaults to: the current master version, network, private nodes, Workload Identity and each node pool's size, machine type, labels and service account. Kubernetes objects are data plane and are not exported, nor is anything else the seed file cannot declare (release channel, node pool versions, autoscaling, taints...). A pool resized to zero nodes comes back with one, as the seed creates every pool with at least one node, and a cluster without node pools gets the default pool.
func (*Service) LoopbackAddrs ¶ added in v0.6.0
LoopbackAddrs implements emu.LoopbackHolder: the 127.0.0.x addresses LoadBalancer Services and Services served to host clients hold. It takes only loopMu, which is never held while calling another Service, and reads the store, so peers may call it holding their own locks.
func (*Service) Operations ¶ added in v0.2.0
func (s *Service) Operations() []emu.OperationInfo
Operations implements emu.OperationLister.
func (*Service) Reset ¶
Reset cancels the background work started before it and waits for it (bounded by ctx), so none of it touches a runtime object afterwards (#293). It then tears down every cluster's containers and volumes and drops the staged datastores; the store is reset by the instance. Work started after Reset runs on a fresh background context, unless the service is stopped.
func (*Service) ResourceCounts ¶ added in v0.2.0
ResourceCounts implements emu.ResourceCounter.
func (*Service) Restore ¶ added in v0.6.0
Restore implements emu.Snapshotter: Reset tore the data planes down and cleared the staged datastores; each saved datastore is staged and the restored clusters are brought up again in the background, reporting RECONCILING until they are back.
func (*Service) RoutePublic ¶ added in v0.6.0
RoutePublic implements emu.PublicRouter: nodes with external IPs route the load balancer's forwarding rule addresses via the Edge relay, so pods reach load balancers by their public address with the node's address as the client, as on GCP (#187). Private nodes reach them through Cloud NAT instead (compute).
func (*Service) Snapshot ¶ added in v0.6.0
Snapshot implements emu.Snapshotter. Clusters that are not RUNNING (provisioning, reconciling, stopping or failed) are skipped and restore with a fresh data plane.
func (*Service) WaitConditions ¶ added in v0.10.0
func (s *Service) WaitConditions() map[string]emu.WaitCondition
WaitConditions implements emu.WaitConditioner (FR-CI-007).
Source Files
¶
- addons.go
- api.go
- cainject.go
- clusteriprelay.go
- connectgateway.go
- dataplane.go
- gke.go
- hostsvc.go
- igm.go
- internal.go
- kube.go
- kubeconfig.go
- l7.go
- l7gateway.go
- l7translate.go
- lan.go
- lbservices.go
- locations.go
- metrics.go
- model.go
- neg.go
- netpol.go
- nodeagent.go
- nodepools.go
- operations.go
- provider.go
- rest.go
- routes.go
- secretaccess.go
- secretsync.go
- seed.go
- snapshot.go
- versions.go
- vpc.go
- wait.go
- wi.go