resource

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package resource defines the core domain types for Kubernetes resource tracking. These types are independent of any TUI framework and can be used by the store, simulation, analysis, and presentation layers.

Index

Constants

View Source
const NearSimultaneousWindow = time.Second

NearSimultaneousWindow is the wall-clock gap below which two adjacent revisions are treated as near-simultaneous: their relative order is inferred (from resourceVersion, or arrival) rather than clearly separated in time.

Variables

This section is empty.

Functions

func CloneMap

func CloneMap(m map[string]any) map[string]any

CloneMap performs a recursive clone of a map[string]any. It recursively clones nested maps and slices, but shares scalar values (which are immutable in Go). Non-JSON collection shapes other than map[string]any, []any, and []map[string]any are shared by reference; this is safe for the unstructured Kubernetes objects loog handles, which decode only into those shapes.

func CompareRevisionsNewestFirst

func CompareRevisionsNewestFirst(a, b Revision) int

CompareRevisionsNewestFirst orders two revisions newest-first. It prefers resourceVersion (a global, causal order on etcd-backed clusters) when both revisions carry one, and falls back to wall-clock time otherwise. Returns a value suitable for sort funcs (<0 if a should sort before b).

func DeepEqual

func DeepEqual(a, b map[string]any) bool

DeepEqual compares two maps for equality using the fast path from diffmap (type-switch based) which avoids the cost of reflect.DeepEqual for the common types found in Kubernetes objects.

func FormatTimestamp

func FormatTimestamp(t time.Time) string

FormatTimestamp returns a short timestamp like "14:32:05".

func GroupTimelineByBurst

func GroupTimelineByBurst(entries []TimelineEntry, window time.Duration) []any

GroupTimelineByBurst groups timeline entries into bursts. Entries within the given window duration of each other are grouped together. Returns a slice of either TimelineEntry or BurstGroup values.

func MatchesSubstring

func MatchesSubstring(query string, r Resource) bool

MatchesSubstring returns true if the query (already lowercased) appears as a case-insensitive substring in any of the resource's name, kind, namespace, or kind/name combination. Returns true for an empty query.

func RelativeTime

func RelativeTime(t time.Time) string

RelativeTime formats a time as a human-readable relative string (e.g., "5m", "2h").

func SortByKindName

func SortByKindName(rds []*Data)

SortByKindName sorts a slice of *Data by kind then name (ascending).

func SortTimelineNewestFirst

func SortTimelineNewestFirst(entries []TimelineEntry)

SortTimelineNewestFirst sorts timeline entries in place, newest-first, using causal order where resourceVersion is available (see CompareRevisionsNewestFirst).

func WindowHalfDuration

func WindowHalfDuration(w WindowMode) time.Duration

WindowHalfDuration returns the half-span for a WindowMode. Returns 0 for WindowAll (no filter).

Types

type AnalysisResult

type AnalysisResult struct {
	ResourceUID string
	Tags        map[RevisionID][]ChangeTag
	LoopInfo    LoopInfo
}

AnalysisResult holds the result of background analysis for a resource.

func Analyze

func Analyze(rd *Data, loopWindowSize int) AnalysisResult

Analyze performs synchronous analysis on a Data: tags each revision and detects reconcile loops. This is a pure computation with no side effects.

type BurstGroup

type BurstGroup struct {
	Entries []TimelineEntry
}

BurstGroup represents a group of timeline entries that occurred within a short window, likely from a single operator reconciliation cycle.

type ChangeTag

type ChangeTag string

ChangeTag classifies what kind of change a revision represents.

const (
	TagSpec     ChangeTag = "spec"
	TagStatus   ChangeTag = "status"
	TagImage    ChangeTag = "image"
	TagLabels   ChangeTag = "labels"
	TagConfig   ChangeTag = "config"
	TagReplicas ChangeTag = "replicas"
	TagUnknown  ChangeTag = "unknown"
)

func TagRevision

func TagRevision(prev, curr map[string]any) []ChangeTag

TagRevision classifies a revision by comparing two object states. Returns one or more tags describing what changed.

type CompareItem

type CompareItem struct {
	Resource Resource
	Revision Revision
}

CompareItem is one side of a comparison.

type CompareSelection

type CompareSelection struct {
	Left  *CompareItem
	Right *CompareItem
}

CompareSelection holds the two items being compared.

type Data

type Data struct {
	Resource  Resource
	Revisions []Revision // sorted oldest-first (index 0 = oldest)
}

Data holds a resource and all its revisions.

func (*Data) AnalyzeLoop

func (rd *Data) AnalyzeLoop(windowSize int) LoopInfo

AnalyzeLoop performs detailed loop analysis on recent revisions.

func (*Data) ChangeFrequency

func (rd *Data) ChangeFrequency() float64

