Skip to content

Commit eb2654a

Browse files
committed
furthere improvements
1 parent 0443140 commit eb2654a

File tree

3 files changed

+73
-64
lines changed

3 files changed

+73
-64
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesResourceMatcher.java

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@ static <R extends HasMetadata, P extends HasMetadata> Matcher<R, P> matcherFor(
5151
@Override
5252
public Result<R> match(R actualResource, P primary, Context<P> context) {
5353
var desired = dependentResource.desired(primary, context);
54-
return match(desired, actualResource, true, false, false);
55-
}
56-
57-
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
58-
boolean considerMetadata) {
59-
return match(desired, actualResource, considerMetadata, false);
54+
return match(desired, actualResource, false, false, false);
6055
}
6156

6257
/**
@@ -65,10 +60,14 @@ public static <R extends HasMetadata> Result<R> match(R desired, R actualResourc
6560
*
6661
* @param desired the desired resource
6762
* @param actualResource the actual resource
68-
* @param considerMetadata {@code true} if labels and annotations will be checked for equality,
69-
* {@code false} otherwise (meaning that metadata changes will be ignored for matching
70-
* purposes)
71-
* @param equality if {@code false}, the algorithm checks if the properties in the desired
63+
* @param considerLabelsAndAnnotations {@code true} if labels and annotations will be checked for
64+
* equality, {@code false} otherwise (meaning that metadata changes will be ignored for
65+
* matching purposes)
66+
* @param labelsAndAnnotationsEquality if true labels and annotation match exactly in the actual
67+
* and desired state if false, additional elements are allowed in actual annotations.
68+
* Considered only if considerLabelsAndAnnotations is true.
69+
*
70+
* @param specEquality if {@code false}, the algorithm checks if the properties in the desired
7271
* resource spec are same as in the actual resource spec. The reason is that admission
7372
* controllers and default Kubernetes controllers might add default values to some
7473
* properties which are not set in the desired resources' spec and comparing it with simple
@@ -84,22 +83,41 @@ public static <R extends HasMetadata> Result<R> match(R desired, R actualResourc
8483
* @param <R> resource
8584
*/
8685
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
87-
boolean considerMetadata, boolean equality) {
88-
return match(desired, actualResource, considerMetadata, false, equality);
89-
}
90-
91-
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
92-
boolean considerMetadata, String... ignoreList) {
93-
return match(desired, actualResource, considerMetadata, false, false, ignoreList);
86+
boolean considerLabelsAndAnnotations, boolean labelsAndAnnotationsEquality,
87+
boolean specEquality) {
88+
return match(desired, actualResource, considerLabelsAndAnnotations,
89+
labelsAndAnnotationsEquality, specEquality, new String[0]);
9490
}
9591

92+
/**
93+
* Determines whether the specified actual resource matches the specified desired resource,
94+
* possibly considering metadata and deeper equality checks.
95+
*
96+
* @param desired the desired resource
97+
* @param actualResource the actual resource
98+
* @param considerLabelsAndAnnotations {@code true} if labels and annotations will be checked for
99+
* equality, {@code false} otherwise (meaning that metadata changes will be ignored for
100+
* matching purposes)
101+
* @param labelsAndAnnotationsEquality if true labels and annotation match exactly in the actual
102+
* and desired state if false, additional elements are allowed in actual annotations.
103+
* Considered only if considerLabelsAndAnnotations is true.
104+
*
105+
* @param ignorePaths are paths in the resource that are ignored on matching (basically an ignore
106+
* list). All changes with a target prefix path on a calculated JSON Patch between actual
107+
* and desired will be ignored. If there are other changes, non-present on ignore list
108+
* match fails.
109+
* @return results of matching
110+
* @param <R> resource
111+
*/
96112
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
97-
boolean considerMetadata, boolean metadataEquality, String... ignoreList) {
98-
return match(desired, actualResource, considerMetadata, metadataEquality, false, ignoreList);
113+
boolean considerLabelsAndAnnotations, boolean labelsAndAnnotationsEquality,
114+
String... ignorePaths) {
115+
return match(desired, actualResource, considerLabelsAndAnnotations,
116+
labelsAndAnnotationsEquality, false, ignorePaths);
99117
}
100118

