source

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 22 Imported by: ? modules, 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 source is the single read path for every read-only command. A command talks to a Source and never to a client directly, so the same code serves a live cluster and a must-gather bundle replayed offline.

Nothing in this package writes to a cluster: the interface has no verb that creates, updates, patches or deletes.

Index

Constants

View Source
const (
	// DRADriverName is the DRA driver the GPU operator publishes
	// ResourceSlices under.
	DRADriverName = "gpu.nvidia.com"

	// LabelPCIVendor is set by node-feature-discovery on any node carrying a
	// PCI device with NVIDIA's vendor id (0x10de). It is a hint, not
	// membership: see the discovery rule below.
	LabelPCIVendor = "feature.node.kubernetes.io/pci-10de.present"

	// LabelGPUPresent is set by the GPU operator's feature discovery. It is a
	// hint, not membership.
	LabelGPUPresent = "nvidia.com/gpu.present"

	// ResourceGPU is the extended resource the device plugin advertises.
	ResourceGPU corev1.ResourceName = "nvidia.com/gpu"
)

Variables

View Source
var ErrNotInstalled = errors.New("the GPU Operator is not installed: the cluster serves no nvidia.com/v1 clusterpolicies")

ErrNotInstalled reports that the GPU Operator is not installed: the cluster serves no nvidia.com/v1 clusterpolicies resource, because the CRD was never applied. That is an answer, not a failure, so it carries no exit code and the caller decides what to make of it. A command that only reports on the operator will want to say so plainly; one that also checks nodes can carry on without it.

Functions

This section is empty.

Types

type Bundle

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

Bundle reads a must-gather directory. The files carry Kubernetes-native YAML: the json field names, with quantities as the strings the API serves, which is what "kubectl get -o yaml" prints and what gpu-operator's hack/must-gather.sh writes. They are decoded with sigs.k8s.io/yaml, the same path the API machinery uses, so a quantity keeps its amount.

Nothing is cached between calls. A directory can be replaced while this process holds it, and two reads that disagree are better than one read that quietly describes a directory that is no longer there.

func NewBundle

func NewBundle(dir string) (*Bundle, error)

NewBundle opens the must-gather directory at dir. The directory is checked up front; individual files are read lazily, because a bundle is routinely missing some of them.

func (*Bundle) ClassifyNodes

func (b *Bundle) ClassifyNodes(ctx context.Context) (gpu, hinted []corev1.Node, err error)

ClassifyNodes reads nodes.yaml and, when the bundle carries one, the slice file, and sorts the nodes into the two sets. Both files are decoded on every call.

A missing nodes.yaml is the end of it: there is nothing to classify. A missing slice file is not, but it does mean one of the two membership rules could not be evaluated, so both sets come back with a PartialError naming resourceslices. The answer is then wrong in a known direction: a node that is a GPU node only by its slice falls out of both sets rather than into the wrong one.

func (*Bundle) ClusterPolicies

func (b *Bundle) ClusterPolicies(ctx context.Context) ([]unstructured.Unstructured, error)

ClusterPolicies returns the single ClusterPolicy recorded in cluster_policy.yaml.

func (*Bundle) HintedNodes

func (b *Bundle) HintedNodes(ctx context.Context) ([]corev1.Node, error)

HintedNodes returns the nodes in nodes.yaml that carry an NVIDIA discovery label without backing it with capacity or a slice.

func (*Bundle) Nodes

func (b *Bundle) Nodes(ctx context.Context) ([]corev1.Node, error)

Nodes returns the GPU nodes recorded in nodes.yaml. The same classifyNodes the live path uses is what decides, so the rule lives in one place.

func (*Bundle) Pods

func (b *Bundle) Pods(ctx context.Context, namespace, selector string) ([]corev1.Pod, error)

Pods returns the operand pods recorded in gpu_operand_pods.yaml, filtered by namespace and selector the way the API server would have filtered them.

