10
10
import org .slf4j .LoggerFactory ;
11
11
12
12
import java .util .ArrayList ;
13
+ import java .util .Map ;
14
+ import java .util .concurrent .ConcurrentHashMap ;
13
15
14
16
/**
15
17
* Dispatches events to the Controller and handles Finalizers for a single type of Custom Resource.
@@ -21,13 +23,16 @@ public class EventDispatcher {
21
23
private final ResourceController controller ;
22
24
private final String resourceDefaultFinalizer ;
23
25
private final CustomResourceFacade customResourceFacade ;
26
+ private final boolean generationAware ;
27
+ private final Map <String , Long > lastGenerationProcessedSuccessfully = new ConcurrentHashMap <>();
24
28
25
29
public EventDispatcher (ResourceController controller ,
26
30
String defaultFinalizer ,
27
- CustomResourceFacade customResourceFacade ) {
31
+ CustomResourceFacade customResourceFacade , boolean generationAware ) {
28
32
this .controller = controller ;
29
33
this .customResourceFacade = customResourceFacade ;
30
34
this .resourceDefaultFinalizer = defaultFinalizer ;
35
+ this .generationAware = generationAware ;
31
36
}
32
37
33
38
public void handleEvent (CustomResourceEvent event ) {
@@ -47,6 +52,7 @@ public void handleEvent(CustomResourceEvent event) {
47
52
if (removeFinalizer && ControllerUtils .hasDefaultFinalizer (resource , resourceDefaultFinalizer )) {
48
53
removeDefaultFinalizer (resource );
49
54
}
55
+ cleanup (resource );
50
56
} else {
51
57
if (!ControllerUtils .hasDefaultFinalizer (resource , resourceDefaultFinalizer ) && !markedForDeletion (resource )) {
52
58
/* We always add the default finalizer if missing and not marked for deletion.
@@ -56,16 +62,42 @@ public void handleEvent(CustomResourceEvent event) {
56
62
*/
57
63
updateCustomResourceWithFinalizer (resource );
58
64
} else {
59
- UpdateControl <? extends CustomResource > updateControl = controller .createOrUpdateResource (resource , context );
60
- if (updateControl .isUpdateStatusSubResource ()) {
61
- customResourceFacade .updateStatus (updateControl .getCustomResource ());
62
- } else if (updateControl .isUpdateCustomResource ()) {
63
- updateCustomResource (updateControl .getCustomResource ());
65
+ if (!generationAware || largerGenerationThenProcessedBefore (resource )) {
66
+ UpdateControl <? extends CustomResource > updateControl = controller .createOrUpdateResource (resource , context );
67
+ if (updateControl .isUpdateStatusSubResource ()) {
68
+ customResourceFacade .updateStatus (updateControl .getCustomResource ());
69
+ } else if (updateControl .isUpdateCustomResource ()) {
70
+ updateCustomResource (updateControl .getCustomResource ());
71
+ }
72
+ markLastGenerationProcessed (resource );
73
+ } else {
74
+ log .debug ("Skipping processing since generation not increased. Event: {}" , event );
64
75
}
65
76
}
66
77
}
67
78
}
68
79
80
+ public boolean largerGenerationThenProcessedBefore (CustomResource resource ) {
81
+ Long lastGeneration = lastGenerationProcessedSuccessfully .get (resource .getMetadata ().getUid ());
82
+ if (lastGeneration == null ) {
83
+ return true ;
84
+ } else {
85
+ return resource .getMetadata ().getGeneration () > lastGeneration ;
86
+ }
87
+ }
88
+
89
+ private void cleanup (CustomResource resource ) {
90
+ if (generationAware ) {
91
+ lastGenerationProcessedSuccessfully .remove (resource .getMetadata ().getUid ());
92
+ }
93
+ }
94
+
95
+ private void markLastGenerationProcessed (CustomResource resource ) {
96
+ if (generationAware ) {
97
+ lastGenerationProcessedSuccessfully .put (resource .getMetadata ().getUid (), resource .getMetadata ().getGeneration ());
98
+ }
99
+ }
100
+
69
101
private void updateCustomResourceWithFinalizer (CustomResource resource ) {
70
102
log .debug ("Adding finalizer for resource: {} version: {}" , resource .getMetadata ().getName (),
71
103
resource .getMetadata ().getResourceVersion ());
0 commit comments