From dfbd89c9dbcff44f792a08e2ddd9ccd6860b6221 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Fri, 7 Nov 2025 10:53:04 +0800 Subject: [PATCH] fix: vm nic charge type support (#23703) Co-authored-by: Qiu Jian --- pkg/apis/compute/api.go | 2 ++ pkg/apis/compute/guestnetwork.go | 14 +++++++++--- pkg/cloudcommon/cmdline/parser.go | 2 ++ pkg/compute/guestdrivers/baremetals.go | 2 ++ .../guestdrivers/cloudpods-baremetals.go | 2 ++ pkg/compute/guestdrivers/virtualization.go | 2 ++ pkg/compute/models/guestnetworks.go | 13 ++++++++++- pkg/compute/models/guests.go | 22 +++++++++---------- 8 files changed, 43 insertions(+), 16 deletions(-) diff --git a/pkg/apis/compute/api.go b/pkg/apis/compute/api.go index ab83c70083..6378757162 100644 --- a/pkg/apis/compute/api.go +++ b/pkg/apis/compute/api.go @@ -121,6 +121,8 @@ type NetworkConfig struct { PortMappings GuestPortMappings `json:"port_mappings"` + ChargeType string `json:"charge_type"` + // swagger:ignore Project string `json:"project_id"` diff --git a/pkg/apis/compute/guestnetwork.go b/pkg/apis/compute/guestnetwork.go index 006858b5cc..71683b408d 100644 --- a/pkg/apis/compute/guestnetwork.go +++ b/pkg/apis/compute/guestnetwork.go @@ -50,7 +50,6 @@ type GuestnetworkShortDesc struct { // IP地址 IpAddr string `json:"ip_addr"` // 是否为外网网卡 - // Deprecated IsExit bool `json:"is_exit"` // IPv6地址 Ip6Addr string `json:"ip6_addr"` @@ -66,8 +65,14 @@ type GuestnetworkShortDesc struct { SubIps string `json:"sub_ips"` // 端口映射 PortMappings GuestPortMappings `json:"port_mappings"` - + // Bandwidth + BwLimitMbps int `json:"bw_limit_mbps"` + // 网卡名称 + Ifname string `json:"ifname"` + // 是否为缺省路由网关 IsDefault bool `json:"is_default"` + // 计费模式 + ChargeType string `json:"charge_type"` } type GuestnetworkListInput struct { @@ -97,8 +102,11 @@ type GuestnetworkUpdateInput struct { Index *int8 `json:"index"` - IsDefault *bool `json:"is_default"` + IsDefault *bool `json:"is_default"` + PortMappings GuestPortMappings `json:"port_mappings"` + + ChargeType string `json:"charge_type"` } type GuestnetworkBaseDesc struct { diff --git a/pkg/cloudcommon/cmdline/parser.go b/pkg/cloudcommon/cmdline/parser.go index 190205ddd5..6ccaf95e84 100644 --- a/pkg/cloudcommon/cmdline/parser.go +++ b/pkg/cloudcommon/cmdline/parser.go @@ -357,6 +357,8 @@ func ParseNetworkConfig(desc string, idx int) (*compute.NetworkConfig, error) { netConfig.Driver = p } else if strings.HasPrefix(p, "num-queues=") { netConfig.NumQueues, _ = strconv.Atoi(p[len("num-queues="):]) + } else if strings.HasPrefix(p, "charge-type=") { + netConfig.ChargeType = p[len("charge-type="):] } else if regutils.MatchSize(p) { bw, err := fileutils.GetSizeMb(p, 'M', 1000) if err != nil { diff --git a/pkg/compute/guestdrivers/baremetals.go b/pkg/compute/guestdrivers/baremetals.go index 9ce22e84c3..04fc6568e3 100644 --- a/pkg/compute/guestdrivers/baremetals.go +++ b/pkg/compute/guestdrivers/baremetals.go @@ -273,6 +273,8 @@ func (self *SBaremetalGuestDriver) Attach2RandomNetwork(guest *models.SGuest, ct NicConfs: nicConfs, IsDefault: netConfig.IsDefault, + + ChargeType: netConfig.ChargeType, }) } return nil, fmt.Errorf("No appropriate host virtual network...") diff --git a/pkg/compute/guestdrivers/cloudpods-baremetals.go b/pkg/compute/guestdrivers/cloudpods-baremetals.go index f840f3aefc..449d57e38d 100644 --- a/pkg/compute/guestdrivers/cloudpods-baremetals.go +++ b/pkg/compute/guestdrivers/cloudpods-baremetals.go @@ -273,6 +273,8 @@ func (self *SCloudpodsBaremetalGuestDriver) Attach2RandomNetwork(guest *models.S NicConfs: nicConfs, IsDefault: netConfig.IsDefault, + + ChargeType: netConfig.ChargeType, }) } return nil, fmt.Errorf("No appropriate host virtual network...") diff --git a/pkg/compute/guestdrivers/virtualization.go b/pkg/compute/guestdrivers/virtualization.go index 407c6db0a6..01d87cdc3a 100644 --- a/pkg/compute/guestdrivers/virtualization.go +++ b/pkg/compute/guestdrivers/virtualization.go @@ -213,6 +213,8 @@ func (self *SVirtualizedGuestDriver) Attach2RandomNetwork(guest *models.SGuest, NicConfs: nicConfs, IsDefault: netConfig.IsDefault, + + ChargeType: netConfig.ChargeType, }) return gn, err } diff --git a/pkg/compute/models/guestnetworks.go b/pkg/compute/models/guestnetworks.go index 969772cc3d..7ebfa80158 100644 --- a/pkg/compute/models/guestnetworks.go +++ b/pkg/compute/models/guestnetworks.go @@ -123,6 +123,10 @@ type SGuestnetwork struct { // 端口映射 PortMappings api.GuestPortMappings `length:"long" list:"user" update:"user"` + + // 计费类型: 流量、带宽 + // example: bandwidth + ChargeType string `width:"64" name:"charge_type" default:"bandwidth" list:"user" create:"optional"` } func (gn SGuestnetwork) GetIP() string { @@ -275,6 +279,8 @@ type newGuestNetworkArgs struct { virtual bool portMappings api.GuestPortMappings + + chargeType string } func (manager *SGuestnetworkManager) newGuestNetwork( @@ -323,6 +329,7 @@ func (manager *SGuestnetworkManager) newGuestNetwork( gn.BwLimit = bwLimit } gn.PortMappings = args.portMappings + gn.ChargeType = args.chargeType lockman.LockObject(ctx, network) defer lockman.ReleaseObject(ctx, network) @@ -978,7 +985,7 @@ func (gn *SGuestnetwork) LogDetachEvent(ctx context.Context, userCred mcclient.T } network = netTmp.(*SNetwork) } - db.OpsLog.LogDetachEvent(ctx, guest, network, userCred, nil) + db.OpsLog.LogDetachEvent(ctx, guest, network, userCred, gn.GetShortDesc(ctx)) } func (gn *SGuestnetwork) Delete(ctx context.Context, userCred mcclient.TokenCredential) error { @@ -1276,6 +1283,10 @@ func (gn *SGuestnetwork) GetShortDesc(ctx context.Context) *jsonutils.JSONDict { if len(gn.TeamWith) > 0 { desc.TeamWith = gn.TeamWith } + desc.BwLimitMbps = gn.getBandwidth() + desc.Ifname = gn.Ifname + desc.IsDefault = gn.IsDefault + desc.ChargeType = gn.ChargeType return jsonutils.Marshal(desc).(*jsonutils.JSONDict) } diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 4e2455de19..fe4b600d6a 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -3620,6 +3620,8 @@ type Attach2NetworkArgs struct { IsDefault bool PortMappings api.GuestPortMappings + ChargeType string + PendingUsage quotas.IQuota } @@ -3652,6 +3654,8 @@ func (args *Attach2NetworkArgs) onceArgs(i int) attach2NetworkOnceArgs { pendingUsage: args.PendingUsage, portMappings: args.PortMappings, + + chargeType: args.ChargeType, } if i > 0 { r.ipAddr = "" @@ -3696,6 +3700,8 @@ type attach2NetworkOnceArgs struct { pendingUsage quotas.IQuota portMappings api.GuestPortMappings + + chargeType string } func (self *SGuest) Attach2Network( @@ -3774,6 +3780,8 @@ func (self *SGuest) attach2NetworkOnce( isDefault: args.isDefault, portMappings: args.portMappings, + + chargeType: args.chargeType, } lockman.LockClass(ctx, QuotaManager, self.ProjectId) defer lockman.ReleaseClass(ctx, QuotaManager, self.ProjectId) @@ -3808,18 +3816,6 @@ func (self *SGuest) attach2NetworkOnce( return guestnic, nil } -type sRemoveGuestnic struct { - nic *SGuestnetwork - reserve bool -} - -type sAddGuestnic struct { - index int - nic cloudprovider.ICloudNic - net *SNetwork - reserve bool -} - func getCloudNicNetwork(ctx context.Context, vnic cloudprovider.ICloudNic, host *SHost, ipList []string, index int) (*SNetwork, error) { vnetId := vnic.GetINetworkId() if len(vnetId) == 0 { @@ -4563,6 +4559,8 @@ func (self *SGuest) attach2NamedNetworkDesc(ctx context.Context, userCred mcclie IsDefault: netConfig.IsDefault, PortMappings: netConfig.PortMappings, + + ChargeType: netConfig.ChargeType, }) if err != nil { return nil, errors.Wrap(err, "Attach2Network fail")