fix(host): not purge isolated device when detaching it (#22224)

This commit is contained in:
Zexi Li
2025-03-05 19:33:24 +08:00
committed by GitHub
parent c25e1b5981
commit 1f215d316c
5 changed files with 22 additions and 2 deletions
@@ -26,4 +26,5 @@ func init() {
cmd.Update(&compute.DeviceUpdateOptions{})
cmd.Show(&compute.DeviceShowOptions{})
cmd.BatchDelete(&compute.DeviceDeleteOptions{})
cmd.BatchPerform("purge", &compute.DevicePurgeOptions{})
}
+4
View File
@@ -23,6 +23,10 @@ import (
"yunion.io/x/onecloud/pkg/apis"
)
const (
ErrMsgIsolatedDeviceUsedByServer = "Isolated device used by server"
)
type IsolateDeviceDetails struct {
apis.StandaloneResourceDetails
HostResourceInfo
+1 -1
View File
@@ -948,7 +948,7 @@ func (self *SIsolatedDevice) PerformPurge(ctx context.Context, userCred mcclient
func (self *SIsolatedDevice) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
if len(self.GuestId) > 0 {
if !jsonutils.QueryBoolean(data, "purge", false) {
return httperrors.NewBadRequestError("Isolated device used by server: %s", self.GuestId)
return httperrors.NewBadRequestError("%s: %s", api.ErrMsgIsolatedDeviceUsedByServer, self.GuestId)
}
iGuest, err := GuestManager.FetchById(self.GuestId)
if err != nil {
@@ -402,11 +402,15 @@ func (man *isolatedDeviceManager) StartDetachTask() {
log.Infof("Start delete cloud device %s", jsonutils.Marshal(dev))
if _, err := modules.IsolatedDevices.PerformAction(man.getSession(), dev.Id, "purge",
jsonutils.Marshal(map[string]interface{}{
"purge": true,
//"purge": true,
})); err != nil {
if errors.Cause(err) == httperrors.ErrResourceNotFound {
break
}
if strings.Contains(err.Error(), api.ErrMsgIsolatedDeviceUsedByServer) {
log.Warningf("Purge isolated device %s failed: %v", jsonutils.Marshal(dev), err)
break
}
log.Errorf("Detach device %s failed: %v, try again later", dev.Id, err)
time.Sleep(30 * time.Second)
continue
@@ -61,3 +61,14 @@ type DeviceUpdateOptions struct {
func (o *DeviceUpdateOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(o), nil
}
type DevicePurgeOptions struct {
options.BaseIdsOptions
Purge bool `help:"purge devices when attached to guest"`
}
func (o *DevicePurgeOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(map[string]interface{}{
"purge": o.Purge,
}), nil
}