gc

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 25 Imported by: 0 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 gc finds the assets in an environment's bootstrap bucket and repository that no live stack refers to, and reports or tags them, with the CDK CLI's tags, so that both tools can collect the same environment: toolkit-lib's GarbageCollector, which the CLI's cdk gc runs (M.3).

This is part 1 of M.3, which changes nothing but tags: the print and tag actions (M.3-D1). Part 2 adds the actions that delete, delete-tagged and full, the confirmation, CDK_TOOLKIT_I9210, and the public action that runs Collect in each environment.

Safety rules

What part 2 deletes is decided here, so these mirror upstream's exactly:

  • The active set is the text of every stack ListStacks gives, by ID, except those in CREATE_FAILED, DELETE_COMPLETE, DELETE_IN_PROGRESS, DELETE_FAILED and REVIEW_IN_PROGRESS: its template body, as GetTemplate returns it, followed by GetTemplateSummary's parameters as JSON.stringify writes the JavaScript SDK's, "undefined" when the response has none. A stack is left out only when its BootstrapVersion parameter's default has exactly four "/"-separated parts, /cdk-bootstrap/<qualifier>/version, the third not the toolkit stack's Qualifier parameter, and the toolkit stack has one. Texts are only added during a run, never removed.
  • An object is in use when its key up to its first "." is in any active text, so a key starting with "." is in use whenever there is a live stack (M.3-D17), and one whose prefix is in "undefined" whenever a live stack has no parameters (M.3-D16); an image is in use when any of its tags is. Images without tags are never listed, so never touched.
  • A batch holds at least 1,000 assets, listed a page at a time, of those last modified, or pushed, strictly before the pass's start less the created buffer; one with no time counts as modified at the start, so never old enough.
  • With no rollback buffer, no tag is read or written; a negative buffer is refused (M.3-D4). With one, an object's tags are read, and the isolated are tagged when they have no isolated tag with a time, the assets in use untagged when they have any isolated tag, and an isolated asset whose tag is strictly older than the start less the buffer is one part 2 may delete.
  • The S3 tag is aws-cdk:isolated, matched by key, its value the pass's start in milliseconds, in decimal, the same for every object it tags. The ECR tag is <n>-aws-cdk.isolated-<ms>, n the image's index among the batch's images to tag and ms the time it is tagged, since ':' isn't valid in an ECR tag; it is found as any tag containing aws-cdk.isolated, and its time is its fourth "-"-separated field, read from the first such tag that has one.
  • A tag's time is its value converted as JavaScript's Number converts it, and compared as a Date: truncated to a whole millisecond, and never older than anything when it isn't a valid Date, beyond 8.64e15 milliseconds either side of the epoch. A time that is missing, empty or white space alone is no time, so the asset is tagged again and gets the whole buffer (M.3-D13), where upstream converts white space to 0, the epoch. A cutoff is a Date too, so a fractional one is truncated.
  • A day is 86,400,000 milliseconds.

The JavaScript these rules are written in is copied from the port's cfntemplate.StringToNumber, which is on another stack, and Date's TimeClip; see js.go.

Behaviour

Collect reads the toolkit stack's Qualifier, reads every live stack, then refreshes the active set in the background, 5 minutes after the start and then every 5 minutes, or at once after a refresh that took longer; before each batch it waits for a refresh when the last is more than 10 minutes old, and fails with StackRefreshTimeout if none lands within 10 minutes, or with a background refresh's error, StackRefreshFailed, at once, once one has failed. The objects come first, then the images, as opts.Type says. Each pass reports its progress every second, and once more when it ends, in upstream's words, without colour. Every call is made one at a time, as upstream's loops await each call before the next (M.3-D11).

Tagging an image puts its manifest again with the new tag; an access-denied error fails the collection, and any other is a debug message, the image tagged on a later run. Untagging an image is BatchDeleteImage naming the isolated tag, which removes that tag; ECR deletes an image whose last tag goes, but an image in use has another tag, unless its isolated tag is itself the only one of its tags a stack names (M.3-D18). Tagging an object puts its other tags with the isolated tag; an object with S3's limit of 10 tags already is left untagged, with a warning, so it is never marked, and the pass goes on, while any error S3 returns fails the pass; untagging one deletes its tag set and puts back the other tags.

Where the port differs from upstream, for its own rules:

  • stopping the background refresh cancels a refresh in progress and waits for it, so that no goroutine outlives the collection and no call is made after it returns, where upstream's carries on (G.10);
  • a cancelled collection stops before its next call, and its error keeps ctx's error, which upstream's message-only errors would lose (G.10);
  • a progress report the host fails is returned when the pass ends, as the port's hosts fail an action, where upstream doesn't wait for it;
  • the toolkit stack is looked up once, through bootstrap.Registry, where upstream looks it up for each of its three outputs.

It fixes upstream's defects, as the owner approved (#95):

  • tagging an object keeps its other tags, where upstream's replaces its tag set with the isolated tag (M.3-D7, isolatedTagSet);
  • an isolated tag whose time is blank is no tag, so the asset is tagged again, where upstream reads white space as 1970, old enough to delete, and never tags again an asset whose tag has no time (M.3-D13, isolatedSince);
  • a ListImages page with no image IDs but a next token follows the token, where upstream's loop asks for the same page again until the context ends (M.3-D14, repoBatches);
  • only the images whose tagging succeeded are counted as tagged, where upstream counts its failures too (M.3-D15, tagECR);
  • a negative rollback or created buffer is refused before anything is sent, where upstream checks neither, so a negative rollback buffer counts as none, and a negative created buffer puts the cutoff after the start, protecting nothing, not even the assets a deployment in progress has just uploaded (M.3-D4, refuseNegative in Collect, since part 1 has no public action);
  • errors keep their causes, so that errors.As finds an AWS error, where upstream passes the error where its ToolkitError takes a message, so only the text is kept, stringified again at each wrapping (M.3-D5, failed);
  • a failed background refresh is kept, and the next wait returns it at once, StackRefreshFailed with its cause, so no further batch is processed, where upstream's is an unhandled rejection, and the next batch fails with StackRefreshTimeout 10 minutes later (M.3-D8, refresher.fail).

