mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #746 in YUNIONIO/onecloud from ~QUXUAN/onecloud:hotfix/qx-conflict-resolve-2.3.0 to release/2.3.0
* commit '064d082ef27a6920307691d28e46a67916703700': 避免同步cache中的安全组
This commit is contained in:
@@ -123,6 +123,23 @@ func (manager *SSecurityGroupCacheManager) GetSecgroupCache(ctx context.Context,
|
||||
return &secgroupCache
|
||||
}
|
||||
|
||||
func (manager *SSecurityGroupCacheManager) CheckExist(ctx context.Context, userCred mcclient.TokenCredential, externalId, vpcId, regionId string, providerId string) (*SSecurityGroup, bool) {
|
||||
secgroupCaches := []SSecurityGroupCache{}
|
||||
query := manager.Query()
|
||||
cond := sqlchemy.AND(sqlchemy.Equals(query.Field("external_id"), externalId), sqlchemy.Equals(query.Field("vpc_id"), vpcId), sqlchemy.Equals(query.Field("cloudregion_id"), regionId), sqlchemy.Equals(query.Field("manager_id"), providerId))
|
||||
query = query.Filter(cond)
|
||||
|
||||
if err := query.All(&secgroupCaches); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
for _, secgroupCache := range secgroupCaches {
|
||||
if secgroup, err := SecurityGroupManager.FetchById(secgroupCache.SecgroupId); err == nil {
|
||||
return secgroup.(*SSecurityGroup), true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (manager *SSecurityGroupCacheManager) Register(ctx context.Context, userCred mcclient.TokenCredential, secgroupId, vpcId, regionId string, providerId string) *SSecurityGroupCache {
|
||||
lockman.LockClass(ctx, manager, userCred.GetProjectId())
|
||||
defer lockman.ReleaseClass(ctx, manager, userCred.GetProjectId())
|
||||
|
||||
@@ -228,7 +228,12 @@ func (self *SSecurityGroup) SyncWithCloudSecurityGroup(userCred mcclient.TokenCr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup, vpc *SVpc, projectId string) (*SSecurityGroup, error) {
|
||||
func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup, vpc *SVpc, projectId string) (*SSecurityGroup, bool, error) {
|
||||
if secgroup, exist := SecurityGroupCacheManager.CheckExist(context.Background(), userCred, extSec.GetGlobalId(), extSec.GetVpcId(), vpc.CloudregionId, vpc.ManagerId); exist {
|
||||
//避免重复同步
|
||||
return secgroup, true, nil
|
||||
}
|
||||
|
||||
secgroup := SSecurityGroup{}
|
||||
secgroup.SetModelManager(manager)
|
||||
secgroup.Name = extSec.GetName()
|
||||
@@ -240,7 +245,7 @@ func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCre
|
||||
}
|
||||
|
||||
if err := manager.TableSpec().Insert(&secgroup); err != nil {
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if secgroupcache := SecurityGroupCacheManager.Register(context.Background(), userCred, secgroup.Id, extSec.GetVpcId(), vpc.CloudregionId, vpc.ManagerId); secgroupcache != nil {
|
||||
@@ -249,7 +254,7 @@ func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCre
|
||||
}
|
||||
}
|
||||
|
||||
return &secgroup, nil
|
||||
return &secgroup, false, nil
|
||||
}
|
||||
|
||||
func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCred mcclient.TokenCredential, secgroups []cloudprovider.ICloudSecurityGroup, vpc *SVpc, projectId string, projectSync bool) ([]SSecurityGroup, []cloudprovider.ICloudSecurityGroup, compare.SyncResult) {
|
||||
@@ -293,11 +298,14 @@ func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCre
|
||||
syncResult.AddError(err)
|
||||
continue
|
||||
}
|
||||
new, err := manager.newFromCloudVpc(userCred, added[i], vpc, projectId)
|
||||
new, exist, err := manager.newFromCloudVpc(userCred, added[i], vpc, projectId)
|
||||
if err != nil {
|
||||
syncResult.AddError(err)
|
||||
continue
|
||||
}
|
||||
if exist {
|
||||
continue
|
||||
}
|
||||
localSecgroups = append(localSecgroups, *new)
|
||||
remoteSecgroups = append(remoteSecgroups, added[i])
|
||||
SecurityGroupRuleManager.SyncRules(ctx, userCred, new, rules)
|
||||
|
||||
Reference in New Issue
Block a user