1
1
package io .javaoperatorsdk .operator .processing .dependent .kubernetes ;
2
2
3
- import java .util .HashMap ;
3
+ import java .util .Map ;
4
4
import java .util .Optional ;
5
5
import java .util .Set ;
6
6
7
7
import org .slf4j .Logger ;
8
8
import org .slf4j .LoggerFactory ;
9
9
10
10
import io .fabric8 .kubernetes .api .model .HasMetadata ;
11
- import io .fabric8 .kubernetes .api .model .Namespaced ;
12
11
import io .fabric8 .kubernetes .client .KubernetesClient ;
13
12
import io .fabric8 .kubernetes .client .dsl .Resource ;
14
13
import io .javaoperatorsdk .operator .OperatorException ;
@@ -103,29 +102,6 @@ public void configureWith(InformerEventSource<R, P> informerEventSource) {
103
102
setEventSource (informerEventSource );
104
103
}
105
104
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
-
129
105
@ SuppressWarnings ("unused" )
130
106
public R create (R target , P primary , Context <P > context ) {
131
107
if (useSSA (context )) {
@@ -137,6 +113,7 @@ public R create(R target, P primary, Context<P> context) {
137
113
target .getMetadata ().setResourceVersion ("1" );
138
114
}
139
115
}
116
+ addMetadata (false , null , target , primary );
140
117
final var resource = prepare (target , primary , "Creating" );
141
118
return useSSA (context )
142
119
? resource
@@ -152,6 +129,7 @@ public R update(R actual, R target, P primary, Context<P> context) {
152
129
actual .getMetadata ().getResourceVersion ());
153
130
}
154
131
R updatedResource ;
132
+ addMetadata (false , actual , target , primary );
155
133
if (useSSA (context )) {
156
134
updatedResource = prepare (target , primary , "Updating" )
157
135
.fieldManager (context .getControllerConfiguration ().fieldManager ())
@@ -165,38 +143,58 @@ public R update(R actual, R target, P primary, Context<P> context) {
165
143
return updatedResource ;
166
144
}
167
145
146
+ @ Override
168
147
public Result <R > match (R actualResource , P primary , Context <P > context ) {
169
148
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 ) {
170
162
final boolean matches ;
163
+ addMetadata (true , actualResource , desired , primary );
171
164
if (useSSA (context )) {
172
- addReferenceHandlingMetadata (desired , primary );
173
165
matches = SSABasedGenericKubernetesResourceMatcher .getInstance ()
174
166
.matches (actualResource , desired , context );
175
167
} else {
176
- matches = updaterMatcher .matches (actualResource , desired , context );
168
+ matches = matcher .matches (actualResource , desired , context );
177
169
}
178
170
return Result .computed (matches , desired );
179
171
}
180
172
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 );
192
188
}
189
+ addReferenceHandlingMetadata (target , primary );
193
190
}
194
191
195
192
private boolean useSSA (Context <P > context ) {
196
193
return context .getControllerConfiguration ().getConfigurationService ()
197
194
.ssaBasedCreateUpdateMatchForDependentResources ();
198
195
}
199
196
197
+ @ Override
200
198
protected void handleDelete (P primary , R secondary , Context <P > context ) {
201
199
if (secondary != null ) {
202
200
client .resource (secondary ).delete ();
@@ -214,13 +212,7 @@ protected Resource<R> prepare(R desired, P primary, String actionName) {
214
212
desired .getClass (),
215
213
ResourceID .fromResource (desired ));
216
214
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 );
224
216
}
225
217
226
218
protected void addReferenceHandlingMetadata (R desired , P primary ) {
@@ -254,7 +246,7 @@ protected InformerEventSource<R, P> createEventSource(EventSourceContext<P> cont
254
246
"Using default configuration for {} KubernetesDependentResource, call configureWith to provide configuration" ,
255
247
resourceType ().getSimpleName ());
256
248
}
257
- return ( InformerEventSource < R , P >) eventSource ().orElseThrow ();
249
+ return eventSource ().orElseThrow ();
258
250
}
259
251
260
252
private boolean useDefaultAnnotationsToIdentifyPrimary () {
@@ -263,10 +255,6 @@ private boolean useDefaultAnnotationsToIdentifyPrimary() {
263
255
264
256
private void addDefaultSecondaryToPrimaryMapperAnnotations (R desired , P primary ) {
265
257
var annotations = desired .getMetadata ().getAnnotations ();
266
- if (annotations == null ) {
267
- annotations = new HashMap <>();
268
- desired .getMetadata ().setAnnotations (annotations );
269
- }
270
258
annotations .put (Mappers .DEFAULT_ANNOTATION_FOR_NAME , primary .getMetadata ().getName ());
271
259
var primaryNamespaces = primary .getMetadata ().getNamespace ();
272
260
if (primaryNamespaces != null ) {
@@ -294,16 +282,6 @@ protected R desired(P primary, Context<P> context) {
294
282
return super .desired (primary , context );
295
283
}
296
284
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
-
307
285
@ Override
308
286
public Optional <KubernetesDependentResourceConfig <R >> configuration () {
309
287
return Optional .ofNullable (kubernetesDependentResourceConfig );
0 commit comments