cleanup

package
v1.35.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cleanup reclaims podman disk lerd's own image rebuilds leave behind. The safe tier only ever removes what is provably lerd's: an image with a dev.lerd.* label, or the lerd-php*-fpm-base repo name only lerd's pre-built base images use. The deep tier additionally reaps every dangling image, which is untagged and unreferenced by definition so removing it strands nothing, and catalog service images no service references any more. Neither tier ever touches a tagged image in use, a container, or a named volume.

Index

Constants

View Source
const (
	KindImage = "image"
	KindFiles = "files"
)

Target kinds. An image target is removed through podman, a files target is a directory on disk lerd rendered and no longer has an owner for.

View Source
const (
	OwnerLerd  = "lerd"
	OwnerOther = "other"
)

Owners. A target is lerd's own when lerd built or pulled it as part of the stack it manages; everything else on the host is foreign, which is what lets the dashboard say how much of the reclaim is lerd cleaning up after itself.

Variables

This section is empty.

Functions

func Apply

func Apply(p Plan) (removed int, reclaimed int64)

Apply removes every target in the plan and returns how many images were actually removed and the disk reclaimed (a skipped target counts toward neither). It sweeps in repeated passes, retrying targets that failed until a full pass frees nothing new: podman refuses a parent image while a child is still present, so removing a dangling build chain listed parent-first needs the children gone first. A target that never succeeds (e.g. it became referenced since Inspect) is simply left, so one stuck image can't abort the sweep.

func SweepDeep added in v1.28.0

func SweepDeep() (images int, bytes int64, err error)

SweepDeep runs the deep tier: the managed tier plus every remaining dangling image on the host, foreign leftovers included. Interactive only, never run unattended.

func SweepManaged added in v1.28.0

func SweepManaged() (images int, bytes int64, err error)

SweepManaged runs the managed tier (safe orphans plus lerd's unused catalog images), the daily watcher's sweep: it reclaims an upgrade's leftovers unattended without ever touching a foreign dangling image.

func SweepRefs

func SweepRefs(refs ...string)

SweepRefs reaps the exact image references lerd is dropping (the superseded version after a service update, the removed service's images after a remove). Each ref is one lerd itself recorded, so it is provably lerd's; a ref another service still references (in the protected set) is skipped. Reaping precise refs rather than a whole repo means a user's own same-repo image is never touched. Gated by auto_cleanup.

func SweepSafe

func SweepSafe() (images int, bytes int64, err error)

SweepSafe runs the safe-tier cleanup immediately when auto_cleanup is on. It is the event hook for the moment a PHP image rebuild orphans the old image, reaping it at once instead of waiting for the daily watcher.

Types

type HeldByContainers added in v1.28.0

type HeldByContainers struct {
	Count int
	Bytes int64
}

HeldByContainers tallies reclaimable disk a restart would free.

type Plan

type Plan struct {
	Targets []Target
	// Used is every image lerd's own stack occupies disk with, live ones
	// included. It is context for the reclaimable total, not part of it.
	Used []UsedImage
	// UsedTotal is the disk those images hold with shared layers counted once.
	UsedTotal int64
	// Held counts dangling images a running container still holds that would
	// otherwise be reaped. They can't be removed now, but a restart recreates the
	// container on the current image and releases them, so the caller can hint it.
	Held HeldByContainers
}

Plan is the set of lerd-owned resources that are safe to reclaim.

func Inspect

func Inspect(scope Scope) (Plan, error)

Inspect returns the cleanup plan. The always-safe tier reclaims what is provably lerd's:

  • derived images lerd built and then orphaned: lerd tags every live build with a fixed :local tag, so a rebuild re-points the tag and leaves the old image dangling — the unambiguous "superseded" signal.
  • pre-built base images nothing live is built on: a base for an old Containerfile hash, or for a PHP version no longer installed.

Both removals are refcount-safe: layers a live image still shares are kept.

ScopeManaged widens this to lerd's catalog upgrade leftovers, ScopeDeep to every remaining dangling image and every unused image (foreign included). An image a container holds is always skipped, and the unused reap is skipped if the protected set fails.

func (Plan) ReclaimBytes

func (p Plan) ReclaimBytes() int64

ReclaimBytes is the total disk the plan would free.

func (Plan) ReclaimBytesBy added in v1.35.0

func (p Plan) ReclaimBytesBy(owner string) int64

ReclaimBytesBy is the disk the plan would free from one owner's targets.

func (Plan) UsedBytes added in v1.35.0

func (p Plan) UsedBytes() int64

UsedBytes is the disk lerd's own images occupy right now, with a layer shared between them counted once. The per-image Used rows do not sum to it: each one carries the whole cost of its chain, most of which its neighbours share.

type Scope added in v1.28.0

type Scope int

Scope selects how wide a cleanup reaches. The levels are ordered and additive, so each tier includes everything the tier before it removes.

const (
	// ScopeSafe reclaims only images provably lerd's own: orphaned derived builds
	// and unreferenced base images.
	ScopeSafe Scope = iota
	// ScopeManaged additionally reclaims catalog service images no service
	// references any more (upgrade leftovers). Still lerd-scoped, so it is safe to
	// run unattended; this is the daily watcher's tier.
	ScopeManaged
	// ScopeDeep additionally reclaims every remaining dangling image on the host
	// and every unused image regardless of who pulled it, foreign ones included:
	// the stranded base layer of a custom container lives here and nothing
	// narrower can see it. Interactive only, and the default for `lerd cleanup`.
	ScopeDeep
)

type Target

type Target struct {
	Kind  string // KindImage or KindFiles
	ID    string // short image ID, or the directory path for KindFiles
	Desc  string // human description for the plan output
	Owner string // OwnerLerd or OwnerOther
	Bytes int64
}

Target is one reclaimable resource.

type UsedImage added in v1.35.0

type UsedImage struct {
	Ref   string
	InUse bool
	Bytes int64
}

UsedImage is one image lerd's stack occupies disk with. InUse marks the ones a container currently holds, which is what tells a live service's image apart from one sitting there between rebuilds.

Jump to

Keyboard shortcuts

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