11package io .javaoperatorsdk .operator .processing .dependent .kubernetes ;
22
3- import java .util .HashMap ;
3+ import java .util .Map ;
44import java .util .Optional ;
55import java .util .Set ;
66
77import org .slf4j .Logger ;
88import org .slf4j .LoggerFactory ;
99
1010import io .fabric8 .kubernetes .api .model .HasMetadata ;
11- import io .fabric8 .kubernetes .api .model .Namespaced ;
1211import io .fabric8 .kubernetes .client .KubernetesClient ;
1312import io .fabric8 .kubernetes .client .dsl .Resource ;
1413import io .javaoperatorsdk .operator .OperatorException ;
@@ -103,29 +102,6 @@ public void configureWith(InformerEventSource<R, P> informerEventSource) {
103102 setEventSource (informerEventSource );
104103 }
105104
106-
107- protected R handleCreate (R desired , P primary , Context <P > context ) {
108- ResourceID resourceID = ResourceID .fromResource (desired );
109- try {
110- prepareEventFiltering (desired , resourceID );
111- return super .handleCreate (desired , primary , context );
112- } catch (RuntimeException e ) {
113- cleanupAfterEventFiltering (resourceID );
114- throw e ;
115- }
116- }
117-
118- protected R handleUpdate (R actual , R desired , P primary , Context <P > context ) {
119- ResourceID resourceID = ResourceID .fromResource (desired );
120- try {
121- prepareEventFiltering (desired , resourceID );
122- return super .handleUpdate (actual , desired , primary , context );
123- } catch (RuntimeException e ) {
124- cleanupAfterEventFiltering (resourceID );
125- throw e ;
126- }
127- }
128-
129105 @ SuppressWarnings ("unused" )
130106 public R create (R target , P primary , Context <P > context ) {
131107 if (useSSA (context )) {
@@ -137,6 +113,7 @@ public R create(R target, P primary, Context<P> context) {
137113 target .getMetadata ().setResourceVersion ("1" );
138114 }
139115 }
116+ addMetadata (false , null , target , primary );
140117 final var resource = prepare (target , primary , "Creating" );
141118 return useSSA (context )
142119 ? resource
@@ -152,6 +129,7 @@ public R update(R actual, R target, P primary, Context<P> context) {
152129 actual .getMetadata ().getResourceVersion ());
153130 }
154131 R updatedResource ;
132+ addMetadata (false , actual , target , primary );
155133 if (useSSA (context )) {
156134 updatedResource = prepare (target , primary , "Updating" )
157135 .fieldManager (context .getControllerConfiguration ().fieldManager ())
@@ -165,38 +143,58 @@ public R update(R actual, R target, P primary, Context<P> context) {
165143 return updatedResource ;
166144 }
167145
146+ @ Override
168147 public Result <R > match (R actualResource , P primary , Context <P > context ) {
169148 final var desired = desired (primary , context );
149+ return match (actualResource , desired , primary , updaterMatcher , context );
150+ }
151+
152+ @ SuppressWarnings ({"unused" , "unchecked" })
153+ public Result <R > match (R actualResource , R desired , P primary , Context <P > context ) {
154+ return match (actualResource , desired , primary ,
155+ (ResourceUpdaterMatcher <R >) GenericResourceUpdaterMatcher
156+ .updaterMatcherFor (actualResource .getClass ()),
157+ context );
158+ }
159+
160+ public Result <R > match (R actualResource , R desired , P primary , ResourceUpdaterMatcher <R > matcher ,
161+ Context <P > context ) {
170162 final boolean matches ;
163+ addMetadata (true , actualResource , desired , primary );
171164 if (useSSA (context )) {
172- addReferenceHandlingMetadata (desired , primary );
173165 matches = SSABasedGenericKubernetesResourceMatcher .getInstance ()
174166 .matches (actualResource , desired , context );
175167 } else {
176- matches = updaterMatcher .matches (actualResource , desired , context );
168+ matches = matcher .matches (actualResource , desired , context );
177169 }
178170 return Result .computed (matches , desired );
179171 }
180172
181- @ SuppressWarnings ("unused" )
182- public Result <R > match (R actualResource , R desired , P primary , Context <P > context ) {
183- if (useSSA (context )) {
184- addReferenceHandlingMetadata (desired , primary );
185- var matches = SSABasedGenericKubernetesResourceMatcher .getInstance ()
186- .matches (actualResource , desired , context );
187- return Result .computed (matches , desired );
188- } else {
189- return GenericKubernetesResourceMatcher
190- .match (desired , actualResource , true ,
191- false , false , context );
173+ protected void addMetadata (boolean forMatch , R actualResource , final R target , P primary ) {
174+ if (forMatch ) { // keep the current
175+ String actual = actualResource .getMetadata ().getAnnotations ()
176+ .get (InformerEventSource .PREVIOUS_ANNOTATION_KEY );
177+ Map <String , String > annotations = target .getMetadata ().getAnnotations ();
178+ if (actual != null ) {
179+ annotations .put (InformerEventSource .PREVIOUS_ANNOTATION_KEY , actual );
180+ } else {
181+ annotations .remove (InformerEventSource .PREVIOUS_ANNOTATION_KEY );
182+ }
183+ } else { // set a new one
184+ eventSource ().orElseThrow ().addPreviousAnnotation (
185+ Optional .ofNullable (actualResource ).map (r -> r .getMetadata ().getResourceVersion ())
186+ .orElse (null ),
187+ target );
192188 }
189+ addReferenceHandlingMetadata (target , primary );
193190 }
194191
195192 private boolean useSSA (Context <P > context ) {
196193 return context .getControllerConfiguration ().getConfigurationService ()
197194 .ssaBasedCreateUpdateMatchForDependentResources ();
198195 }
199196
197+ @ Override
200198 protected void handleDelete (P primary , R secondary , Context <P > context ) {
201199 if (secondary != null ) {
202200 client .resource (secondary ).delete ();
@@ -214,13 +212,7 @@ protected Resource<R> prepare(R desired, P primary, String actionName) {
214212 desired .getClass (),
215213 ResourceID .fromResource (desired ));
216214
217- addReferenceHandlingMetadata (desired , primary );
218-
219- if (desired instanceof Namespaced ) {
220- return client .resource (desired ).inNamespace (desired .getMetadata ().getNamespace ());
221- } else {
222- return client .resource (desired );
223- }
215+ return client .resource (desired );
224216 }
225217
226218 protected void addReferenceHandlingMetadata (R desired , P primary ) {
@@ -254,7 +246,7 @@ protected InformerEventSource<R, P> createEventSource(EventSourceContext<P> cont
254246 "Using default configuration for {} KubernetesDependentResource, call configureWith to provide configuration" ,
255247 resourceType ().getSimpleName ());
256248 }
257- return ( InformerEventSource < R , P >) eventSource ().orElseThrow ();
249+ return eventSource ().orElseThrow ();
258250 }
259251
260252 private boolean useDefaultAnnotationsToIdentifyPrimary () {
@@ -263,10 +255,6 @@ private boolean useDefaultAnnotationsToIdentifyPrimary() {
263255
264256 private void addDefaultSecondaryToPrimaryMapperAnnotations (R desired , P primary ) {
265257 var annotations = desired .getMetadata ().getAnnotations ();
266- if (annotations == null ) {
267- annotations = new HashMap <>();
268- desired .getMetadata ().setAnnotations (annotations );
269- }
270258 annotations .put (Mappers .DEFAULT_ANNOTATION_FOR_NAME , primary .getMetadata ().getName ());
271259 var primaryNamespaces = primary .getMetadata ().getNamespace ();
272260 if (primaryNamespaces != null ) {
@@ -294,16 +282,6 @@ protected R desired(P primary, Context<P> context) {
294282 return super .desired (primary , context );
295283 }
296284
297- private void prepareEventFiltering (R desired , ResourceID resourceID ) {
298- ((InformerEventSource <R , P >) eventSource ().orElseThrow ())
299- .prepareForCreateOrUpdateEventFiltering (resourceID , desired );
300- }
301-
302- private void cleanupAfterEventFiltering (ResourceID resourceID ) {
303- ((InformerEventSource <R , P >) eventSource ().orElseThrow ())
304- .cleanupOnCreateOrUpdateEventFiltering (resourceID );
305- }
306-
307285 @ Override
308286 public Optional <KubernetesDependentResourceConfig <R >> configuration () {
309287 return Optional .ofNullable (kubernetesDependentResourceConfig );
0 commit comments