1
1
package io .javaoperatorsdk .operator .processing .event .internal ;
2
2
3
3
import java .io .IOException ;
4
- import java .util .List ;
4
+ import java .util .Objects ;
5
+ import java .util .Set ;
6
+ import java .util .function .Function ;
5
7
6
8
import io .fabric8 .kubernetes .api .model .HasMetadata ;
7
9
import io .fabric8 .kubernetes .client .KubernetesClient ;
8
10
import io .fabric8 .kubernetes .client .informers .ResourceEventHandler ;
9
11
import io .fabric8 .kubernetes .client .informers .SharedInformer ;
12
+ import io .fabric8 .kubernetes .client .informers .cache .Cache ;
10
13
import io .fabric8 .kubernetes .client .informers .cache .Store ;
11
14
import io .javaoperatorsdk .operator .processing .event .AbstractEventSource ;
12
15
13
16
public class InformerEventSource <T extends HasMetadata > extends AbstractEventSource {
14
17
15
18
private final SharedInformer <T > sharedInformer ;
16
- private final ResourceToRelatedCustomResourceUIDMapper <T > mapper ;
19
+ private final Function <T , Set <String >> resourceToUIDs ;
20
+ private final Function <HasMetadata , T > associatedWith ;
17
21
private final boolean skipUpdateEventPropagationIfNoChange ;
18
22
19
23
public InformerEventSource (SharedInformer <T > sharedInformer ,
20
- ResourceToRelatedCustomResourceUIDMapper < T > mapper ) {
21
- this (sharedInformer , mapper , true );
24
+ Function < T , Set < String >> resourceToUIDs ) {
25
+ this (sharedInformer , resourceToUIDs , null , true );
22
26
}
23
27
24
- InformerEventSource (KubernetesClient client , Class <T > type ,
25
- ResourceToRelatedCustomResourceUIDMapper < T > mapper ) {
26
- this (client , type , mapper , false );
28
+ public InformerEventSource (KubernetesClient client , Class <T > type ,
29
+ Function < T , Set < String >> resourceToUIDs ) {
30
+ this (client , type , resourceToUIDs , false );
27
31
}
28
32
29
33
InformerEventSource (KubernetesClient client , Class <T > type ,
30
- ResourceToRelatedCustomResourceUIDMapper < T > mapper ,
34
+ Function < T , Set < String >> resourceToUIDs ,
31
35
boolean skipUpdateEventPropagationIfNoChange ) {
32
- this (client .informers ().sharedIndexInformerFor (type , 0 ), mapper ,
36
+ this (client .informers ().sharedIndexInformerFor (type , 0 ), resourceToUIDs , null ,
33
37
skipUpdateEventPropagationIfNoChange );
34
38
}
35
39
36
40
public InformerEventSource (SharedInformer <T > sharedInformer ,
37
- ResourceToRelatedCustomResourceUIDMapper <T > mapper ,
41
+ Function <T , Set <String >> resourceToUIDs ,
42
+ Function <HasMetadata , T > associatedWith ,
38
43
boolean skipUpdateEventPropagationIfNoChange ) {
39
44
this .sharedInformer = sharedInformer ;
40
- this .mapper = mapper ;
45
+ this .resourceToUIDs = resourceToUIDs ;
41
46
this .skipUpdateEventPropagationIfNoChange = skipUpdateEventPropagationIfNoChange ;
42
47
43
- sharedInformer .addEventHandler (new ResourceEventHandler <T >() {
48
+ this .associatedWith = Objects .requireNonNullElseGet (associatedWith , () -> cr -> {
49
+ final var metadata = cr .getMetadata ();
50
+ return getStore ().getByKey (Cache .namespaceKeyFunc (metadata .getNamespace (),
51
+ metadata .getName ()));
52
+ });
53
+
54
+ sharedInformer .addEventHandler (new ResourceEventHandler <>() {
44
55
@ Override
45
56
public void onAdd (T t ) {
46
57
propagateEvent (InformerEvent .Action .ADD , t , null );
@@ -64,7 +75,7 @@ public void onDelete(T t, boolean b) {
64
75
}
65
76
66
77
private void propagateEvent (InformerEvent .Action action , T object , T oldObject ) {
67
- var uids = mapper . map (object );
78
+ var uids = resourceToUIDs . apply (object );
68
79
if (uids .isEmpty ()) {
69
80
return ;
70
81
}
@@ -88,12 +99,19 @@ public Store<T> getStore() {
88
99
return sharedInformer .getStore ();
89
100
}
90
101
91
- public SharedInformer <T > getSharedInformer () {
92
- return sharedInformer ;
102
+ /**
103
+ * Retrieves the informed resource associated with the specified primary resource as defined by
104
+ * the function provided when this InformerEventSource was created
105
+ *
106
+ * @param resource the primary resource we want to retrieve the associated resource for
107
+ * @return the informed resource associated with the specified primary resource
108
+ */
109
+ public T getAssociated (HasMetadata resource ) {
110
+ return associatedWith .apply (resource );
93
111
}
94
112
95
- public interface ResourceToRelatedCustomResourceUIDMapper <T > {
96
- List <String > map (T resource );
97
- }
98
113
114
+ public SharedInformer <T > getSharedInformer () {
115
+ return sharedInformer ;
116
+ }
99
117
}
0 commit comments