chainsaw

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 27 Imported by: 1 module, 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 chainsaw executes Chainsaw-style assertions against a live Kubernetes cluster, in-process. It supports two modes:

  • Raw K8s resource YAML: pure field matching via the chainsaw Go library (assertRawResources → checks.Check).
  • Chainsaw Test format (apiVersion: chainsaw.kyverno.io/v1alpha1): walks Spec.Steps[].Try[] and dispatches the assert / error operations to the same checks.Check engine (runChainsawTestInProcess in inprocess.go).

The earlier `runChainsawBinary` path that exec'd /usr/local/bin/chainsaw was removed in #1236; the read-only allowlist (pkg/chainsaw/allowlist.go) restricts registry- declared content to assert/error only, which is exactly the subset the in-process executor implements. No external binary is shipped or invoked.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertedResources

func AssertedResources(yamlContent string) ([]map[string]any, error)

AssertedResources returns the resource of each assert operation in the try blocks of the Tests in yamlContent, in document order, skipping error operations. It returns an error if a document is not a Test or an assert has no resource mapping.

func IsChainsawTest

func IsChainsawTest(raw string) bool

IsChainsawTest reports whether any YAML document is a Chainsaw Test. See validation.IsChainsawTest for detection rules.

func NewDynamicClientForConfig

func NewDynamicClientForConfig(restConfig *rest.Config) (dynamic.Interface, error)

NewDynamicClientForConfig builds the dynamic client the fetcher reads through, carrying the same request bound NewRESTMapperForConfig applies. Exported for callers that construct the two halves separately (the deployment validator keeps its own client so ctx.DynamicClient stays an injection seam) — going through it keeps both halves bounded alike.

Client construction itself lives in pkg/k8s/client: a caller that only wants a dynamic client must not have to depend on this in-process test executor.

func NewRESTMapperForConfig

func NewRESTMapperForConfig(restConfig *rest.Config) (meta.RESTMapper, error)

NewRESTMapperForConfig builds the discovery-backed RESTMapper the fetcher uses to resolve a GroupVersionKind to a resource and its scope.

Prefer NewClusterFetcherWithClient when the mapper is destined for a fetcher: it also wires the partial-discovery probe, which a mapper alone cannot carry.

Construction itself lives in pkg/k8s/client, for the reason NewDynamicClientForConfig gives.

func ValidateTestReadOnly

func ValidateTestReadOnly(component, yamlContent string) error

ValidateTestReadOnly rejects Chainsaw Test operations outside the read-only allowlist. See validation.ValidateTestReadOnly for the validation rules.

Types

type ClusterFetcherOption

type ClusterFetcherOption func(*clusterFetcher)

ClusterFetcherOption configures a cluster fetcher.

func WithGroupDiscovery

func WithGroupDiscovery(d groupDiscoverer) ClusterFetcherOption

WithGroupDiscovery supplies the discovery client the fetcher consults when the RESTMapper reports no match for a kind, so a group that discovery could not enumerate is reported as ErrCodeUnavailable rather than "this cluster does not serve that kind".

Pass the SAME cached discovery client that backs the mapper. Production callers get this wiring for free from NewClusterFetcherForConfig / NewClusterFetcherWithClient.

type ComponentAssert

type ComponentAssert struct {
	// Name is the component name (e.g., "gpu-operator").
	Name string

	// AssertYAML is the raw Chainsaw assert file content.
	AssertYAML string
}

ComponentAssert holds the data needed to run assertions for one component.

type ResourceFetcher

type ResourceFetcher interface {
	// Fetch retrieves a single Kubernetes resource as an unstructured map.
	// Returns ErrCodeNotFound when the resource doesn't exist.
	Fetch(ctx context.Context, apiVersion, kind, namespace, name string) (map[string]any, error)

	// List enumerates Kubernetes resources of the given kind in the
	// given namespace, optionally narrowed by labels (empty = no
	// selector). Cluster-scoped resources should pass an empty
	// namespace. Returns an empty slice (not error) when no resources
	// match — the caller distinguishes "list returned empty" from
	// "list call failed".
	//
	// Added in #1236 so the in-process Chainsaw Test executor can
	// handle assertions / error blocks that target a namespace + label
	// selector without specifying a resource name (the pod-phase /
	// container-state patterns that dominate the registry-declared
	// health checks).
	List(ctx context.Context, apiVersion, kind, namespace string, labels map[string]string) ([]map[string]any, error)
}

ResourceFetcher abstracts fetching Kubernetes resources for testability.

func NewClusterFetcher

func NewClusterFetcher(client dynamic.Interface, mapper meta.RESTMapper, opts ...ClusterFetcherOption) ResourceFetcher

NewClusterFetcher creates a ResourceFetcher that queries a live Kubernetes cluster.

Without WithGroupDiscovery the fetcher cannot tell a genuine no-match apart from a kind whose API group failed discovery, and classifies a bare NoKindMatchError as ErrCodeNotFound. That is the fail-open direction for a negative assertion, so every production path builds the fetcher through NewClusterFetcherForConfig or NewClusterFetcherWithClient, which wire the probe. This constructor remains the injection seam for tests that supply a hand-built RESTMapper.

func NewClusterFetcherForConfig

func NewClusterFetcherForConfig(restConfig *rest.Config) (ResourceFetcher, error)

NewClusterFetcherForConfig builds a ResourceFetcher from a client configuration, constructing the dynamic client it reads through, the discovery-backed RESTMapper it resolves scope with, and the partial-discovery probe that keeps a no-match honest.

Callers that already hold a dynamic client (the deployment validator keeps ctx.DynamicClient as an injection seam) should use NewClusterFetcherWithClient so the mapper and probe wiring is still shared.

func NewClusterFetcherWithClient

func NewClusterFetcherWithClient(client dynamic.Interface, restConfig *rest.Config) (ResourceFetcher, error)

NewClusterFetcherWithClient builds a ResourceFetcher around a caller-supplied dynamic client, deriving the RESTMapper and its discovery probe from restConfig. Both are backed by one cached discovery client, so invalidating the mapper's cache also invalidates the probe's view.

type Result

type Result struct {
	// Component is the component name.
	Component string

	// Passed indicates whether the assertion passed.
	Passed bool

	// Output contains diagnostic detail for failures.
	Output string

	// Error contains any error from executing the assertion.
	Error error
}

Result holds the outcome of an assertion run for one component.

func Run

func Run(ctx context.Context, asserts []ComponentAssert, timeout time.Duration, fetcher ResourceFetcher) []Result

Run executes assertions for a set of components against live cluster resources. Components are run concurrently with bounded parallelism. Chainsaw Test format dispatches to the in-process executor (runChainsawTestInProcess); raw K8s resource YAML uses the Go library assertion engine (assertRawResources).

Directories

Path Synopsis
Package validation checks Chainsaw YAML without executing assertions.
Package validation checks Chainsaw YAML without executing assertions.

Jump to

Keyboard shortcuts

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