must-gather records the operator namespace and nothing else, so the bundle cannot answer for the rest of the cluster. A question about another namespace, or about every namespace, is answered with what is there plus a PartialError naming pods: an empty result would otherwise read as "no such pods" when it means "this bundle never looked".

func (*Bundle) ResourceSlices

func (b *Bundle) ResourceSlices(ctx context.Context) ([]resourcev1.ResourceSlice, error)

ResourceSlices returns the slices recorded in resourceslices.yaml that were published by DRADriverName.

A bundle gathered before must-gather started collecting them has no such file, and neither does one where the slice read was denied. That is not an empty cluster, so it is reported as a PartialError naming resourceslices rather than as an empty success. The error names the API resource, not the file, because that is what the caller could not learn about.

type BundleFileTooLargeError

type BundleFileTooLargeError struct {
	Name  string
	Limit int64
}

BundleFileTooLargeError reports a bundle entry past the read cap.

func (*BundleFileTooLargeError) Error

func (e *BundleFileTooLargeError) Error() string

type LegacyBundleError

type LegacyBundleError struct {
	Name string
}

LegacyBundleError reports a bundle written by kubectl-nv v0.1.x, whose files carry lowercased Go field names instead of the json ones.

It is worth a name of its own because the failure is otherwise invisible: a file holding a quantity gives itself away when the decoder refuses the value, but a pod list has no quantity in it and decodes cleanly into a list of empty objects. A reader would see a bundle with no pods in it rather than a bundle it cannot read.

func (*LegacyBundleError) Error

func (e *LegacyBundleError) Error() string

type Live

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

Live reads a running cluster. Both clients are injected so that tests can hand it a fake.

func NewLive

func NewLive(clientset kubernetes.Interface, dyn dynamic.Interface) *Live

NewLive returns a Source backed by the given clients.

func (*Live) ClassifyNodes

func (l *Live) ClassifyNodes(ctx context.Context) (gpu, hinted []corev1.Node, err error)

ClassifyNodes lists every node once and sorts them into the two sets client-side. One paginated list serves the whole classification: neither rule is expressible as a server-side selector (selectors are an AND of terms, and capacity is not selectable at all), and there is no per-node Get, which on a 1200-node cluster would be 1200 round trips for data the list already carried.

A node list that fails ends the call, whatever the reason: without the nodes there is nothing to classify, so there is no partial answer to give.

A denied ResourceSlice list does leave an answer. The capacity rule still holds for most of the cluster, so both sets are returned with a PartialError naming resourceslices, and the answer is then wrong in a known direction: a node that is a GPU node only by its slice looks hinted, or looks like nothing at all. Any other ResourceSlice failure is a plain error, because a cluster that cannot be talked to has not been read at all.

func (*Live) ClusterPolicies

func (l *Live) ClusterPolicies(ctx context.Context) ([]unstructured.Unstructured, error)

ClusterPolicies returns the nvidia.com/v1 ClusterPolicy objects.

A cluster without the GPU Operator has no such CRD, so the API server reports NotFound for the whole collection. That is reported as ErrNotInstalled rather than as a partial read: nothing was denied and nothing is missing, the operator is simply not there.

func (*Live) HintedNodes

func (l *Live) HintedNodes(ctx context.Context) ([]corev1.Node, error)

HintedNodes returns the nodes that carry an NVIDIA discovery label without backing it with capacity or a ResourceSlice.

func (*Live) Nodes

func (l *Live) Nodes(ctx context.Context) ([]corev1.Node, error)

Nodes returns the GPU nodes. See source.go for what makes a node one.

func (*Live) Pods

func (l *Live) Pods(ctx context.Context, namespace, selector string) ([]corev1.Pod, error)

Pods lists the pods in namespace matching selector. An empty namespace means every namespace.

func (*Live) ResourceSlices

func (l *Live) ResourceSlices(ctx context.Context) ([]resourcev1.ResourceSlice, error)