Upstream behaviours mirrored as the owner approved (#95), each documented where it happens: a stack with no parameters contributes the text "undefined" (M.3-D16, parametersJSON); a key starting with "." is in use while any stack is live (M.3-D17, fileName); and untagging an image is BatchDeleteImage naming the isolated tag (M.3-D18, untagECR).

One difference comes from the Go SDK and is not settled: an image's push time, which ECR gives in fractional seconds, is read to the millisecond by truncation, where the JavaScript SDK rounds, so it may be a millisecond earlier.

Part 2's public action is to turn a declined confirmation into a cdkerr.KindAborted error, M.3-D5's other half, where upstream's arrives as a failure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect(ctx context.Context, io *iohelper.Helper, env cloudassembly.Environment, cfg aws.Config, opts Options) error

Collect looks for the assets in env's bootstrap bucket and repository that no live stack refers to, and reports or tags them as opts.Action says: upstream's GarbageCollector.garbageCollect. env must be resolved, and cfg its base credentials for writing, never a deploy role.

A negative buffer fails with a cdkerr.Error of Kind cdkerr.KindToolkit naming the option, before anything is sent or read, which fixes an upstream defect (M.3-D4, #95): upstream checks neither, so a negative rollback buffer counts as none, and a negative created buffer puts the cutoff after the start, so that it protects nothing, not even the assets a deployment in progress has just uploaded. Part 1 has no public action, so Collect refuses it, where the recommendation has the public action refuse it.

It fails with a cdkerr.Error of Kind cdkerr.KindToolkit: Code "StackRefreshFailed" when the stacks can't be read at the start, the toolkit stack's errors when it can't be found, and otherwise Code "GarbageCollectionFailed"; each wraps its cause, so that errors.As finds an AWS error (M.3-D5). A cancelled collection's error satisfies errors.Is(err, ctx.Err()).

Types

type Action

type Action int

Action is what a collection does with the unused assets it finds: upstream's GcAction, without the actions that delete, which part 2 of M.3 adds.

const (
	// ActionPrint reports the unused assets, and changes nothing.
	ActionPrint Action = iota + 1

	// ActionTag tags the unused assets, when there is a rollback buffer, and untags those in use again.
	ActionTag
)

type AssetType

type AssetType int

AssetType is which assets a collection looks at.

const (
	// TypeAll is the objects in the bootstrap bucket, then the images in the bootstrap repository.
	TypeAll AssetType = iota
	// TypeS3 is the objects in the bootstrap bucket.
	TypeS3
	// TypeECR is the images in the bootstrap repository.
	TypeECR
)

type CloudFormation

CloudFormation is the part of the CloudFormation API the active set reads.

type ECR

type ECR interface {
	ListImages(ctx context.Context, in *ecr.ListImagesInput, opts ...func(*ecr.Options)) (*ecr.ListImagesOutput, error)
	DescribeImages(ctx context.Context, in *ecr.DescribeImagesInput, opts ...func(*ecr.Options)) (*ecr.DescribeImagesOutput, error)
	BatchGetImage(ctx context.Context, in *ecr.BatchGetImageInput, opts ...func(*ecr.Options)) (*ecr.BatchGetImageOutput, error)
	PutImage(ctx context.Context, in *ecr.PutImageInput, opts ...func(*ecr.Options)) (*ecr.PutImageOutput, error)
	BatchDeleteImage(ctx context.Context, in *ecr.BatchDeleteImageInput, opts ...func(*ecr.Options)) (*ecr.BatchDeleteImageOutput, error)
}

ECR is the part of the ECR API a collection uses.

type Options

type Options struct {
	Action Action
	Type   AssetType

	// RollbackBufferDays is how many days an unused asset stays tagged before it may be deleted; with none, nothing is tagged.
	// A negative value is refused (M.3-D4).
	RollbackBufferDays float64

	// CreatedBufferDays protects assets written less than this many days before the pass starts; zero protects none but those written since.
	// A negative value is refused (M.3-D4).
	CreatedBufferDays float64

	// ToolkitStackName names the environment's toolkit stack; empty means [bootstrap.DefaultToolkitStackName].
	ToolkitStackName string
}

Options are a collection's options: upstream's GarbageCollectorProps, without the environment, credentials and host, which Collect takes.

type S3

type S3 interface {
	ListObjectsV2(ctx context.Context, in *s3.ListObjectsV2Input, opts ...func(*s3.Options)) (*s3.ListObjectsV2Output, error)
	GetObjectTagging(ctx context.Context, in *s3.GetObjectTaggingInput, opts ...func(*s3.Options)) (*s3.GetObjectTaggingOutput, error)
	PutObjectTagging(ctx context.Context, in *s3.PutObjectTaggingInput, opts ...func(*s3.Options)) (*s3.PutObjectTaggingOutput, error)
	DeleteObjectTagging(ctx context.Context, in *s3.DeleteObjectTaggingInput, opts ...func(*s3.Options)) (*s3.DeleteObjectTaggingOutput, error)
}

S3 is the part of the S3 API a collection uses.

Jump to

Keyboard shortcuts

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