From 90fd8c868f78a415a41a23880385d633993ea49e Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Mon, 22 Mar 2021 18:42:56 +0800 Subject: [PATCH] region: attachnetwork: allow attach by network name isValidNetworkInfo allows names. Otherwise the code will panic when a named was passed and nil was returned by the fetch call --- pkg/compute/models/guest_actions.go | 2 +- pkg/compute/models/guests.go | 2 +- pkg/compute/models/networks.go | 4 ++-- pkg/scheduler/algorithm/predicates/quota_predicate.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index 44da8ef1e5..b91bde09f1 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -2259,7 +2259,7 @@ func (self *SGuest) PerformAttachnetwork(ctx context.Context, userCred mcclient. if err != nil { return nil, err } - if IsExitNetworkInfo(input.Nets[i]) { + if IsExitNetworkInfo(userCred, input.Nets[i]) { enicCnt = count // ebw = input.BwLimit } else { diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 657fae5c74..7a63ee5c96 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -1753,7 +1753,7 @@ func getGuestResourceRequirements( eBw := 0 iBw := 0 for _, netConfig := range input.Networks { - if IsExitNetworkInfo(netConfig) { + if IsExitNetworkInfo(userCred, netConfig) { eNicCnt += 1 eBw += netConfig.BwLimit } else { diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index 907cddabc8..5ca523c7c5 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -1052,9 +1052,9 @@ func isValidNetworkInfo(userCred mcclient.TokenCredential, netConfig *api.Networ return nil } -func IsExitNetworkInfo(netConfig *api.NetworkConfig) bool { +func IsExitNetworkInfo(userCred mcclient.TokenCredential, netConfig *api.NetworkConfig) bool { if len(netConfig.Network) > 0 { - netObj, _ := NetworkManager.FetchById(netConfig.Network) + netObj, _ := NetworkManager.FetchByIdOrName(userCred, netConfig.Network) net := netObj.(*SNetwork) if net.IsExitNetwork() { return true diff --git a/pkg/scheduler/algorithm/predicates/quota_predicate.go b/pkg/scheduler/algorithm/predicates/quota_predicate.go index a9be32fe6d..992712c6c8 100644 --- a/pkg/scheduler/algorithm/predicates/quota_predicate.go +++ b/pkg/scheduler/algorithm/predicates/quota_predicate.go @@ -59,7 +59,7 @@ func fetchGuestUsageFromSchedInfo(s *api.SchedInfo) (computemodels.SQuota, compu iNicCnt := 0 for _, netConfig := range s.Networks { - if computemodels.IsExitNetworkInfo(netConfig) { + if computemodels.IsExitNetworkInfo(s.UserCred, netConfig) { eNicCnt += 1 } else { iNicCnt += 1