kplanetest

package module
v0.0.0-...-ffb5860 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

README

kplanetest

envtest-compatible Kubernetes test environment backed by kplane virtual control planes.

What it is

kplanetest is a drop-in replacement for sigs.k8s.io/controller-runtime/pkg/envtest. It preserves the envtest contract (Start()/Stop() returning *rest.Config), but swaps per‑test apiserver/etcd for a shared kplane control‑plane core.

Why it matters

envtest launches a real apiserver and etcd per test, which gets expensive at scale. kplanetest gives each test its own virtual control plane surface while sharing the core, so memory/CPU per test is dramatically lower.

How the apiserver works (high level)

  • A shared API server hosts many virtual control planes (VCPs).
  • Each VCP is isolated by request path, with native Kubernetes semantics.
  • The storage layer uses cluster‑scoped keying so data stays isolated.
  • Admission/webhooks are per‑VCP, so behavior stays compatible.

This keeps the Kubernetes API contract while eliminating the per‑test control‑plane tax.

Status

Initial v1 is implemented as an envtest-compatible surface with:

  • Environment Start() / Stop() contract parity.
  • Upstream envtest.Environment as the canonical configuration contract.
  • Default backend using kplane-dev/apiserver with kcp embedded etcd.
  • CRD path and webhook options passthrough via envtest-compatible flow.
  • In-memory lifecycle metrics for test assertions.
  • e2e conformance parity tests against upstream envtest.

Intended usage

env := &kplanetest.Environment{
  CRDDirectoryPaths: []string{"../config/crd/bases"},
}
cfg, err := env.Start()
if err != nil {
  // handle error
}
defer env.Stop()

Test strategy

  • Unit tests validate lifecycle behavior and metrics.
  • Conformance (e2e) runs identical API behavior checks against envtest and kplanetest.
  • Performance includes benchmark coverage for metrics overhead and optional startup guardrails.

Backend selection

Default backend is kplane (kplane-dev/apiserver + embedded etcd).

Optional overrides:

  • KPLANETEST_BACKEND=envtest for upstream envtest backend.
  • KPLANETEST_BACKEND=envtest-shared for shared envtest prototype backend.

Kplane assets setup (envtest-like model)

kplanetest can use prebuilt/cached apiserver binaries instead of building at runtime.

Install setup helper:

go install github.com/kplane-dev/kplanetest/cmd/setup-kplanetest@latest

Prepare assets cache and print path:

export KPLANETEST_ASSETS="$(setup-kplanetest use -p path v0.0.8)"

Or set a direct binary path:

export KPLANETEST_APISERVER_BINARY=/absolute/path/to/kplane-apiserver

Resolution order:

  1. KPLANETEST_APISERVER_BINARY
  2. KPLANETEST_ASSETS + /kplane-apiserver
  3. Runtime build fallback (current behavior if nothing is configured)

Run local unit tests:

go test ./...

Run e2e conformance/perf (requires envtest assets):

KUBEBUILDER_ASSETS=/path/to/envtest/bin go test -tags=e2e ./test/e2e/conformance/...
KUBEBUILDER_ASSETS=/path/to/envtest/bin KPLANETEST_PERF=1 go test -tags=e2e ./test/perf/...

Run package-level perf comparison and enforce kplane wins:

KUBEBUILDER_ASSETS=/path/to/envtest/bin \
KPLANETEST_PERF=1 \
KPLANETEST_PERF_BATCH=10 \
KPLANETEST_PERF_ASSERT_WIN=1 \
go test -tags=e2e ./test/perf -run TestPackageLevelEnvComparison -v -count=1

Run internal kplane backend validation:

KPLANETEST_INTERNAL_E2E=1 go test -tags=e2e ./test/e2e/conformance -run TestInternalKplane -v

Run optional live-kplane validation (requires a reachable kplane kubeconfig):

KPLANETEST_KPLANE_KUBECONFIG=/path/to/kplane.kubeconfig go test -tags=e2e ./test/e2e/conformance -run TestKplaneExternalCRUD -v
KUBEBUILDER_ASSETS=/path/to/envtest/bin KPLANETEST_PERF=1 KPLANETEST_KPLANE_KUBECONFIG=/path/to/kplane.kubeconfig go test -tags=e2e ./test/perf -run TestKplaneExternalCRUDPerformance -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	Start(env *envtest.Environment) (*rest.Config, error)
	Stop(env *envtest.Environment) error
}

Backend defines the control-plane operations required by Environment. v1 defaults to EnvtestBackend; future commits can swap this implementation.

type Environment

type Environment envtest.Environment

Environment is a drop-in compatible envtest surface. It keeps the same field contract as upstream envtest while allowing backend selection and instrumentation through package internals.

func (*Environment) AddUser

func (e *Environment) AddUser(user envtest.User, baseConfig *rest.Config) (*envtest.AuthenticatedUser, error)

AddUser matches upstream envtest behavior.

func (*Environment) MetricsSnapshot

func (e *Environment) MetricsSnapshot() MetricsSnapshot

MetricsSnapshot returns lifecycle metrics for this Environment.

func (*Environment) SetBackend

func (e *Environment) SetBackend(b Backend)

SetBackend overrides backend selection for this Environment instance.

func (*Environment) Start

func (e *Environment) Start() (*rest.Config, error)

Start boots the environment and returns a rest.Config, following upstream envtest behavior.

func (*Environment) Stop

func (e *Environment) Stop() error

Stop tears down the environment, following upstream envtest behavior.

type EnvtestBackend

type EnvtestBackend struct{}

EnvtestBackend delegates lifecycle operations to upstream envtest directly.

func (EnvtestBackend) Start

func (b EnvtestBackend) Start(env *envtest.Environment) (*rest.Config, error)

func (EnvtestBackend) Stop

func (b EnvtestBackend) Stop(env *envtest.Environment) error

type KplaneBackend

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

KplaneBackend starts a shared kplane-dev/apiserver and points envtest to it. It uses kcp embeddedetcd for local in-process etcd.

func NewKplaneBackend

func NewKplaneBackend() *KplaneBackend

func (*KplaneBackend) Start

func (b *KplaneBackend) Start(env *envtest.Environment) (*rest.Config, error)

func (*KplaneBackend) Stop

func (b *KplaneBackend) Stop(env *envtest.Environment) error

type Metrics

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

Metrics stores lifecycle timings in-memory for tests and assertions.

func NewMetrics

func NewMetrics() *Metrics

func (*Metrics) ObserveStart

func (m *Metrics) ObserveStart(d time.Duration, success bool)

func (*Metrics) ObserveStop

func (m *Metrics) ObserveStop(d time.Duration, success bool)

func (*Metrics) Snapshot

func (m *Metrics) Snapshot() MetricsSnapshot

Snapshot returns counters and percentile timings for lifecycle operations.

type MetricsSnapshot

type MetricsSnapshot struct {
	StartAttempts  int
	StartSuccesses int
	StopAttempts   int
	StopSuccesses  int
	StartP50       time.Duration
	StartP95       time.Duration
	StopP50        time.Duration
	StopP95        time.Duration
}

MetricsSnapshot is a point-in-time view of observed lifecycle metrics.

type SharedEnvtestBackend

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

SharedEnvtestBackend is an experimental backend that reuses a single upstream envtest control plane across multiple Environment users in-process.

func NewSharedEnvtestBackend

func NewSharedEnvtestBackend() *SharedEnvtestBackend

func (*SharedEnvtestBackend) Start

func (*SharedEnvtestBackend) Stop

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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