1
1
package io .javaoperatorsdk .operator .processing .dependent .kubernetes ;
2
2
3
- import java .util .*;
3
+ import java .util .ArrayList ;
4
+ import java .util .Arrays ;
5
+ import java .util .Collections ;
6
+ import java .util .List ;
7
+ import java .util .Objects ;
8
+ import java .util .Optional ;
4
9
5
10
import io .fabric8 .kubernetes .api .model .ConfigMap ;
6
11
import io .fabric8 .kubernetes .api .model .HasMetadata ;
@@ -122,7 +127,7 @@ private static <R extends HasMetadata> Result<R> matchSpec(R desired, boolean sp
122
127
List <String > ignoreList , JsonNode wholeDiffJsonPatch , boolean considerIgnoreList ) {
123
128
// reflection will be replaced by this:
124
129
// https://github.com/fabric8io/kubernetes-client/issues/3816
125
- var specDiffJsonPatch = getDiffsWithPathSuffix (wholeDiffJsonPatch , "/spec" );
130
+ var specDiffJsonPatch = getDiffsImpactingPathsWithPrefixes (wholeDiffJsonPatch , "/spec" );
126
131
// In case of equality is set to true, no diffs are allowed, so we return early if diffs exist
127
132
// On contrary (if equality is false), "add" is allowed for cases when for some
128
133
// resources Kubernetes fills-in values into spec.
@@ -154,7 +159,7 @@ private static <R extends HasMetadata> Optional<Result<R>> matchMetadata(R desir
154
159
return Optional .of (Result .computed (false , desired ));
155
160
}
156
161
} else {
157
- var metadataJSonDiffs = getDiffsWithPathSuffix (wholeDiffJsonPatch ,
162
+ var metadataJSonDiffs = getDiffsImpactingPathsWithPrefixes (wholeDiffJsonPatch ,
158
163
"/metadata/labels" ,
159
164
"/metadata/annotations" );
160
165
if (!allDiffsAreAddOps (metadataJSonDiffs )) {
@@ -176,20 +181,25 @@ private static boolean allDiffsOnIgnoreList(List<JsonNode> metadataJSonDiffs,
176
181
if (metadataJSonDiffs .isEmpty ()) {
177
182
return false ;
178
183
}
179
- return metadataJSonDiffs .stream ().allMatch (n -> {
180
- var path = n .get ("path" ).asText ();
181
- return ignoreList .stream ().anyMatch (path ::startsWith );
182
- });
184
+ return metadataJSonDiffs .stream ().allMatch (n -> nodeIsChildOf (n , ignoreList ));
183
185
}
184
186
185
- private static List <JsonNode > getDiffsWithPathSuffix (JsonNode diffJsonPatch ,
186
- String ... ignorePaths ) {
187
+ private static boolean nodeIsChildOf (JsonNode n , List <String > prefixes ) {
188
+ var path = getPath (n );
189
+ return prefixes .stream ().anyMatch (path ::startsWith );
190
+ }
191
+
192
+ private static String getPath (JsonNode n ) {
193
+ return n .get ("path" ).asText ();
194
+ }
195
+
196
+ private static List <JsonNode > getDiffsImpactingPathsWithPrefixes (JsonNode diffJsonPatch ,
197
+ String ... prefixes ) {
187
198
var res = new ArrayList <JsonNode >();
188
- var prefixList = Arrays .asList (ignorePaths );
199
+ var prefixList = Arrays .asList (prefixes );
189
200
for (int i = 0 ; i < diffJsonPatch .size (); i ++) {
190
201
var node = diffJsonPatch .get (i );
191
- String path = diffJsonPatch .get (i ).get ("path" ).asText ();
192
- if (prefixList .stream ().anyMatch (path ::startsWith )) {
202
+ if (nodeIsChildOf (node , prefixList )) {
193
203
res .add (node );
194
204
}
195
205
}
0 commit comments