@@ -29,17 +29,14 @@ public class EventDispatcher {
2929 private final ResourceController controller ;
3030 private final String resourceFinalizer ;
3131 private final CustomResourceFacade customResourceFacade ;
32- private final boolean generationAware ;
33- private final Map <String , Long > lastGenerationProcessedSuccessfully = new ConcurrentHashMap <>();
3432 private EventSourceManager eventSourceManager ;
3533
3634 public EventDispatcher (ResourceController controller ,
3735 String finalizer ,
38- CustomResourceFacade customResourceFacade , boolean generationAware ) {
36+ CustomResourceFacade customResourceFacade ) {
3937 this .controller = controller ;
4038 this .customResourceFacade = customResourceFacade ;
4139 this .resourceFinalizer = finalizer ;
42- this .generationAware = generationAware ;
4340 }
4441
4542 public void setEventSourceManager (EventSourceManager eventSourceManager ) {
@@ -60,7 +57,6 @@ private PostExecutionControl handDispatch(ExecutionScope executionScope) {
6057 log .debug ("Handling events: {} for resource {}" , executionScope .getEvents (), resource .getMetadata ());
6158
6259 if (containsCustomResourceDeletedEvent (executionScope .getEvents ())) {
63- cleanup (executionScope .getCustomResource ());
6460 log .debug ("Skipping dispatch processing because of a Delete event: {} with version: {}" ,
6561 getUID (resource ), getVersion (resource ));
6662 return PostExecutionControl .defaultDispatch ();
@@ -87,55 +83,30 @@ private PostExecutionControl handleCreateOrUpdate(ExecutionScope executionScope,
8783 updateCustomResourceWithFinalizer (resource );
8884 return PostExecutionControl .onlyFinalizerAdded ();
8985 } else {
90- if (!skipBecauseOfGenerations (executionScope )) {
91- log .debug ("Executing createOrUpdate for resource {} with version: {} with execution scope: {}" ,
92- getUID (resource ), getVersion (resource ), executionScope );
93- UpdateControl <? extends CustomResource > updateControl = controller .createOrUpdateResource (resource , context );
94- CustomResource updatedCustomResource = null ;
95- if (updateControl .isUpdateStatusSubResource ()) {
96- updatedCustomResource = customResourceFacade .updateStatus (updateControl .getCustomResource ());
97- } else if (updateControl .isUpdateCustomResource ()) {
98- updatedCustomResource = updateCustomResource (updateControl .getCustomResource ());
99- }
100- markLastGenerationProcessed (resource );
101- if (updatedCustomResource != null ) {
102- return PostExecutionControl .customResourceUpdated (updatedCustomResource );
103- } else {
104- return PostExecutionControl .defaultDispatch ();
105- }
86+ log .debug ("Executing createOrUpdate for resource {} with version: {} with execution scope: {}" ,
87+ getUID (resource ), getVersion (resource ), executionScope );
88+ UpdateControl <? extends CustomResource > updateControl = controller .createOrUpdateResource (resource , context );
89+ CustomResource updatedCustomResource = null ;
90+ if (updateControl .isUpdateStatusSubResource ()) {
91+ updatedCustomResource = customResourceFacade .updateStatus (updateControl .getCustomResource ());
92+ } else if (updateControl .isUpdateCustomResource ()) {
93+ updatedCustomResource = updateCustomResource (updateControl .getCustomResource ());
94+ }
95+ if (updatedCustomResource != null ) {
96+ return PostExecutionControl .customResourceUpdated (updatedCustomResource );
10697 } else {
107- log .debug ("Skipping event processing because generations: {} with version: {}" ,
108- getUID (resource ), getVersion (resource ));
10998 return PostExecutionControl .defaultDispatch ();
11099 }
111100 }
112101 }
113102
114- private boolean skipBecauseOfGenerations (ExecutionScope executionScope ) {
115- if (!generationAware ) {
116- return false ;
117- }
118- if (executionScope .getEvents ().size () == 1 ) {
119- Event event = executionScope .getEvents ().get (0 );
120- if (event instanceof CustomResourceEvent ) {
121- Long actualGeneration = executionScope .getCustomResource ().getMetadata ().getGeneration ();
122- Long lastGeneration = lastGenerationProcessedSuccessfully .get (executionScope .getCustomResourceUid ());
123- if (lastGeneration == null ) {
124- return false ;
125- }
126- return actualGeneration <= lastGeneration ;
127- }
128- }
129- return false ;
130- }
131103
132104 private PostExecutionControl handleDelete (CustomResource resource , Context context ) {
133105 log .debug ("Executing delete for resource: {} with version: {}" , getUID (resource ), getVersion (resource ));
134106 DeleteControl deleteControl = controller .deleteResource (resource , context );
135107 boolean hasFinalizer = ControllerUtils .hasGivenFinalizer (resource , resourceFinalizer );
136108 if (deleteControl == DeleteControl .DEFAULT_DELETE && hasFinalizer ) {
137109 CustomResource customResource = removeFinalizer (resource );
138- cleanup (resource );
139110 return PostExecutionControl .customResourceUpdated (customResource );
140111 } else {
141112 log .debug ("Skipping finalizer remove for resource: {} with version: {}. delete control: {}, hasFinalizer: {} " ,
@@ -144,27 +115,6 @@ private PostExecutionControl handleDelete(CustomResource resource, Context conte
144115 }
145116 }
146117
147- public boolean largerGenerationThenProcessedBefore (CustomResource resource ) {
148- Long lastGeneration = lastGenerationProcessedSuccessfully .get (resource .getMetadata ().getUid ());
149- if (lastGeneration == null ) {
150- return true ;
151- } else {
152- return resource .getMetadata ().getGeneration () > lastGeneration ;
153- }
154- }
155-
156- private void cleanup (CustomResource resource ) {
157- if (generationAware ) {
158- lastGenerationProcessedSuccessfully .remove (resource .getMetadata ().getUid ());
159- }
160- }
161-
162- private void markLastGenerationProcessed (CustomResource resource ) {
163- if (generationAware ) {
164- lastGenerationProcessedSuccessfully .put (resource .getMetadata ().getUid (), resource .getMetadata ().getGeneration ());
165- }
166- }
167-
168118 private void updateCustomResourceWithFinalizer (CustomResource resource ) {
169119 log .debug ("Adding finalizer for resource: {} version: {}" , getUID (resource ),
170120 getVersion (resource ));
@@ -201,10 +151,6 @@ private void addFinalizerIfNotPresent(CustomResource resource) {
201151 }
202152 }
203153
204- private boolean markedForDeletion (CustomResource resource ) {
205- return resource .getMetadata ().getDeletionTimestamp () != null && !resource .getMetadata ().getDeletionTimestamp ().isEmpty ();
206- }
207-
208154 // created to support unit testing
209155 public static class CustomResourceFacade {
210156
0 commit comments