Skip to content

Commit e60985a

Browse files
committed
improve: matcher always considers metadata (#2273)
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 57b49cc commit e60985a

File tree

4 files changed

+22
-47
lines changed

4 files changed

+22
-47
lines changed

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

+14-40
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static <R extends HasMetadata, P extends HasMetadata> Matcher<R, P> matcherFor(
5858
@Override
5959
public Result<R> match(R actualResource, P primary, Context<P> context) {
6060
var desired = dependentResource.desired(primary, context);
61-
return match(desired, actualResource, false, false, false, context);
61+
return match(desired, actualResource, false, false, context);
6262
}
6363

6464
/**
@@ -67,9 +67,6 @@ public Result<R> match(R actualResource, P primary, Context<P> context) {
6767
*
6868
* @param desired the desired resource
6969
* @param actualResource the actual resource
70-
* @param considerLabelsAndAnnotations {@code true} if labels and annotations will be checked for
71-
* equality, {@code false} otherwise (meaning that metadata changes will be ignored for
72-
* matching purposes)
7370
* @param labelsAndAnnotationsEquality if true labels and annotation match exactly in the actual
7471
* and desired state if false, additional elements are allowed in actual annotations.
7572
* Considered only if considerLabelsAndAnnotations is true.
@@ -89,9 +86,9 @@ public Result<R> match(R actualResource, P primary, Context<P> context) {
8986
*/
9087
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R desired,
9188
R actualResource,
92-
boolean considerLabelsAndAnnotations, boolean labelsAndAnnotationsEquality,
89+
boolean labelsAndAnnotationsEquality,
9390
boolean valuesEquality, Context<P> context) {
94-
return match(desired, actualResource, considerLabelsAndAnnotations,
91+
return match(desired, actualResource,
9592
labelsAndAnnotationsEquality, valuesEquality, context, EMPTY_ARRAY);
9693
}
9794

@@ -101,9 +98,6 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R d
10198
*
10299
* @param desired the desired resource
103100
* @param actualResource the actual resource
104-
* @param considerLabelsAndAnnotations {@code true} if labels and annotations will be checked for
105-
* equality, {@code false} otherwise (meaning that metadata changes will be ignored for
106-
* matching purposes)
107101
* @param labelsAndAnnotationsEquality if true labels and annotation match exactly in the actual
108102
* and desired state if false, additional elements are allowed in actual annotations.
109103
* Considered only if considerLabelsAndAnnotations is true.
@@ -116,9 +110,9 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R d
116110
*/
117111
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R desired,
118112
R actualResource,
119-
boolean considerLabelsAndAnnotations, boolean labelsAndAnnotationsEquality,
113+
boolean labelsAndAnnotationsEquality,
120114
Context<P> context, String... ignorePaths) {
121-
return match(desired, actualResource, considerLabelsAndAnnotations,
115+
return match(desired, actualResource,
122116
labelsAndAnnotationsEquality, false, context, ignorePaths);
123117
}
124118

@@ -133,9 +127,6 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R d
133127
* matches the desired state or not
134128
* @param primary the primary resource from which we want to compute the desired state
135129
* @param context the {@link Context} instance within which this method is called
136-
* @param considerLabelsAndAnnotations {@code true} to consider the metadata of the actual
137-
* resource when determining if it matches the desired state, {@code false} if matching
138-
* should occur only considering the spec of the resources
139130
* @param labelsAndAnnotationsEquality if true labels and annotation match exactly in the actual
140131
* and desired state if false, additional elements are allowed in actual annotations.
141132
* Considered only if considerLabelsAndAnnotations is true.
@@ -150,28 +141,28 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R d
150141
*/
151142
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
152143
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
153-
Context<P> context, boolean considerLabelsAndAnnotations,
144+
Context<P> context,
154145
boolean labelsAndAnnotationsEquality,
155146
String... ignorePaths) {
156147
final var desired = dependentResource.desired(primary, context);
157-
return match(desired, actualResource, considerLabelsAndAnnotations,
148+
return match(desired, actualResource,
158149
labelsAndAnnotationsEquality, context,
159150
ignorePaths);
160151
}
161152

162153
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
163154
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
164-
Context<P> context, boolean considerLabelsAndAnnotations,
155+
Context<P> context,
156+
boolean specEquality,
165157
boolean labelsAndAnnotationsEquality,
166-
boolean specEquality) {
158+
String... ignorePaths) {
167159
final var desired = dependentResource.desired(primary, context);
168-
return match(desired, actualResource, considerLabelsAndAnnotations,
169-
labelsAndAnnotationsEquality, specEquality, context);
160+
return match(desired, actualResource,
161+
labelsAndAnnotationsEquality, specEquality, context, ignorePaths);
170162
}
171163

172164
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R desired,
173-
R actualResource,
174-
boolean considerMetadata, boolean labelsAndAnnotationsEquality, boolean valuesEquality,
165+
R actualResource, boolean labelsAndAnnotationsEquality, boolean valuesEquality,
175166
Context<P> context,
176167
String... ignoredPaths) {
177168
final List<String> ignoreList =
@@ -195,8 +186,7 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R d
195186
matched = match(valuesEquality, node, ignoreList);
196187
} else if (nodeIsChildOf(node, List.of(METADATA))) {
197188
// conditionally consider labels and annotations
198-
if (considerMetadata
199-
&& nodeIsChildOf(node, List.of(METADATA_LABELS, METADATA_ANNOTATIONS))) {
189+
if (nodeIsChildOf(node, List.of(METADATA_LABELS, METADATA_ANNOTATIONS))) {
200190
matched = match(labelsAndAnnotationsEquality, node, Collections.emptyList());
201191
}
202192
} else if (!nodeIsChildOf(node, IGNORED_FIELDS)) {
@@ -227,20 +217,4 @@ static String getPath(JsonNode n) {
227217
return n.get(PATH).asText();
228218
}
229219

230-
@Deprecated(forRemoval = true)
231-
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
232-
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
233-
Context<P> context, boolean considerLabelsAndAnnotations, boolean specEquality) {
234-
final var desired = dependentResource.desired(primary, context);
235-
return match(desired, actualResource, considerLabelsAndAnnotations, specEquality, context);
236-
}
237-
238-
@Deprecated(forRemoval = true)
239-
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
240-
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
241-
Context<P> context, boolean considerLabelsAndAnnotations, String... ignorePaths) {
242-
final var desired = dependentResource.desired(primary, context);
243-
return match(desired, actualResource, considerLabelsAndAnnotations, true, context, ignorePaths);
244-
}
245-
246220
}

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/updatermatcher/GenericResourceUpdaterMatcher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public R updateResource(R actual, R desired, Context<?> context) {
4343

4444
@Override
4545
public boolean matches(R actual, R desired, Context<?> context) {
46-
return GenericKubernetesResourceMatcher.match(desired, actual, true,
46+
return GenericKubernetesResourceMatcher.match(desired, actual,
4747
false, false, context).matched();
4848
}
4949

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ void matchesAdditiveOnlyChanges() {
5555
@Test
5656
void matchesWithStrongSpecEquality() {
5757
actual.getSpec().getTemplate().getMetadata().getLabels().put("new-key", "val");
58-
assertThat(match(dependentResource, actual, null, context, true, true,
59-
true)
58+
assertThat(match(desired, actual, true, true, context)
6059
.matched())
6160
.withFailMessage("Adding values should fail matching when strong equality is required")
6261
.isFalse();
@@ -127,11 +126,11 @@ void matchesMetadata() {
127126
.withFailMessage("Annotations shouldn't matter when metadata is not considered")
128127
.isTrue();
129128

130-
assertThat(match(dependentResource, actual, null, context, true, true, true).matched())
129+
assertThat(match(desired, actual, true, true, context).matched())
131130
.withFailMessage("Annotations should matter when metadata is considered")
132131
.isFalse();
133132

134-
assertThat(match(dependentResource, actual, null, context, true, false).matched())
133+
assertThat(match(desired, actual, false, false, context).matched())
135134
.withFailMessage(
136135
"Should match when strong equality is not considered and only additive changes are made")
137136
.isTrue();
@@ -157,7 +156,7 @@ void matchConfigMap() {
157156
var actual = createConfigMap();
158157
actual.getData().put("key2", "val2");
159158

160-
var match = GenericKubernetesResourceMatcher.match(desired, actual, true,
159+
var match = GenericKubernetesResourceMatcher.match(desired, actual,
161160
true, false, context);
162161
assertThat(match.matched()).isTrue();
163162
}

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/ssalegacymatcher/ServiceDependentResource.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ protected Service desired(SSALegacyMatcherCustomResource primary,
3939
@Override
4040
public Result<Service> match(Service actualResource, SSALegacyMatcherCustomResource primary,
4141
Context<SSALegacyMatcherCustomResource> context) {
42+
var desired = desired(primary, context);
43+
4244
return GenericKubernetesResourceMatcher.match(this, actualResource, primary, context,
43-
true, false, false);
45+
false, false);
4446
}
4547

4648
// override just to check the exec count

0 commit comments

Comments
 (0)