4
4
import java .util .Arrays ;
5
5
import java .util .Collections ;
6
6
import java .util .List ;
7
- import java .util .Objects ;
8
- import java .util .Optional ;
9
7
import java .util .Set ;
10
8
11
9
import io .fabric8 .kubernetes .api .model .ConfigMap ;
@@ -188,33 +186,18 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R d
188
186
"Equality should be false in case of ignore list provided" );
189
187
}
190
188
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
189
final var kubernetesSerialization = context .getClient ().getKubernetesSerialization ();
208
190
var desiredNode = kubernetesSerialization .convertValue (desired , JsonNode .class );
209
- var actualNode = kubernetesSerialization .convertValue (actual , JsonNode .class );
191
+ var actualNode = kubernetesSerialization .convertValue (actualResource , JsonNode .class );
210
192
var wholeDiffJsonPatch = JsonDiff .asJson (desiredNode , actualNode );
211
193
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 );
194
+ boolean specMatch = match (specEquality , wholeDiffJsonPatch , ignoreList , SPEC );
216
195
if (!specMatch ) {
217
- return false ;
196
+ return Result .computed (false , desired );
197
+ }
198
+ if (considerMetadata && !match (labelsAndAnnotationsEquality , wholeDiffJsonPatch , ignoreList ,
199
+ METADATA_LABELS , METADATA_ANNOTATIONS )) {
200
+ return Result .computed (false , desired );
218
201
}
219
202
// expect everything else to be equal
220
203
var names = desiredNode .fieldNames ();
@@ -225,7 +208,8 @@ private static <R extends HasMetadata> boolean match(R actual, R desired, boolea
225
208
prefixes .add (prefix );
226
209
}
227
210
}
228
- return match (true , wholeDiffJsonPatch , ignoreList , prefixes .toArray (String []::new ));
211
+ boolean matched = match (true , wholeDiffJsonPatch , ignoreList , prefixes .toArray (String []::new ));
212
+ return Result .computed (matched , desired );
229
213
}
230
214
231
215
private static boolean match (boolean equality , JsonNode wholeDiffJsonPatch ,
@@ -241,49 +225,11 @@ private static boolean match(boolean equality, JsonNode wholeDiffJsonPatch,
241
225
return false ;
242
226
}
243
227
if (!ignoreList .isEmpty ()) {
244
- return allDiffsOnIgnoreList ( diffJsonPatch , ignoreList );
228
+ return diffJsonPatch . stream (). allMatch ( n -> nodeIsChildOf ( n , ignoreList ) );
245
229
}
246
230
return allDiffsAreAddOps (diffJsonPatch );
247
231
}
248
232
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
233
static boolean nodeIsChildOf (JsonNode n , List <String > prefixes ) {
288
234
var path = getPath (n );
289
235
return prefixes .stream ().anyMatch (path ::startsWith );
0 commit comments