44import java .util .Arrays ;
55import java .util .Collections ;
66import java .util .List ;
7- import java .util .Objects ;
8- import java .util .Optional ;
97import java .util .Set ;
108
119import io .fabric8 .kubernetes .api .model .ConfigMap ;
@@ -188,33 +186,18 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(R d
188186 "Equality should be false in case of ignore list provided" );
189187 }
190188
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-
207189 final var kubernetesSerialization = context .getClient ().getKubernetesSerialization ();
208190 var desiredNode = kubernetesSerialization .convertValue (desired , JsonNode .class );
209- var actualNode = kubernetesSerialization .convertValue (actual , JsonNode .class );
191+ var actualNode = kubernetesSerialization .convertValue (actualResource , JsonNode .class );
210192 var wholeDiffJsonPatch = JsonDiff .asJson (desiredNode , actualNode );
211193
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 );
216195 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 );
218201 }
219202 // expect everything else to be equal
220203 var names = desiredNode .fieldNames ();
@@ -225,7 +208,8 @@ private static <R extends HasMetadata> boolean match(R actual, R desired, boolea
225208 prefixes .add (prefix );
226209 }
227210 }
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 );
229213 }
230214
231215 private static boolean match (boolean equality , JsonNode wholeDiffJsonPatch ,
@@ -241,49 +225,11 @@ private static boolean match(boolean equality, JsonNode wholeDiffJsonPatch,
241225 return false ;
242226 }
243227 if (!ignoreList .isEmpty ()) {
244- return allDiffsOnIgnoreList ( diffJsonPatch , ignoreList );
228+ return diffJsonPatch . stream (). allMatch ( n -> nodeIsChildOf ( n , ignoreList ) );
245229 }
246230 return allDiffsAreAddOps (diffJsonPatch );
247231 }
248232
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-
287233 static boolean nodeIsChildOf (JsonNode n , List <String > prefixes ) {
288234 var path = getPath (n );
289235 return prefixes .stream ().anyMatch (path ::startsWith );
0 commit comments