@@ -29,17 +29,14 @@ public class EventDispatcher {
29
29
private final ResourceController controller ;
30
30
private final String resourceFinalizer ;
31
31
private final CustomResourceFacade customResourceFacade ;
32
- private final boolean generationAware ;
33
- private final Map <String , Long > lastGenerationProcessedSuccessfully = new ConcurrentHashMap <>();
34
32
private EventSourceManager eventSourceManager ;
35
33
36
34
public EventDispatcher (ResourceController controller ,
37
35
String finalizer ,
38
- CustomResourceFacade customResourceFacade , boolean generationAware ) {
36
+ CustomResourceFacade customResourceFacade ) {
39
37
this .controller = controller ;
40
38
this .customResourceFacade = customResourceFacade ;
41
39
this .resourceFinalizer = finalizer ;
42
- this .generationAware = generationAware ;
43
40
}
44
41
45
42
public void setEventSourceManager (EventSourceManager eventSourceManager ) {
@@ -60,7 +57,6 @@ private PostExecutionControl handDispatch(ExecutionScope executionScope) {
60
57
log .debug ("Handling events: {} for resource {}" , executionScope .getEvents (), resource .getMetadata ());
61
58
62
59
if (containsCustomResourceDeletedEvent (executionScope .getEvents ())) {
63
- cleanup (executionScope .getCustomResource ());
64
60
log .debug ("Skipping dispatch processing because of a Delete event: {} with version: {}" ,
65
61
getUID (resource ), getVersion (resource ));
66
62
return PostExecutionControl .defaultDispatch ();
@@ -87,55 +83,30 @@ private PostExecutionControl handleCreateOrUpdate(ExecutionScope executionScope,
87
83
updateCustomResourceWithFinalizer (resource );
88
84
return PostExecutionControl .onlyFinalizerAdded ();
89
85
} 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 );
106
97
} else {
107
- log .debug ("Skipping event processing because generations: {} with version: {}" ,
108
- getUID (resource ), getVersion (resource ));
109
98
return PostExecutionControl .defaultDispatch ();
110
99
}
111
100
}
112
101
}
113
102
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
- }
131
103
132
104
private PostExecutionControl handleDelete (CustomResource resource , Context context ) {
133
105
log .debug ("Executing delete for resource: {} with version: {}" , getUID (resource ), getVersion (resource ));
134
106
DeleteControl deleteControl = controller .deleteResource (resource , context );
135
107
boolean hasFinalizer = ControllerUtils .hasGivenFinalizer (resource , resourceFinalizer );
136
108
if (deleteControl == DeleteControl .DEFAULT_DELETE && hasFinalizer ) {
137
109
CustomResource customResource = removeFinalizer (resource );
138
- cleanup (resource );
139
110
return PostExecutionControl .customResourceUpdated (customResource );
140
111
} else {
141
112
log .debug ("Skipping finalizer remove for resource: {} with version: {}. delete control: {}, hasFinalizer: {} " ,
@@ -144,27 +115,6 @@ private PostExecutionControl handleDelete(CustomResource resource, Context conte
144
115
}
145
116
}
146
117
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
-
168
118
private void updateCustomResourceWithFinalizer (CustomResource resource ) {
169
119
log .debug ("Adding finalizer for resource: {} version: {}" , getUID (resource ),
170
120
getVersion (resource ));
@@ -201,10 +151,6 @@ private void addFinalizerIfNotPresent(CustomResource resource) {
201
151
}
202
152
}
203
153
204
- private boolean markedForDeletion (CustomResource resource ) {
205
- return resource .getMetadata ().getDeletionTimestamp () != null && !resource .getMetadata ().getDeletionTimestamp ().isEmpty ();
206
- }
207
-
208
154
// created to support unit testing
209
155
public static class CustomResourceFacade {
210
156
0 commit comments