Documentation
¶
Overview ¶
Package output carries the machine readable output contract of kubectl-nv: the API version and kinds every command stamps its output with, the printers behind -o, the exit code mapping, a stderr logger, and a deterministic order for findings.
The kinds themselves live in the packages that own them. A command defines its own typed payload, for instance
type GPUNode struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitzero"`
Spec GPUNodeSpec `json:"spec,omitzero"`
Status GPUNodeStatus `json:"status,omitzero"`
}
stamps it with TypeMetaFor(KindGPUNode), implements TableRows on it, and hands it to a Printer from this package. Object and List here are the small generic forms used by this package's own tests and by output that carries nothing but names.
Machine readable output is rendered by the k8s.io/cli-runtime printers, which marshal through sigs.k8s.io/yaml. That package reads the JSON struct tags for both JSON and YAML output, so the JSON tags are what the wire format follows; the yaml tags are there for callers that reach for a plain YAML marshaller.
Index ¶
- Constants
- Variables
- func ExitCode(status ReportStatus, failOn Severity) int
- func ExitCodeFor(err error) int
- func SortFindings(findings []Finding)
- func TypeMetaFor(kind string) metav1.TypeMeta
- type Column
- type Finding
- type Format
- type List
- type Logger
- type LoggerOptions
- type Object
- type Printer
- type ReportStatus
- type Severity
- type TablePrinter
- type TableRows
- type Unreachable
- type UsageError
Constants ¶
const ( // GroupName is the API group every kubectl-nv envelope is stamped with. GroupName = "kubectl-nv.nvidia.com" // Version is the API version of the envelope. Version = "v1alpha1" // APIVersion is the group and version as they appear on the wire. APIVersion = GroupName + "/" + Version )
const ( // KindGPUNodeList is a list of GPU capable nodes. KindGPUNodeList = "GPUNodeList" // KindGPUNode is a single GPU capable node. KindGPUNode = "GPUNode" // KindHealthReport is the result of a health check run. KindHealthReport = "HealthReport" // KindTopologyReport is the device topology of one node or of the cluster. KindTopologyReport = "TopologyReport" // KindBundleManifest is the index of a must-gather bundle. KindBundleManifest = "BundleManifest" )
The kinds kubectl-nv emits. The payload of each one is defined by the package that produces it; the kinds are fixed here so that every consumer, and every track, can switch on them from day one.
const ( // ExitOK means the run completed and found nothing at or above --fail-on. ExitOK = 0 // ExitFindings means the run found something at or above --fail-on. ExitFindings = 1 // ExitUsage means the command line or the configuration was wrong. ExitUsage = 2 // ExitPartial means the run completed over less than it was asked for, for // instance because RBAC or a denied exec hid part of the cluster. What was // missed is listed in the output. ExitPartial = 3 // ExitError means the run could not happen: the API server was unreachable, // or kubectl-nv hit an internal error. Nothing was checked, so nothing can // be concluded from it. ExitError = 4 )
The exit codes kubectl-nv uses. They are part of the contract: scripts branch on them, so a new meaning needs a new code rather than a new reading of one.
Variables ¶
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: Version}
GroupVersion is the group and version of every kind in this contract.
Functions ¶
func ExitCode ¶
func ExitCode(status ReportStatus, failOn Severity) int
ExitCode maps a report onto the process exit code. failOn is the threshold from --fail-on, as returned by ParseSeverity; its empty value means SeverityError.
Findings win over partial results: a run that found something at or above the threshold exits ExitFindings even when part of the cluster stayed out of reach, because the caller needs to hear about what was found first. A run that found nothing but could not see everything exits ExitPartial, so that "clean" is never reported for a scan that did not happen.
func ExitCodeFor ¶
ExitCodeFor maps an error onto the process exit code. Any error that carries its own code, this package's UsageError included, decides for itself; anything else means the run could not happen, which is ExitError.
func SortFindings ¶
func SortFindings(findings []Finding)
SortFindings orders findings by node name and then by code, in place. The sort is stable, so findings that share a node and a code keep the order the command produced them in. Output has to be byte identical across runs for diffs and golden files to be worth anything.
func TypeMetaFor ¶
TypeMetaFor returns the TypeMeta a kind of this contract is stamped with. It is how a payload type in another package joins the contract.
Types ¶
type Column ¶
type Column struct {
// Header is the column title.
Header string
}
Column is one column of the table output.
type Finding ¶
type Finding struct {
// Node is the node the finding is about. It is empty for a finding about
// the cluster as a whole.
Node string `json:"node,omitempty" yaml:"node,omitempty"`
// Code is the stable identifier of the check that produced the finding.
Code string `json:"code,omitempty" yaml:"code,omitempty"`
// Severity decides whether this finding fails the run.
Severity Severity `json:"severity,omitempty" yaml:"severity,omitempty"`
// Message is the human readable detail.
Message string `json:"message,omitempty" yaml:"message,omitempty"`
}
Finding is one thing a command noticed about one node.
type Format ¶
type Format string
Format is the value of the -o flag.
const ( // FormatTable is the human readable default. FormatTable Format = "table" // FormatWide is the table with the extra columns. FormatWide Format = "wide" // FormatJSON is the envelope as JSON. FormatJSON Format = "json" // FormatYAML is the envelope as YAML. FormatYAML Format = "yaml" // FormatName prints one "kind/name" line per item. FormatName Format = "name" )
func ParseFormat ¶
ParseFormat turns an -o value into a Format. An empty value is the table default; anything unrecognised is a usage error, which exits 2.
type List ¶
type List struct {
metav1.TypeMeta `json:",inline" yaml:",inline"`
metav1.ListMeta `json:"metadata,omitzero" yaml:"metadata,omitempty"`
// Items keeps the order the command produced. Nothing downstream reorders
// it, so a command that wants a stable order sorts before handing it over.
Items []Object `json:"items" yaml:"items"`
}
List is the generic envelope holding several Objects of one kind.
func (*List) DeepCopyInto ¶
DeepCopyInto copies the receiver into out.
func (*List) DeepCopyObject ¶
DeepCopyObject implements runtime.Object.
func (List) MarshalJSON ¶
MarshalJSON renders a nil Items as an empty array. A consumer that has to special case null before it can range over items is a consumer that will get it wrong, and the guarantee belongs on the type rather than on NewList.
func (*List) TableColumns ¶
TableColumns implements TableRows.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger writes progress and warnings to the error stream. Nothing it prints ever reaches stdout, which belongs to the machine readable output alone, and it never asks the user anything: every command has to stay usable in a pipe.
It is safe to use from several goroutines, because a scan reports progress from whichever goroutine noticed something.
func NewLogger ¶
func NewLogger(streams genericiooptions.IOStreams, opts LoggerOptions) *Logger
NewLogger returns a logger writing to the error stream of streams. Color is used only when the caller did not pass --no-color, NO_COLOR is not set in the environment, and the error stream is a terminal.
type LoggerOptions ¶
type LoggerOptions struct {
// NoColor turns color off whatever the stream is, for --no-color.
NoColor bool
// IsTerminal reports whether the error stream is a terminal. A nil value
// means the real check; a test passes its own so that the color decision
// can be exercised without a pty.
IsTerminal func(w io.Writer) bool
}
LoggerOptions configures a Logger.
type Object ¶
type Object struct {
metav1.TypeMeta `json:",inline" yaml:",inline"`
// ObjectMeta carries the name the output is keyed by. It is the apimachinery
// type so that the printers can read the name through meta.Accessor.
metav1.ObjectMeta `json:"metadata,omitzero" yaml:"metadata,omitempty"`
}
Object is the generic envelope entry: a kind and a name, with no payload. A command with a payload defines its own type instead.
func (*Object) DeepCopyInto ¶
DeepCopyInto copies the receiver into out.
func (*Object) DeepCopyObject ¶
DeepCopyObject implements runtime.Object.
func (*Object) TableColumns ¶
TableColumns implements TableRows.
type Printer ¶
Printer renders a kind of this contract onto a writer.
func NewPrinter ¶
NewPrinter returns the printer for an -o value, or a usage error when the value is not one this package knows.
type ReportStatus ¶
type ReportStatus struct {
// Findings is what the run found, in the order it should be printed.
Findings []Finding `json:"findings" yaml:"findings"`
// Partial is set when the run could not see everything it was asked about.
Partial bool `json:"partial,omitempty" yaml:"partial,omitempty"`
// Unreachable lists what Partial is about, so that a reader never has to
// guess which part of the cluster is missing from the findings.
Unreachable []Unreachable `json:"unreachable,omitempty" yaml:"unreachable,omitempty"`
}
ReportStatus is the outcome of a run: what it found, and what it could not see. Every command that reports findings embeds it, and the exit code is read from it.
func (ReportStatus) MarshalJSON ¶
func (s ReportStatus) MarshalJSON() ([]byte, error)
MarshalJSON renders a nil Findings as an empty array, for the same reason List does: ranging over the result must not need a null check first.
type Severity ¶
type Severity string
Severity is how bad one finding is.
func ParseSeverity ¶
ParseSeverity turns a --fail-on value into a Severity. An empty value means SeverityError, the conservative default. Anything else that is not a severity is a usage error: a typo in --fail-on is the difference between a gate and no gate, so it has to stop the run rather than be read as something else.
type TablePrinter ¶
type TablePrinter struct {
// Wide turns on the columns the kind marks as wide.
Wide bool
}
TablePrinter renders a kind that implements TableRows as an aligned table. Rows keep the order the kind produced them in.
type TableRows ¶
type TableRows interface {
// TableColumns returns the headers, with the wide ones when wide is set.
TableColumns(wide bool) []Column
// TableRows returns one row per item, each with one cell per column.
TableRows(wide bool) [][]string
}
TableRows is implemented by a kind that can render itself as a table. It is how a payload type in another package gets a default and a wide table without this package knowing any of its fields.
type Unreachable ¶
type Unreachable struct {
// Resource is what could not be read, for instance "nodes/node-a/proxy".
Resource string `json:"resource,omitempty" yaml:"resource,omitempty"`
// Node is the node it belongs to, empty for a cluster scoped resource.
Node string `json:"node,omitempty" yaml:"node,omitempty"`
// Reason is why it could not be read, for instance "forbidden".
Reason string `json:"reason,omitempty" yaml:"reason,omitempty"`
}
Unreachable is one thing a run was not allowed, or not able, to look at.
type UsageError ¶
type UsageError struct {
// Err is the cause, when there is one to unwrap to.
Err error
// contains filtered or unexported fields
}
UsageError is a command line or configuration error: something the user can fix by typing something else.
func NewUsageError ¶
func NewUsageError(format string, a ...any) *UsageError
NewUsageError returns a UsageError with the formatted message and no cause.
func WrapUsageError ¶
func WrapUsageError(err error, format string, a ...any) *UsageError
WrapUsageError returns a UsageError carrying err as its cause.
func (*UsageError) ExitCode ¶
func (e *UsageError) ExitCode() int
ExitCode is the code a command exits with after a usage error.
func (*UsageError) Unwrap ¶
func (e *UsageError) Unwrap() error
Unwrap returns the cause, so that errors.Is and errors.As reach it.