From 7904bead0a1e8ac0660cb106198d34ba4c3f3e1b Mon Sep 17 00:00:00 2001 From: KMAnju-2021 Date: Thu, 12 Dec 2024 00:32:51 +0530 Subject: [PATCH] Delete secondaryNetwork OVS ports correctly after an Agent restart Signed-off-by: KMAnju-2021 --- .../secondarynetwork/podwatch/controller.go | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/pkg/agent/secondarynetwork/podwatch/controller.go b/pkg/agent/secondarynetwork/podwatch/controller.go index db9c576e614..dcecf75d0e5 100644 --- a/pkg/agent/secondarynetwork/podwatch/controller.go +++ b/pkg/agent/secondarynetwork/podwatch/controller.go @@ -291,6 +291,9 @@ func (pc *PodController) syncPod(key string) error { if err := pc.removeInterfaces(storedInterfaces); err != nil { return err } + + } else { + return nil } } @@ -488,6 +491,7 @@ func (pc *PodController) Run(stopCh <-chan struct{}) { if !cache.WaitForNamedCacheSync(controllerName, stopCh, pc.podInformer.HasSynced) { return } + pc.RestoreSecondaryNetwork() for i := 0; i < numWorkers; i++ { go wait.Until(pc.Worker, time.Second, stopCh) } @@ -502,3 +506,66 @@ func checkForPodSecondaryNetworkAttachement(pod *corev1.Pod) (string, bool) { return netObj, false } } + +// RestoreSecondaryNetwork restore interfacedstore and cniCache when agent restart. +func (pc *PodController) RestoreSecondaryNetwork() error { + err := pc.InitializeInterfaceStore() + if err != nil { + klog.ErrorS(err, "Failed to initialize the secondary bridge interface store") + return err + } + + knownInterfaces := pc.interfaceStore.GetInterfacesByType(interfacestore.ContainerInterface) + for _, containerConfig := range knownInterfaces { + event := types.PodUpdate{ + IsAdd: true, + PodName: containerConfig.ContainerInterfaceConfig.PodName, + PodNamespace: containerConfig.ContainerInterfaceConfig.PodNamespace, + ContainerID: containerConfig.ContainerInterfaceConfig.ContainerID, + } + pc.processCNIUpdate(event) + } + + return nil +} + +func (pc *PodController) InitializeInterfaceStore() error { + ovsPorts, err := pc.ovsBridgeClient.GetPortList() + if err != nil { + klog.ErrorS(err, "Failed to list OVS ports for the secondary bridge") + return err + } + + ifaceList := make([]*interfacestore.InterfaceConfig, 0, len(ovsPorts)) + for index := range ovsPorts { + port := &ovsPorts[index] + ovsPort := &interfacestore.OVSPortConfig{ + PortUUID: port.UUID, + OFPort: port.OFPort, + } + + interfaceType, ok := port.ExternalIDs[interfacestore.AntreaInterfaceTypeKey] + if !ok { + klog.InfoS("Interface type is not set for the secondary bridge", "interfaceName", port.Name) + continue + } + + var intf *interfacestore.InterfaceConfig + switch interfaceType { + case interfacestore.AntreaContainer: + intf = cniserver.ParseOVSPortInterfaceConfig(port, ovsPort) + default: + klog.InfoS("Unknown Antrea interface type for the secondary bridge", "type", interfaceType) + } + + if intf != nil { + ifaceList = append(ifaceList, intf) + } + + } + + pc.interfaceStore.Initialize(ifaceList) + klog.InfoS("Successfully initialized the secondary bridge interface store") + + return nil +}