Skip to content

feat(snapshot s3): add --allow-empty-bucket to report an empty bucket as an empty snapshot - #1405

Merged
AlexKantor87 merged 3 commits into
mainfrom
1404-empty-bucket-empty-snapshot
Nov 24, 2026
Merged

AlexKantor87 merged 3 commits into
mainfrom
1404-empty-bucket-empty-snapshot

Conversation

@gsavage

@gsavage gsavage commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

kosli snapshot s3 fails with no matching file or dirs in bucket when the bucket holds no objects. An empty bucket is a legitimate state, so this adds an opt-in flag, --allow-empty-bucket, that reports it as an empty snapshot instead. Related issue: #1404.

What changes

  • Default unchanged. Without the flag, an empty bucket fails exactly as before, with the same message and exit code.
  • --allow-empty-bucket. A bucket that lists no objects (or only folder markers) logs bucket [name] holds no objects; reporting an empty snapshot, sends a snapshot with no artifacts and exits 0. It works with both --fingerprint-source content and metadata, and alongside --skip-unusable-checksums.
  • Filters still fail. When --include, --include-regex, --exclude or --exclude-regex is set and nothing is left, the snapshot fails even with the flag, since a mistyped filter is the likelier cause.
  • The long help text, the flag help and the examples describe the flag.

How it works

getS3DataWithSource returns a typed aws.EmptyBucketError when the listing is empty and no filter is set; its message is the one the error has always had. snapshotS3Options.harvest turns that error into an empty snapshot when the flag is set. The harvest functions keep their signatures.

History

The first commit made the empty snapshot the default. That changed the exit code from 1 to 0 for existing callers, so the second commit restores the default and puts the behaviour behind the flag; the third records the flag for the flag audit. They could be squashed on merge.

Things to decide in review

  • An empty bucket with a filter set still fails with the flag. The check is "any filter set", not "the filter removed something".

Testing

  • go test ./internal/aws/ passes: the typed error is returned only for an unfiltered empty listing, in both fingerprint sources.
  • New unit tests in cmd/kosli/snapshotS3Harvest_test.go pass locally; they drive harvest against the fake S3 client and need no server.
  • make lint passes.
  • SnapshotS3TestSuite has three new cases with an empty fake bucket, including that the server accepts a snapshot with no artifacts for an S3 environment. I could not run it locally (it needs the local Kosli server); it passes in CI.
  • --allow-empty-bucket is recorded in testdata/empty-flag-audit-coverage.json, which the flag audit test requires for every new flag.

Checklist

  • Docs are autogenerated from CLI help — make sure the help text reflects your changes
  • Helm chart (charts/k8s-reporter/) updated, if needed. Note: these changes live in a separate PR
  • Terraform provider and related changes (terraform-provider-kosli, terraform-aws-kosli-reporter) updated, if needed

🤖 Generated with Claude Code

A bucket that holds no objects failed the snapshot with "no matching
file or dirs in bucket", whatever the fingerprint source. An empty
bucket is a state the bucket can be in, not a mistake in the command: a
caller snapshotting a set of buckets on a schedule failed on every run
while one of them was empty, and a bucket that had been emptied kept
its last snapshot in Kosli, which went on showing an artifact that was
no longer there.

The check dates from when the include and exclude filters were added,
where nothing left to report most likely means a mistyped filter. That
case keeps the error. With no filter set, the command now warns and
reports an empty snapshot, exiting 0, as --skip-unusable-checksums
already does when every object is skipped. A bucket holding only folder
markers counts as empty, as it did for the error.

This changes the exit code of an unfiltered snapshot of an empty bucket
from 1 to 0. A caller that relied on the failure to notice an
unexpectedly empty bucket now has the warning on stderr instead.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions github-actions Bot added go Pull requests that update go code feat labels Nov 23, 2026
@claude

claude Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @gsavage's task in 2m 27s —— View job


Follow-up review complete

  • Fetch previous review comments
  • Diff BEFORE..AFTER to scope review
  • Assess status of each previous finding
  • Check changed lines for new bugs/regressions
  • Post summary comment

