Skip to content

Commit ddcbbfd

Browse files
authored
Merge pull request #1015 from yue9944882/chore/refactor-informer-use-common-interface
Switch to kubernetes object common interfaces for informers
2 parents 4f5f6cb + 5830e75 commit ddcbbfd

File tree

15 files changed

+169
-244
lines changed

15 files changed

+169
-244
lines changed

util/src/main/java/io/kubernetes/client/informer/ResyncRunnable.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.kubernetes.client.informer;
22

3-
import io.kubernetes.client.common.KubernetesObject;
4-
import io.kubernetes.client.informer.cache.Store;
3+
import io.kubernetes.client.informer.cache.DeltaFIFO;
54
import java.util.function.Supplier;
65
import org.slf4j.Logger;
76
import org.slf4j.LoggerFactory;
@@ -10,14 +9,14 @@
109
* ResyncRunnable class implements Runnable interface. It calls the resync function of Store
1110
* interface which is actually always implemented by DeltaFIFO.
1211
*/
13-
public class ResyncRunnable<ApiType extends KubernetesObject> implements Runnable {
12+
public class ResyncRunnable implements Runnable {
1413

1514
private static final Logger log = LoggerFactory.getLogger(ResyncRunnable.class);
1615

17-
private Store<ApiType> store;
16+
private DeltaFIFO store;
1817
private Supplier<Boolean> shouldResyncFunc;
1918

20-
public ResyncRunnable(Store<ApiType> store, Supplier<Boolean> shouldResyncFunc) {
19+
public ResyncRunnable(DeltaFIFO store, Supplier<Boolean> shouldResyncFunc) {
2120
this.store = store;
2221
this.shouldResyncFunc = shouldResyncFunc;
2322
}

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

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ public class Cache<ApiType extends KubernetesObject> implements Indexer<ApiType>
2323
/** keyFunc defines how to map objects into indices */
2424
private Function<ApiType, String> keyFunc;
2525

26-
/**
27-
* DEPRECATE: use Caches#NAMESPACE_INDEX instead. TODO: remove after 7.0.0
28-
*
29-
* <p>NAMESPACE_INDEX is the default index function for caching objects
30-
*/
31-
@Deprecated public static final String NAMESPACE_INDEX = "namespace";
32-
3326
/** indexers stores index functions by their names */
3427
private Map<String, Function<ApiType, List<String>>> indexers = new HashMap<>();
3528

@@ -432,48 +425,4 @@ public Function<ApiType, String> getKeyFunc() {
432425
public void setKeyFunc(Function<ApiType, String> keyFunc) {
433426
this.keyFunc = keyFunc;
434427
}
435-
436-
/**
437-
* DEPRECATE: use Caches#deletionHandlingMetaNamespaceKeyFunc instead. TODO: remove after 7.0.0
438-
*
439-
* <p>deletionHandlingMetaNamespaceKeyFunc checks for DeletedFinalStateUnknown objects before
440-
* calling metaNamespaceKeyFunc.
441-
*
442-
* @param <ApiType> the type parameter
443-
* @param object specific object
444-
* @return the key
445-
*/
446-
@Deprecated
447-
public static <ApiType> String deletionHandlingMetaNamespaceKeyFunc(ApiType object) {
448-
return Caches.deletionHandlingMetaNamespaceKeyFunc(object);
449-
}
450-
451-
/**
452-
* DEPRECATE: use Caches#metaNamespaceKeyFunc instead. TODO: remove after 7.0.0
453-
*
454-
* <p>MetaNamespaceKeyFunc is a convenient default KeyFunc which knows how to make keys for API
455-
* objects which implement HasMetadata Interface. The key uses the format <namespace>/<name>
456-
* unless <namespace> is empty, then it's just <name>.
457-
*
458-
* @param obj specific object
459-
* @return the key
460-
*/
461-
@Deprecated
462-
public static String metaNamespaceKeyFunc(Object obj) {
463-
return Caches.metaNamespaceKeyFunc(obj);
464-
}
465-
466-
/**
467-
* DEPRECATE: use Caches#metaNamespaceIndexFunc instead. TODO: remove after 7.0.0
468-
*
469-
* <p>metaNamespaceIndexFunc is a default index function that indexes based on an object's
470-
* namespace.
471-
*
472-
* @param obj specific object
473-
* @return the indexed value
474-
*/
475-
@Deprecated
476-
public static List<String> metaNamespaceIndexFunc(Object obj) {
477-
return Caches.metaNamespaceIndexFunc(obj);
478-
}
479428
}

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

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.kubernetes.client.informer.cache;
22

33
import com.google.common.base.Strings;
4-
import io.kubernetes.client.informer.exception.BadObjectException;
4+
import io.kubernetes.client.common.KubernetesObject;
55
import io.kubernetes.client.openapi.models.V1ObjectMeta;
66
import io.kubernetes.client.util.ObjectAccessor;
77
import io.kubernetes.client.util.exception.ObjectMetaReflectException;
@@ -22,9 +22,11 @@ public class Caches {
2222
* @param object specific object
2323
* @return the key
2424
*/
25-
public static <ApiType> String deletionHandlingMetaNamespaceKeyFunc(ApiType object) {
25+
public static <ApiType extends KubernetesObject> String deletionHandlingMetaNamespaceKeyFunc(
26+
ApiType object) {
2627
if (object instanceof DeltaFIFO.DeletedFinalStateUnknown) {
27-
DeltaFIFO.DeletedFinalStateUnknown deleteObj = (DeltaFIFO.DeletedFinalStateUnknown) object;
28+
DeltaFIFO.DeletedFinalStateUnknown<ApiType> deleteObj =
29+
(DeltaFIFO.DeletedFinalStateUnknown<ApiType>) object;
2830
return deleteObj.getKey();
2931
}
3032
return metaNamespaceKeyFunc(object);
@@ -38,27 +40,12 @@ public static <ApiType> String deletionHandlingMetaNamespaceKeyFunc(ApiType obje
3840
* @param obj specific object
3941
* @return the key
4042
*/
41-
public static String metaNamespaceKeyFunc(Object obj) {
42-
try {
43-
V1ObjectMeta metadata;
44-
if (obj instanceof String) {
45-
return (String) obj;
46-
} else if (obj instanceof V1ObjectMeta) {
47-
metadata = (V1ObjectMeta) obj;
48-
} else {
49-
metadata = ObjectAccessor.objectMetadata(obj);
50-
if (metadata == null) {
51-
throw new BadObjectException(obj);
52-
}
53-
}
54-
if (!Strings.isNullOrEmpty(metadata.getNamespace())) {
55-
return metadata.getNamespace() + "/" + metadata.getName();
56-
}
57-
return metadata.getName();
58-
} catch (ObjectMetaReflectException e) {
59-
// NOTE(yue9944882): might want to handle this as a checked exception
60-
throw new RuntimeException(e);
43+
public static String metaNamespaceKeyFunc(KubernetesObject obj) {
44+
V1ObjectMeta metadata = obj.getMetadata();
45+
if (!Strings.isNullOrEmpty(metadata.getNamespace())) {
46+
return metadata.getNamespace() + "/" + metadata.getName();
6147
}
48+
return metadata.getName();
6249
}
6350

6451
/**

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Controller<
2828
private long fullResyncPeriod;
2929

3030
/** Queue stores deltas produced by reflector */
31-
private DeltaFIFO<ApiType> queue;
31+
private DeltaFIFO queue;
3232

3333
private ListerWatcher<ApiType, ApiListType> listerWatcher;
3434

@@ -37,7 +37,7 @@ public class Controller<
3737
private Supplier<Boolean> resyncFunc;
3838

3939
/** how we actually process items from the queue */
40-
private Consumer<Deque<MutablePair<DeltaFIFO.DeltaType, Object>>> processFunc;
40+
private Consumer<Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>>> processFunc;
4141

4242
private ScheduledExecutorService reflectExecutor;
4343

@@ -51,9 +51,9 @@ public class Controller<
5151

5252
public Controller(
5353
Class<ApiType> apiTypeClass,
54-
DeltaFIFO<ApiType> queue,
54+
DeltaFIFO queue,
5555
ListerWatcher<ApiType, ApiListType> listerWatcher,
56-
Consumer<Deque<MutablePair<DeltaFIFO.DeltaType, Object>>> processFunc,
56+
Consumer<Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>>> processFunc,
5757
Supplier<Boolean> resyncFunc,
5858
long fullResyncPeriod) {
5959
this.queue = queue;
@@ -80,9 +80,9 @@ public Controller(
8080

8181
public Controller(
8282
Class<ApiType> apiTypeClass,
83-
DeltaFIFO<ApiType> queue,
83+
DeltaFIFO queue,
8484
ListerWatcher<ApiType, ApiListType> listerWatcher,
85-
Consumer<Deque<MutablePair<DeltaFIFO.DeltaType, Object>>> popProcessFunc) {
85+
Consumer<Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>>> popProcessFunc) {
8686
this(apiTypeClass, queue, listerWatcher, popProcessFunc, null, 0);
8787
}
8888

0 commit comments

Comments
 (0)