Skip to content

Conversation

roykharman
Copy link
Contributor

@roykharman roykharman commented Oct 13, 2024

This pull request refactors the policy and endpoint packages to improve code clarity, reduce unnecessary complexity, and ensure clear distinction between production and test-specific code, addressing issue #34069.

Key Changes:

[1] Removed Unused testing.TB Parameters:
In the l4.go file, the unused testing.TB parameters were removed from test-related functions. This reduces unnecessary complexity and ensures these functions are cleaner and not tied to the testing package when it's not needed.

[2] Renamed Test-Specific Functions:
Renamed the Equals and Diff functions in l4.go to TestingOnlyEquals and TestingOnlyDiff, respectively. This change explicitly marks these functions as intended for testing purposes only, helping to prevent accidental use in production code.

[3] Clarifying Note in NewTestEndpointWithState:
Added a comment to the NewTestEndpointWithState function in the endpoint package, clearly indicating that this function is intended solely for testing purposes. This helps future developers quickly understand the function's purpose.

[4] Linting and Error Fixes:
Fixed several linting errors and addressed undefined references caused by the initial renaming of test-specific functions. All necessary functions were renamed appropriately, and the code passes linting checks.

Challenges Faced:
While we initially aimed to refactor parts of the kvstore package, the decision was ultimately made to focus on renaming functions and removing unnecessary parameters in the policy and endpoint packages. The final solution ensures that test-specific functions are clearly marked, simplifying future maintenance and reducing the risk of test code being accidentally used in production environments.

Regarding the kvstore package we attempted to move test-specific code into separate test files to better isolate testing logic from production code. However, this approach caused several undefined reference errors between packages, complicating access to internal functions. We also considered moving the test logic to utility files, but this did not resolve the accessibility issues.

Towards: #34069
Signed-off-by: Roy roykharman@gmail.com
Reviewed-by: Sameeh Jubran sameeh.j@gmail.com

Refactored the endpoint and policy packages to separate test-specific code from production code.

@roykharman roykharman requested review from a team as code owners October 13, 2024 08:30
@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Oct 13, 2024
@github-actions github-actions bot added sig/policy Impacts whether traffic is allowed or denied based on user-defined policies. kind/community-contribution This was a contribution made by a community member. labels Oct 13, 2024
@maintainer-s-little-helper
Copy link

Commit 680e375 does not match "(?m)^Signed-off-by:".

Please follow instructions provided in https://docs.cilium.io/en/stable/contributing/development/contributing_guide/#developer-s-certificate-of-origin

@maintainer-s-little-helper maintainer-s-little-helper bot added the dont-merge/needs-sign-off The author needs to add signoff to their commits before merge. label Oct 13, 2024
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-sign-off The author needs to add signoff to their commits before merge. label Oct 13, 2024
@xmulligan
Copy link
Member

/test

Copy link
Member

@giorio94 giorio94 left a comment

Choose a reason for hiding this comment

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

Thanks @roykharman for taking care of this.

The first two commits look pretty straightforward to me. I've additionally cced the original authors who had introduced the usage of testing.T in these methods, to get their opinion as well. Based on the original commit messages, the idea there was to make it clear that the given methods are intended for testing purposes only, and to prevent using them in production code.

I'm a bit more doubtful about the kvstore changes. Although I agree with the overall intent, the current changes are leading to duplicated code (which will deverge over time), and the need of exposing methods that were intended for testing only. I've left a few suggestions on how to try to refactor those to simplify the logic and replace the usage of some internal methods, but I'm not totally sure if it will be possible to achieve a really good solution without a significant refactoring of the kvstore package. Similarly, I'm also not really sure whether avoiding to import the testing package in production code really outweighs the extra complexity introduced here, but I guess that will become clearer after addressing the initial suggestions.

Please also note that the PR currently contains a merge commit, which are not allowed in Cilium. Please rebase your PR onto the main branch to fix the conflicts and remove it.

@sameehj
Copy link

sameehj commented Oct 15, 2024

Hi @giorio94,

Thank you for your very detailed review.

I do agree the added complexity outweighs the benefit.

We saw this issue and thought it would be a straight forward place to contribute to a better code base.

Thanks @roykharman for taking care of this.

The first two commits look pretty straightforward to me. I've additionally cced the original authors who had introduced the usage of testing.T in these methods, to get their opinion as well. Based on the original commit messages, the idea there was to make it clear that the given methods are intended for testing purposes only, and to prevent using them in production code.

let's see what the original authors think about this and proceed from there.

I'm a bit more doubtful about the kvstore changes. Although I agree with the overall intent, the current changes are leading to duplicated code (which will deverge over time), and the need of exposing methods that were intended for testing only. I've left a few suggestions on how to try to refactor those to simplify the logic and replace the usage of some internal methods, but I'm not totally sure if it will be possible to achieve a really good solution without a significant refactoring of the kvstore package. Similarly, I'm also not really sure whether avoiding to import the testing package in production code really outweighs the extra complexity introduced here, but I guess that will become clearer after addressing the initial suggestions.

