cleanup

package
v0.27.2 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cleanup selects and removes old image tags, build cache, and dangling images for ssd deployments. Selection is pure; removal is delegated to an ImageCleaner interface implemented per runtime.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PruneOldTags

func PruneOldTags(ctx context.Context, c ImageCleaner, imageName string, retention, running int) ([]string, error)

PruneOldTags lists tags for the given image, picks old ones per the retention and running version, and removes them via the cleaner.

Semantics:

  • retention == 0 → no-op (auto cleanup disabled)
  • ListTags errors propagate to the caller (something real is wrong)
  • RemoveImage errors per tag are logged and swallowed — cleanup is best-effort. Common cause: image still referenced by a running pod (docker/nerdctl will refuse). That is the desired behavior.

Returns the list of refs that were *attempted* to be removed.

Types

type ComposeCleaner

type ComposeCleaner struct {
	// contains filtered or unexported fields
}

ComposeCleaner implements ImageCleaner for the compose runtime via docker commands over SSH.

func NewComposeCleaner

func NewComposeCleaner(ssh SSHRunner) *ComposeCleaner

NewComposeCleaner wires a compose cleaner to an SSH runner.

func (*ComposeCleaner) ListTags

func (c *ComposeCleaner) ListTags(ctx context.Context, imageName string) ([]Tag, error)

ListTags returns every tag of the given repository on the server. Numeric tags have Numeric populated; everything else (including <none> intermediate layers) is dropped.

func (*ComposeCleaner) PruneBuildCache

func (c *ComposeCleaner) PruneBuildCache(ctx context.Context) error

PruneBuildCache runs `docker builder prune -af --filter until=168h`. Removes build cache entries untouched for at least 7 days.

func (*ComposeCleaner) PruneDangling

func (c *ComposeCleaner) PruneDangling(ctx context.Context) error

PruneDangling runs `docker image prune -f` to remove untagged images not referenced by any container.

func (*ComposeCleaner) RemoveImage

func (c *ComposeCleaner) RemoveImage(ctx context.Context, imageRef string) error

RemoveImage runs `docker rmi` against the given reference. Refuses any reference not prefixed with "ssd-" for safety.

type ImageCleaner

type ImageCleaner interface {
	ListTags(ctx context.Context, imageName string) ([]Tag, error)
	RemoveImage(ctx context.Context, imageRef string) error
	PruneBuildCache(ctx context.Context) error
	PruneDangling(ctx context.Context) error
}

ImageCleaner is implemented per runtime (compose, k3s) to list tags, remove images, and prune build cache / dangling images.

func NewCleaner

func NewCleaner(runtime string, ssh SSHRunner) ImageCleaner

NewCleaner returns an ImageCleaner for the given runtime. "compose" → docker, "k3s" → nerdctl/buildctl.

type K3sCleaner

type K3sCleaner struct {
	// contains filtered or unexported fields
}

K3sCleaner implements ImageCleaner for the k3s runtime via nerdctl and buildctl over SSH.

Differences from the compose runtime (verified on byteink.main):

  • `nerdctl images <name>` name-filter returns nothing → list all, filter by prefix in Go.
  • nerdctl emits many `<none>:<none>` intermediate layer lines that must be dropped.
  • Build cache lives in buildkitd (not containerd), so pruning uses `buildctl --addr unix:///run/buildkit/buildkitd.sock prune`, which requires sudo.

func NewK3sCleaner

func NewK3sCleaner(ssh SSHRunner) *K3sCleaner

NewK3sCleaner wires a k3s cleaner to an SSH runner.

func (*K3sCleaner) ListTags

func (c *K3sCleaner) ListTags(ctx context.Context, imageName string) ([]Tag, error)

ListTags lists all images in the k8s.io namespace and filters to the requested repository client-side. Numeric tags are parsed; everything else is dropped.

func (*K3sCleaner) PruneBuildCache

func (c *K3sCleaner) PruneBuildCache(ctx context.Context) error

PruneBuildCache runs `sudo buildctl prune --keep-duration 168h` against the buildkit daemon socket. Sudo is required — buildkitd.sock is root-owned on byteink.main.

func (*K3sCleaner) PruneDangling

func (c *K3sCleaner) PruneDangling(ctx context.Context) error

PruneDangling runs `nerdctl image prune -f` in the k8s.io namespace.

func (*K3sCleaner) RemoveImage

func (c *K3sCleaner) RemoveImage(ctx context.Context, imageRef string) error

RemoveImage runs `nerdctl rmi` in the k8s.io namespace. Refuses non-ssd refs for safety.

type SSHRunner

type SSHRunner interface {
	SSH(ctx context.Context, command string) (string, error)
}

SSHRunner is the minimum surface of remote.RemoteClient that cleanup needs. Keeping it narrow lets callers pass any client (real or mock) without pulling the whole RemoteClient interface into this package.

type Tag

type Tag struct {
	Numeric int
	Raw     string
}

Tag describes a single image tag on the server. Numeric is the parsed integer version when the tag matches ssd's scheme (e.g. "ssd-foo-web:57" → Numeric=57). Raw holds the original tag string for non-numeric or untagged entries ("<none>", "latest").

func SelectOldTags

func SelectOldTags(tags []Tag, keep, running int) []Tag

SelectOldTags returns the tags that are safe to remove.

Rules (non-negotiable):

  • Never remove the running tag.
  • Keep the top N numeric tags (N = max(keep, 1)).
  • Non-numeric tags are ignored entirely (not produced by ssd; user data).
  • Duplicates collapse — a tag numeric is kept or removed once.

Returns an empty slice when nothing should be removed.

Jump to

Keyboard shortcuts

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