@@ -188,33 +188,17 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R d
188
188
"Equality should be false in case of ignore list provided" );
189
189
}
190
190
191
- if (considerMetadata ) {
192
- Optional <Result <R >> res =
193
- matchMetadata (desired , actualResource , labelsAndAnnotationsEquality , context );
194
- if (res .isPresent ()) {
195
- return res .orElseThrow ();
196
- }
197
- }
198
-
199
- final var matched = match (actualResource , desired , specEquality , context , ignoredPaths );
200
- return Result .computed (matched , desired );
201
- }
202
-
203
- private static <R extends HasMetadata > boolean match (R actual , R desired , boolean equality ,
204
- Context <?> context ,
205
- String [] ignoredPaths ) {
206
-
207
191
final var kubernetesSerialization = context .getClient ().getKubernetesSerialization ();
208
192
var desiredNode = kubernetesSerialization .convertValue (desired , JsonNode .class );
209
- var actualNode = kubernetesSerialization .convertValue (actual , JsonNode .class );
193
+ var actualNode = kubernetesSerialization .convertValue (actualResource , JsonNode .class );
210
194
var wholeDiffJsonPatch = JsonDiff .asJson (desiredNode , actualNode );
211
195
212
- final List <String > ignoreList =
213
- ignoredPaths != null && ignoredPaths .length > 0 ? Arrays .asList (ignoredPaths )
214
- : Collections .emptyList ();
215
- boolean specMatch = match (equality , wholeDiffJsonPatch , ignoreList , SPEC );
196
+ boolean specMatch = match (specEquality , wholeDiffJsonPatch , ignoreList , SPEC );
216
197
if (!specMatch ) {
217
- return false ;
198
+ return Result .computed (false , desired );
199
+ }
200
+ if (considerMetadata && !match (labelsAndAnnotationsEquality , wholeDiffJsonPatch , ignoreList , METADATA_LABELS , METADATA_ANNOTATIONS )) {
201
+ return Result .computed (false , desired );
218
202
}
219
203
// expect everything else to be equal
220
204
var names = desiredNode .fieldNames ();
@@ -225,7 +209,8 @@ private static <R extends HasMetadata> boolean match(R actual, R desired, boolea
225
209
prefixes .add (prefix );
226
210
}
227
211
}
228
- return match (true , wholeDiffJsonPatch , ignoreList , prefixes .toArray (String []::new ));
212
+ boolean matched = match (true , wholeDiffJsonPatch , ignoreList , prefixes .toArray (String []::new ));
213
+ return Result .computed (matched , desired );
229
214
}
230
215
231
216
private static boolean match (boolean equality , JsonNode wholeDiffJsonPatch ,
@@ -241,49 +226,11 @@ private static boolean match(boolean equality, JsonNode wholeDiffJsonPatch,
241
226
return false ;
242
227
}
243
228
if (!ignoreList .isEmpty ()) {
244
- return allDiffsOnIgnoreList ( diffJsonPatch , ignoreList );
229
+ return diffJsonPatch . stream (). allMatch ( n -> nodeIsChildOf ( n , ignoreList ) );
245
230
}
246
231
return allDiffsAreAddOps (diffJsonPatch );
247
232
}
248
233
249
- private static boolean allDiffsOnIgnoreList (List <JsonNode > metadataJSonDiffs ,
250
- List <String > ignoreList ) {
251
- if (metadataJSonDiffs .isEmpty ()) {
252
- return false ;
253
- }
254
- return metadataJSonDiffs .stream ().allMatch (n -> nodeIsChildOf (n , ignoreList ));
255
- }
256
-
257
- private static <R extends HasMetadata , P extends HasMetadata > Optional <Result <R >> matchMetadata (
258
- R desired ,
259
- R actualResource ,
260
- boolean labelsAndAnnotationsEquality , Context <P > context ) {
261
-
262
- if (labelsAndAnnotationsEquality ) {
263
- final var desiredMetadata = desired .getMetadata ();
264
- final var actualMetadata = actualResource .getMetadata ();
265
-
266
- final var matched =
267
- Objects .equals (desiredMetadata .getAnnotations (), actualMetadata .getAnnotations ()) &&
268
- Objects .equals (desiredMetadata .getLabels (), actualMetadata .getLabels ());
269
- if (!matched ) {
270
- return Optional .of (Result .computed (false , desired ));
271
- }
272
- } else {
273
- final var objectMapper = context .getClient ().getKubernetesSerialization ();
274
- var desiredNode = objectMapper .convertValue (desired , JsonNode .class );
275
- var actualNode = objectMapper .convertValue (actualResource , JsonNode .class );
276
- var wholeDiffJsonPatch = JsonDiff .asJson (desiredNode , actualNode );
277
- var metadataJSonDiffs = getDiffsImpactingPathsWithPrefixes (wholeDiffJsonPatch ,
278
- METADATA_LABELS ,
279
- METADATA_ANNOTATIONS );
280
- if (!allDiffsAreAddOps (metadataJSonDiffs )) {
281
- return Optional .of (Result .computed (false , desired ));
282
- }
283
- }
284
- return Optional .empty ();
285
- }
286
-
287
234
static boolean nodeIsChildOf (JsonNode n , List <String > prefixes ) {
288
235
var path = getPath (n );
289
236
return prefixes .stream ().anyMatch (path ::startsWith );
0 commit comments