Scope: git diff 118632ab..c47b7690 — the only new commit (c47b7690) adds the allow-empty-bucket entry to cmd/kosli/testdata/empty-flag-audit-coverage.json. It doesn't touch internal/aws/aws.go or cmd/kosli/snapshotS3.go.

Previous findings (already marked resolved on GitHub):

  • filtersSet naming suggestion — resolved: current code has a filtersSet local and a typed EmptyBucketError, with a dedicated test for "an empty bucket with a filter set".
  • Command-level empty-bucket test gap — resolved for the real risk: TestSnapshotS3CmdOnAnEmptyBucket now exercises the fake S3 client end-to-end against the local server (fails without the flag, succeeds with it, in both fingerprint sources). The one piece not covered at command level — --allow-empty-bucket combined with --include still failing — is already unit-tested at the internal/aws level.

New issues in the changed lines: none — the testdata fixture addition is correct and alphabetically placed.

Full summary posted here.

Comment thread internal/aws/aws.go Resolved
Comment thread cmd/kosli/snapshotS3.go Resolved
The previous commit made an unfiltered snapshot of an empty bucket
report an empty snapshot by default, which changed the command's exit
code from 1 to 0. This is a public tool, and a caller may rely on that
failure to notice a bucket that is unexpectedly empty, so changing it
silently is a breaking change.

The default is restored: an empty bucket fails with "no matching file
or dirs in bucket", as before. --allow-empty-bucket opts in to the new
behaviour, a warning and an empty snapshot, for either fingerprint
source. The flag is about the bucket and not the filters, so a filter
that leaves no object still fails with it set.

The harvest now returns EmptyBucketError for a bucket that lists
nothing with no filter set, carrying the message both cases have always
had, and the command turns that into an empty snapshot when the flag is
set. That keeps the policy in the command and the signatures of the
harvest functions unchanged.

Command-level cases with an empty fake bucket are added to
SnapshotS3TestSuite, which also checks that the server accepts a
snapshot with no artifacts for an S3 environment.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@gsavage gsavage changed the title feat(snapshot s3): report an empty snapshot for an empty bucket feat(snapshot s3): add --allow-empty-bucket to report an empty bucket as an empty snapshot Nov 23, 2026
TestEmptyFlagAuditCoversEveryCommandAndFlag compares the command tree
against the surface the empty-flag audit is expected to cover, and
failed in CI because --allow-empty-bucket was added to snapshot s3
without being recorded there. The coverage file is regenerated from the
command tree, which adds the flag as a boolean.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@claude

claude Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Follow-up review: 118632a..c47b769

The only new commit in this range (c47b7690) adds the allow-empty-bucket entry to cmd/kosli/testdata/empty-flag-audit-coverage.json — a trivial, correctly-ordered fixture update for the new flag. It doesn't touch internal/aws/aws.go or cmd/kosli/snapshotS3.go, so no new issues to flag there.

Previous findings (both raised on cmd/kosli/snapshotS3.go / internal/aws/aws.go, already marked resolved on GitHub):

  • filtersSet/hasS3Filters naming — resolved. Current internal/aws/aws.go has a filtersSet local and the check is now backed by a typed EmptyBucketError, with a dedicated test case ("an empty bucket with a filter set").
  • Command-level test gap — resolved for the main risk. cmd/kosli/snapshotS3_test.go (TestSnapshotS3CmdOnAnEmptyBucket) now covers: empty bucket fails without the flag, succeeds with --allow-empty-bucket (both content and metadata sources) and hits the local server with the was reported to environment golden. One part of the original suggestion isn't covered at the command level — a case combining --allow-empty-bucket with --include that still fails — though this precedence is already unit-tested in internal/aws/aws_test.go.

No new inline comments.

View job run

@gsavage
gsavage requested a review from mbevc1 November 23, 2026 21:57
@AlexKantor87
AlexKantor87 merged commit 7cff44f into main Nov 24, 2026
23 checks passed
@AlexKantor87
AlexKantor87 deleted the 1404-empty-bucket-empty-snapshot branch November 24, 2026 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat go Pull requests that update go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants