Skip to content

Commit d08ee43

Browse files
committed
fix issue 912: do dedupe with resourceVersion
1 parent ae0d63d commit d08ee43

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

util/src/main/java/io/kubernetes/client/informer/cache/DeltaFIFO.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.function.Consumer;
3030
import java.util.function.Function;
3131
import org.apache.commons.collections4.CollectionUtils;
32+
import org.apache.commons.lang3.StringUtils;
3233
import org.apache.commons.lang3.tuple.MutablePair;
3334
import org.slf4j.Logger;
3435
import org.slf4j.LoggerFactory;
@@ -443,7 +444,15 @@ private Deque<MutablePair<DeltaType, KubernetesObject>> combineDeltas(
443444
*/
444445
private MutablePair<DeltaType, KubernetesObject> isDuplicate(
445446
MutablePair<DeltaType, KubernetesObject> d1, MutablePair<DeltaType, KubernetesObject> d2) {
446-
return isDeletionDup(d1, d2);
447+
MutablePair<DeltaType, KubernetesObject> deletionDelta = isDeletionDup(d1, d2);
448+
if (deletionDelta != null) {
449+
return deletionDelta;
450+
}
451+
if (d1.getLeft() != DeltaType.Deleted && d2.getLeft() != DeltaType.Deleted &&
452+
StringUtils.equals(d1.getRight().getMetadata().getResourceVersion(), d2.getRight().getMetadata().getResourceVersion())) {
453+
return d1;
454+
}
455+
return null;
447456
}
448457

449458
/**

util/src/test/java/io/kubernetes/client/informer/cache/DeltaFIFOTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.kubernetes.client.openapi.models.V1ObjectMeta;
2121
import io.kubernetes.client.openapi.models.V1Pod;
2222
import java.util.Arrays;
23+
import java.util.Collections;
2324
import java.util.Deque;
2425
import java.util.LinkedList;
2526
import org.apache.commons.lang3.tuple.MutablePair;
@@ -94,7 +95,7 @@ public void testDeltaFIFOBasic() throws InterruptedException {
9495

9596
@Test
9697
public void testDeltaFIFODedup() {
97-
V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default"));
98+
V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default").resourceVersion("ver"));
9899
Cache cache = new Cache();
99100
DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, cache);
100101
Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>> deltas;
@@ -121,6 +122,12 @@ public void testDeltaFIFODedup() {
121122
assertEquals(foo1, deltas.peekFirst().getRight());
122123
assertEquals(2, deltas.size());
123124
deltaFIFO.getItems().remove(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
125+
126+
// add-sync dedupe
127+
deltaFIFO.add(foo1);
128+
deltaFIFO.replace(Collections.singletonList(foo1), foo1.getMetadata().getResourceVersion());
129+
deltas = deltaFIFO.getItems().get(Caches.deletionHandlingMetaNamespaceKeyFunc(foo1));
130+
assertEquals(1, deltas.size());
124131
}
125132

126133
@Test

0 commit comments

Comments
 (0)