Skip to content

Commit

Permalink
virt-handler, podIfaceName discovery: support not found links
Browse files Browse the repository at this point in the history
The VM can hotplugged interface that weren't yet plugged to the pod, it
is a valid scenario.

Signed-off-by: Alona Paz <[email protected]>
  • Loading branch information
AlonaKaplan committed Jun 15, 2023
1 parent c249c1e commit 1c5cf68
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
10 changes: 9 additions & 1 deletion pkg/network/link/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package link

import (
"errors"
"fmt"
"strings"

Expand All @@ -34,6 +35,7 @@ import (
// DiscoverByNetwork return the pod interface link of the given network name.
// If link not found, it will try to get the link using the pod interface's ordinal name (net1, net2,...)
// based on the subject network position in the given networks slice.
// If no link is found, a nil link will be returned.
func DiscoverByNetwork(handler driver.NetworkHandler, networks []v1.Network, subjectNetwork v1.Network) (netlink.Link, error) {
ifaceNames, err := networkInterfaceNames(networks, subjectNetwork)
if err != nil {
Expand All @@ -60,7 +62,13 @@ func linkByNames(handler driver.NetworkHandler, names []string) (netlink.Link, e
if err == nil {
return link, nil
}
errs = append(errs, fmt.Sprintf("could not get link with name %q: %v", name, err))
var linkNotFoundErr netlink.LinkNotFoundError
if !errors.As(err, &linkNotFoundErr) {
errs = append(errs, fmt.Sprintf("could not get link with name %q: %v", name, err))
}
}
if len(errs) == 0 {
return nil, nil
}
return nil, fmt.Errorf(strings.Join(errs, ", "))
}
16 changes: 8 additions & 8 deletions pkg/network/setup/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func preConfigStateRun(nics []podNIC) ([]podNIC, error) {

func discoverPodInterfaces(nics []podNIC) ([]podNIC, error) {
for idx, nic := range nics {
podIfaceName, err := discoverPodInterfaceName(nic.handler, nic.vmi.Spec.Networks, *nic.vmiSpecNetwork, *nic.vmiSpecIface)
podIfaceName, err := discoverPodInterfaceName(nic.handler, nic.vmi.Spec.Networks, *nic.vmiSpecNetwork)
if err != nil {
return nil, err
}
Expand All @@ -146,15 +146,15 @@ func discoverPodInterfaces(nics []podNIC) ([]podNIC, error) {
return nics, nil
}

func discoverPodInterfaceName(handler netdriver.NetworkHandler, networks []v1.Network, subjectNetwork v1.Network, subjectedIface v1.Interface) (string, error) {
func discoverPodInterfaceName(handler netdriver.NetworkHandler, networks []v1.Network, subjectNetwork v1.Network) (string, error) {
ifaceLink, err := link.DiscoverByNetwork(handler, networks, subjectNetwork)
if err != nil {
if subjectedIface.State != v1.InterfaceStateAbsent {
return "", err
}
// For a network marked for removal, it is reasonable not to find any interface.
return "", nil
return "", err
} else {
if ifaceLink == nil {
// couldn't find any interface
return "", nil
}
return ifaceLink.Attrs().Name, nil
}
}
Expand All @@ -179,7 +179,7 @@ func (n *VMNetworkConfigurator) UnplugPodNetworksPhase1(vmi *v1.VirtualMachineIn
func() (map[string]string, error) {
podIfaceNameByNet := map[string]string{}
for _, net := range vmi.Spec.Networks {
podIfaceName, err := discoverPodInterfaceName(n.handler, vmi.Spec.Networks, net, *vmispec.LookupInterfaceByName(vmi.Spec.Domain.Devices.Interfaces, net.Name))
podIfaceName, err := discoverPodInterfaceName(n.handler, vmi.Spec.Networks, net)
if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/network/setup/podnic.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func newPhase2PodNIC(vmi *v1.VirtualMachineInstance, network *v1.Network, handle
if err != nil {
return nil, err
}
podnic.podInterfaceName = ifaceLink.Attrs().Name
if ifaceLink == nil {
podnic.podInterfaceName = ""
} else {
podnic.podInterfaceName = ifaceLink.Attrs().Name
}

podnic.dhcpConfigurator = podnic.newDHCPConfigurator()
podnic.domainGenerator = podnic.newLibvirtSpecGenerator(domain)
Expand Down

0 comments on commit 1c5cf68

Please sign in to comment.