Skip to content
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

✨ Ensure NICs are connected / start connected #805

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions pkg/providers/vsphere/session/session_vm_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ func (s *Session) prePowerOnVMConfigSpec(
}
configSpec.DeviceChange = append(configSpec.DeviceChange, ethCardDeviceChanges...)

vmopv1util.ReconcileNetworkDeviceConnectionState(config, configSpec)

var expectedPCIDevices []vimtypes.BaseVirtualDevice
if configSpecDevs := pkgutil.DevicesFromConfigSpec(&updateArgs.ConfigSpec); len(configSpecDevs) > 0 {
pciPassthruFromConfigSpec := pkgutil.SelectVirtualPCIPassthrough(configSpecDevs)
Expand Down Expand Up @@ -800,6 +802,7 @@ func (s *Session) poweredOnVMReconfigure(

UpdateConfigSpecExtraConfig(vmCtx, config, configSpec, nil, nil, vmCtx.VM, nil)
UpdateConfigSpecChangeBlockTracking(vmCtx, config, configSpec, nil, vmCtx.VM.Spec)
vmopv1util.ReconcileNetworkDeviceConnectionState(config, configSpec)

if pkgcfg.FromContext(vmCtx).Features.IsoSupport {
if err := virtualmachine.UpdateConfigSpecCdromDeviceConnection(vmCtx, s.Client.RestClient(), s.K8sClient, config, configSpec); err != nil {
Expand Down Expand Up @@ -892,13 +895,17 @@ func (s *Session) resizeVMWhenPoweredStateOff(

return false, err
}
} else if err := vmopv1util.OverwriteAlwaysResizeConfigSpec(
vmCtx,
*vmCtx.VM,
*moVM.Config,
&configSpec); err != nil {
} else {
vmopv1util.ReconcileNetworkDeviceConnectionState(moVM.Config, &configSpec)

return false, err
if err := vmopv1util.OverwriteAlwaysResizeConfigSpec(
vmCtx,
*vmCtx.VM,
*moVM.Config,
&configSpec); err != nil {

return false, err
}
}

refetchProps, err := doReconfigure(
Expand Down
102 changes: 102 additions & 0 deletions pkg/util/vmopv1/resize_overwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func OverwriteResizeConfigSpec(

overwriteGuestID(vm, ci, cs)
overwriteExtraConfig(vm, ci, cs)
ReconcileNetworkDeviceConnectionState(&ci, cs)

return nil
}
Expand Down Expand Up @@ -244,6 +245,107 @@ func updateV1Alpha1CompatibleEC(
}
}

// ReconcileNetworkDeviceConnectionState ensures the VMs network interface
// devices are connected and start connected. Please note, this function should
// only modify the configSpec if:
// - there is a network device in configInfo that is not connected or does not
// start connected and is not being removed in the configSpec.
// - there is a new network device in configSpec (i.e. it has a negative
// device key) and is not marked connected or not marked as starting
// connected.
//
// The ci parameter may be nil when this function is used to ensure the correct
// behavior for a ConfigSpec used to create a new VM.
func ReconcileNetworkDeviceConnectionState(
ci *vimtypes.VirtualMachineConfigInfo,
cs *vimtypes.VirtualMachineConfigSpec) {

// Get a list of current NICs.
curDevLst := []*vimtypes.VirtualEthernetCard{}
curDevMap := map[int32]*vimtypes.VirtualEthernetCard{}
if ci != nil {
for i := range ci.Hardware.Device {
if bd, ok := ci.Hardware.Device[i].(vimtypes.BaseVirtualEthernetCard); ok {
d := bd.GetVirtualEthernetCard()
curDevMap[d.Key] = d
curDevLst = append(curDevLst, d)
}
}
}

// Parse the ConfigSpec for new/modified NICs.
modDevs := map[int32]*vimtypes.VirtualEthernetCard{}
if cs != nil {
for i := range cs.DeviceChange {
ds := cs.DeviceChange[i].GetVirtualDeviceConfigSpec()
if bd, ok := ds.Device.(vimtypes.BaseVirtualEthernetCard); ok {
d := bd.GetVirtualEthernetCard()
if _, ok := curDevMap[d.Key]; ok {
//
// NIC already exists.
//
if ds.Operation == vimtypes.VirtualDeviceConfigSpecOperationRemove {
//
// NIC is being removed.
//
delete(curDevMap, d.Key)
} else {
//
// NIC is being updated.
//
modDevs[d.Key] = d
}
} else {
//
// NIC is being added, ensure it is connected.
//
if d.Connectable == nil {
d.Connectable = &vimtypes.VirtualDeviceConnectInfo{}
}
d.Connectable.StartConnected = true
d.Connectable.Connected = true
}
}
}
}

// Ensure all current NICs are connected and start connected.
for _, d := range curDevLst {
if _, ok := curDevMap[d.Key]; ok {

if d.Connectable == nil ||
!d.Connectable.Connected ||
!d.Connectable.StartConnected {

connectable := d.Connectable
if connectable == nil {
connectable = &vimtypes.VirtualDeviceConnectInfo{}
}
connectable.Connected = true
connectable.StartConnected = true

// The NIC is either not connected or not set to start
// connected.
if md, ok := modDevs[d.Key]; ok {
// The NIC is already part of the ConfigSpec, so update
// it there so its connection state information is
// correct.
md.Connectable = connectable
} else if cs != nil {
// Add the NIC to the ConfigSpec to ensure it is
// connected and starts connected.
d.Connectable = connectable
cs.DeviceChange = append(cs.DeviceChange,
&vimtypes.VirtualDeviceConfigSpec{
Operation: vimtypes.VirtualDeviceConfigSpecOperationEdit,
Device: d,
})
}
}
}
}
}

func hasvGPUOrDDPIODevicesInVM(
config vimtypes.VirtualMachineConfigInfo) bool {

Expand Down
Loading
Loading