Skip to content

Commit

Permalink
Delete secondaryNetwork OVS ports correctly after an Agent restart
Browse files Browse the repository at this point in the history
Signed-off-by: KMAnju-2021 <[email protected]>
  • Loading branch information
KMAnju-2021 committed Jan 16, 2025
1 parent 5ee28ec commit dd90f3c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/antrea-agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ func run(o *Options) error {
o.config.ClientConnection, o.config.KubeAPIServerOverride,
k8sClient, localPodInformer.Get(),
podUpdateChannel,
&o.config.SecondaryNetwork, ovsdbConnection)
&o.config.SecondaryNetwork, ovsdbConnection, ifaceStore)
if err != nil {
return fmt.Errorf("failed to create secondary network controller: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/agent/secondarynetwork/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
componentbaseconfig "k8s.io/component-base/config"
"k8s.io/klog/v2"

"antrea.io/antrea/pkg/agent/interfacestore"
"antrea.io/antrea/pkg/agent/secondarynetwork/podwatch"
agentconfig "antrea.io/antrea/pkg/config/agent"
"antrea.io/antrea/pkg/ovs/ovsconfig"
Expand All @@ -48,6 +49,7 @@ func NewController(
podInformer cache.SharedIndexInformer,
podUpdateSubscriber channel.Subscriber,
secNetConfig *agentconfig.SecondaryNetworkConfig, ovsdb *ovsdb.OVSDB,
pIfacestore interfacestore.InterfaceStore,
) (*Controller, error) {
ovsBridgeClient, err := createOVSBridge(secNetConfig.OVSBridges, ovsdb)
if err != nil {
Expand All @@ -65,7 +67,7 @@ func NewController(
// k8s.v1.cni.cncf.io/networks Annotation defined.
podWatchController, err := podwatch.NewPodController(
k8sClient, netAttachDefClient, podInformer,
podUpdateSubscriber, ovsBridgeClient)
podUpdateSubscriber, ovsBridgeClient, pIfacestore)
if err != nil {
return nil, err
}
Expand Down
79 changes: 79 additions & 0 deletions pkg/agent/secondarynetwork/podwatch/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func NewPodController(
podInformer cache.SharedIndexInformer,
podUpdateSubscriber channel.Subscriber,
ovsBridgeClient ovsconfig.OVSBridgeClient,
pIfacestore interfacestore.InterfaceStore,
) (*PodController, error) {
ifaceStore := interfacestore.NewInterfaceStore()
interfaceConfigurator, err := cniserver.NewSecondaryInterfaceConfigurator(ovsBridgeClient, ifaceStore)
Expand Down Expand Up @@ -133,6 +134,11 @@ func NewPodController(
},
resyncPeriod,
)

if err := pc.reconcileSecondaryInterfaces(pIfacestore); err != nil {
klog.ErrorS(err, "Failed to restore the secondary bridge interface store")
}

// podUpdateSubscriber can be nil with test code.
if podUpdateSubscriber != nil {
// Subscribe Pod CNI add/del events.
Expand Down Expand Up @@ -291,6 +297,8 @@ func (pc *PodController) syncPod(key string) error {
if err := pc.removeInterfaces(storedInterfaces); err != nil {
return err
}
} else {
return nil
}
}

Expand Down Expand Up @@ -502,3 +510,74 @@ func checkForPodSecondaryNetworkAttachement(pod *corev1.Pod) (string, bool) {
return netObj, false
}
}

// reconcileSecondaryInterfaces restore interfacestore and cniCache when agent restart.
func (pc *PodController) reconcileSecondaryInterfaces(pIfacestore interfacestore.InterfaceStore) error {
err := pc.initializeInterfaceStore()
if err != nil {
klog.ErrorS(err, "Failed to initialize the secondary bridge interface store")
return err
}

knownInterfaces := pIfacestore.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)
}

secondaryInterfaces := pc.interfaceStore.GetInterfacesByType(interfacestore.ContainerInterface)
for _, containerConfig := range secondaryInterfaces {
containerId := containerConfig.ContainerID
_, exists := pIfacestore.GetContainerInterface(containerId)
if !exists {
pc.interfaceStore.DeleteInterface(containerConfig)
}
}
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", "bridgeName", pc.ovsBridgeClient.GetBridgeName())
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
}
2 changes: 1 addition & 1 deletion pkg/agent/secondarynetwork/podwatch/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestPodControllerRun(t *testing.T) {
client,
netdefclient,
informerFactory.Core().V1().Pods().Informer(),
nil, nil)
nil, nil, nil)
podController.interfaceConfigurator = interfaceConfigurator
podController.ipamAllocator = mockIPAM
cniCache := &podController.cniCache
Expand Down

0 comments on commit dd90f3c

Please sign in to comment.