Skip to content

Commit 2934159

Browse files
committed
Addressing comments
Signed-off-by: Dyanngg <[email protected]>
1 parent 25eb037 commit 2934159

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

pkg/apiserver/apiserver.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,6 @@ func installHandlers(c *ExtraConfig, s *genericapiserver.GenericAPIServer) {
310310

311311
// Get new NetworkPolicyValidator
312312
v := controllernetworkpolicy.NewNetworkPolicyValidator(c.networkPolicyController)
313-
// Set up Tier event handlers to notify validator when Tiers are actually created/deleted
314-
c.networkPolicyController.SetupTierEventHandlersForValidator(v)
315313
// Install handlers for NetworkPolicy related validation
316314
s.Handler.NonGoRestfulMux.HandleFunc("/validate/tier", webhook.HandlerForValidateFunc(v.Validate))
317315
s.Handler.NonGoRestfulMux.HandleFunc("/validate/acnp", webhook.HandlerForValidateFunc(v.Validate))
@@ -331,6 +329,12 @@ func installHandlers(c *ExtraConfig, s *genericapiserver.GenericAPIServer) {
331329
}()
332330
return nil
333331
})
332+
333+
// Start the NetworkPolicyValidator background routines
334+
s.AddPostStartHook("start-validator-routines", func(context genericapiserver.PostStartHookContext) error {
335+
go v.Run(context.Done())
336+
return nil
337+
})
334338
}
335339

336340
if features.DefaultFeatureGate.Enabled(features.Egress) || features.DefaultFeatureGate.Enabled(features.ServiceExternalIP) {

pkg/controller/networkpolicy/validate.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type priorityReservation struct {
104104
func newTierPriorityTracker() *tierPriorityTracker {
105105
return &tierPriorityTracker{
106106
pendingPriorities: make(map[int32]*priorityReservation),
107-
validationTimeout: 30 * time.Second, // timeout for waiting for other validations
107+
validationTimeout: 5 * time.Second, // timeout for waiting for other validations
108108
creationTimeout: 60 * time.Second, // timeout for waiting for actual K8s creation
109109
}
110110
}
@@ -120,7 +120,7 @@ func (t *tierPriorityTracker) reservePriorityForValidation(priority int32, tierN
120120
if existing, exists := t.pendingPriorities[priority]; exists {
121121
// Check if the existing reservation has timed out
122122
if time.Since(existing.createdAt) > t.creationTimeout {
123-
klog.Warningf("Tier priority %d reservation for %s has timed out, allowing new reservation", priority, existing.tierName)
123+
klog.V(2).InfoS("Tier priority reservation for has timed out, allowing new reservation", "priority", priority, "tier", existing.tierName)
124124
close(existing.waitChan)
125125
delete(t.pendingPriorities, priority)
126126
} else {
@@ -281,12 +281,22 @@ func NewNetworkPolicyValidator(networkPolicyController *NetworkPolicyController)
281281
vr.RegisterGroupValidator(&gv)
282282
vr.RegisterAdminNetworkPolicyValidator(&av)
283283

284-
// Start cleanup routine for expired reservations from the tierValidator
285-
go tv.startCleanupRoutine()
286-
284+
// Set up Tier event handlers to notify validator when Tiers are actually created/deleted
285+
networkPolicyController.SetupTierEventHandlersForValidator(&vr)
287286
return &vr
288287
}
289288

289+
// Run starts the background routines for the NetworkPolicyValidator.
290+
func (v *NetworkPolicyValidator) Run(stopCh <-chan struct{}) {
291+
// Start cleanup routine for expired reservations from the tierValidator
292+
for _, val := range v.tierValidators {
293+
if tv, ok := val.(*tierValidator); ok {
294+
go tv.startCleanupRoutine(stopCh)
295+
break
296+
}
297+
}
298+
}
299+
290300
// Validate function validates a Group, ClusterGroup, Tier or Antrea Policy object
291301
func (v *NetworkPolicyValidator) Validate(ar *admv1.AdmissionReview) *admv1.AdmissionResponse {
292302
var result *metav1.Status
@@ -627,7 +637,7 @@ func (v *NetworkPolicyValidator) validateTier(curTier, oldTier *crdv1beta1.Tier,
627637
}
628638

629639
// startCleanupRoutine starts a background routine to clean up expired priority reservations
630-
func (t *tierValidator) startCleanupRoutine() {
640+
func (t *tierValidator) startCleanupRoutine(stopCh <-chan struct{}) {
631641
if t.priorityTracker == nil {
632642
klog.ErrorS(nil, "Priority tracker is nil, cannot start cleanup routine")
633643
return
@@ -636,8 +646,14 @@ func (t *tierValidator) startCleanupRoutine() {
636646
ticker := time.NewTicker(30 * time.Second) // Check every 30 seconds
637647
defer ticker.Stop()
638648

639-
for range ticker.C {
640-
t.priorityTracker.cleanupExpiredReservations()
649+
for {
650+
select {
651+
case <-ticker.C:
652+
t.priorityTracker.cleanupExpiredReservations()
653+
case <-stopCh:
654+
klog.InfoS("Stopping Tier priority tracker cleanup routine")
655+
return
656+
}
641657
}
642658
}
643659

0 commit comments

Comments
 (0)