Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: propagate time through context values #5858

Merged
merged 2 commits into from
Jan 3, 2024

Conversation

knqyf263
Copy link
Collaborator

@knqyf263 knqyf263 commented Jan 2, 2024

Description

In Trivy, time.Now() has been traditionally handled through two methods:

  • Functional Options Pattern (example)
  • Global variables (here)

While Functional Options offer advantages in unit testing, they present challenges in integration testing, especially when multiple instances of time.Now() need to be mocked with a consistent, fixed time. This approach often requires repetitive coding in multiple places. Conversely, global variables, though convenient, complicate ensuring thread safety.

To address these issues, this PR introduces a new approach: storing and passing time.Time within the context. As per the documentation guidelines, context values should be used cautiously and are not meant for every situation.

Use context values only for request-scoped data that transits processes and API boundaries, not for passing optional parameters to functions.

However, leveraging context values is a viable solution for data that is integral throughout the entire process flow.

This change allows us to maintain a uniform, fixed time reference throughout a single process while ensuring thread safety. We are open to future improvements and will consider any better methods that may emerge.

This modification enhances our integration testing capabilities. By enabling the fixation of time in tests, we can prevent failures due to End-Of-Life (EOL) related time issues, as was previously encountered in our project, like this.

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).

Signed-off-by: knqyf263 <knqyf263@gmail.com>
@knqyf263 knqyf263 self-assigned this Jan 2, 2024
@knqyf263 knqyf263 marked this pull request as ready for review January 3, 2024 05:31
@knqyf263
Copy link
Collaborator Author

knqyf263 commented Jan 3, 2024

All other PRs are failing due to an EOL issue, and this PR should fix it. @chen-keinan @simar7 Can you please take a look?

pkg/k8s/report/table.go Outdated Show resolved Hide resolved
pkg/report/json_test.go Outdated Show resolved Hide resolved
Signed-off-by: knqyf263 <knqyf263@gmail.com>
@knqyf263
Copy link
Collaborator Author

knqyf263 commented Jan 3, 2024

@chen-keinan Addressed your comments. Can you please take another look?

Copy link
Contributor

@chen-keinan chen-keinan left a comment

Choose a reason for hiding this comment

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

lgtm

@knqyf263 knqyf263 added this pull request to the merge queue Jan 3, 2024
Merged via the queue into aquasecurity:main with commit da597c4 Jan 3, 2024
12 checks passed
@knqyf263 knqyf263 deleted the refactor/time branch January 3, 2024 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants