Skip to content

Commit d6cd275

Browse files
committed
Revert "MTV-2025 | MTV-1673 | NICs mapping is unordered"
This reverts commit f1df4c9. Signed-off-by: Martin Necas <[email protected]>
1 parent 625eefe commit d6cd275

File tree

1 file changed

+68
-54
lines changed

1 file changed

+68
-54
lines changed

pkg/controller/plan/adapter/vsphere/builder.go

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -683,77 +683,91 @@ func (r *Builder) mapNetworks(vm *model.VM, object *cnv.VirtualMachineSpec) (err
683683

684684
numNetworks := 0
685685
netMapIn := r.Context.Map.Network.Spec.Map
686+
for i := range netMapIn {
687+
mapped := &netMapIn[i]
686688

687-
for _, nic := range vm.NICs {
688-
mapped := r.findNetworkMapping(nic, netMapIn)
689-
690-
// Skip if no valid mapping found or the destination type is Ignored
691-
if mapped == nil || mapped.Destination.Type == Ignored {
689+
// Skip network mappings with destination type 'Ignored'
690+
if mapped.Destination.Type == Ignored {
692691
continue
693692
}
694693

695-
networkName := fmt.Sprintf("net-%v", numNetworks)
694+
ref := mapped.Source
695+
network := &model.Network{}
696+
fErr := r.Source.Inventory.Find(network, ref)
697+
if fErr != nil {
698+
err = fErr
699+
return
700+
}
696701

697-
// If a name template is defined, try to use it
698-
networkNameTemplate := r.getNetworkNameTemplate(vm)
699-
if networkNameTemplate != "" {
700-
templateData := api.NetworkNameTemplateData{
701-
NetworkName: mapped.Destination.Name,
702-
NetworkNamespace: mapped.Destination.Namespace,
703-
NetworkType: mapped.Destination.Type,
704-
NetworkIndex: numNetworks,
705-
}
706-
if generated, err := r.executeTemplate(networkNameTemplate, &templateData); err == nil && generated != "" {
707-
networkName = generated
708-
} else {
709-
r.Log.Info("Failed to generate network name using template, using default", "template", networkNameTemplate, "error", err)
702+
needed := []vsphere.NIC{}
703+
for _, nic := range vm.NICs {
704+
switch network.Variant {
705+
case vsphere.NetDvPortGroup, vsphere.OpaqueNetwork:
706+
if nic.Network.ID == network.Key {
707+
needed = append(needed, nic)
708+
}
709+
default:
710+
if nic.Network.ID == network.ID {
711+
needed = append(needed, nic)
712+
}
710713
}
711714
}
712-
713-
numNetworks++
714-
kNetwork := cnv.Network{Name: networkName}
715-
kInterface := cnv.Interface{
716-
Name: networkName,
717-
Model: Virtio,
718-
MacAddress: nic.MAC,
715+
if len(needed) == 0 {
716+
continue
719717
}
718+
for _, nic := range needed {
719+
networkName := fmt.Sprintf("net-%v", numNetworks)
720+
721+
// If the network name template is set, use it to generate the network name.
722+
networkNameTemplate := r.getNetworkNameTemplate(vm)
723+
if networkNameTemplate != "" {
724+
// Create template data
725+
templateData := api.NetworkNameTemplateData{
726+
NetworkName: mapped.Destination.Name,
727+
NetworkNamespace: mapped.Destination.Namespace,
728+
NetworkType: mapped.Destination.Type,
729+
NetworkIndex: numNetworks,
730+
}
731+
732+
networkName, err = r.executeTemplate(networkNameTemplate, &templateData)
733+
if err != nil {
734+
// Failed to generate network name using template
735+
r.Log.Info("Failed to generate network name using template, using default name", "template", networkNameTemplate, "error", err)
720736

721-
switch mapped.Destination.Type {
722-
case Pod:
723-
kNetwork.Pod = &cnv.PodNetwork{}
724-
kInterface.Masquerade = &cnv.InterfaceMasquerade{}
725-
case Multus:
726-
kNetwork.Multus = &cnv.MultusNetwork{
727-
NetworkName: path.Join(mapped.Destination.Namespace, mapped.Destination.Name),
737+
// Fallback to default name and reset error
738+
networkName = fmt.Sprintf("net-%v", numNetworks)
739+
err = nil
740+
}
728741
}
729-
kInterface.Bridge = &cnv.InterfaceBridge{}
730-
}
731742

732-
kNetworks = append(kNetworks, kNetwork)
733-
kInterfaces = append(kInterfaces, kInterface)
743+
numNetworks++
744+
kNetwork := cnv.Network{
745+
Name: networkName,
746+
}
747+
kInterface := cnv.Interface{
748+
Name: networkName,
749+
Model: Virtio,
750+
MacAddress: nic.MAC,
751+
}
752+
switch mapped.Destination.Type {
753+
case Pod:
754+
kNetwork.Pod = &cnv.PodNetwork{}
755+
kInterface.Masquerade = &cnv.InterfaceMasquerade{}
756+
case Multus:
757+
kNetwork.Multus = &cnv.MultusNetwork{
758+
NetworkName: path.Join(mapped.Destination.Namespace, mapped.Destination.Name),
759+
}
760+
kInterface.Bridge = &cnv.InterfaceBridge{}
761+
}
762+
kNetworks = append(kNetworks, kNetwork)
763+
kInterfaces = append(kInterfaces, kInterface)
764+
}
734765
}
735-
736766
object.Template.Spec.Networks = kNetworks
737767
object.Template.Spec.Domain.Devices.Interfaces = kInterfaces
738768
return
739769
}
740770

741-
func (r *Builder) findNetworkMapping(nic vsphere.NIC, netMap []api.NetworkPair) *api.NetworkPair {
742-
for i := range netMap {
743-
candidate := &netMap[i]
744-
network := &model.Network{}
745-
if err := r.Source.Inventory.Find(network, candidate.Source); err != nil {
746-
continue
747-
}
748-
749-
if (network.Variant == vsphere.NetDvPortGroup || network.Variant == vsphere.OpaqueNetwork) &&
750-
nic.Network.ID == network.Key || nic.Network.ID == network.ID {
751-
return candidate
752-
}
753-
}
754-
return nil
755-
}
756-
757771
func (r *Builder) mapInput(object *cnv.VirtualMachineSpec) {
758772
tablet := cnv.Input{
759773
Type: Tablet,

0 commit comments

Comments
 (0)