diff --git a/pkg/compute/models/secgroups.go b/pkg/compute/models/secgroups.go index 6cd50d0736..616659f9b9 100644 --- a/pkg/compute/models/secgroups.go +++ b/pkg/compute/models/secgroups.go @@ -201,7 +201,7 @@ func (manager *SSecurityGroupManager) getSecurityGroups() ([]SSecurityGroup, err } } -func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCred mcclient.TokenCredential, secgroups []cloudprovider.ICloudSecurityGroup) ([]SSecurityGroup, []cloudprovider.ICloudSecurityGroup, compare.SyncResult) { +func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCred mcclient.TokenCredential, secgroups []cloudprovider.ICloudSecurityGroup, projectId string, projectSync bool) ([]SSecurityGroup, []cloudprovider.ICloudSecurityGroup, compare.SyncResult) { localSecgroups := make([]SSecurityGroup, 0) remoteSecgroups := make([]cloudprovider.ICloudSecurityGroup, 0) syncResult := compare.SyncResult{} @@ -223,7 +223,7 @@ func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCre if rules, err := commonext[i].GetRules(); err != nil { syncResult.Error(err) } else if len(rules) > 0 { - if err = commondb[i].SyncWithCloudSecurityGroup(userCred, commonext[i]); err != nil { + if err = commondb[i].SyncWithCloudSecurityGroup(userCred, commonext[i], projectId, projectSync); err != nil { syncResult.UpdateError(err) } else { localSecgroups = append(localSecgroups, commondb[i]) @@ -244,7 +244,7 @@ func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCre if rules, err := added[i].GetRules(); err != nil { syncResult.AddError(err) } else if len(rules) > 0 { - if new, err := manager.newFromCloudVpc(userCred, added[i]); err != nil { + if new, err := manager.newFromCloudVpc(userCred, added[i], projectId); err != nil { syncResult.AddError(err) } else if len(rules) > 0 { localSecgroups = append(localSecgroups, *new) @@ -258,12 +258,15 @@ func (manager *SSecurityGroupManager) SyncSecgroups(ctx context.Context, userCre return localSecgroups, remoteSecgroups, syncResult } -func (self *SSecurityGroup) SyncWithCloudSecurityGroup(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup) error { +func (self *SSecurityGroup) SyncWithCloudSecurityGroup(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup, projectId string, projectSync bool) error { if _, err := self.GetModelManager().TableSpec().Update(self, func() error { extSec.Refresh() self.Name = extSec.GetName() self.Description = extSec.GetDescription() self.ProjectId = userCred.GetProjectId() + if projectSync && len(projectId) > 0 { + self.ProjectId = projectId + } return nil }); err != nil { log.Errorf("syncWithCloudSecurityGroup error %s", err) @@ -272,13 +275,16 @@ func (self *SSecurityGroup) SyncWithCloudSecurityGroup(userCred mcclient.TokenCr return nil } -func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup) (*SSecurityGroup, error) { +func (manager *SSecurityGroupManager) newFromCloudVpc(userCred mcclient.TokenCredential, extSec cloudprovider.ICloudSecurityGroup, projectId string) (*SSecurityGroup, error) { secgroup := SSecurityGroup{} secgroup.SetModelManager(manager) secgroup.Name = extSec.GetName() secgroup.ExternalId = extSec.GetGlobalId() secgroup.Description = extSec.GetDescription() secgroup.ProjectId = userCred.GetProjectId() + if len(projectId) > 0 { + secgroup.ProjectId = projectId + } if err := manager.TableSpec().Insert(&secgroup); err != nil { return nil, err diff --git a/pkg/compute/tasks/cloud_provider_sync_info_task.go b/pkg/compute/tasks/cloud_provider_sync_info_task.go index 9295147fa7..18e75d4d03 100644 --- a/pkg/compute/tasks/cloud_provider_sync_info_task.go +++ b/pkg/compute/tasks/cloud_provider_sync_info_task.go @@ -208,18 +208,18 @@ func syncRegionVPCs(ctx context.Context, provider *models.SCloudprovider, task * logclient.AddActionLog(provider, getAction(task.Params), notes, task.UserCred, true) for j := 0; j < len(localVpcs); j += 1 { syncVpcWires(ctx, provider, task, &localVpcs[j], remoteVpcs[j], syncRange) - syncVpcSecGroup(ctx, provider, task, &localVpcs[j], remoteVpcs[j]) + syncVpcSecGroup(ctx, provider, task, &localVpcs[j], remoteVpcs[j], syncRange) } } -func syncVpcSecGroup(ctx context.Context, provider *models.SCloudprovider, task *CloudProviderSyncInfoTask, localVpc *models.SVpc, remoteVpc cloudprovider.ICloudVpc) { +func syncVpcSecGroup(ctx context.Context, provider *models.SCloudprovider, task *CloudProviderSyncInfoTask, localVpc *models.SVpc, remoteVpc cloudprovider.ICloudVpc, syncRange *models.SSyncRange) { if secgroups, err := remoteVpc.GetISecurityGroups(); err != nil { msg := fmt.Sprintf("GetISecurityGroups for vpc %s failed %s", remoteVpc.GetId(), err) log.Errorf(msg) logSyncFailed(provider, task, msg) return } else { - _, _, result := models.SecurityGroupManager.SyncSecgroups(ctx, task.UserCred, secgroups) + _, _, result := models.SecurityGroupManager.SyncSecgroups(ctx, task.UserCred, secgroups, provider.ProjectId, syncRange.ProjectSync) msg := result.Result() notes := fmt.Sprintf("SyncSecurityGroup for VPC %s result: %s", localVpc.Name, msg) log.Infof(notes) diff --git a/pkg/scheduler/manager/task_queue.go b/pkg/scheduler/manager/task_queue.go index 0a413c8703..0fffd0268d 100644 --- a/pkg/scheduler/manager/task_queue.go +++ b/pkg/scheduler/manager/task_queue.go @@ -200,8 +200,8 @@ type TaskManager struct { func NewTaskManager(stopCh <-chan struct{}) *TaskManager { return &TaskManager{ taskExecutorQueueManager: NewTaskExecutorQueueManager(stopCh), - stopCh: stopCh, - lock: sync.Mutex{}, + stopCh: stopCh, + lock: sync.Mutex{}, } }