101119
private static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
102-
boolean considerMetadata, boolean metadataEquality, boolean specEquality,
120+
boolean considerMetadata, boolean labelsAndAnnotationsEquality, boolean specEquality,
103121
String... ignoredPaths) {
104122
final List<String> ignoreList =
105123
ignoredPaths != null && ignoredPaths.length > 0 ? Arrays.asList(ignoredPaths)
@@ -120,7 +138,7 @@ private static <R extends HasMetadata> Result<R> match(R desired, R actualResour
120138

121139
if (considerMetadata) {
122140
Optional<Result<R>> res =
123-
matchMetadata(desired, actualResource, metadataEquality, wholeDiffJsonPatch);
141+
matchMetadata(desired, actualResource, labelsAndAnnotationsEquality, wholeDiffJsonPatch);
124142
if (res.isPresent()) {
125143
return res.orElseThrow();
126144
}
@@ -162,8 +180,8 @@ private static <R extends HasMetadata> Result<R> matchSpec(R desired, boolean sp
162180
}
163181

164182
private static <R extends HasMetadata> Optional<Result<R>> matchMetadata(R desired,
165-
R actualResource, boolean metadataEquality, JsonNode wholeDiffJsonPatch) {
166-
if (metadataEquality) {
183+
R actualResource, boolean labelsAndAnnotationsEquality, JsonNode wholeDiffJsonPatch) {
184+
if (labelsAndAnnotationsEquality) {
167185
final var desiredMetadata = desired.getMetadata();
168186
final var actualMetadata = actualResource.getMetadata();
169187

@@ -224,31 +242,20 @@ private static List<JsonNode> getDiffsImpactingPathsWithPrefixes(JsonNode diffJs
224242
return Collections.emptyList();
225243
}
226244

227-
/**
228-
* Determines whether the specified actual resource matches the desired state defined by the
229-
* specified {@link KubernetesDependentResource} based on the observed state of the associated
230-
* specified primary resource.
231-
*
232-
* @param dependentResource the {@link KubernetesDependentResource} implementation used to
233-
* computed the desired state associated with the specified primary resource
234-
* @param actualResource the observed dependent resource for which we want to determine whether it
235-
* matches the desired state or not
236-
* @param primary the primary resource from which we want to compute the desired state
237-
* @param context the {@link Context} instance within which this method is called
238-
* @param considerMetadata {@code true} to consider the metadata of the actual resource when
239-
* determining if it matches the desired state, {@code false} if matching should occur only
240-
* considering the spec of the resources
241-
* @return a {@link io.javaoperatorsdk.operator.processing.dependent.Matcher.Result} object
242-
* @param <R> the type of resource we want to determine whether they match or not
243-
* @param <P> the type of primary resources associated with the secondary resources we want to
244-
* match
245-
* @param strongEquality if the resource should match exactly
246-
*/
245+
@Deprecated(forRemoval = true)
246+
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
247+
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
248+
Context<P> context, boolean considerLabelsAndAnnotations, boolean specEquality) {
249+
final var desired = dependentResource.desired(primary, context);
250+
return match(desired, actualResource, considerLabelsAndAnnotations, specEquality);
251+
}
252+
253+
@Deprecated(forRemoval = true)
247254
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
248255
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
249-
Context<P> context, boolean considerMetadata, boolean strongEquality) {
256+
Context<P> context, boolean considerLabelsAndAnnotations, String... ignorePaths) {
250257
final var desired = dependentResource.desired(primary, context);
251-
return match(desired, actualResource, considerMetadata, strongEquality);
258+
return match(desired, actualResource, considerLabelsAndAnnotations, true, ignorePaths);
252259
}
253260

254261
/**
@@ -262,39 +269,38 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
262269
* matches the desired state or not
263270
* @param primary the primary resource from which we want to compute the desired state
264271
* @param context the {@link Context} instance within which this method is called
265-
* @param considerMetadata {@code true} to consider the metadata of the actual resource when
266-
* determining if it matches the desired state, {@code false} if matching should occur only
267-
* considering the spec of the resources
268-
* @return a {@link io.javaoperatorsdk.operator.processing.dependent.Matcher.Result} object
272+
* @param considerLabelsAndAnnotations {@code true} to consider the metadata of the actual
273+
* resource when determining if it matches the desired state, {@code false} if matching
274+
* should occur only considering the spec of the resources
275+
* @param labelsAndAnnotationsEquality if true labels and annotation match exactly in the actual
276+
* and desired state if false, additional elements are allowed in actual annotations.
277+
* Considered only if considerLabelsAndAnnotations is true.
269278
* @param <R> the type of resource we want to determine whether they match or not
270279
* @param <P> the type of primary resources associated with the secondary resources we want to
271280
* match
272281
* @param ignorePaths are paths in the resource that are ignored on matching (basically an ignore
273282
* list). All changes with a target prefix path on a calculated JSON Patch between actual
274283
* and desired will be ignored. If there are other changes, non-present on ignore list
275284
* match fails.
276-
*
285+
* @return a {@link io.javaoperatorsdk.operator.processing.dependent.Matcher.Result} object
277286
*/
278287
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
279288
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
280-
Context<P> context, boolean considerMetadata, String... ignorePaths) {
281-
final var desired = dependentResource.desired(primary, context);
282-
return match(desired, actualResource, considerMetadata, ignorePaths);
283-
}
284-
285-
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
286-
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
287-
Context<P> context, boolean considerMetadata, boolean metadataEquality,
289+
Context<P> context, boolean considerLabelsAndAnnotations,
290+
boolean labelsAndAnnotationsEquality,
288291
String... ignorePaths) {
289292
final var desired = dependentResource.desired(primary, context);
290-
return match(desired, actualResource, considerMetadata, metadataEquality, ignorePaths);
293+
return match(desired, actualResource, considerLabelsAndAnnotations,
294+
labelsAndAnnotationsEquality, ignorePaths);
291295
}
292296

293297
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
294298
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
295-
Context<P> context, boolean considerMetadata, boolean metadataEquality,
296-
boolean strongEquality) {
299+
Context<P> context, boolean considerLabelsAndAnnotations,
300+
boolean labelsAndAnnotationsEquality,
301+
boolean specEquality) {
297302
final var desired = dependentResource.desired(primary, context);
298-
return match(desired, actualResource, considerMetadata, metadataEquality, strongEquality);
303+
return match(desired, actualResource, considerLabelsAndAnnotations,
304+
labelsAndAnnotationsEquality, specEquality);
299305
}
300306
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ public Result<R> match(R actualResource, P primary, Context<P> context) {
145145

146146
@SuppressWarnings("unused")
147147
public Result<R> match(R actualResource, R desired, P primary, Context<P> context) {
148-
return GenericKubernetesResourceMatcher.match(desired, actualResource, false);
148+
return GenericKubernetesResourceMatcher.match(desired, actualResource, false,
149+
false, false);
149150
}
150151

151152
protected void handleDelete(P primary, R secondary, Context<P> context) {

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesResourceMatcherTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ void matchesAdditiveOnlyChanges() {
5353
void matchesWithStrongSpecEquality() {
5454
actual.getSpec().getTemplate().getMetadata().getLabels().put("new-key", "val");
5555
assertThat(GenericKubernetesResourceMatcher
56-
.match(dependentResource, actual, null, context, true, true).matched())
56+
.match(dependentResource, actual, null, context, true, true,
57+
true)
58+
.matched())
5759
.withFailMessage("Adding values should fail matching when strong equality is required")
5860
.isFalse();
5961
}

0 commit comments

Comments
 (0)