4
4
import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getUID ;
5
5
import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getVersion ;
6
6
7
- import java .io .IOException ;
8
- import java .util .LinkedList ;
9
- import java .util .List ;
10
- import java .util .Map ;
11
- import java .util .concurrent .ConcurrentHashMap ;
12
-
13
- import org .slf4j .Logger ;
14
- import org .slf4j .LoggerFactory ;
15
-
16
7
import io .fabric8 .kubernetes .api .model .ListOptions ;
17
8
import io .fabric8 .kubernetes .client .CustomResource ;
18
9
import io .fabric8 .kubernetes .client .Watch ;
24
15
import io .javaoperatorsdk .operator .processing .CustomResourceCache ;
25
16
import io .javaoperatorsdk .operator .processing .KubernetesResourceUtils ;
26
17
import io .javaoperatorsdk .operator .processing .event .AbstractEventSource ;
18
+ import java .io .IOException ;
19
+ import java .util .LinkedList ;
20
+ import java .util .List ;
21
+ import org .slf4j .Logger ;
22
+ import org .slf4j .LoggerFactory ;
27
23
28
- /** This is a special case since is not bound to a single custom resource */
24
+ /**
25
+ * This is a special case since is not bound to a single custom resource
26
+ */
29
27
public class CustomResourceEventSource <T extends CustomResource <?, ?>> extends AbstractEventSource
30
28
implements Watcher <T > {
31
29
32
30
private static final Logger log = LoggerFactory .getLogger (CustomResourceEventSource .class );
33
31
34
32
private final ConfiguredController <T > controller ;
35
- private final Map <String , Long > lastGenerationProcessedSuccessfully = new ConcurrentHashMap <>();
36
33
private final List <Watch > watches ;
37
- private final CustomResourceCache customResourceCache ;
34
+ private final CustomResourceCache < T > customResourceCache ;
38
35
39
36
public CustomResourceEventSource (ConfiguredController <T > controller ) {
40
37
this .controller = controller ;
41
38
this .watches = new LinkedList <>();
42
- this .customResourceCache = new CustomResourceCache (
39
+ this .customResourceCache = new CustomResourceCache <> (
43
40
controller .getConfiguration ().getConfigurationService ().getObjectMapper ());
44
41
}
45
42
@@ -86,6 +83,9 @@ public void eventReceived(Watcher.Action action, T customResource) {
86
83
log .debug (
87
84
"Event received for action: {}, resource: {}" , action .name (), getName (customResource ));
88
85
86
+ final String uuid = KubernetesResourceUtils .getUID (customResource );
87
+ final T oldResource = customResourceCache .getLatestResource (uuid ).orElse (null );
88
+
89
89
// cache the latest version of the CR
90
90
customResourceCache .cacheResource (customResource );
91
91
@@ -98,9 +98,11 @@ public void eventReceived(Watcher.Action action, T customResource) {
98
98
return ;
99
99
}
100
100
101
- if (!skipBecauseOfGeneration (customResource )) {
101
+ boolean fire = controller .getConfiguration ().getCustomResourcePredicate ().test (
102
+ controller .getConfiguration (), oldResource , customResource );
103
+
104
+ if (fire ) {
102
105
eventHandler .handleEvent (new CustomResourceEvent (action , customResource , this ));
103
- markLastGenerationProcessed (customResource );
104
106
} else {
105
107
log .debug (
106
108
"Skipping event handling resource {} with version: {}" ,
@@ -109,38 +111,6 @@ public void eventReceived(Watcher.Action action, T customResource) {
109
111
}
110
112
}
111
113
112
- private void markLastGenerationProcessed (T resource ) {
113
- if (controller .getConfiguration ().isGenerationAware ()
114
- && resource .hasFinalizer (controller .getConfiguration ().getFinalizer ())) {
115
- lastGenerationProcessedSuccessfully .put (
116
- KubernetesResourceUtils .getUID (resource ), resource .getMetadata ().getGeneration ());
117
- }
118
- }
119
-
120
- private boolean skipBecauseOfGeneration (T customResource ) {
121
- if (!controller .getConfiguration ().isGenerationAware ()) {
122
- return false ;
123
- }
124
- // if CR being deleted generation is naturally not changing, so we process all the events
125
- if (customResource .isMarkedForDeletion ()) {
126
- return false ;
127
- }
128
-
129
- // only proceed if we haven't already seen this custom resource generation
130
- Long lastGeneration =
131
- lastGenerationProcessedSuccessfully .get (customResource .getMetadata ().getUid ());
132
- if (lastGeneration == null ) {
133
- return false ;
134
- } else {
135
- return customResource .getMetadata ().getGeneration () <= lastGeneration ;
136
- }
137
- }
138
-
139
- @ Override
140
- public void eventSourceDeRegisteredForResource (String customResourceUid ) {
141
- lastGenerationProcessedSuccessfully .remove (customResourceUid );
142
- }
143
-
144
114
@ Override
145
115
public void onClose (WatcherException e ) {
146
116
if (e == null ) {
@@ -164,7 +134,7 @@ public void onClose(WatcherException e) {
164
134
}
165
135
166
136
// todo: remove
167
- public CustomResourceCache getCache () {
137
+ public CustomResourceCache < T > getCache () {
168
138
return customResourceCache ;
169
139
}
170
140
}
0 commit comments