mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 08:17:26 +00:00
Cherry pick the fix https://github.com/kubernetes/kubernetes/pull/70291
This commit is contained in:
parent
b67e7d6e2c
commit
3a9b6bd50a
@ -214,6 +214,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 {
|
||||
klog.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)
|
||||
|
@ -114,6 +114,26 @@ func TestVirtualMachine(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
for _, turnOff := range []bool{true, false} {
|
||||
// Turn off for checking if exist return true
|
||||
if turnOff {
|
||||
_, _ = vm.PowerOff(ctx)
|
||||
}
|
||||
|
||||
exist, err := vm.Exists(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !exist {
|
||||
t.Errorf("exist=%t, expected=%t", exist, true)
|
||||
}
|
||||
|
||||
// Turn back on
|
||||
if turnOff {
|
||||
_, _ = vm.PowerOn(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
for _, expect := range []bool{true, false} {
|
||||
active, err := vm.IsActive(ctx)
|
||||
if err != nil {
|
||||
@ -140,5 +160,11 @@ func TestVirtualMachine(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Expecting Exists func to throw error if VM deleted
|
||||
_, err = vm.Exists(ctx)
|
||||
if err == nil {
|
||||
t.Error("expected error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -857,14 +857,16 @@ func (vs *VSphere) InstanceID(ctx context.Context, nodeName k8stypes.NodeName) (
|
||||
klog.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 {
|
||||
klog.Errorf("Failed to check whether node %q is active. err: %+v.", convertToString(nodeName), err)
|
||||
klog.Errorf("Failed to check whether node %q still exists. err: %+v.", convertToString(nodeName), err)
|
||||
return "", err
|
||||
}
|
||||
if isActive {
|
||||
if exists {
|
||||
return vs.vmUUID, nil
|
||||
}
|
||||
|
||||
klog.Warningf("The VM: %s is not in %s state", convertToString(nodeName), vclib.ActivePowerState)
|
||||
return "", cloudprovider.InstanceNotFound
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user