@@ -24,7 +24,6 @@ import (
24
24
"time"
25
25
26
26
apierrors "k8s.io/apimachinery/pkg/api/errors"
27
- "k8s.io/apimachinery/pkg/api/meta"
28
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
28
"k8s.io/apimachinery/pkg/runtime"
30
29
"k8s.io/apimachinery/pkg/types"
@@ -182,65 +181,24 @@ func (r *AddressGroupBindingReconciler) reconcileNormal(ctx context.Context, bin
182
181
}
183
182
if err := r .Get (ctx , addressGroupKey , addressGroup ); err != nil {
184
183
if apierrors .IsNotFound (err ) {
185
- // Check if we already have a condition for AddressGroupNotFound with the same generation
186
- var existingCondition * metav1.Condition
187
- for i := range binding .Status .Conditions {
188
- if binding .Status .Conditions [i ].Type == netguardv1alpha1 .ConditionReady &&
189
- binding .Status .Conditions [i ].Reason == "AddressGroupNotFound" &&
190
- binding .Status .Conditions [i ].ObservedGeneration == binding .Generation {
191
- existingCondition = & binding .Status .Conditions [i ]
192
- break
193
- }
194
- }
195
-
196
- // If condition already exists with the same generation, update with detailed message and don't requeue
197
- if existingCondition != nil {
198
- logger .Info ("AddressGroup not found, not requeuing until resource changes" ,
199
- "addressGroupName" , addressGroupRef .GetName (),
200
- "addressGroupNamespace" , addressGroupNamespace )
201
-
202
- // Update the message with more detailed information
203
- meta .SetStatusCondition (& binding .Status .Conditions , metav1.Condition {
204
- Type : netguardv1alpha1 .ConditionReady ,
205
- Status : metav1 .ConditionFalse ,
206
- Reason : "AddressGroupNotFound" ,
207
- Message : fmt .Sprintf ("AddressGroup %s not found in namespace %s. This binding will not be reconciled until the AddressGroup is created or the resource is modified." ,
208
- addressGroupRef .GetName (), addressGroupNamespace ),
209
- ObservedGeneration : binding .Generation ,
210
- LastTransitionTime : existingCondition .LastTransitionTime ,
211
- })
212
-
213
- if err := UpdateStatusWithRetry (ctx , r .Client , binding , DefaultMaxRetries ); err != nil {
214
- logger .Error (err , "Failed to update AddressGroupBinding status" )
215
- return ctrl.Result {}, err
216
- }
217
-
218
- // Don't requeue
219
- return ctrl.Result {}, nil
220
- }
221
-
222
- // First time seeing this issue or generation changed, set condition and requeue once
223
- logger .Info ("AddressGroup not found, will requeue once to update status" ,
184
+ logger .Info ("AddressGroup not found, deleting AddressGroupBinding to maintain consistency" ,
224
185
"addressGroupName" , addressGroupRef .GetName (),
225
- "addressGroupNamespace" , addressGroupNamespace )
226
-
227
- meta .SetStatusCondition (& binding .Status .Conditions , metav1.Condition {
228
- Type : netguardv1alpha1 .ConditionReady ,
229
- Status : metav1 .ConditionFalse ,
230
- Reason : "AddressGroupNotFound" ,
231
- Message : fmt .Sprintf ("AddressGroup %s not found in namespace %s. This binding will be reconciled once more to update status." ,
232
- addressGroupRef .GetName (), addressGroupNamespace ),
233
- ObservedGeneration : binding .Generation ,
234
- LastTransitionTime : metav1 .Now (),
235
- })
186
+ "addressGroupNamespace" , addressGroupNamespace ,
187
+ "binding" , binding .GetName ())
236
188
237
- if err := UpdateStatusWithRetry (ctx , r .Client , binding , DefaultMaxRetries ); err != nil {
238
- logger .Error (err , "Failed to update AddressGroupBinding status" )
189
+ // Delete the binding since its referenced AddressGroup no longer exists
190
+ if err := r .Delete (ctx , binding ); err != nil {
191
+ logger .Error (err , "Failed to delete AddressGroupBinding after AddressGroup deletion" ,
192
+ "addressGroup" , addressGroupRef .GetName (),
193
+ "binding" , binding .GetName ())
239
194
return ctrl.Result {}, err
240
195
}
241
196
242
- // Requeue after a short time to update the status with the final message
243
- return ctrl.Result {RequeueAfter : time .Second * 5 }, nil
197
+ logger .Info ("Successfully initiated deletion of AddressGroupBinding" ,
198
+ "addressGroup" , addressGroupRef .GetName (),
199
+ "binding" , binding .GetName ())
200
+
201
+ return ctrl.Result {}, nil
244
202
}
245
203
logger .Error (err , "Failed to get AddressGroup" )
246
204
return ctrl.Result {}, err
@@ -343,7 +301,18 @@ func (r *AddressGroupBindingReconciler) reconcileNormal(ctx context.Context, bin
343
301
"addressGroup" , fmt .Sprintf ("%s/%s" , addressGroup .Kind , addressGroup .Name ),
344
302
"addressGroupUID" , addressGroup .UID )
345
303
346
- binding .OwnerReferences = append (binding .OwnerReferences , agOwnerRef )
304
+ // Remove existing owner references for the same AddressGroup (by Kind+Name+APIVersion)
305
+ var updatedOwnerRefs []metav1.OwnerReference
306
+ for _ , ref := range binding .GetOwnerReferences () {
307
+ if ! (ref .Kind == agOwnerRef .Kind &&
308
+ ref .Name == agOwnerRef .Name &&
309
+ ref .APIVersion == agOwnerRef .APIVersion ) {
310
+ updatedOwnerRefs = append (updatedOwnerRefs , ref )
311
+ }
312
+ }
313
+ // Add the new owner reference
314
+ updatedOwnerRefs = append (updatedOwnerRefs , agOwnerRef )
315
+ binding .OwnerReferences = updatedOwnerRefs
347
316
ownerRefsUpdated = true
348
317
}
349
318
@@ -424,18 +393,14 @@ func (r *AddressGroupBindingReconciler) reconcileNormal(ctx context.Context, bin
424
393
logger .Info ("Ports have changed, updating ServicePortsRef" ,
425
394
"service" , fmt .Sprintf ("%s/%s" , sp .GetNamespace (), sp .GetName ()))
426
395
427
- // Create a copy for patching
428
396
original := portMapping .DeepCopy ()
429
-
430
- // Update the ports
431
397
portMapping .AccessPorts .Items [i ].Ports = servicePortsRef .Ports
432
-
433
- // Apply patch with retry
434
398
patch := client .MergeFrom (original )
435
399
if err := PatchWithRetry (ctx , r .Client , portMapping , patch , DefaultMaxRetries ); err != nil {
436
400
logger .Error (err , "Failed to update AddressGroupPortMapping.AccessPorts" )
437
401
return ctrl.Result {}, err
438
402
}
403
+
439
404
logger .Info ("Successfully updated Service ports in AddressGroupPortMapping" ,
440
405
"service" , service .GetName (),
441
406
"addressGroup" , addressGroupRef .GetName ())
@@ -454,11 +419,7 @@ func (r *AddressGroupBindingReconciler) reconcileNormal(ctx context.Context, bin
454
419
455
420
// Create a copy for patching
456
421
original := portMapping .DeepCopy ()
457
-
458
- // Add the service to the list
459
422
portMapping .AccessPorts .Items = append (portMapping .AccessPorts .Items , servicePortsRef )
460
-
461
- // Apply patch with retry
462
423
patch := client .MergeFrom (original )
463
424
if err := PatchWithRetry (ctx , r .Client , portMapping , patch , DefaultMaxRetries ); err != nil {
464
425
logger .Error (err , "Failed to add Service to AddressGroupPortMapping.AccessPorts" )
@@ -490,22 +451,6 @@ func (r *AddressGroupBindingReconciler) reconcileNormal(ctx context.Context, bin
490
451
"namespace" , binding .Namespace ,
491
452
"generation" , binding .Generation ,
492
453
"resourceVersion" , binding .ResourceVersion )
493
-
494
- // TEMPORARY-DEBUG-CODE: Final state logging for problematic resources
495
- if binding .Name == "dynamic-2rx8z" || binding .Name == "dynamic-7dls7" ||
496
- binding .Name == "dynamic-fb5qw" || binding .Name == "dynamic-g6jfj" ||
497
- binding .Name == "dynamic-jd2b7" || binding .Name == "dynamic-lsjlt" {
498
-
499
- logger .Info ("TEMPORARY-DEBUG-CODE: Final state of problematic binding after successful reconciliation" ,
500
- "name" , binding .Name ,
501
- "namespace" , binding .Namespace ,
502
- "generation" , binding .Generation ,
503
- "resourceVersion" , binding .ResourceVersion ,
504
- "finalizers" , binding .Finalizers ,
505
- "ownerReferences" , formatOwnerReferences (binding .OwnerReferences ),
506
- "conditions" , formatConditions (binding .Status .Conditions ))
507
- }
508
-
509
454
return ctrl.Result {}, nil
510
455
}
511
456
@@ -518,23 +463,6 @@ func (r *AddressGroupBindingReconciler) reconcileDelete(ctx context.Context, bin
518
463
"finalizers" , binding .Finalizers ,
519
464
"conditions" , formatConditions (binding .Status .Conditions ))
520
465
521
- // TEMPORARY-DEBUG-CODE: Detailed logging for problematic resources being deleted
522
- if binding .Name == "dynamic-2rx8z" || binding .Name == "dynamic-7dls7" ||
523
- binding .Name == "dynamic-fb5qw" || binding .Name == "dynamic-g6jfj" ||
524
- binding .Name == "dynamic-jd2b7" || binding .Name == "dynamic-lsjlt" {
525
-
526
- logger .Info ("TEMPORARY-DEBUG-CODE: Detailed state of problematic binding being deleted" ,
527
- "name" , binding .Name ,
528
- "namespace" , binding .Namespace ,
529
- "generation" , binding .Generation ,
530
- "resourceVersion" , binding .ResourceVersion ,
531
- "finalizers" , binding .Finalizers ,
532
- "ownerReferences" , formatOwnerReferences (binding .OwnerReferences ),
533
- "serviceRef" , formatObjectReference (binding .Spec .ServiceRef ),
534
- "addressGroupRef" , formatNamespacedObjectReference (binding .Spec .AddressGroupRef ),
535
- "conditions" , formatConditions (binding .Status .Conditions ))
536
- }
537
-
538
466
// 1. Remove AddressGroup from Service.AddressGroups
539
467
serviceRef := binding .Spec .ServiceRef
540
468
service := & netguardv1alpha1.Service {}
@@ -683,19 +611,6 @@ func (r *AddressGroupBindingReconciler) reconcileDelete(ctx context.Context, bin
683
611
"name" , binding .GetName (),
684
612
"namespace" , binding .GetNamespace ())
685
613
686
- // TEMPORARY-DEBUG-CODE: Final state logging for problematic resources being deleted
687
- if binding .Name == "dynamic-2rx8z" || binding .Name == "dynamic-7dls7" ||
688
- binding .Name == "dynamic-fb5qw" || binding .Name == "dynamic-g6jfj" ||
689
- binding .Name == "dynamic-jd2b7" || binding .Name == "dynamic-lsjlt" {
690
-
691
- logger .Info ("TEMPORARY-DEBUG-CODE: Final state of problematic binding after successful deletion" ,
692
- "name" , binding .Name ,
693
- "namespace" , binding .Namespace ,
694
- "generation" , binding .Generation ,
695
- "resourceVersion" , binding .ResourceVersion ,
696
- "finalizers" , binding .Finalizers )
697
- }
698
-
699
614
return ctrl.Result {}, nil
700
615
}
701
616
@@ -721,20 +636,34 @@ func setCondition(binding *netguardv1alpha1.AddressGroupBinding, conditionType s
721
636
}
722
637
}
723
638
724
- // Condition not found, append it
725
639
binding .Status .Conditions = append (binding .Status .Conditions , condition )
726
640
}
727
641
728
642
// containsOwnerReference checks if the list of owner references contains a reference with the same UID
729
643
func containsOwnerReference (refs []metav1.OwnerReference , ref metav1.OwnerReference ) bool {
730
644
for _ , r := range refs {
731
645
if r .UID == ref .UID {
646
+ // Если BlockOwnerDeletion отличается, считаем что нужно обновить
647
+ if ! equalBoolPtr (r .BlockOwnerDeletion , ref .BlockOwnerDeletion ) {
648
+ return false
649
+ }
732
650
return true
733
651
}
734
652
}
735
653
return false
736
654
}
737
655
656
+ // equalBoolPtr сравнивает два указателя на bool
657
+ func equalBoolPtr (a , b * bool ) bool {
658
+ if a == nil && b == nil {
659
+ return true
660
+ }
661
+ if a == nil || b == nil {
662
+ return false
663
+ }
664
+ return * a == * b
665
+ }
666
+
738
667
// formatConditions formats a slice of conditions into a readable string
739
668
func formatConditions (conditions []metav1.Condition ) string {
740
669
var result []string
@@ -832,19 +761,34 @@ func (r *AddressGroupBindingReconciler) findBindingsForAddressGroup(ctx context.
832
761
return nil
833
762
}
834
763
764
+ logger := log .FromContext (ctx )
765
+ logger .Info ("Finding bindings for AddressGroup" ,
766
+ "addressGroup" , addressGroup .GetName (),
767
+ "namespace" , addressGroup .GetNamespace ())
768
+
835
769
// Get all AddressGroupBinding
836
770
bindingList := & netguardv1alpha1.AddressGroupBindingList {}
837
771
if err := r .List (ctx , bindingList ); err != nil {
772
+ logger .Error (err , "Failed to list AddressGroupBindings" )
838
773
return nil
839
774
}
840
775
841
776
var requests []reconcile.Request
842
777
843
778
// Filter bindings that reference this address group
844
779
for _ , binding := range bindingList .Items {
780
+ // Resolve the namespace for the AddressGroupRef
781
+ resolvedNamespace := v1alpha1 .ResolveNamespace (binding .Spec .AddressGroupRef .GetNamespace (), binding .GetNamespace ())
782
+
845
783
if binding .Spec .AddressGroupRef .GetName () == addressGroup .GetName () &&
846
- (binding .Spec .AddressGroupRef .GetNamespace () == addressGroup .GetNamespace () ||
847
- binding .Spec .AddressGroupRef .GetNamespace () == "" ) {
784
+ resolvedNamespace == addressGroup .GetNamespace () {
785
+
786
+ logger .Info ("Found binding that references this AddressGroup" ,
787
+ "binding" , binding .GetName (),
788
+ "bindingNamespace" , binding .GetNamespace (),
789
+ "addressGroupRef" , binding .Spec .AddressGroupRef .GetName (),
790
+ "resolvedNamespace" , resolvedNamespace )
791
+
848
792
requests = append (requests , reconcile.Request {
849
793
NamespacedName : types.NamespacedName {
850
794
Name : binding .GetName (),
@@ -854,6 +798,10 @@ func (r *AddressGroupBindingReconciler) findBindingsForAddressGroup(ctx context.
854
798
}
855
799
}
856
800
801
+ logger .Info ("Found bindings for AddressGroup" ,
802
+ "addressGroup" , addressGroup .GetName (),
803
+ "bindingsCount" , len (requests ))
804
+
857
805
return requests
858
806
}
859
807
0 commit comments