Skip to content

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 7 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 is set to true, no diffs are allowed, so we return early if diffs exist
// On contrary (if equality is false), "add" is allowed for cases when for some
// resources Kubernetes fills-in values into spec.
if (equality && diffJsonPatch.size() > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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")) {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ void checksIfDesiredValuesAreTheSame() {
.withFailMessage("Additive changes should be ok")
.isTrue();

assertThat(GenericKubernetesResourceMatcher
.match(dependentResource, actual, null, context, true, true).matched())
.withFailMessage("Strong equality does not ignore additive changes on spec")
.isFalse();

actual = createDeployment();
assertThat(matcher.match(actual, createPrimary("removed"), context).matched())
.withFailMessage("Removed value should not be ok")
Expand Down