1
1
package io .javaoperatorsdk .operator .processing .event .source .cache ;
2
2
3
+ import java .lang .reflect .Constructor ;
3
4
import java .lang .reflect .InvocationTargetException ;
4
5
import java .util .Map ;
5
6
import java .util .concurrent .ConcurrentHashMap ;
14
15
import io .fabric8 .kubernetes .client .KubernetesClient ;
15
16
import io .fabric8 .kubernetes .client .informers .cache .Cache ;
16
17
import io .fabric8 .kubernetes .client .informers .cache .ItemStore ;
18
+ import io .javaoperatorsdk .operator .api .config .Utils ;
17
19
18
20
public class BoundedItemStore <R extends HasMetadata >
19
21
implements ItemStore <R > {
@@ -24,7 +26,7 @@ public class BoundedItemStore<R extends HasMetadata>
24
26
private final BoundedCache <String , R > cache ;
25
27
private final Function <R , String > keyFunction ;
26
28
private final Map <String , R > existingMinimalResources = new ConcurrentHashMap <>();
27
- private final Class <R > resourceClass ;
29
+ private final Constructor <R > resourceConstructor ;
28
30
29
31
public BoundedItemStore (BoundedCache <String , R > cache , Class <R > resourceClass ,
30
32
KubernetesClient client ) {
@@ -39,7 +41,7 @@ public BoundedItemStore(BoundedCache<String, R> cache,
39
41
this .resourceFetcher = resourceFetcher ;
40
42
this .cache = cache ;
41
43
this .keyFunction = keyFunction ;
42
- this .resourceClass = resourceClass ;
44
+ this .resourceConstructor = Utils . getConstructor ( resourceClass ) ;
43
45
}
44
46
45
47
@ Override
@@ -57,14 +59,15 @@ public synchronized R put(String key, R obj) {
57
59
58
60
private R createMinimalResource (R obj ) {
59
61
try {
60
- R minimal = resourceClass .getConstructor ().newInstance ();
61
- minimal .setMetadata (new ObjectMetaBuilder ().build ());
62
- minimal .getMetadata ().setName (obj .getMetadata ().getName ());
63
- minimal .getMetadata ().setNamespace (obj .getMetadata ().getNamespace ());
64
- minimal .getMetadata ().setResourceVersion (obj .getMetadata ().getResourceVersion ());
62
+ R minimal = resourceConstructor .newInstance ();
63
+ final var metadata = obj .getMetadata ();
64
+ minimal .setMetadata (new ObjectMetaBuilder ()
65
+ .withName (metadata .getName ())
66
+ .withNamespace (metadata .getNamespace ())
67
+ .withResourceVersion (metadata .getResourceVersion ())
68
+ .build ());
65
69
return minimal ;
66
- } catch (InstantiationException | IllegalAccessException | InvocationTargetException
67
- | NoSuchMethodException e ) {
70
+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException e ) {
68
71
throw new IllegalStateException (e );
69
72
}
70
73
}
0 commit comments