Skip to content

Commit 4951b85

Browse files
committed
implement DefaultSharedIndexInformer#addIndexers
1 parent e96d8f6 commit 4951b85

4 files changed

Lines changed: 98 additions & 12 deletions

File tree

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,38 @@ public List<ApiType> byIndex(String indexName, String indexKey) {
319319
}
320320
}
321321

322+
/**
323+
* Return the indexers registered with the cache.
324+
*
325+
* @return registered indexers
326+
*/
327+
@Override
328+
public Map<String, Function<ApiType, List<String>>> getIndexers() {
329+
return indexers;
330+
}
331+
332+
/**
333+
* Add additional indexers to the cache.
334+
*
335+
* @param newIndexers indexers to add
336+
*/
337+
@Override
338+
public void addIndexers(Map<String, Function<ApiType, List<String>>> newIndexers) {
339+
if (!items.isEmpty()) {
340+
throw new IllegalStateException("cannot add indexers to a non-empty cache");
341+
}
342+
Set<String> oldKeys = indexers.keySet();
343+
Set<String> newKeys = newIndexers.keySet();
344+
Set<String> intersection = new HashSet<>(oldKeys);
345+
intersection.retainAll(newKeys);
346+
if (!intersection.isEmpty()) {
347+
throw new IllegalArgumentException("indexer conflict: " + intersection);
348+
}
349+
for (Map.Entry<String, Function<ApiType, List<String>>> indexEntry : newIndexers.entrySet()) {
350+
addIndexFunc(indexEntry.getKey(), indexEntry.getValue());
351+
}
352+
}
353+
322354
/**
323355
* updateIndices modifies the objects location in the managed indexes, if this is an update, you
324356
* must provide an oldObj.
@@ -381,13 +413,8 @@ private void deleteFromIndices(ApiType oldObj, String key) {
381413
}
382414
}
383415

384-
/**
385-
* Add index func.
386-
*
387-
* @param indexName the index name
388-
* @param indexFunc the index func
389-
*/
390-
public void addIndexFunc(String indexName, Function<ApiType, List<String>> indexFunc) {
416+
/** Protected for testing. */
417+
protected void addIndexFunc(String indexName, Function<ApiType, List<String>> indexFunc) {
391418
this.indices.put(indexName, new HashMap<>());
392419
this.indexers.put(indexName, indexFunc);
393420
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.kubernetes.client.informer.cache;
22

33
import java.util.List;
4+
import java.util.Map;
5+
import java.util.function.Function;
46

57
/** Indexer extends Store interface and adds index/de-index methods. */
68
public interface Indexer<ApiType> extends Store<ApiType> {
@@ -30,4 +32,18 @@ public interface Indexer<ApiType> extends Store<ApiType> {
3032
* @return matched objects
3133
*/
3234
List<ApiType> byIndex(String indexName, String indexKey);
35+
36+
/**
37+
* Return the indexers registered with the store.
38+
*
39+
* @return registered indexers
40+
*/
41+
Map<String, Function<ApiType, List<String>>> getIndexers();
42+
43+
/**
44+
* Add additional indexers to the store.
45+
*
46+
* @param indexers indexers to add
47+
*/
48+
void addIndexers(Map<String, Function<ApiType, List<String>>> indexers);
3349
}

util/src/main/java/io/kubernetes/client/informer/impl/DefaultSharedIndexInformer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Deque;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.function.Function;
1516
import org.apache.commons.collections4.CollectionUtils;
1617
import org.apache.commons.lang3.tuple.MutablePair;
1718
import org.slf4j.Logger;
@@ -223,8 +224,11 @@ private void handleDeltas(Deque<MutablePair<DeltaFIFO.DeltaType, Object>> deltas
223224
}
224225

225226
@Override
226-
public void addIndexers(Map indexers) {
227-
throw new RuntimeException("unimplemented!");
227+
public void addIndexers(Map<String, Function<ApiType, List<String>>> indexers) {
228+
if (started) {
229+
throw new IllegalStateException("cannot add indexers to a running informer");
230+
}
231+
indexer.addIndexers(indexers);
228232
}
229233

230234
@Override

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
import io.kubernetes.client.openapi.models.V1ObjectMeta;
66
import io.kubernetes.client.openapi.models.V1Pod;
77
import io.kubernetes.client.openapi.models.V1PodSpec;
8-
import java.util.Arrays;
9-
import java.util.Collection;
10-
import java.util.List;
8+
import java.util.*;
9+
import java.util.function.Function;
1110
import org.junit.Rule;
1211
import org.junit.Test;
1312
import org.junit.contrib.java.lang.system.EnvironmentVariables;
@@ -144,4 +143,44 @@ public void testMultiIndexFuncCacheStore() {
144143
List<V1Pod> nodeNameIndexedPods = podCache.byIndex(testIndexFuncName, "node1");
145144
assertEquals(1, nodeNameIndexedPods.size());
146145
}
146+
147+
@Test
148+
public void testAddIndexers() {
149+
Cache<V1Pod> podCache = new Cache<>();
150+
151+
String nodeIndex = "node-index";
152+
String clusterIndex = "cluster-index";
153+
154+
Map<String, Function<V1Pod, List<String>>> indexers = new HashMap<>();
155+
156+
indexers.put(
157+
nodeIndex,
158+
(V1Pod pod) -> {
159+
return Arrays.asList(pod.getSpec().getNodeName());
160+
});
161+
162+
indexers.put(
163+
clusterIndex,
164+
(V1Pod pod) -> {
165+
return Arrays.asList(pod.getMetadata().getClusterName());
166+
});
167+
168+
podCache.addIndexers(indexers);
169+
170+
V1Pod testPod =
171+
new V1Pod()
172+
.metadata(new V1ObjectMeta().namespace("ns").name("n").clusterName("cluster1"))
173+
.spec(new V1PodSpec().nodeName("node1"));
174+
175+
podCache.add(testPod);
176+
177+
List<V1Pod> namespaceIndexedPods = podCache.byIndex(Caches.NAMESPACE_INDEX, "ns");
178+
assertEquals(1, namespaceIndexedPods.size());
179+
180+
List<V1Pod> nodeNameIndexedPods = podCache.byIndex(nodeIndex, "node1");
181+
assertEquals(1, nodeNameIndexedPods.size());
182+
183+
List<V1Pod> clusterNameIndexedPods = podCache.byIndex(clusterIndex, "cluster1");
184+
assertEquals(1, clusterNameIndexedPods.size());
185+
}
147186
}

0 commit comments

Comments
 (0)