-
Notifications
You must be signed in to change notification settings - Fork 220
feat: strong equality matcher #1733
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff8adb3
feat: string equality matcher
csviri c14c8ad
feat: strong equality matcher
csviri fe52640
CR fixes
csviri 60ee663
docs: try to clarify javadoc a little
metacosm 4a7c642
fix: format
metacosm 8caee91
added comment
csviri 7d01c7c
fix: improve wording
metacosm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,11 +29,40 @@ static <R extends HasMetadata, P extends HasMetadata> Matcher<R, P> matcherFor( | |
@Override | ||
public Result<R> match(R actualResource, P primary, Context<P> context) { | ||
var desired = dependentResource.desired(primary, context); | ||
return match(desired, actualResource, false); | ||
return match(desired, actualResource, false, false); | ||
} | ||
|
||
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource, | ||
boolean considerMetadata) { | ||
return match(desired, actualResource, considerMetadata, false); | ||
} | ||
|
||
/** | ||
* Determines whether the specified actual resource matches the specified desired resource, | ||
* possibly considering metadata and deeper equality checks. | ||
* | ||
* @param desired the desired resource | ||
* @param actualResource the actual resource | ||
* @param considerMetadata {@code true} if labels and annotations will be checked for equality, | ||
* {@code false} otherwise (meaning that metadata changes will be ignored for matching | ||
* purposes) | ||
* @param equality if {@code false}, the algorithm checks if the properties in the desired | ||
* resource spec are same as in the actual resource spec. The reason is that admission | ||
* controllers and default Kubernetes controllers might add default values to some | ||
* properties which are not set in the desired resources' spec and comparing it with simple | ||
* equality check would mean that such resource will not match (while conceptually should). | ||
* However, there is an issue with this for example if desired spec contains a list of | ||
* values and a value is removed, this still will match the actual state from previous | ||
* reconciliation. Setting this parameter to {@code true}, will match the resources only if | ||
* all properties and values are equal. This could be implemented also by overriding equals | ||
* method of spec, should be done as an optimization - this implementation does not require | ||
* that. | ||
* | ||
* @return results of matching | ||
* @param <R> resource | ||
*/ | ||
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource, | ||
boolean considerMetadata, boolean equality) { | ||
if (considerMetadata) { | ||
final var desiredMetadata = desired.getMetadata(); | ||
final var actualMetadata = actualResource.getMetadata(); | ||
|
@@ -61,6 +90,12 @@ public static <R extends HasMetadata> Result<R> match(R desired, R actualResourc | |
var desiredSpecNode = objectMapper.valueToTree(ReconcilerUtils.getSpec(desired)); | ||
var actualSpecNode = objectMapper.valueToTree(ReconcilerUtils.getSpec(actualResource)); | ||
var diffJsonPatch = JsonDiff.asJson(desiredSpecNode, actualSpecNode); | ||
// In case of equality no diffs are allowed, so the both will return if found any with | ||
// non-match. On contrary (if equality is false), "add" is allowed for cases when for some | ||
metacosm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// resources Kubernetes fills-in values into spec. | ||
if (equality && diffJsonPatch.size() > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still would like a comment here to explain why this makes a difference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added comment |
||
return Result.computed(false, desired); | ||
} | ||
for (int i = 0; i < diffJsonPatch.size(); i++) { | ||
String operation = diffJsonPatch.get(i).get("op").asText(); | ||
if (!operation.equals("add")) { | ||
|
@@ -90,10 +125,17 @@ public static <R extends HasMetadata> Result<R> match(R desired, R actualResourc | |
* @param <P> the type of primary resources associated with the secondary resources we want to | ||
* match | ||
*/ | ||
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match( | ||
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary, | ||
Context<P> context, boolean considerMetadata, boolean strongEquality) { | ||
final var desired = dependentResource.desired(primary, context); | ||
return match(desired, actualResource, considerMetadata, strongEquality); | ||
} | ||
|
||
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match( | ||
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary, | ||
Context<P> context, boolean considerMetadata) { | ||
final var desired = dependentResource.desired(primary, context); | ||
return match(desired, actualResource, considerMetadata); | ||
return match(desired, actualResource, considerMetadata, false); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.