@@ -594,6 +594,8 @@ func (r *Reconciler) Apply(ctx context.Context, inventory *[]*InventoryItem, obj
594594 item .Phase = PhaseCompleted
595595 item .Status = ""
596596 } else {
597+ // TODO: should we (similar to the delete cases) check deletion timestamp and ownership to void deadlocks if object
598+ // was recreated by someone else
597599 numToBeCompleted ++
598600 }
599601 }
@@ -793,11 +795,20 @@ func (r *Reconciler) Apply(ctx context.Context, inventory *[]*InventoryItem, obj
793795 numToBeDeleted ++
794796 }
795797 case PhaseDeleting :
796- // if object is gone, we can remove it from inventory
797798 if existingObject == nil {
799+ // if object is gone, we can remove it from inventory
798800 item .Phase = ""
799- } else {
801+ } else if ! existingObject .GetDeletionTimestamp ().IsZero () {
802+ // object is still there and deleting, waiting until it goes away
800803 numToBeDeleted ++
804+ } else if existingObject .GetLabels ()[r .labelKeyOwnerId ] != hashedOwnerId {
805+ // object is there but not deleting; if we are not owning it that means that somebody else has
806+ // recreated it in the meantime; so we consider this as not our problem and remove it from inventory
807+ log .V (1 ).Info ("orphaning resurrected object (probably it was recreated by someone else)" , "key" , types .ObjectKeyToString (item ))
808+ item .Phase = ""
809+ } else {
810+ // object is there, not deleting, but we own it; that is really strange and should actually not happen
811+ return false , fmt .Errorf ("object %s was already deleted but has no deletion timestamp" , types .ObjectKeyToString (item ))
801812 }
802813 default :
803814 // note: any other phase value would indicate a severe code problem, so we want to see the panic in that case
@@ -872,11 +883,20 @@ func (r *Reconciler) Delete(ctx context.Context, inventory *[]*InventoryItem, ow
872883
873884 switch item .Phase {
874885 case PhaseDeleting :
875- // if object is gone, we can remove it from inventory
876886 if existingObject == nil {
887+ // if object is gone, we can remove it from inventory
877888 item .Phase = ""
878- } else {
889+ } else if ! existingObject .GetDeletionTimestamp ().IsZero () {
890+ // object is still there and deleting, waiting until it goes away
879891 numToBeDeleted ++
892+ } else if existingObject .GetLabels ()[r .labelKeyOwnerId ] != hashedOwnerId {
893+ // object is there but not deleting; if we are not owning it that means that somebody else has
894+ // recreated it in the meantime; so we consider this as not our problem and remove it from inventory
895+ log .V (1 ).Info ("orphaning resurrected object (probably it was recreated by someone else)" , "key" , types .ObjectKeyToString (item ))
896+ item .Phase = ""
897+ } else {
898+ // object is there, not deleting, but we own it; that is really strange and should actually not happen
899+ return false , fmt .Errorf ("object %s was already deleted but has no deletion timestamp" , types .ObjectKeyToString (item ))
880900 }
881901 default :
882902 // delete namespaces after all contained inventory items
0 commit comments