Skip to content

Commit f792af5

Browse files
pperiyasamyjcaamano
authored andcommitted
Use namespace reconcilation loop for syncing network policies
This commit makes network reconcilation loop to sync only namespace object and network policies sync to happen from namespace reconcilation loop. Signed-off-by: Periyasamy Palanisamy <[email protected]>
1 parent 96db6fd commit f792af5

File tree

4 files changed

+37
-39
lines changed

4 files changed

+37
-39
lines changed

go-controller/pkg/ovn/base_network_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ func (oc *BaseNetworkController) doReconcile(reconcileRoutes, reconcilePendingPo
260260
}
261261
}
262262

263+
// reconciles namespaces that were added to the network, this will trigger namespace add event and
264+
// network controller creates the address set for the namespace.
265+
// To update gress policy ACLs with peer namespace address set, invoke requeuePeerNamespace method after
266+
// address set is created for the namespace.
263267
namespaceAdded := false
264268
for _, ns := range reconcileNamespaces {
265269
namespace, err := oc.watchFactory.GetNamespace(ns)
@@ -277,11 +281,6 @@ func (oc *BaseNetworkController) doReconcile(reconcileRoutes, reconcilePendingPo
277281
if namespaceAdded {
278282
oc.retryNamespaces.RequestRetryObjs()
279283
}
280-
281-
err := oc.requeuePeerNamespaces(reconcileNamespaces)
282-
if err != nil {
283-
klog.Infof("Failed to retry network policy peer namespaces for network %s: %v", oc.GetNetworkName(), err)
284-
}
285284
}
286285

287286
// BaseSecondaryNetworkController structure holds per-network fields and network specific

go-controller/pkg/ovn/base_network_controller_policy.go

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,20 +1496,10 @@ func (bnc *BaseNetworkController) peerNamespaceUpdate(np *networkPolicy, gp *gre
14961496
return err
14971497
}
14981498

1499-
// requeuePeerNamespaces enqueues the namespace into network policy peer namespace
1499+
// requeuePeerNamespace enqueues the namespace into network policy peer namespace
15001500
// retry framework object(s) which need to be retried immediately with add event.
1501-
func (bnc *BaseNetworkController) requeuePeerNamespaces(namespaces []string) error {
1501+
func (bnc *BaseNetworkController) requeuePeerNamespace(namespace *corev1.Namespace) error {
15021502
var errors []error
1503-
var peerNamespaces []*corev1.Namespace
1504-
for _, ns := range namespaces {
1505-
namespace, err := bnc.watchFactory.GetNamespace(ns)
1506-
if err != nil {
1507-
errors = append(errors, fmt.Errorf("failed to retrieve namespace %s for reconciling network %s: %w",
1508-
ns, bnc.GetNetworkName(), err))
1509-
continue
1510-
}
1511-
peerNamespaces = append(peerNamespaces, namespace)
1512-
}
15131503
npKeys := bnc.networkPolicies.GetKeys()
15141504
for _, npKey := range npKeys {
15151505
err := bnc.networkPolicies.DoWithLock(npKey, func(npKey string) error {
@@ -1519,26 +1509,23 @@ func (bnc *BaseNetworkController) requeuePeerNamespaces(namespaces []string) err
15191509
}
15201510
np.RLock()
15211511
defer np.RUnlock()
1512+
if np.deleted {
1513+
return nil
1514+
}
15221515
var errors []error
15231516
for _, reconcilePeerNamespace := range np.reconcilePeerNamespaces {
1524-
namespaceAdded := false
1525-
for _, namespace := range peerNamespaces {
1526-
// Filter out namespace when it's labels not matching with network policy peer namespace
1527-
// selector.
1528-
if !reconcilePeerNamespace.handler.FilterFunc(namespace) {
1529-
continue
1530-
}
1531-
err := reconcilePeerNamespace.retryFramework.AddRetryObjWithAddNoBackoff(namespace)
1532-
if err != nil {
1533-
errors = append(errors, fmt.Errorf("failed to retry peer namespace %s for network policy %s on network %s: %w",
1534-
namespace.Name, npKey, bnc.GetNetworkName(), err))
1535-
continue
1536-
}
1537-
namespaceAdded = true
1517+
// Filter out namespace when it's labels not matching with network policy peer namespace
1518+
// selector.
1519+
if !reconcilePeerNamespace.handler.FilterFunc(namespace) {
1520+
continue
15381521
}
1539-
if namespaceAdded {
1540-
reconcilePeerNamespace.retryFramework.RequestRetryObjs()
1522+
err := reconcilePeerNamespace.retryFramework.AddRetryObjWithAddNoBackoff(namespace)
1523+
if err != nil {
1524+
errors = append(errors, fmt.Errorf("failed to retry peer namespace %s for network policy %s on network %s: %w",
1525+
namespace.Name, npKey, bnc.GetNetworkName(), err))
1526+
continue
15411527
}
1528+
reconcilePeerNamespace.retryFramework.RequestRetryObjs()
15421529
}
15431530
return utilerrors.Join(errors...)
15441531
})
@@ -1587,11 +1574,13 @@ func (bnc *BaseNetworkController) addPeerNamespaceHandler(
15871574
// a new peer namespace is newly created later under UDN network, it gets reconciled and
15881575
// address set is created for the namespace. so we must reconcile it for network policy
15891576
// as well to update gress policy ACL with matching peer namespace address set.
1590-
np.Lock()
1591-
np.reconcilePeerNamespaces = append(np.reconcilePeerNamespaces,
1592-
&peerNamespacesRetry{retryFramework: retryPeerNamespaces,
1593-
handler: namespaceHandler})
1594-
np.Unlock()
1577+
if bnc.IsPrimaryNetwork() {
1578+
np.Lock()
1579+
np.reconcilePeerNamespaces = append(np.reconcilePeerNamespaces,
1580+
&peerNamespacesRetry{retryFramework: retryPeerNamespaces,
1581+
handler: namespaceHandler})
1582+
np.Unlock()
1583+
}
15951584

15961585
return nil
15971586
}

go-controller/pkg/ovn/base_network_controller_secondary.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,15 @@ func (bsnc *BaseSecondaryNetworkController) AddNamespaceForSecondaryNetwork(ns *
679679
if err != nil {
680680
return fmt.Errorf("failed to ensure namespace locked: %v", err)
681681
}
682-
defer nsUnlock()
682+
nsUnlock()
683+
// Enqueue the UDN namespace into network policy controller if it needs to be
684+
// processed by network policy peer namespace handlers.
685+
if bsnc.IsPrimaryNetwork() {
686+
err = bsnc.requeuePeerNamespace(ns)
687+
if err != nil {
688+
return fmt.Errorf("failed to requeue peer namespace %s: %v", ns.Name, err)
689+
}
690+
}
683691
return nil
684692
}
685693

test/e2e/network_segmentation_policy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ var _ = ginkgo.Describe("Network Segmentation: Network Policies", feature.Networ
298298
return updatedPod.Status.Phase
299299
}, 1*time.Minute, 6*time.Second).Should(gomega.Equal(v1.PodPending))
300300

301+
// The pod won't run and the namespace address set won't be created until the NAD for the network is added
302+
// to the namespace and we test here that once that happens the policy is reconciled to account for it.
301303
ginkgo.By("creating NAD for red and orange namespaces and check pod moves into running state")
302304
for _, namespace := range []string{namespaceRed, namespaceOrange} {
303305
ginkgo.By("creating the attachment configuration for " + netConfName + " in namespace " + namespace)

0 commit comments

Comments
 (0)