network: refactor newpodNIC
The factory function newPodNIC exists only to accomodate our tests.
However, it also did something unrelated: verify that a network is
implementable by KubeVirt.
Extracting the verififcation part would allow us to simplify newPodNIC
in a followup commit.
@@ -71,6 +71,13 @@ func getNetworksAndCniNetworks(vmi *v1.VirtualMachineInstance) (map[string]*v1.N
return networks, cniNetworks
}
+func networkIsImplementable(network *v1.Network) error {
+ if network.Pod == nil && network.Multus == nil {
+ return fmt.Errorf("Network not implemented")
+ }
+ return nil
+}
+
func getPodInterfaceName(networks map[string]*v1.Network, cniNetworks map[string]int, ifaceName string) string {
if networks[ifaceName].Multus != nil && !networks[ifaceName].Multus.Default {
// multus pod interfaces named netX
@@ -87,6 +94,10 @@ func SetupPodNetworkPhase1(vmi *v1.VirtualMachineInstance, pid int, cacheFactory
if !ok {
return fmt.Errorf("failed to find a network %s", iface.Name)
}
+ err := networkIsImplementable(network)
+ if err != nil {
+ return err
+ }
podnic, err := podNICFactory(network, cacheFactory)
if err != nil {
return err
@@ -107,6 +118,10 @@ func SetupPodNetworkPhase2(vmi *v1.VirtualMachineInstance, domain *api.Domain, c
if !ok {
return fmt.Errorf("failed to find a network %s", iface.Name)
}
+ err := networkIsImplementable(network)
+ if err != nil {
+ return err
+ }
podnic, err := podNICFactory(network, cacheFactory)
if err != nil {
return err
@@ -121,8 +136,5 @@ func SetupPodNetworkPhase2(vmi *v1.VirtualMachineInstance, domain *api.Domain, c
}
func newpodNIC(network *v1.Network, cacheFactory cache.InterfaceCacheFactory) (podNIC, error) {
- if network.Pod != nil || network.Multus != nil {
return &podNICImpl{cacheFactory: cacheFactory}, nil
}
- return nil, fmt.Errorf("Network not implemented")
-}
