Documentation
¶
Overview ¶
Package dockerprune keeps the Docker images and build cache of a lab machine under a size: the unused images created or tagged longest ago go first, the build cache is pruned by Docker's own least-recently-used order. An image a container uses, one a kind lab's node holds, or one created or tagged since a keep time is never removed.
Index ¶
- Constants
- Variables
- func Importer(args []string) bool
- func Normalize(ref string) string
- func ParseSize(s string) (int64, error)
- func TrimImages(ctx context.Context, d Docker, candidates []Image, total, target int64, ...) (freed int64, removed []string, err error)
- type CacheRecord
- type CacheTrim
- type Docker
- type Image
- type InUse
- type Usage
Constants ¶
const KindLabel = "io.x-k8s.kind.cluster"
KindLabel is the label kind puts on its node containers.
Variables ¶
var ErrBusy = errors.New("an image import runs")
ErrBusy stops a trim: an image import or build began while it ran.
Functions ¶
func Importer ¶
Importer says an argument vector loads or builds images: kind load and kind create (which loads its node image), docker build, load, save and import, buildx, and containerd's ctr import. The verb is one of the first three words that are no flag: docker image load, docker --context x build, ctr -n k8s.io images import.
func Normalize ¶
Normalize names a reference as containerd does: docker.io/library/nginx:1 for nginx:1.
func ParseSize ¶
ParseSize reads a size as docker prints it: decimal units, "1.5GB", "512kB", "0B", with an optional percentage after a space.
func TrimImages ¶
func TrimImages(ctx context.Context, d Docker, candidates []Image, total, target int64, busy func() bool) (freed int64, removed []string, err error)
TrimImages removes candidates in order until the images' size, from total, is at most target, asking busy before each removal. An image Docker refuses to remove (a container started on it since) is skipped. It returns what it freed by the images' unique sizes and the names of the images it removed.
Types ¶
type CacheRecord ¶ added in v0.174.0
type CacheRecord struct {
ID string
// Size is what the record lists; shared layers may free less.
Size int64
// Reclaimable is Docker saying no build or container uses the record.
// A record that is not reclaimable stays, whoever asks.
Reclaimable bool
// UsedAgo is how long before the reading the record was last used;
// -1 when Docker's age is unreadable, which keeps the record.
UsedAgo time.Duration
}
CacheRecord is one build cache record of `docker builder du`.
func CacheRecords ¶ added in v0.174.0
func CacheRecords(ctx context.Context, d Docker) ([]CacheRecord, error)
CacheRecords reads the build cache's records with their sizes and ages from `docker builder du`.
type CacheTrim ¶ added in v0.174.0
type CacheTrim struct {
// Freed is the listed size of the records removed; shared layers may
// free less, so the freed bytes of a run come from the df re-read.
Freed int64
// Removed names how many records went.
Removed int
// Kept counts the records that stay above the target because no
// removal may take them (a build or container uses them), and KeptBytes
// is what they hold.
Kept int
KeptBytes int64
}
CacheTrim is one build cache trim's result.
func PruneBuildCache ¶
PruneBuildCache brings the build cache down to keep bytes, removing the reclaimable record used longest ago first. Docker's own `builder prune --max-used-space` cannot be asked for this: it is a best-effort pass of the daemon's GC policy and frees nothing while the policy's reserved or min-free-space floor sits above the target, which on a nearly full disk is always. A record Docker does not call reclaimable is never touched and is counted in Kept.
type Image ¶
type Image struct {
ID string
// Refs are its repository:tag names, none for a dangling image.
Refs []string
Digest string
Created time.Time
// Tagged is when a tag was last put on it (docker build, tag or load):
// an image rebuilt from cache or retagged keeps its old Created.
Tagged time.Time
// Unique is what removing it frees: the layers no other image shares.
Unique int64
Containers int
}
Image is one image of the machine's Docker.
func Candidates ¶
Candidates are the images a trim may remove, in the order it removes them: dangling ones first, then the ones created or tagged longest ago. An image created or tagged after keep is no candidate: a session may have produced it for a lab it has not loaded yet.
func ListImages ¶
ListImages reads every image with what removing it frees, from docker system df -v, which lists an image once per tag, and when it was tagged last, from docker image inspect.
type InUse ¶
InUse are the images that must stay: by ID, normalized reference and digest.
func KindImages ¶
KindImages are the images every kind node holds, and the node images themselves through the containers using them. A node that is not running cannot say what it holds: then nothing is known to be unused, and the call fails.