Skip to content

Commit

Permalink
hotunplug, unpluggedPodNic: implemet reverse phase1
Browse files Browse the repository at this point in the history
The commit includes removal of bridge, tap and dummy nic if they exist.
Follow up commits will take care of the caches files deletion.

Notice:
1. The cleanup is general and not binding specific. Any link created
by whatecer binding should be removed (although we curretnly support only
bridge binding hotunplug).
2. Error removing one of the links doesn't block removal on the others.

Signed-off-by: Alona Paz <alkaplan@redhat.com>
  • Loading branch information
AlonaKaplan committed May 15, 2023
1 parent 9029ecb commit 7327506
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/network/driver/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type NetworkHandler interface {
LinkSetUp(link netlink.Link) error
LinkSetName(link netlink.Link, name string) error
LinkAdd(link netlink.Link) error
LinkDel(link netlink.Link) error
LinkSetLearningOff(link netlink.Link) error
ParseAddr(s string) (*netlink.Addr, error)
LinkSetHardwareAddr(link netlink.Link, hwaddr net.HardwareAddr) error
Expand Down Expand Up @@ -127,6 +128,9 @@ func (h *NetworkUtilsHandler) LinkSetName(link netlink.Link, name string) error
func (h *NetworkUtilsHandler) LinkAdd(link netlink.Link) error {
return netlink.LinkAdd(link)
}
func (h *NetworkUtilsHandler) LinkDel(link netlink.Link) error {
return netlink.LinkDel(link)
}
func (h *NetworkUtilsHandler) LinkSetLearningOff(link netlink.Link) error {
return netlink.LinkSetLearning(link, false)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/setup/configstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (c *ConfigState) UnplugNetworks(vmi *v1.VirtualMachineInstance, cleanupFunc
if err != nil {
return err
}
// c.DeleteCacheInterfaces()
// c.cache.Init()
return nil
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/network/setup/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ func (n *VMNetworkConfigurator) UnplugPodNetworksPhase1(vmi *v1.VirtualMachineIn
err := configState.UnplugNetworks(
vmi,
func(podIfaceName string) error {
// TODO clean cache
// TODO remove bridge and tap device
return nil
unpluggedPodNic := unpluggedpodnic{podInterfaceName: podIfaceName, handler: n.handler}
return unpluggedPodNic.UnplugPhase1()
})
if err != nil {
return fmt.Errorf("failed unplug pod networks phase1: %w", err)
Expand Down
55 changes: 55 additions & 0 deletions pkg/network/setup/unpluggedpodnic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package network

import (
"errors"
"github.com/vishvananda/netlink"
k8serrors "k8s.io/apimachinery/pkg/util/errors"
netdriver "kubevirt.io/kubevirt/pkg/network/driver"
virtnetlink "kubevirt.io/kubevirt/pkg/network/link"
)

type unpluggedpodnic struct {
podInterfaceName string
handler netdriver.NetworkHandler
}

func (c unpluggedpodnic) UnplugPhase1() error {
var unplugErrors []error

bridgeName := generateInPodBridgeInterfaceName(c.podInterfaceName)
err := c.delLinkIfExists(bridgeName)
if err != nil {
unplugErrors = append(unplugErrors, err)
}

// remove extra nic
dummyIfaceName := virtnetlink.GenerateNewBridgedVmiInterfaceName(c.podInterfaceName)
err = c.delLinkIfExists(dummyIfaceName)
if err != nil {
unplugErrors = append(unplugErrors, err)
}

// remove tap if exists
tapDeviceName := virtnetlink.GenerateTapDeviceName(c.podInterfaceName)
err = c.delLinkIfExists(tapDeviceName)
if err != nil {
unplugErrors = append(unplugErrors, err)
}

// clean caches
// TODO remove all three cache files

return k8serrors.NewAggregate(unplugErrors)

}

func (c unpluggedpodnic) delLinkIfExists(linkName string) error {
link, err := c.handler.LinkByName(linkName)
if err != nil {
var linkNotFoundErr netlink.LinkNotFoundError
if !errors.As(err, linkNotFoundErr) {
return err
}
}
return c.handler.LinkDel(link)
}

0 comments on commit 7327506

Please sign in to comment.