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

Automated cherry pick of #70291: Add code for including shutdown nodes #70808

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkg/cloudprovider/providers/vsphere/vclib/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,29 @@ func (vm *VirtualMachine) IsActive(ctx context.Context) (bool, error) {
return false, nil
}

// Exists checks if VM exists and is not terminated
func (vm *VirtualMachine) Exists(ctx context.Context) (bool, error) {
vmMoList, err := vm.Datacenter.GetVMMoList(ctx, []*VirtualMachine{vm}, []string{"summary.runtime.powerState"})
if err != nil {
glog.Errorf("Failed to get VM Managed object with property summary. err: +%v", err)
return false, err
}
// We check for VMs which are still available in vcenter and has not been terminated/removed from
// disk and hence we consider PoweredOn,PoweredOff and Suspended as alive states.
aliveStates := []types.VirtualMachinePowerState{
types.VirtualMachinePowerStatePoweredOff,
types.VirtualMachinePowerStatePoweredOn,
types.VirtualMachinePowerStateSuspended,
}
currentState := vmMoList[0].Summary.Runtime.PowerState
for _, state := range aliveStates {
if state == currentState {
return true, nil
}
}
return false, nil
}

// GetAllAccessibleDatastores gets the list of accessible Datastores for the given Virtual Machine
func (vm *VirtualMachine) GetAllAccessibleDatastores(ctx context.Context) ([]*DatastoreInfo, error) {
host, err := vm.HostSystem(ctx)
Expand Down
6 changes: 3 additions & 3 deletions pkg/cloudprovider/providers/vsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,12 @@ func (vs *VSphere) InstanceID(ctx context.Context, nodeName k8stypes.NodeName) (
glog.Errorf("Failed to get VM object for node: %q. err: +%v", convertToString(nodeName), err)
return "", err
}
isActive, err := vm.IsActive(ctx)
exists, err := vm.Exists(ctx)
if err != nil {
glog.Errorf("Failed to check whether node %q is active. err: %+v.", convertToString(nodeName), err)
glog.Errorf("Failed to check whether node %q still exists. err: %+v.", convertToString(nodeName), err)
return "", err
}
if isActive {
if exists {
return vs.vmUUID, nil
}
glog.Warningf("The VM: %s is not in %s state", convertToString(nodeName), vclib.ActivePowerState)
Expand Down