Skip to content

Conversation

@knqyf263
Copy link
Collaborator

@knqyf263 knqyf263 commented Oct 7, 2025

Description

Background

Trivy is primarily distributed as a CLI tool, and using Trivy as a library is not recommended for community users. However, Aqua Security internally imports and uses Trivy as a library in our products. This refactoring helps us:

  • Clearly distinguish between the stable public API (in pkg/) that we maintain for backward compatibility
  • Move internal implementation details to internal/ where we can refactor freely without worrying about breaking changes
  • Make it explicit which packages are safe to import externally vs. which are internal-only

Why now?

This refactoring was previously attempted in PRs #291 and #887 six years ago but was abandoned because:

  • Trivy was evolving rapidly with frequent major changes
  • The development velocity was too high to maintain a stable internal/ boundary
  • The codebase was still in flux and not mature enough

Now, Trivy has matured:

  • The core architecture has stabilized
  • Major structural changes have become less frequent than before
  • We can now maintain a clearer separation between public and internal APIs

API Change Detection

We use go-apidiff to automatically detect API changes in PRs. With this refactoring:

  • go-apidiff automatically excludes internal/ packages from analysis
  • This means API change detection will now focus on truly important changes in pkg/
  • We'll get cleaner, more actionable API change reports without noise from internal refactoring
  • Breaking changes in public APIs will be more visible and easier to track

Changes

  • Moved packages from pkg/ to internal/
  • Updated all import paths
  • Updated .golangci.yaml and magefiles to reference correct paths

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@knqyf263 knqyf263 force-pushed the refactor/move-packages-to-internal branch from 00f0f8f to 55e85bb Compare October 7, 2025 13:15
@github-actions github-actions bot added the apidiff Indicates Go API changes relevant to library consumers (CLI compatibility may be unaffected) label Oct 7, 2025
@github-actions

This comment was marked as off-topic.

@aqua-bot aqua-bot requested a review from a team October 7, 2025 13:17
@knqyf263 knqyf263 force-pushed the refactor/move-packages-to-internal branch from 55e85bb to 0330e3b Compare October 7, 2025 19:03
Move compliance, iac/providers, and iac/state to pkg/ as they are part of the public API.
Keep clock, cloud, and policy in internal/ as they are internal implementation.
Export BuildComplianceReport and BuildSummary functions along with required types (Report, SummaryReport, ControlCheckResult, ControlCheckSummary) in pkg/compliance for external use.
knqyf263 added a commit to knqyf263/trivy-operator that referenced this pull request Oct 8, 2025
- Replace mapfs.New() with fstest.MapFS in createDataFS()
- Replace CycloneDXWriter with k8s.Write in cluster controller
- Update compliance imports to use public types package
- Update go.mod to use Trivy fork with public API changes

TODO: Update go.mod to use main branch after
aquasecurity/trivy#9606 merges
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file appears as newly created on the GitHub UI because the proportion of import statements is high compared to the total number of lines, resulting in a low similarity to the original file, but it is correctly recognized as a rename in git.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

CleanShot 2025-10-07 at 23 15 36

@knqyf263 knqyf263 marked this pull request as ready for review October 8, 2025 09:04
Copy link
Contributor

@DmitriyLewen DmitriyLewen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I left comment about aws config.
Also it looks like we can move dependency and vulnerability packages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default settings and trivial logic are used to load the AWS config, so I think it can be reimplemented in trivy-aws.

Copy link
Contributor

@DmitriyLewen DmitriyLewen Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use Digest in Package.
Can we move this package into internal?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed earlier, let's leave pkg/iac/terraform public for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, let's leave pkg/iac/scanners/terraform public for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apidiff Indicates Go API changes relevant to library consumers (CLI compatibility may be unaffected)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants