mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Merge pull request #92608 from lubronzhan/remove_node_when_instance_not_found_vsphere
Remove node when the vm is deleted in vsphere
This commit is contained in:
commit
0469db9fe7
@ -213,6 +213,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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -766,11 +766,14 @@ func (vs *VSphere) InstanceExistsByProviderID(ctx context.Context, providerID st
|
||||
return false, err
|
||||
}
|
||||
_, err = vs.InstanceID(ctx, convertToK8sType(nodeName))
|
||||
if err == nil {
|
||||
return true, nil
|
||||
if err != nil {
|
||||
if err == cloudprovider.InstanceNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
return false, err
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// InstanceShutdownByProviderID returns true if the instance is in safe state to detach volumes
|
||||
@ -832,15 +835,17 @@ 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)
|
||||
|
||||
klog.Warningf("The VM: %s doesn't exist", convertToString(nodeName))
|
||||
return "", cloudprovider.InstanceNotFound
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user