-
Notifications
You must be signed in to change notification settings - Fork 12
[FEATURE] Add option to select network interface model while importing VM #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package source | ||
|
||
import ( | ||
"fmt" | ||
|
||
kubevirt "kubevirt.io/api/core/v1" | ||
|
||
migration "github.com/harvester/vm-import-controller/pkg/apis/migration.harvesterhci.io/v1beta1" | ||
) | ||
|
||
type NetworkInfo struct { | ||
NetworkName string | ||
MAC string | ||
MappedNetwork string | ||
Model string | ||
} | ||
|
||
func MapNetworks(networkInfos []NetworkInfo, networkMappings []migration.NetworkMapping) []NetworkInfo { | ||
result := make([]NetworkInfo, 0) | ||
|
||
for _, ni := range networkInfos { | ||
for _, nm := range networkMappings { | ||
if nm.SourceNetwork == ni.NetworkName { | ||
ni.MappedNetwork = nm.DestinationNetwork | ||
|
||
// Override the auto-detected interface model if it is | ||
// customized by the user via the `NetworkMapping`. | ||
if nm.NetworkInterfaceModel != nil { | ||
ni.Model = nm.GetNetworkInterfaceModel() | ||
} | ||
|
||
result = append(result, ni) | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func GenerateNetworkInterfaceConfigs(networkInfos []NetworkInfo, defaultNetworkInterfaceModel string) ([]kubevirt.Network, []kubevirt.Interface) { | ||
networks := make([]kubevirt.Network, 0, len(networkInfos)) | ||
interfaces := make([]kubevirt.Interface, 0, len(networkInfos)) | ||
|
||
for i, ni := range networkInfos { | ||
networks = append(networks, kubevirt.Network{ | ||
NetworkSource: kubevirt.NetworkSource{ | ||
Multus: &kubevirt.MultusNetwork{ | ||
NetworkName: ni.MappedNetwork, | ||
}, | ||
}, | ||
Name: fmt.Sprintf("migrated-%d", i), | ||
}) | ||
|
||
interfaces = append(interfaces, kubevirt.Interface{ | ||
Name: fmt.Sprintf("migrated-%d", i), | ||
MacAddress: ni.MAC, | ||
Model: ni.Model, | ||
InterfaceBindingMethod: kubevirt.InterfaceBindingMethod{ | ||
Bridge: &kubevirt.InterfaceBridge{}, | ||
}, | ||
}) | ||
} | ||
|
||
// If there is no network, attach to Pod network. Essential for VM to | ||
// be booted up. | ||
if len(networks) == 0 { | ||
networks = append(networks, kubevirt.Network{ | ||
Name: "pod-network", | ||
NetworkSource: kubevirt.NetworkSource{ | ||
Pod: &kubevirt.PodNetwork{}, | ||
}, | ||
}) | ||
interfaces = append(interfaces, kubevirt.Interface{ | ||
Name: "pod-network", | ||
Model: defaultNetworkInterfaceModel, | ||
InterfaceBindingMethod: kubevirt.InterfaceBindingMethod{ | ||
Masquerade: &kubevirt.InterfaceMasquerade{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason not using the bridge interface here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, I can't say anything here as I have adopted the code 1:1. @ibrokethecloud added the origin code. Could you tell use more please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if there is no valid source to destination mapping we default to adding the masquerade network which is the current default behaviour of harvester during vm creation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood. Thanks for clarifying. I was just considering what might be the most compatible configuration :D |
||
}, | ||
}) | ||
} | ||
|
||
return networks, interfaces | ||
} |
Uh oh!
There was an error while loading. Please reload this page.