@@ -219,12 +219,22 @@ impl ResourceControllerImpl {
219
219
let mut tmp: Vec < ( String , String ) > = Default :: default ( ) ;
220
220
if d. applies_to_all_namespaces ( ) {
221
221
for ns in cache. iter ( ) {
222
- if let Some ( x) = d. applies_to ( event, ns. name ( ) . as_str ( ) ) {
223
- tmp. push ( x) ;
222
+ // Make sure we skip deleted namespaces, as otherwise the finalizers on the synced
223
+ // destination objects will prevent the namespace from being deleted.
224
+ if ns. metadata . deletion_timestamp . is_none ( ) {
225
+ if let Some ( x) = d. applies_to ( event, ns. name ( ) . as_str ( ) ) {
226
+ tmp. push ( x) ;
227
+ }
224
228
}
225
229
}
226
230
} else if let Some ( x) = d. applies_to ( event, d. namespace . as_str ( ) ) {
227
- tmp. push ( x) ;
231
+ if let Some ( ns) = cache. iter ( ) . find ( |ns| ns. name ( ) == d. namespace ) {
232
+ // Make sure we skip deleted namespaces, as otherwise the finalizers on the synced
233
+ // destination objects will prevent the namespace from being deleted.
234
+ if ns. metadata . deletion_timestamp . is_none ( ) {
235
+ tmp. push ( x) ;
236
+ }
237
+ }
228
238
}
229
239
tmp
230
240
} )
@@ -253,7 +263,7 @@ impl ResourceControllerImpl {
253
263
source. name( )
254
264
) ,
255
265
) ;
256
- let mut upserted = 0usize ;
266
+ let mut changed = false ;
257
267
let mut pp = PostParams :: default ( ) ;
258
268
pp. field_manager = Some ( MANAGER . to_string ( ) ) ;
259
269
@@ -285,7 +295,23 @@ impl ResourceControllerImpl {
285
295
while retry_attempts > 0 {
286
296
retry_attempts -= 1 ;
287
297
match api. get ( d. name . as_str ( ) ) . await {
288
- Ok ( current) => {
298
+ Ok ( mut current) => {
299
+ if current. metadata . deletion_timestamp . is_some ( ) {
300
+ // If a destination object has been deleted, remove the finalizer to make sure
301
+ // it gets properly removed by the API server and that we can recreate and sync
302
+ // it.
303
+ match remove_finalizer ( api. clone ( ) , & mut current, FINALIZER ) . await {
304
+ Ok ( true ) => debug ! (
305
+ "removed finalizer from deleted object {} {}/{}" ,
306
+ self . gvk. kind, d. namespace, d. name
307
+ ) ,
308
+ Err ( e) => warn ! (
309
+ "failed to remove finalizer from deleted object {} {}/{}: {}" ,
310
+ self . gvk. kind, d. namespace, d. name, e
311
+ ) ,
312
+ _ => ( ) ,
313
+ }
314
+ }
289
315
let dst_version = ObjectRevision {
290
316
uid : current. uid ( ) ,
291
317
resource_version : current. resource_version ( ) ,
@@ -302,7 +328,7 @@ impl ResourceControllerImpl {
302
328
resource_version : updated. resource_version ( ) ,
303
329
} ) ;
304
330
d. source_version = Some ( src_version. clone ( ) ) ;
305
- upserted += 1 ;
331
+ changed = true ;
306
332
observed_success += 1 ;
307
333
break ;
308
334
}
@@ -333,7 +359,7 @@ impl ResourceControllerImpl {
333
359
resource_version : created. resource_version ( ) ,
334
360
} ) ;
335
361
d. source_version = Some ( src_version. clone ( ) ) ;
336
- upserted += 1 ;
362
+ changed = true ;
337
363
observed_success += 1 ;
338
364
break ;
339
365
}
@@ -362,7 +388,7 @@ impl ResourceControllerImpl {
362
388
}
363
389
}
364
390
}
365
- Ok ( ( upserted > 0 , expected_success, observed_success) )
391
+ Ok ( ( changed , expected_success, observed_success) )
366
392
}
367
393
368
394
fn is_same_destination ( me : & DestinationStatus , other : & DestinationStatus ) -> bool {
@@ -403,7 +429,9 @@ impl ResourceControllerImpl {
403
429
expected_destinations. push ( expected_dst) ;
404
430
}
405
431
// 1. Remove stale destinations.
432
+ let stales = stale_destinations. len ( ) ;
406
433
let stale_remnants = delete_destinations ( self . client ( ) , & stale_destinations) . await ?;
434
+ let removed_count = stales - stale_remnants. len ( ) ;
407
435
// 2. Update status sub-resources with active destinations.
408
436
// Deterministically sort destinations to avoid unnecessary updates.
409
437
// Note, it is important that we update the status sub-resource before
@@ -420,7 +448,7 @@ impl ResourceControllerImpl {
420
448
let ( updated, expected, observed) = self
421
449
. upsert_destinations ( & source, destinations, & stale_remnants)
422
450
. await ?;
423
- if updated {
451
+ if updated || removed_count > 0 {
424
452
let errors = expected - observed;
425
453
Some ( Condition :: new (
426
454
IN_SYNC ,
@@ -641,7 +669,7 @@ impl ResourceControllerImpl {
641
669
} ) ;
642
670
if let Some ( ct) = & namespace. metadata . creation_timestamp {
643
671
let age = Utc :: now ( ) - ct. 0 ;
644
- if is_target_namespace && age. num_seconds ( ) > 300 {
672
+ if is_target_namespace && age. num_seconds ( ) < 300 {
645
673
// reconcile all source if namespace has been created in the last 5 minutes.
646
674
let guard = futures:: executor:: block_on ( sources. read ( ) ) ;
647
675
let tmp: Vec < ObjectRef < DynamicObject > > = guard
0 commit comments