Valid point, can be done on a separate stage.

Please also note that the PR currently contains a merge commit, which are not allowed in Cilium. Please rebase your PR onto the main branch to fix the conflicts and remove it.

Probably happened during rebase, @roykharman will fix it.

Thank you for you very detailed and fast review.

Copy link
Member

@nathanjsweet nathanjsweet left a comment

Choose a reason for hiding this comment

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

Thank you so much @roykharman !
Some small nits.

@aanm aanm requested review from giorio94 and nathanjsweet October 21, 2024 10:02
@aanm aanm added the release-note/misc This PR makes changes that have no direct user impact. label Oct 21, 2024
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Oct 21, 2024
@aanm
Copy link
Member

aanm commented Oct 21, 2024

/test

@aanm aanm enabled auto-merge October 21, 2024 10:02
@aanm aanm self-requested a review October 21, 2024 13:58
Copy link
Member

@aanm aanm left a comment

Choose a reason for hiding this comment

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

👍 for the comment that Marco ping me about.

@giorio94
Copy link
Member

Thanks for the changes, overall look good to me. Before approving it, could you please

  • Fix the linting errors (there are apparently a couple more occurrences to rename)
  • Update the PR title/description to reflect the fact that the kvstore package is no longer modified, to prevent future confusion

@roykharman
Copy link
Contributor Author

Thank you for the feedback, @giorio94!

I'll go ahead and fix the remaining linting errors by renaming the occurrences I missed and I'll also update the PR title and description to reflect that the kvstore package is no longer modified, as suggested.
I'll push the changes shortly.

…ilium#34069)

- Removed an unused `testing.TB` parameter from the `NewTestEndpointWithState` function in `endpoint.go`.
- This parameter was not being utilized within the function, and its removal simplifies the function signature.
- The `testing.TB` parameter seems redundant in this context, as the function does not rely on it for any testing behavior.
- Removing this parameter also eliminates the need for the `testing` package import in production code, contributing to the resolution of issue cilium#34069 by reducing the unnecessary weight and surface area of the production build.

Signed-off-by: Roy <roykharman@gmail.com>
Reviewed-by: Sameeh Jubran <sameeh.j@gmail.com>
- Removed an unused `testing.TB` parameter from a function in `l4.go`.
- This parameter was not being used within the function and was likely a leftover placeholder.
- By removing this unused parameter, the function is simplified and no longer depends on the `testing` package.
- This change contributes to resolving issue cilium#34069 by removing the unnecessary inclusion of the Go built-in "testing" package from production builds, reducing both weight and surface area.

Signed-off-by: Roy <roykharman@gmail.com>
Reviewed-by: Sameeh Jubran <sameeh.j@gmail.com>
- Renamed two functions in l4.go to 'TestingOnly*' prefix to clarify their use for testing only.
- Added a note to NewTestEndpointWithState indicating it is for testing purposes.
- This change contributes to resolving issue cilium#34069 by making test-specific code more explicit.

Signed-off-by: Roy <roykharman@gmail.com>
Reviewed-by: Sameeh Jubran <sameeh.j@gmail.com>
…#34069)

- Renamed remaining Equals and Diff functions to TestingOnlyEquals and TestingOnlyDiff.
- Fixed all linting errors.

Signed-off-by: Roy <roykharman@gmail.com>
auto-merge was automatically disabled October 21, 2024 20:19

Head branch was pushed to by a user without write access

@giorio94
Copy link
Member

/test

Copy link
Member

@giorio94 giorio94 left a comment

Choose a reason for hiding this comment

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

Thanks!

@giorio94 giorio94 changed the title Refactor kvstore and policy packages to separate test code from production code (Fixes #34069) Refactor endpoint and policy packages to separate test code from production code Oct 22, 2024
@giorio94
Copy link
Member

(I've slightly edited the title and description of the PR to remove the remaining references to the kvstore package, and prevent the linked issue from being automatically closed when this PR gets merged, given that is not yet fully addressed.

@roykharman
Copy link
Contributor Author

Thank you all for the reviews and approvals, @giorio94, @nathanjsweet and @aanm.

Before moving forward, I wanted to check if any additional changes are required or if there is anything else that needs addressing. I also noticed that two checks are currently failing. While the pull request has been approved, I’m unsure if these failures are related to my changes. Could you please provide guidance on whether I need to address these failing checks?

Thank you again for your feedback and assistance.

@giorio94
Copy link
Member

Before moving forward, I wanted to check if any additional changes are required or if there is anything else that needs addressing. I also noticed that two checks are currently failing.

The two failures are unrelated, and caused by flakes --- I've just retriggered them. No action is required on your side.

@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Oct 22, 2024
@dylandreimerink dylandreimerink added this pull request to the merge queue Oct 22, 2024
Merged via the queue into cilium:main with commit 05d9439 Oct 22, 2024
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/community-contribution This was a contribution made by a community member. ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/misc This PR makes changes that have no direct user impact. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants