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

🌱Export conditions.HasSameState method #11253

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

m-messiah
Copy link
Member

@m-messiah m-messiah commented Oct 4, 2024

What this PR does / why we need it:

There are cases when one wants to compare existing condition and the condition to be applied before applying them, to avoid extra PATCH request to the server, but the only way to do it today is to wrap it in gomega.Matcher, like this:

condition := clusterv1.Condition{...}
if existingCondition := conditions.Get(obj, condition.Type); existingCondition != nil {
	if ok, _ := conditions.HaveSameStateOf(&condition).Match(existingCondition); ok {
		return nil
	}
}
conditions.Set(obj, &condition)
return client.Update(ctx, obj)

In this PR I propose to export HasSameState, to simplify the code above as (I understand not a big benefit, but just not clear why should we use gomega for simple comparison

condition := clusterv1.Condition{...}
if existingCondition := conditions.Get(obj, condition.Type); existingCondition != nil {
	if conditions.HaveSameState(existingCondition, condition) {
		return nil
	}
}
conditions.Set(obj, &condition)
return client.Update(ctx, obj)

/area util

The only question is about conditions.v1beta2 - how close they are and does it make sense to improve the current code? It looks like those would not use the comparison helpers and meta.SetStatusCondition already returns the answer if the condition is changed.

@k8s-ci-robot k8s-ci-robot added the area/util Issues or PRs related to utils label Oct 4, 2024
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 4, 2024
Copy link
Member

@neolit123 neolit123 left a comment

Choose a reason for hiding this comment

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

general +1, but if this is done i would also mark HaveSameStateOf as deprecated and remove it on the next minor release. there are no rules around that, but i'm not a fan of importing goomega/* code in non _test.go files or in non-test dedicated util packages.

@m-messiah
Copy link
Member Author

general +1, but if this is done i would also mark HaveSameStateOf as deprecated and remove it on the next minor release. there are no rules around that, but i'm not a fan of importing goomega/* code in non _test.go files or in non-test dedicated util packages.

me too, that's why it is created, but it looks like HaveSameStateOf is used solely in tests of the library as a matcher and does the same job as MatchCondition matcher (but for pointers)

Copy link
Member

@vincepri vincepri left a comment

Choose a reason for hiding this comment

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

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 6, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: ee5fe662a86f3d98a24df1844090e4e0480c1bbc

@fabriziopandini
Copy link
Member

There are cases when one wants to compare existing condition and the condition to be applied before applying them, to avoid extra PATCH request to the server

If using patch helper, it does already this for you.

the only way to do it today is to wrap it in gomega.Matcher

this is expected, HaveSameStateOf has been designed as a matcher to be used in tests.
But if you need to expose also HasSameState no objection

general +1, but if this is done i would also mark HaveSameStateOf as deprecated and remove it on the next minor release

having a matcher helps in writing test, I would Not deprecate it.

The only question is about conditions.v1beta2 - how close they are and does it make sense to improve the current code? It looks like those would not use the comparison helpers and meta.SetStatusCondition already returns the answer if the condition is changed.

New conditions helpers recently merged, see #10997

Copy link
Member

@fabriziopandini fabriziopandini left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: fabriziopandini

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 7, 2024
@m-messiah
Copy link
Member Author

New conditions helpers recently merged, see #10997

yes, I saw it, but was questioning when they would become the base, replacing the code I am fixing here.
If it was three weeks ago, I assume the move forward is not soon, so ok.

@k8s-ci-robot k8s-ci-robot merged commit a3711b4 into kubernetes-sigs:main Oct 7, 2024
25 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.9 milestone Oct 7, 2024
@m-messiah m-messiah deleted the export-compare branch October 7, 2024 09:27
@sbueringer
Copy link
Member

Yeah definitely fine to improve the current helpers while they are still around

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/util Issues or PRs related to utils cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants