Skip to content

Commit cf774b9

Browse files
committed
refactor: minor clean-ups
1 parent ed36981 commit cf774b9

File tree

5 files changed

+78
-110
lines changed

5 files changed

+78
-110
lines changed

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/informers/cache/Cache.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@
3131
public interface Cache<T> extends Indexer<T> {
3232

3333
// NAMESPACE_INDEX is the default index function for caching objects
34-
public static final String NAMESPACE_INDEX = "namespace";
34+
String NAMESPACE_INDEX = "namespace";
3535

3636
/**
37-
* It's is a convenient default KeyFunc which know show to make keys for API
37+
* A convenient default KeyFunc which knows how to make keys for API
3838
* objects which implement HasMetadata interface. The key uses the format
3939
* namespace/name unless namespace is empty, then it's just name
4040
*
4141
* @param obj specific object
4242
* @return the key
4343
*/
44-
public static String metaNamespaceKeyFunc(HasMetadata obj) {
44+
static String metaNamespaceKeyFunc(HasMetadata obj) {
4545
if (obj == null) {
4646
return "";
4747
}
@@ -53,7 +53,7 @@ public static String metaNamespaceKeyFunc(HasMetadata obj) {
5353
return namespaceKeyFunc(metadata.getNamespace(), metadata.getName());
5454
}
5555

56-
public static String metaUidKeyFunc(HasMetadata obj) {
56+
static String metaUidKeyFunc(HasMetadata obj) {
5757
if (obj == null || obj.getMetadata() == null) {
5858
return "";
5959
}
@@ -66,21 +66,22 @@ public static String metaUidKeyFunc(HasMetadata obj) {
6666
*
6767
* @see #metaNamespaceKeyFunc
6868
*/
69-
public static String namespaceKeyFunc(String objectNamespace, String objectName) {
69+
static String namespaceKeyFunc(String objectNamespace, String objectName) {
7070
if (Utils.isNullOrEmpty(objectNamespace)) {
7171
return objectName;
7272
}
7373
return objectNamespace + "/" + objectName;
7474
}
7575

7676
/**
77-
* It is a default index function that indexes based on an object's namespace
77+
* Default index function that indexes based on an object's namespace
7878
*
7979
* @param obj the specific object
8080
* @return the indexed value
8181
*/
82-
public static List<String> metaNamespaceIndexFunc(HasMetadata obj) {
83-
return Optional.ofNullable(obj).map(HasMetadata::getMetadata)
84-
.map(metadata -> Collections.singletonList(metadata.getNamespace())).orElse(Collections.emptyList());
82+
static List<String> metaNamespaceIndexFunc(HasMetadata obj) {
83+
return Optional.ofNullable(obj)
84+
.map(hm -> List.of(hm.getMetadata().getNamespace()))
85+
.orElse(Collections.emptyList());
8586
}
8687
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/informers/cache/ItemStore.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
* all entries until they are deleted. At its simplest this is just a map coupled with a key function.
2626
* <p>
2727
* Modifications to this store once the informer is running, by anything other than the informer will alter the event stream. If
28-
* for example an item is not found, an subsequent update from the api version will send notifications to
28+
* for example an item is not found, any subsequent update from the api version will send notifications to
2929
* {@link ResourceEventHandler}s as an add.
3030
* <p>
31-
* Direct modifications to this store by anything other than the informer will not updated indexes nor emit events.
31+
* Direct modifications to this store by anything other than the informer will not update indexes nor emit events.
3232
* <p>
3333
* The implementation should be safe with respect to concurrency. Modifications from the informer
3434
* will be single threaded, but not necessarily the same thread. Reads may be concurrent with writes.
@@ -60,8 +60,6 @@ public interface ItemStore<V> {
6060
* <br>
6161
* If false, then the initial add events must be processed as they
6262
* occur - meaning that the store state may not be complete.
63-
*
64-
* @return
6563
*/
6664
default boolean isFullState() {
6765
return true;

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/informers/cache/ReducedStateItemStore.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@
3737
public class ReducedStateItemStore<V extends HasMetadata> implements ItemStore<V> {
3838

3939
private static final String METADATA = "metadata";
40+
private static final Object[] NOT_FOUND_KEY_VALUE = new Object[1];
4041
private final ConcurrentHashMap<String, Object[]> store = new ConcurrentHashMap<>();
4142
private final List<String[]> fields = new ArrayList<>();
4243
private final Class<V> typeClass;
4344
private final KeyState keyState;
44-
private KubernetesSerialization serialization;
45+
private final KubernetesSerialization serialization;
4546

4647
public static class KeyState {
4748

@@ -64,17 +65,20 @@ public KeyState(Function<HasMetadata, String> keyFunction, Function<String, Stri
6465

6566
}
6667

68+
private static final String NAMESPACE = "namespace";
69+
private static final String NAME = "name";
6770
public static final KeyState NAME_KEY_STATE = new KeyState(Cache::metaNamespaceKeyFunc,
6871
k -> {
69-
int index = k.indexOf("/");
72+
int index = k.indexOf('/');
7073
if (index == -1) {
7174
return new String[] { null, k };
7275
}
7376
return new String[] { k.substring(0, index), k.substring(index + 1) };
74-
}, new String[] { METADATA, "namespace" }, new String[] { METADATA, "name" });
77+
}, new String[] { METADATA, NAMESPACE }, new String[] { METADATA, NAME });
7578

79+
private static final String UID = "uid";
7680
public static final KeyState UID_KEY_STATE = new KeyState(Cache::metaUidKeyFunc,
77-
k -> new String[] { k }, new String[] { METADATA, "uid" });
81+
k -> new String[] { k }, new String[] { METADATA, UID });
7882

7983
/**
8084
* Create a state store with only the fields specified.
@@ -106,14 +110,15 @@ public ReducedStateItemStore(KeyState keyState, Class<V> typeClass, KubernetesSe
106110
this.keyState = keyState;
107111
fields.add(new String[] { METADATA, "resourceVersion" });
108112
if (valueFields != null) {
109-
for (int i = 0; i < valueFields.length; i++) {
110-
fields.add(valueFields[i].split("\\."));
113+
for (String valueField : valueFields) {
114+
fields.add(valueField.split("\\."));
111115
}
112116
}
113117
this.typeClass = typeClass;
114118
this.serialization = serialization;
115119
}
116120

121+
@SuppressWarnings("unchecked")
117122
Object[] store(V value) {
118123
if (value == null) {
119124
return null;
@@ -134,6 +139,7 @@ V restore(String key, Object[] values) {
134139
return serialization.convertValue(raw, typeClass);
135140
}
136141

142+
@SuppressWarnings("unchecked")
137143
private static void applyFields(Object[] values, Map<String, Object> raw, List<String[]> fields) {
138144
for (int i = 0; i < fields.size(); i++) {
139145
Object value = values[i];
@@ -175,7 +181,7 @@ public V get(String key) {
175181
}
176182

177183
public String getResourceVersion(String key) {
178-
return (String) store.getOrDefault(key, new Object[1])[0];
184+
return (String) store.getOrDefault(key, NOT_FOUND_KEY_VALUE)[0];
179185
}
180186

181187
@Override

kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/informers/cache/ReducedStateItemStoreTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,27 @@ void testStoreRestore() {
3434
Pod pod = new PodBuilder().withNewSpec().endSpec().withNewMetadata().withUid("x").withName("y").addToLabels("one", "1")
3535
.addToLabels("two", "2").withResourceVersion("2").endMetadata().withNewStatus().endStatus().build();
3636

37+
final var uid = pod.getMetadata().getUid();
38+
3739
Object[] values = store.store(pod);
3840

3941
assertEquals(3, values.length);
4042
assertEquals("2", values[0]); // always the resourceVersion
4143
assertEquals(pod.getMetadata().getLabels(), values[1]);
4244
assertNull(values[2]);
4345

44-
Pod restored = store.restore("x", values);
46+
Pod restored = store.restore(uid, values);
4547

4648
assertNull(restored.getSpec());
4749
assertNull(restored.getStatus());
48-
assertEquals("x", restored.getMetadata().getUid());
50+
assertEquals(uid, restored.getMetadata().getUid());
4951
assertEquals(pod.getMetadata().getLabels(), restored.getMetadata().getLabels());
5052

51-
assertNull(store.put("x", pod));
52-
assertNotNull(store.get("x"));
53-
assertEquals("2", store.getResourceVersion("x"));
53+
assertNull(store.put(uid, pod));
54+
assertNotNull(store.get(uid));
55+
assertEquals("2", store.getResourceVersion(uid));
5456
assertEquals(1, store.size());
55-
assertNotNull(store.remove("x"));
57+
assertNotNull(store.remove(uid));
5658
}
5759

5860
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/impl/cache/CacheImpl.java

Lines changed: 45 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
package io.fabric8.kubernetes.client.informers.impl.cache;
1717

1818
import io.fabric8.kubernetes.api.model.HasMetadata;
19-
import io.fabric8.kubernetes.api.model.ObjectMeta;
2019
import io.fabric8.kubernetes.client.informers.cache.BasicItemStore;
2120
import io.fabric8.kubernetes.client.informers.cache.Cache;
2221
import io.fabric8.kubernetes.client.informers.cache.ItemStore;
23-
import io.fabric8.kubernetes.client.utils.Utils;
2422

2523
import java.util.ArrayList;
2624
import java.util.Collections;
@@ -45,36 +43,14 @@
4543
*/
4644
public class CacheImpl<T extends HasMetadata> implements Cache<T> {
4745

48-
private static class Index {
49-
private Map<Object, Set<String>> values = new ConcurrentHashMap<Object, Set<String>>();
50-
51-
public void update(String indexKey, String key, boolean remove) {
52-
if (remove) {
53-
values.computeIfPresent(indexKey == null ? this : indexKey, (k, v) -> {
54-
v.remove(key);
55-
return v.isEmpty() ? null : v;
56-
});
57-
} else {
58-
values.computeIfAbsent(indexKey == null ? this : indexKey, k -> ConcurrentHashMap.newKeySet()).add(key);
59-
}
60-
}
61-
62-
public Set<String> get(String indexKey) {
63-
return values.getOrDefault(indexKey == null ? this : indexKey, Collections.emptySet());
64-
}
65-
}
66-
6746
// NAMESPACE_INDEX is the default index function for caching objects
6847
public static final String NAMESPACE_INDEX = "namespace";
69-
7048
// indexers stores index functions by their names
7149
private final Map<String, Function<T, List<String>>> indexers = Collections.synchronizedMap(new HashMap<>());
72-
73-
// items stores object instances
74-
private ItemStore<T> items;
75-
7650
// indices stores objects' key by their indices
7751
private final ConcurrentMap<String, Index> indices = new ConcurrentHashMap<>();
52+
// items stores object instances
53+
private ItemStore<T> items;
7854

7955
public CacheImpl() {
8056
this(NAMESPACE_INDEX, Cache::metaNamespaceIndexFunc, Cache::metaNamespaceKeyFunc);
@@ -85,6 +61,30 @@ public CacheImpl(String indexName, Function<T, List<String>> indexFunc, Function
8561
addIndexFunc(indexName, indexFunc);
8662
}
8763

64+
/**
65+
* @deprecated use {@link Cache#metaNamespaceKeyFunc(HasMetadata)} instead
66+
*/
67+
public static String metaNamespaceKeyFunc(Object obj) {
68+
if (obj == null) {
69+
return "";
70+
}
71+
return Cache.metaNamespaceKeyFunc((HasMetadata) obj);
72+
}
73+
74+
/**
75+
* @deprecated Use {@link Cache#namespaceKeyFunc(String, String)} instead
76+
*/
77+
public static String namespaceKeyFunc(String objectNamespace, String objectName) {
78+
return Cache.namespaceKeyFunc(objectNamespace, objectName);
79+
}
80+
81+
/**
82+
* @deprecated Use {@link Cache#metaNamespaceKeyFunc(HasMetadata)} instead
83+
*/
84+
public static List<String> metaNamespaceIndexFunc(Object obj) {
85+
return Cache.metaNamespaceIndexFunc((HasMetadata) obj);
86+
}
87+
8888
public void setItemStore(ItemStore<T> items) {
8989
this.items = items;
9090
}
@@ -263,7 +263,6 @@ public List<T> byIndex(String indexName, String indexKey) {
263263
* UpdateIndices modifies the objects location in the managed indexes, if there is
264264
* an update, you must provide an oldObj
265265
*
266-
*
267266
* @param oldObj old object
268267
* @param newObj new object
269268
* @param key the key
@@ -312,63 +311,6 @@ public synchronized CacheImpl<T> addIndexFunc(String indexName, Function<T, List
312311
return this;
313312
}
314313

315-
/**
316-
* It's is a convenient default KeyFunc which know show to make keys for API
317-
* objects which implement HasMetadata interface. The key uses the format
318-
* namespace/name unless namespace is empty, then it's just name
319-
*
320-
* @param obj specific object
321-
* @return the key
322-
*/
323-
public static String metaNamespaceKeyFunc(Object obj) {
324-
if (obj == null) {
325-
return "";
326-
}
327-
ObjectMeta metadata = null;
328-
if (obj instanceof String) {
329-
return (String) obj;
330-
} else if (obj instanceof ObjectMeta) {
331-
metadata = (ObjectMeta) obj;
332-
} else if (obj instanceof HasMetadata) {
333-
metadata = ((HasMetadata) obj).getMetadata();
334-
}
335-
if (metadata == null) {
336-
throw new RuntimeException("Object is bad :" + obj);
337-
}
338-
339-
return namespaceKeyFunc(metadata.getNamespace(), metadata.getName());
340-
}
341-
342-
/**
343-
* Default index function that indexes based on an object's namespace and name.
344-
*
345-
* @see #metaNamespaceKeyFunc
346-
*/
347-
public static String namespaceKeyFunc(String objectNamespace, String objectName) {
348-
if (Utils.isNullOrEmpty(objectNamespace)) {
349-
return objectName;
350-
}
351-
return objectNamespace + "/" + objectName;
352-
}
353-
354-
/**
355-
* It is a default index function that indexes based on an object's namespace
356-
*
357-
* @param obj the specific object
358-
* @return the indexed value
359-
*/
360-
public static List<String> metaNamespaceIndexFunc(Object obj) {
361-
final ObjectMeta metadata;
362-
if (obj instanceof HasMetadata) {
363-
metadata = ((HasMetadata) obj).getMetadata();
364-
} else if (obj instanceof ObjectMeta) {
365-
metadata = (ObjectMeta) obj;
366-
} else {
367-
metadata = null;
368-
}
369-
return metadata == null ? Collections.emptyList() : Collections.singletonList(metadata.getNamespace());
370-
}
371-
372314
@Override
373315
public synchronized void removeIndexer(String name) {
374316
this.indices.remove(name);
@@ -383,4 +325,23 @@ public Object getLockObject() {
383325
return this;
384326
}
385327

328+
private static class Index {
329+
private final Map<Object, Set<String>> values = new ConcurrentHashMap<>();
330+
331+
public void update(String indexKey, String key, boolean remove) {
332+
if (remove) {
333+
values.computeIfPresent(indexKey == null ? this : indexKey, (k, v) -> {
334+
v.remove(key);
335+
return v.isEmpty() ? null : v;
336+
});
337+
} else {
338+
values.computeIfAbsent(indexKey == null ? this : indexKey, k -> ConcurrentHashMap.newKeySet()).add(key);
339+
}
340+
}
341+
342+
public Set<String> get(String indexKey) {
343+
return values.getOrDefault(indexKey == null ? this : indexKey, Collections.emptySet());
344+
}
345+
}
346+
386347
}

0 commit comments

Comments
 (0)