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

Return early if the AuthPolicy being filtered is marked for deletion #632

Merged
merged 2 commits into from
May 9, 2024

Conversation

Boomatang
Copy link
Contributor

Improves filter function and returns early when searching for AuthPolicies attached to the Gateway.

@Boomatang Boomatang requested a review from a team as a code owner May 8, 2024 13:26
affectedPolicies = utils.Filter(affectedPolicies, func(policy kuadrantgatewayapi.Policy) bool {
p, ok := policy.(*api.AuthPolicy)
if !ok {
return false
}
if p.DeletionTimestamp != nil {
return false
}
return kuadrantgatewayapi.IsTargetRefGateway(policy.GetTargetRef()) &&
ap.GetUID() != policy.GetUID() && p.IsAtomicOverride()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ap.GetUID() != policy.GetUID() && p.IsAtomicOverride()
ap.GetUID() != policy.GetUID() && p.IsAtomicOverride() && p.DeletionTimestamp == nil

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I did have it that way to start. My reasoning for not doing it was why do the extra function calls when it can return early if there is a deletion time stamp.

Copy link
Contributor

Choose a reason for hiding this comment

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

Make it the first condition then?

Suggested change
ap.GetUID() != policy.GetUID() && p.IsAtomicOverride()
return p.DeletionTimestamp == nil &&
kuadrantgatewayapi.IsTargetRefGateway(policy.GetTargetRef()) &&
ap.GetUID() != policy.GetUID() &&
p.IsAtomicOverride()

I'm usually in favour of safeguard clauses but in this case where the return is straightforward, the less ifs the better, no? In fact, if really we want to push it:

Suggested change
ap.GetUID() != policy.GetUID() && p.IsAtomicOverride()
return ok &&
p.DeletionTimestamp == nil &&
kuadrantgatewayapi.IsTargetRefGateway(policy.GetTargetRef()) &&
ap.GetUID() != policy.GetUID() &&
p.IsAtomicOverride()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving the ok check here makes more sense also. I am kinda unsure about the readability, personally I don't like to have logic in return statements. It makes using a debugger harder. But in this case this might be the cleaner option.

@Boomatang Boomatang merged commit c525838 into Kuadrant:main May 9, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants