From 554ec887347e2cf2b0bb0d55c98a13f34b891985 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Wed, 28 Oct 2020 23:40:52 +0800 Subject: [PATCH] fix: fail to delete public_ip eip --- pkg/apis/compute/guests.go | 13 +++++++++++++ pkg/compute/guestdrivers/openstack.go | 2 +- pkg/compute/models/elasticips.go | 2 +- pkg/compute/models/guest_actions.go | 21 +++++++++++++-------- pkg/compute/models/guests.go | 16 ++++++++++------ pkg/compute/tasks/guest_delete_task.go | 2 +- 6 files changed, 39 insertions(+), 17 deletions(-) diff --git a/pkg/apis/compute/guests.go b/pkg/apis/compute/guests.go index 92b8f35671..e791a7d6d5 100644 --- a/pkg/apis/compute/guests.go +++ b/pkg/apis/compute/guests.go @@ -419,3 +419,16 @@ type ServerRemoteUpdateInput struct { // 是否覆盖替换所有标签 ReplaceTags *bool `json:"replace_tags" help:"replace all remote tags"` } + +type ServerAssociateEipInput struct { + // swagger:ignore + // Deprecated + Eip string `json:"eip" yunion-deprecated-by:"eip_id"` + // 弹性公网IP的ID + EipId string `json:"eip_id"` +} + +type ServerDissociateEipInput struct { + // 是否自动释放 + AudoDelete *bool `json:"auto_delete"` +} diff --git a/pkg/compute/guestdrivers/openstack.go b/pkg/compute/guestdrivers/openstack.go index ad5ca8a996..246f973d8a 100644 --- a/pkg/compute/guestdrivers/openstack.go +++ b/pkg/compute/guestdrivers/openstack.go @@ -232,7 +232,7 @@ func (self *SOpenStackGuestDriver) RemoteDeployGuestForRebuildRoot(ctx context.C } defer self.attachDisks(ctx, ihost, instanceId, detachDisks) - eip, err := guest.GetEip() + eip, err := guest.GetElasticIp() if err == nil && eip != nil { ieip, err := eip.GetIEip() if err != nil { diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 615afc58ae..25b5a99848 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -970,7 +970,7 @@ func (self *SElasticip) PerformAssociate(ctx context.Context, userCred mcclient. // IMPORTANT: this serves as a guard against a guest to have multiple // associated elastic_ips - seip, _ := server.GetEip() + seip, _ := server.GetEipOrPublicIp() if seip != nil { return nil, httperrors.NewInvalidStatusError("instance is already associated with eip") } diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index ee3d7e802a..9e4c43406c 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -2624,7 +2624,7 @@ func (self *SGuest) RevokeAllSecgroups(ctx context.Context, userCred mcclient.To } func (self *SGuest) DoPendingDelete(ctx context.Context, userCred mcclient.TokenCredential) { - eip, _ := self.GetEip() + eip, _ := self.GetEipOrPublicIp() if eip != nil { eip.DoPendingDelete(ctx, userCred) } @@ -2666,6 +2666,11 @@ func (self *SGuest) PerformCancelDelete(ctx context.Context, userCred mcclient.T } func (self *SGuest) DoCancelPendingDelete(ctx context.Context, userCred mcclient.TokenCredential) error { + eip, _ := self.GetEipOrPublicIp() + if eip != nil { + eip.DoCancelPendingDelete(ctx, userCred) + } + for _, guestdisk := range self.GetDisks() { disk := guestdisk.GetDisk() disk.DoCancelPendingDelete(ctx, userCred) @@ -2881,12 +2886,12 @@ func (self *SGuest) AllowPerformAssociateEip(ctx context.Context, userCred mccli return self.IsOwner(userCred) || db.IsAdminAllowPerform(userCred, self, "associate-eip") } -func (self *SGuest) PerformAssociateEip(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { +func (self *SGuest) PerformAssociateEip(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ServerAssociateEipInput) (jsonutils.JSONObject, error) { if !utils.IsInStringArray(self.Status, []string{api.VM_READY, api.VM_RUNNING}) { return nil, httperrors.NewInvalidStatusError("cannot associate eip in status %s", self.Status) } - eip, err := self.GetEip() + eip, err := self.GetEipOrPublicIp() if err != nil { log.Errorf("Fail to get Eip %s", err) return nil, httperrors.NewGeneralError(err) @@ -2894,7 +2899,7 @@ func (self *SGuest) PerformAssociateEip(ctx context.Context, userCred mcclient.T if eip != nil { return nil, httperrors.NewInvalidStatusError("already associate with eip") } - eipStr := jsonutils.GetAnyString(data, []string{"eip", "eip_id"}) + eipStr := input.EipId if len(eipStr) == 0 { return nil, httperrors.NewMissingParameterError("eip_id") } @@ -2969,8 +2974,8 @@ func (self *SGuest) AllowPerformDissociateEip(ctx context.Context, userCred mccl return self.IsOwner(userCred) || db.IsAdminAllowPerform(userCred, self, "dissociate-eip") } -func (self *SGuest) PerformDissociateEip(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { - eip, err := self.GetEip() +func (self *SGuest) PerformDissociateEip(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ServerDissociateEipInput) (jsonutils.JSONObject, error) { + eip, err := self.GetElasticIp() if err != nil { log.Errorf("Fail to get Eip %s", err) return nil, httperrors.NewGeneralError(err) @@ -2986,7 +2991,7 @@ func (self *SGuest) PerformDissociateEip(ctx context.Context, userCred mcclient. self.SetStatus(userCred, api.VM_DISSOCIATE_EIP, "associate eip") - autoDelete := jsonutils.QueryBoolean(data, "auto_delete", false) + autoDelete := (input.AudoDelete != nil && *input.AudoDelete) err = eip.StartEipDissociateTask(ctx, userCred, autoDelete, "") if err != nil { @@ -4330,7 +4335,7 @@ func (guest *SGuest) PerformChangeOwner(ctx context.Context, userCred mcclient.T } } - if eip, _ := guest.GetEip(); eip != nil { + if eip, _ := guest.GetEipOrPublicIp(); eip != nil { _, err := eip.PerformChangeOwner(ctx, userCred, query, input) if err != nil { return nil, err diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 8c7eb558d7..f8bde40a5b 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -4718,8 +4718,12 @@ func (self *SGuest) isInReconcile(userCred mcclient.TokenCredential) bool { return false } -func (self *SGuest) GetEip() (*SElasticip, error) { - return ElasticipManager.getEipForInstance(api.EIP_ASSOCIATE_TYPE_SERVER, self.Id) +func (self *SGuest) GetEipOrPublicIp() (*SElasticip, error) { + return ElasticipManager.getEip(api.EIP_ASSOCIATE_TYPE_SERVER, self.Id, "") +} + +func (self *SGuest) GetElasticIp() (*SElasticip, error) { + return ElasticipManager.getEip(api.EIP_ASSOCIATE_TYPE_SERVER, self.Id, api.EIP_MODE_STANDALONE_EIP) } func (self *SGuest) GetPublicIp() (*SElasticip, error) { @@ -4729,7 +4733,7 @@ func (self *SGuest) GetPublicIp() (*SElasticip, error) { func (self *SGuest) SyncVMEip(ctx context.Context, userCred mcclient.TokenCredential, provider *SCloudprovider, extEip cloudprovider.ICloudEIP, syncOwnerId mcclient.IIdentityProvider) compare.SyncResult { result := compare.SyncResult{} - eip, err := self.GetEip() + eip, err := self.GetEipOrPublicIp() if err != nil { result.Error(fmt.Errorf("getEip error %s", err)) return result @@ -4878,7 +4882,7 @@ func (self *SGuest) DetachScheduledTask(ctx context.Context, userCred mcclient.T } func (self *SGuest) DeleteEip(ctx context.Context, userCred mcclient.TokenCredential) error { - eip, err := self.GetEip() + eip, err := self.GetEipOrPublicIp() if err != nil { log.Errorf("Delete eip fail for get Eip %s", err) return err @@ -5180,7 +5184,7 @@ func (self *SGuest) toCreateInput() *api.ServerCreateInput { if host := self.GetHost(); host != nil { r.ResourceType = host.ResourceType } - if eip, _ := self.GetEip(); eip != nil && eip.Mode == api.EIP_MODE_STANDALONE_EIP { + if eip, _ := self.GetEipOrPublicIp(); eip != nil && eip.Mode == api.EIP_MODE_STANDALONE_EIP { r.EipBw = eip.Bandwidth r.EipChargeType = eip.ChargeType } @@ -5354,7 +5358,7 @@ func (self *SGuest) getGuestUsage(guestCount int) (SQuota, SRegionQuota, error) } regionUsage.Port = netCount // regionUsage.Bw = self.getBandwidth(false) - eip, err := self.GetEip() + eip, err := self.GetEipOrPublicIp() if err != nil && errors.Cause(err) != sql.ErrNoRows { return usage, regionUsage, err } diff --git a/pkg/compute/tasks/guest_delete_task.go b/pkg/compute/tasks/guest_delete_task.go index 3db92add35..66a0fd165e 100644 --- a/pkg/compute/tasks/guest_delete_task.go +++ b/pkg/compute/tasks/guest_delete_task.go @@ -118,7 +118,7 @@ func (self *GuestDeleteTask) OnStartEipDissociate(ctx context.Context, guest *mo sourceGuest.StartSyncstatus(ctx, self.UserCred, "") } } - eip, _ := guest.GetEip() + eip, _ := guest.GetEipOrPublicIp() if eip != nil && eip.Mode != api.EIP_MODE_INSTANCE_PUBLICIP { // detach floating EIP only if jsonutils.QueryBoolean(self.Params, "purge", false) {