testutils

package
v1.21.0-pre.2 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Scheme for object types used in Cilium. Excludes the core Kubernetes schema
	// in favour of the slim one.
	//
	// The scheme can be extended by init() functions since [Decoder] is
	// lazily constructed.
	Scheme = runtime.NewScheme()

	// KubernetesScheme is the core Kubernetes scheme.
	KubernetesScheme = runtime.NewScheme()
)
View Source
var (
	DefaultVersion = "1.26"

	// APIResources is the list of API resources for the k8s version that we're mocking.
	// This is mostly relevant for the feature detection at pkg/k8s/version/version.go.
	// The lists here are currently not exhaustive and expanded on need-by-need basis.
	APIResources = map[string][]*metav1.APIResourceList{
		"1.16": {
			CoreV1APIResources,
			CiliumV2APIResources,
		},
		"1.24": {
			CoreV1APIResources,
			DiscoveryV1APIResources,
			DiscoveryV1Beta1APIResources,
			CiliumV2APIResources,
		},
		"1.25": {
			CoreV1APIResources,
			DiscoveryV1APIResources,
			CiliumV2APIResources,
		},
		"1.26": {
			CoreV1APIResources,
			DiscoveryV1APIResources,
			CiliumV2APIResources,
		},
	}

	CoreV1APIResources = &metav1.APIResourceList{
		GroupVersion: corev1.SchemeGroupVersion.String(),
		APIResources: []metav1.APIResource{
			{Name: "nodes", Kind: "Node"},
			{Name: "pods", Namespaced: true, Kind: "Pod"},
			{Name: "services", Namespaced: true, Kind: "Service"},
			{Name: "endpoints", Namespaced: true, Kind: "Endpoint"},
		},
	}

	CiliumV2APIResources = &metav1.APIResourceList{
		TypeMeta:     metav1.TypeMeta{},
		GroupVersion: cilium_v2.SchemeGroupVersion.String(),
		APIResources: []metav1.APIResource{
			{Name: cilium_v2.CNPluralName, Kind: cilium_v2.CNKindDefinition},
			{Name: cilium_v2.CEPPluralName, Namespaced: true, Kind: cilium_v2.CEPKindDefinition},
			{Name: cilium_v2.CIDPluralName, Namespaced: true, Kind: cilium_v2.CIDKindDefinition},
			{Name: cilium_v2.CEGPPluralName, Namespaced: true, Kind: cilium_v2.CEGPKindDefinition},
			{Name: cilium_v2.CNPPluralName, Namespaced: true, Kind: cilium_v2.CNPKindDefinition},
			{Name: cilium_v2.CCNPPluralName, Namespaced: true, Kind: cilium_v2.CCNPKindDefinition},
			{Name: cilium_v2.CLRPPluralName, Namespaced: true, Kind: cilium_v2.CLRPKindDefinition},
			{Name: cilium_v2.CCECPluralName, Namespaced: true, Kind: cilium_v2.CCECKindDefinition},
			{Name: cilium_v2.CECPluralName, Namespaced: true, Kind: cilium_v2.CECKindDefinition},
		},
	}

	DiscoveryV1APIResources = &metav1.APIResourceList{
		TypeMeta:     metav1.TypeMeta{},
		GroupVersion: discov1.SchemeGroupVersion.String(),
		APIResources: []metav1.APIResource{
			{Name: "endpointslices", Namespaced: true, Kind: "EndpointSlice"},
		},
	}

	DiscoveryV1Beta1APIResources = &metav1.APIResourceList{
		GroupVersion: discov1beta1.SchemeGroupVersion.String(),
		APIResources: []metav1.APIResource{
			{Name: "endpointslices", Namespaced: true, Kind: "EndpointSlice"},
		},
	}
)

Functions

func DecodeFile

func DecodeFile(path string) (runtime.Object, error)

func DecodeKubernetesObject added in v1.17.4

func DecodeKubernetesObject(bytes []byte) (runtime.Object, *schema.GroupVersionKind, error)

func DecodeObject

func DecodeObject(bytes []byte) (runtime.Object, error)

func DecodeObjectGVK

func DecodeObjectGVK(bytes []byte) (runtime.Object, *schema.GroupVersionKind, error)

func Decoder

func Decoder() runtime.Decoder

Decoder returns an object decoder for Cilium and Slim objects. The DecodeObject and DecodeFile functions are provided as shorthands for decoding from bytes and files respectively.

Not strict, as the slim types leave out fields that are valid in the full Kubernetes ones. See StrictDecoder.

func KubernetesDecoder added in v1.18.0

func KubernetesDecoder() runtime.Decoder

KubernetesDecoder returns a decoder for the core Kubernetes objects. Unlike Decoder it rejects unknown fields, which the full types can afford as they are not trimmed the way the slim ones are.

func RegisterCRDSchemas

func RegisterCRDSchemas(crds func(logger *slog.Logger) []apiextensionsv1.CustomResourceDefinition)

RegisterCRDSchemas adds CRDs for ValidateCRD to validate against. Call it from an init(), as the CRDs are read on the first validation. Their kinds have to be in Scheme too, since the validation decodes them.

func StrictDecoder

func StrictDecoder() runtime.Decoder

StrictDecoder is like Decoder, but rejects unknown fields instead of dropping them.

func ValidateCRD

func ValidateCRD(logger *slog.Logger, rawYAML []byte, schemaValidation bool) error

ValidateCRD validates a raw CRD YAML document against the embedded CRD schemas, like the kube-apiserver does. Kinds without a known CRD schema are accepted as-is.

With schemaValidation the document is validated against the CRD OpenAPI schema. Unknown fields are rejected either way, as a typo is never intentional.

The input must be the raw YAML and not a decoded object, since the typed decode drops the unknown fields we want to catch.

Types

type FakeListerWatcher

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

FakeListerWatcher implements a fake cache.ListerWatcher that can be fed objects directly, or from YAML files or strings. The YAML data is decoded with a schema that covers the Cilium CRDs and Slim schemas.

func NewFakeListerWatcher

func NewFakeListerWatcher(objs ...runtime.Object) *FakeListerWatcher

func (*FakeListerWatcher) Delete

func (f *FakeListerWatcher) Delete(obj runtime.Object)

func (*FakeListerWatcher) DeleteFromFile

func (f *FakeListerWatcher) DeleteFromFile(path string) error

func (*FakeListerWatcher) DeleteFromText

func (f *FakeListerWatcher) DeleteFromText(content string) error

func (*FakeListerWatcher) List

func (f *FakeListerWatcher) List(options v1.ListOptions) (runtime.Object, error)

List implements cache.ListerWatcher.

func (*FakeListerWatcher) Stop

func (f *FakeListerWatcher) Stop()

func (*FakeListerWatcher) Upsert

func (f *FakeListerWatcher) Upsert(obj runtime.Object)

func (*FakeListerWatcher) UpsertFromFile

func (f *FakeListerWatcher) UpsertFromFile(path string) error

func (*FakeListerWatcher) UpsertFromText

func (f *FakeListerWatcher) UpsertFromText(content string) error

func (*FakeListerWatcher) Watch

func (f *FakeListerWatcher) Watch(options v1.ListOptions) (watch.Interface, error)

Watch implements cache.ListerWatcher.

Jump to

Keyboard shortcuts

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