@@ -18,10 +18,11 @@ import (
18
18
const (
19
19
reconcileDuration = time .Duration (5 * time .Minute )
20
20
21
- contextBackground = "BACKGROUND"
22
- contextApplyDP = "APPLY-DP"
23
- contextAddNetPol = "ADD-NETPOL"
24
- contextDelNetPol = "DEL-NETPOL"
21
+ contextBackground = "BACKGROUND"
22
+ contextApplyDP = "APPLY-DP"
23
+ contextAddNetPol = "ADD-NETPOL"
24
+ contextAddNetPolBootup = "BOOTUP-ADD-NETPOL"
25
+ contextDelNetPol = "DEL-NETPOL"
25
26
)
26
27
27
28
var ErrInvalidApplyConfig = errors .New ("invalid apply config" )
@@ -276,24 +277,21 @@ func (dp *DataPlane) RemoveFromList(listName *ipsets.IPSetMetadata, setNames []*
276
277
// and accordingly makes changes in dataplane. This function helps emulate a single call to
277
278
// dataplane instead of multiple ipset operations calls ipset operations calls to dataplane
278
279
func (dp * DataPlane ) ApplyDataPlane () error {
279
- if dp .applyInBackground {
280
- return dp .incrementBatchAndApplyIfNeeded (contextApplyDP )
280
+ if ! dp .applyInBackground {
281
+ return dp .applyDataPlaneNow (contextApplyDP )
281
282
}
282
283
283
- return dp .applyDataPlaneNow (contextApplyDP )
284
- }
285
-
286
- func (dp * DataPlane ) incrementBatchAndApplyIfNeeded (context string ) error {
284
+ // increment batch and apply dataplane if needed
287
285
dp .applyInfo .Lock ()
288
286
dp .applyInfo .numBatches ++
289
287
newCount := dp .applyInfo .numBatches
290
288
dp .applyInfo .Unlock ()
291
289
292
- klog .Infof ("[DataPlane] [%s] new batch count: %d" , context , newCount )
290
+ klog .Infof ("[DataPlane] [%s] new batch count: %d" , contextApplyDP , newCount )
293
291
294
292
if newCount >= dp .ApplyMaxBatches {
295
- klog .Infof ("[DataPlane] [%s] applying now since reached maximum batch count: %d" , context , newCount )
296
- return dp .applyDataPlaneNow (context )
293
+ klog .Infof ("[DataPlane] [%s] applying now since reached maximum batch count: %d" , contextApplyDP , newCount )
294
+ return dp .applyDataPlaneNow (contextApplyDP )
297
295
}
298
296
299
297
return nil
@@ -380,32 +378,65 @@ func (dp *DataPlane) AddPolicy(policy *policies.NPMNetworkPolicy) error {
380
378
return fmt .Errorf ("[DataPlane] error while adding Rule IPSet references: %w" , err )
381
379
}
382
380
383
- var endpointList map [string ]string
384
- if dp .inBootupPhase () {
381
+ inBootupPhase := false
382
+ if dp .applyInBackground {
383
+ dp .applyInfo .Lock ()
384
+ inBootupPhase = dp .applyInfo .inBootupPhase
385
+ if inBootupPhase {
386
+ // keep holding the lock to block FinishBootupPhase() and prevent PodController from
387
+ // coming back online and causing race issues from updatePod() within applyDataPlaneNow()
388
+ defer dp .applyInfo .Unlock ()
389
+ } else {
390
+ dp .applyInfo .Unlock ()
391
+ }
392
+ }
393
+
394
+ if inBootupPhase {
385
395
// During bootup phase, the Pod controller will not be running.
386
396
// We don't need to worry about adding Policies to Endpoints, so we don't need IPSets in the kernel yet.
387
397
// Ideally, we get all NetworkPolicies in the cache before the Pod controller starts
388
- err = dp .incrementBatchAndApplyIfNeeded (contextAddNetPol )
389
- if err != nil {
390
- return err
391
- }
392
- } else {
393
- err = dp .applyDataPlaneNow (contextAddNetPol )
394
- if err != nil {
395
- return err
398
+
399
+ // increment batch and apply IPSets if needed
400
+ dp .applyInfo .numBatches ++
401
+ newCount := dp .applyInfo .numBatches
402
+ klog .Infof ("[DataPlane] [%s] new batch count: %d" , contextAddNetPolBootup , newCount )
403
+ if newCount >= dp .ApplyMaxBatches {
404
+ klog .Infof ("[DataPlane] [%s] applying now since reached maximum batch count: %d" , contextAddNetPolBootup , newCount )
405
+ klog .Infof ("[DataPlane] [%s] starting to apply ipsets" , contextAddNetPolBootup )
406
+ err = dp .ipsetMgr .ApplyIPSets ()
407
+ if err != nil {
408
+ return fmt .Errorf ("[DataPlane] [%s] error while applying IPSets: %w" , contextAddNetPolBootup , err )
409
+ }
410
+ klog .Infof ("[DataPlane] [%s] finished applying ipsets" , contextAddNetPolBootup )
411
+
412
+ dp .applyInfo .numBatches = 0
396
413
}
397
414
398
- endpointList , err = dp .getEndpointsToApplyPolicy (policy )
415
+ err = dp .policyMgr . AddPolicy (policy , nil )
399
416
if err != nil {
400
- return fmt .Errorf ("[DataPlane] error while getting endpoints to apply policy after applying dataplane : %w" , err )
417
+ return fmt .Errorf ("[DataPlane] [%s] error while adding policy: %w" , contextAddNetPolBootup , err )
401
418
}
419
+
420
+ return nil
421
+ }
422
+
423
+ // standard, non-bootup phase
424
+ err = dp .applyDataPlaneNow (contextAddNetPol )
425
+ if err != nil {
426
+ return err
427
+ }
428
+
429
+ var endpointList map [string ]string
430
+ endpointList , err = dp .getEndpointsToApplyPolicy (policy )
431
+ if err != nil {
432
+ return fmt .Errorf ("[DataPlane] error while getting endpoints to apply policy after applying dataplane: %w" , err )
402
433
}
403
434
404
- // endpointList will be empty if in bootup phase
405
435
err = dp .policyMgr .AddPolicy (policy , endpointList )
406
436
if err != nil {
407
437
return fmt .Errorf ("[DataPlane] error while adding policy: %w" , err )
408
438
}
439
+
409
440
return nil
410
441
}
411
442
@@ -582,14 +613,3 @@ func (dp *DataPlane) deleteIPSetsAndReferences(sets []*ipsets.TranslatedIPSet, n
582
613
}
583
614
return nil
584
615
}
585
-
586
- func (dp * DataPlane ) inBootupPhase () bool {
587
- if ! dp .applyInBackground {
588
- return false
589
- }
590
-
591
- dp .applyInfo .Lock ()
592
- defer dp .applyInfo .Unlock ()
593
-
594
- return dp .applyInfo .inBootupPhase
595
- }
0 commit comments