ChangeFrequency returns changes per minute over the resource's lifetime.

func (*Data) CreationTime

func (rd *Data) CreationTime() time.Time

CreationTime returns the Kubernetes creationTimestamp from the first revision's object metadata. Returns zero time if unavailable.

func (*Data) DetectLoop

func (rd *Data) DetectLoop(windowSize int) bool

DetectLoop checks if the resource is oscillating between states. It returns true if among the last windowSize revisions, the same object state appears more than once (indicating a reconcile loop).

func (*Data) LatestRevision

func (rd *Data) LatestRevision() *Revision

LatestRevision returns the most recent revision, or nil if empty.

func (*Data) RevisionCount

func (rd *Data) RevisionCount() int

type EventType

type EventType string

EventType represents the type of Kubernetes watch event.

const (
	EventAdded    EventType = "ADDED"
	EventModified EventType = "MODIFIED"
	EventDeleted  EventType = "DELETED"
)

func (EventType) Symbol

func (et EventType) Symbol() string

Symbol returns a compact single-character symbol for the event type.

type Kind

type Kind struct {
	Kind       string // e.g., "Pod", "Deployment", "Secret"
	APIVersion string // e.g., "v1", "apps/v1"
	Resource   string // plural resource name, e.g., "pods", "deployments"
	Namespaced bool   // whether instances live in a namespace
}

Kind represents a Kubernetes resource type (CRD or built-in) available on the cluster.

func (Kind) GVR

func (rk Kind) GVR() string

GVR returns the "group/version/resource" string, e.g. "apps/v1/deployments" or "v1/pods".

func (Kind) String

func (rk Kind) String() string

String returns the Kind name (used for display and fuzzy matching).

type KindGroup

type KindGroup struct {
	Kind      string
	Resources []*Data
	Expanded  bool
}

KindGroup represents a collapsible group in the resource tree.

func BuildKindGroups

func BuildKindGroups(resources []*Data) []*KindGroup

BuildKindGroups organizes resources into kind groups for tree display. Groups are sorted in a preferred Kubernetes kind order, and resources within each group are sorted by name.

type LoopInfo

type LoopInfo struct {
	IsLoop bool

	// DistinctStates is the number of unique object states in the loop (e.g., 2 for A<->B).
	DistinctStates int
	// Cycles counts complete oscillation cycles. For A->B->A->B->A, there are 2 full cycles.
	Cycles int
	// Period is the average duration of one full cycle.
	Period    time.Duration
	FirstSeen time.Time

	// LoopRevisions maps revision IDs to their state group label ("A", "B", ...).
	LoopRevisions map[RevisionID]string

	// PatternSample is a short pre-built sample of the oscillation pattern
	// from the tail of the window, e.g., "A->B->A->B". At most 6 labels.
	PatternSample string
}

LoopInfo holds detailed information about a detected reconcile loop.

type Resource

type Resource struct {
	UID       string
	Kind      string
	Name      string
	Namespace string
	Starred   bool
}

Resource represents a tracked Kubernetes resource instance.

func (Resource) KindName

func (r Resource) KindName() string

KindName returns "Kind/name" (e.g., "Pod/nginx-abc").

func (Resource) ShortName

func (r Resource) ShortName(maxLen int) string

ShortName returns a truncated name for narrow panels.

type Revision

type Revision struct {
	ID         RevisionID
	PreviousID RevisionID
	EventType  EventType
	Time       time.Time
	Object     map[string]any // full state at this revision
	Patch      map[string]any // diff from previous (nil for ADD/snapshots)
	// ResourceVersion is the Kubernetes metadata.resourceVersion parsed as a
	// uint64, or 0 if absent/unparseable. On etcd-backed clusters it is a
	// global, monotonically increasing revision, so it orders changes across
	// different resources by true causal order - unlike Time, which is only
	// loog's observation time and can invert for near-simultaneous events on
	// separate watch streams.
	ResourceVersion uint64
}

Revision represents a single recorded version of a resource.

type RevisionID

type RevisionID = store.RevisionID

RevisionID is a 64-bit revision identifier displayed as hex. It is a type alias for store.RevisionID so the two are interchangeable.

type TimelineEntry

type TimelineEntry struct {
	Resource Resource
	Revision Revision
}

TimelineEntry represents a single entry in the unified timeline.

type WindowMode

type WindowMode int

WindowMode represents a time window centered on a selected revision.

const (
	WindowAll WindowMode = iota
	Window15s
	Window30s
	Window1m
	Window5m
)

func NextWindowMode

func NextWindowMode(current WindowMode) WindowMode

NextWindowMode cycles: all -> +/-15s -> +/-30s -> +/-1m -> +/-5m -> all.

func (WindowMode) String

func (w WindowMode) String() string

Jump to

Keyboard shortcuts

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