From f36286f01f98cd1e0bf90e6ae2745ec24557579c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Tue, 21 May 2024 15:36:36 +0800 Subject: [PATCH] fix(region): sync public ip convert eip (#20326) --- pkg/compute/models/capabilities.go | 2 - pkg/compute/models/elasticips.go | 21 ++++- pkg/compute/models/guests.go | 105 +++++++++++----------- pkg/compute/models/storagecachedimages.go | 2 - 4 files changed, 73 insertions(+), 57 deletions(-) diff --git a/pkg/compute/models/capabilities.go b/pkg/compute/models/capabilities.go index 8bf7ab11c9..2c644060f7 100644 --- a/pkg/compute/models/capabilities.go +++ b/pkg/compute/models/capabilities.go @@ -761,8 +761,6 @@ func getStorageTypes( info := []StorageInfo{} - q.DebugQuery() - err := q.All(&info) if err != nil { return nil, errors.Wrapf(err, "q.All") diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 121a7f2999..512ef8bb37 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -441,7 +441,26 @@ func (manager *SElasticipManager) SyncEips( } } for i := 0; i < len(added); i += 1 { - _, err := manager.newFromCloudEip(ctx, userCred, added[i], provider, region, syncOwnerId) + eip := &SElasticip{} + eip.SetModelManager(ElasticipManager, eip) + err := ElasticipManager.Query(). + Equals("ip_addr", added[i].GetIpAddr()). + Equals("manager_id", provider.Id). + Equals("cloudregion_id", region.Id). + Equals("mode", api.EIP_MODE_INSTANCE_PUBLICIP).First(eip) + + // 公网IP转弹性IP + if err == nil { + err = eip.SyncWithCloudEip(ctx, userCred, provider, added[i], syncOwnerId) + if err != nil { + syncResult.UpdateError(err) + continue + } + syncResult.Update() + continue + } + + _, err = manager.newFromCloudEip(ctx, userCred, added[i], provider, region, syncOwnerId) if err != nil { syncResult.AddError(err) } else { diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 68b957aaeb..642ec973ab 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -5953,75 +5953,76 @@ 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.GetPublicIp() + eip, err := self.GetEipOrPublicIp() if err != nil { - result.Error(fmt.Errorf("getPublicIp error %s", err)) + result.Error(fmt.Errorf("GetEipOrPublicIp error %s", err)) return result - } else if eip == nil { - eip, err = self.GetElasticIp() - if err != nil { - result.Error(fmt.Errorf("getEip error %s", err)) - return result - } } - region, _ := self.getRegion() + region, err := self.getRegion() + if err != nil { + result.Error(fmt.Errorf("getRegion error %s", err)) + return result + } if eip == nil && extEip == nil { // do nothing - } else if eip == nil && extEip != nil { + return result + } + if eip == nil && extEip != nil { // add neip, err := ElasticipManager.getEipByExtEip(ctx, userCred, extEip, provider, region, syncOwnerId) if err != nil { result.AddError(errors.Wrapf(err, "getEipByExtEip")) - } else { - err = neip.AssociateInstance(ctx, userCred, api.EIP_ASSOCIATE_TYPE_SERVER, self) - if err != nil { - result.AddError(errors.Wrapf(err, "neip.AssociateInstance")) - } else { - result.Add() - } + return result } - } else if eip != nil && extEip == nil { + err = neip.AssociateInstance(ctx, userCred, api.EIP_ASSOCIATE_TYPE_SERVER, self) + if err != nil { + result.AddError(errors.Wrapf(err, "neip.AssociateInstance")) + return result + } + result.Add() + return result + } + if eip != nil && extEip == nil { // remove err = eip.Dissociate(ctx, userCred) if err != nil { result.DeleteError(err) - } else { - result.Delete() - } - } else { - // sync - if eip.IpAddr != extEip.GetIpAddr() { - // remove then add - err = eip.Dissociate(ctx, userCred) - if err != nil { - // fail to remove - result.DeleteError(err) - } else { - result.Delete() - neip, err := ElasticipManager.getEipByExtEip(ctx, userCred, extEip, provider, region, syncOwnerId) - if err != nil { - result.AddError(err) - } else { - err = neip.AssociateInstance(ctx, userCred, api.EIP_ASSOCIATE_TYPE_SERVER, self) - if err != nil { - result.AddError(err) - } else { - result.Add() - } - } - } - } else { - // do nothing - err := eip.SyncWithCloudEip(ctx, userCred, provider, extEip, syncOwnerId) - if err != nil { - result.UpdateError(err) - } else { - result.Update() - } + return result } + result.Delete() + return result + } + // sync + if eip.IpAddr != extEip.GetIpAddr() { + // remove then add + err = eip.Dissociate(ctx, userCred) + if err != nil { + // fail to remove + result.DeleteError(err) + return result + } + result.Delete() + neip, err := ElasticipManager.getEipByExtEip(ctx, userCred, extEip, provider, region, syncOwnerId) + if err != nil { + result.AddError(err) + return result + } + err = neip.AssociateInstance(ctx, userCred, api.EIP_ASSOCIATE_TYPE_SERVER, self) + if err != nil { + result.AddError(err) + } else { + result.Add() + } + return result + } + // do nothing + err = eip.SyncWithCloudEip(ctx, userCred, provider, extEip, syncOwnerId) + if err != nil { + result.UpdateError(err) + } else { + result.Update() } - return result } diff --git a/pkg/compute/models/storagecachedimages.go b/pkg/compute/models/storagecachedimages.go index 68ed681df9..ad4c824eba 100644 --- a/pkg/compute/models/storagecachedimages.go +++ b/pkg/compute/models/storagecachedimages.go @@ -249,8 +249,6 @@ func (maanger *SStoragecachedimageManager) fetchRefCount(q *sqlchemy.SQuery) (ma StoragecacheId string `json:"storagecache_id"` }{} - q.DebugQuery() - err := q.All(&results) if err != nil { return nil, errors.Wrap(err, "Query")