nodecapacity

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package nodecapacity judges a node's disk, inode, memory and swap headroom against warn and critical thresholds and groups cleanup candidates by safety class.

Index

Constants

View Source
const (
	EnvDiskWarnFree  = "APP_NODE_DISK_WARN_FREE_PCT"
	EnvDiskCritFree  = "APP_NODE_DISK_CRIT_FREE_PCT"
	EnvInodeWarnFree = "APP_NODE_INODE_WARN_FREE_PCT"
	EnvInodeCritFree = "APP_NODE_INODE_CRIT_FREE_PCT"
	EnvMemWarnAvail  = "APP_NODE_MEM_WARN_AVAIL_PCT"
	EnvMemCritAvail  = "APP_NODE_MEM_CRIT_AVAIL_PCT"
	EnvSwapWarnUsed  = "APP_NODE_SWAP_WARN_USED_PCT" //nolint:gosec // env var name, not a credential
	EnvSwapCritUsed  = "APP_NODE_SWAP_CRIT_USED_PCT"
)

Env names for the default thresholds, all percentages.

View Source
const (
	MetricDisk   = "disk"
	MetricInodes = "inodes"
	MetricMemory = "memory"
	MetricSwap   = "swap"
)

Metric names.

View Source
const (
	ClassDanglingImages    = "dangling_images"
	ClassBuildCache        = "build_cache"
	ClassStoppedContainers = "stopped_containers"
	ClassRollbackImages    = "rollback_images"
	ClassOrphanedVolumes   = "orphaned_volumes"
)

Cleanup classes.

Variables

SafeStages are the only stages a guardrail cleanup ever runs: volumes and deploy attempts are excluded.

Functions

This section is empty.

Types

type Band

type Band struct {
	Warn     float64 `json:"warn"`
	Critical float64 `json:"critical"`
}

Band is a warn and critical pair. Free-style metrics (disk, inodes, memory) trip when the free percentage falls to or below the value; swap trips when the used percentage rises to it.

type Class

type Class struct {
	Name      string         `json:"name"`
	Deletable bool           `json:"deletable"`
	Bytes     int64          `json:"bytes"`
	Items     []janitor.Item `json:"items"`
}

Class is one group of cleanup candidates. Deletable false means the items are shown only.

type ImageSource

type ImageSource interface {
	ListAllImages(ctx context.Context) ([]docker.ImageRecord, error)
}

ImageSource lists local images so tagged unused ones can be shown as kept.

type Level

type Level string

Level is how close a metric is to its limit.

const (
	LevelUnknown  Level = "unknown"
	LevelOK       Level = "ok"
	LevelWarn     Level = "warn"
	LevelCritical Level = "critical"
)

Levels, worst last.

type MetricStatus

type MetricStatus struct {
	Name    string  `json:"name"`
	Level   Level   `json:"level"`
	Percent float64 `json:"percent"`
	// Free and Total are bytes, or counts for inodes.
	Free  uint64 `json:"free"`
	Total uint64 `json:"total"`
}

MetricStatus is one metric's reading and verdict.

type Preview

type Preview struct {
	Classes    []Class `json:"classes"`
	TotalBytes int64   `json:"total_bytes"`
}

Preview is a cleanup grouped by class.

func BuildPreview

func BuildPreview(ctx context.Context, run Runner, imgs ImageSource) (Preview, error)

BuildPreview runs a dry pass and groups the outcome. Nothing is removed.

type Result

type Result struct {
	Preview
	FreedBytes int64  `json:"freed_bytes"`
	Error      string `json:"error,omitempty"`
}

Result is an executed cleanup.

func Execute

func Execute(ctx context.Context, run Runner, trigger string) (Result, error)

Execute removes the safe classes only and reports what was freed.

type Runner

type Runner interface {
	Run(ctx context.Context, opts janitor.Options) (janitor.Report, error)
}

Runner is the janitor surface a cleanup needs. *janitor.Engine satisfies it.

type Sample

type Sample struct {
	DiskTotal, DiskFree     uint64
	InodesTotal, InodesFree uint64
	MemTotal, MemAvail      uint64
	SwapTotal, SwapFree     uint64
}

Sample is one reading of a node's host. A zero total means the value is unreadable.

type Status

type Status struct {
	Metrics    []MetricStatus `json:"metrics"`
	Thresholds Thresholds     `json:"thresholds"`
	Worst      Level          `json:"worst"`
	// Reason names the worst metric in words, empty when everything is fine.
	Reason string `json:"reason,omitempty"`
}

Status is a node's overall verdict.

func Evaluate

func Evaluate(s Sample, t Thresholds) Status

Evaluate judges a sample against thresholds. Unreadable metrics are reported as unknown and never block.

func (Status) Blocking

func (s Status) Blocking() bool

Blocking reports whether new builds and deploys must be refused.

type Thresholds

type Thresholds struct {
	Disk   Band `json:"disk_free_pct"`
	Inodes Band `json:"inodes_free_pct"`
	Memory Band `json:"memory_available_pct"`
	Swap   Band `json:"swap_used_pct"`
}

Thresholds are the percentage bands for every watched metric.

func ThresholdsFromEnv

func ThresholdsFromEnv(lookup func(string) (string, bool)) Thresholds

ThresholdsFromEnv reads the defaults, falling back on unset or invalid values.

func (Thresholds) Merge

Merge applies a node's saved overrides on top of the defaults.

func (Thresholds) Validate

func (t Thresholds) Validate() bool

Validate rejects percentages outside 0..100 or a critical band looser than its warn band.

Jump to

Keyboard shortcuts

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