Skip to content

Commit 61c2e0d

Browse files
committed
refactor: extract common code, rename method more correctly
1 parent a5cfd6e commit 61c2e0d

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

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

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package io.javaoperatorsdk.operator.processing.dependent.kubernetes;
22

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;
49

510
import io.fabric8.kubernetes.api.model.ConfigMap;
611
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -122,7 +127,7 @@ private static <R extends HasMetadata> Result<R> matchSpec(R desired, boolean sp
122127
List<String> ignoreList, JsonNode wholeDiffJsonPatch, boolean considerIgnoreList) {
123128
// reflection will be replaced by this:
124129
// https://github.com/fabric8io/kubernetes-client/issues/3816
125-
var specDiffJsonPatch = getDiffsWithPathSuffix(wholeDiffJsonPatch, "/spec");
130+
var specDiffJsonPatch = getDiffsImpactingPathsWithPrefixes(wholeDiffJsonPatch, "/spec");
126131
// In case of equality is set to true, no diffs are allowed, so we return early if diffs exist
127132
// On contrary (if equality is false), "add" is allowed for cases when for some
128133
// resources Kubernetes fills-in values into spec.
@@ -154,7 +159,7 @@ private static <R extends HasMetadata> Optional<Result<R>> matchMetadata(R desir
154159
return Optional.of(Result.computed(false, desired));
155160
}
156161
} else {
157-
var metadataJSonDiffs = getDiffsWithPathSuffix(wholeDiffJsonPatch,
162+
var metadataJSonDiffs = getDiffsImpactingPathsWithPrefixes(wholeDiffJsonPatch,
158163
"/metadata/labels",
159164
"/metadata/annotations");
160165
if (!allDiffsAreAddOps(metadataJSonDiffs)) {
@@ -176,20 +181,25 @@ private static boolean allDiffsOnIgnoreList(List<JsonNode> metadataJSonDiffs,
176181
if (metadataJSonDiffs.isEmpty()) {
177182
return false;
178183
}
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));
183185
}
184186

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) {
187198
var res = new ArrayList<JsonNode>();
188-
var prefixList = Arrays.asList(ignorePaths);
199+
var prefixList = Arrays.asList(prefixes);
189200
for (int i = 0; i < diffJsonPatch.size(); i++) {
190201
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)) {
193203
res.add(node);
194204
}
195205
}

0 commit comments

Comments
 (0)