ResourceSlices returns the slices published by DRADriverName. The driver is filtered client-side: a field selector on spec.driver would cost a second pass over the same data the node classification already needs.

type MissingBundleError

type MissingBundleError struct {
	Dir string
	Err error
}

MissingBundleError reports that the path given to NewBundle is not a readable must-gather directory.

func (*MissingBundleError) Error

func (e *MissingBundleError) Error() string

func (*MissingBundleError) Unwrap

func (e *MissingBundleError) Unwrap() error

Unwrap exposes the stat failure to errors.Is, so a caller can still ask whether the path was absent or merely unreadable.

type PartialError

type PartialError struct {
	// Resource names what could not be read: an API resource for a live
	// cluster ("resourceslices"), a file name for a bundle ("nodes.yaml").
	Resource string
	// Err is the underlying failure, kept so that callers can test it with
	// apierrors.IsForbidden and friends.
	Err error
}

PartialError reports that one resource could not be read while the rest of the run succeeded. The items returned alongside it are usable: a caller must not treat a PartialError as "no result".

It is deliberately narrow. Only two things produce one: an RBAC denial on a secondary resource (pods, resourceslices, clusterpolicies), and a file a must-gather bundle does not contain. Everything else, a node list that failed, a transport error, a dead context, an expired or rejected credential, means the run did not happen and is reported as a plain error. The difference is the exit code, and the exit code is the contract: 3 says "these findings are incomplete, here is what I could not see", 4 says "conclude nothing from this run".

When more than one resource is lost, each gets its own PartialError and they are combined with errors.Join, so every name survives and errors.As still reaches one.

func (*PartialError) Error

func (e *PartialError) Error() string

func (*PartialError) ExitCode

func (e *PartialError) ExitCode() int

ExitCode makes this error carry its own exit status, which is what output.ExitCodeFor reads. The value is output.ExitPartial; it is written out rather than imported so that this package does not depend on the one that formats its results.

func (*PartialError) Unwrap

func (e *PartialError) Unwrap() error

Unwrap exposes the underlying failure to errors.Is and errors.As.

type Source

type Source interface {
	// Nodes returns the GPU nodes, sorted by name and free of duplicates.
	Nodes(ctx context.Context) ([]corev1.Node, error)

	// HintedNodes returns the nodes that carry a hint label but are not GPU
	// nodes, sorted by name and free of duplicates. A node is never in both
	// this set and Nodes.
	HintedNodes(ctx context.Context) ([]corev1.Node, error)

	// ClassifyNodes returns both sets from one read. Nodes and HintedNodes
	// are the halves of it, and a caller that wants both should ask for both
	// here rather than call them in turn: that is one pass over the cluster
	// instead of two, and the two halves are then guaranteed to describe the
	// same moment.
	ClassifyNodes(ctx context.Context) (gpu, hinted []corev1.Node, err error)

	// Pods returns the pods in namespace matching selector. An empty
	// namespace means every namespace, and an empty selector means every pod.
	Pods(ctx context.Context, namespace, selector string) ([]corev1.Pod, error)

	// ResourceSlices returns the ResourceSlices published by DRADriverName.
	ResourceSlices(ctx context.Context) ([]resourcev1.ResourceSlice, error)

	// ClusterPolicies returns the nvidia.com/v1 ClusterPolicy objects.
	ClusterPolicies(ctx context.Context) ([]unstructured.Unstructured, error)
}

Source reads the cluster state the read-only commands need. Every method takes a context and returns API objects: shaping those into an inventory model is the caller's job, not this package's.

type UnsafeBundleFileError

type UnsafeBundleFileError struct {
	Name string
	Mode fs.FileMode
}

UnsafeBundleFileError reports a bundle entry that is not a regular file. A symlink could point anywhere on the machine, and opening a FIFO with no writer blocks forever, so neither is followed.

func (*UnsafeBundleFileError) Error

func (e *UnsafeBundleFileError) Error() string

Jump to

Keyboard shortcuts

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