Skip to content

Commit 77d5813

Browse files
swordqiuQiu Jian
andauthored
fix: log change bandwidth event (#23712)
Co-authored-by: Qiu Jian <[email protected]>
1 parent 85a570f commit 77d5813

File tree

7 files changed

+83
-19
lines changed

7 files changed

+83
-19
lines changed

pkg/apis/compute/guestnetwork.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type GuestnetworkShortDesc struct {
5151
IpAddr string `json:"ip_addr"`
5252
// 是否为外网网卡
5353
IsExit bool `json:"is_exit"`
54+
// 网卡类型
55+
// 可能值:exit | internal | unused
56+
NicType string `json:"nic_type"`
5457
// IPv6地址
5558
Ip6Addr string `json:"ip6_addr"`
5659
// Mac地址
@@ -59,6 +62,8 @@ type GuestnetworkShortDesc struct {
5962
TeamWith string `json:"team_with"`
6063
// 所属Vpc
6164
VpcId string `json:"vpc_id"`
65+
// 所属主机
66+
GuestId string `json:"guest_id"`
6267
// 所属Network
6368
NetworkId string `json:"network_id"`
6469
// 附属IP

pkg/apis/compute/networkaddress.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,20 @@ var NetworkAddressTypes = choices.NewChoices(
2727
NetworkAddressTypeSubIP,
2828
)
2929

30+
type TNetworkAddressParentType string
31+
3032
const (
31-
NetworkAddressParentTypeGuestnetwork = "guestnetwork"
33+
NetworkAddressParentTypeGuestnetwork = TNetworkAddressParentType("guestnetwork")
3234
)
3335

3436
var NetworkAddressParentTypes = choices.NewChoices(
35-
NetworkAddressParentTypeGuestnetwork,
37+
string(NetworkAddressParentTypeGuestnetwork),
3638
)
3739

3840
type NetworkAddressCreateInput struct {
3941
apis.StandaloneAnonResourceCreateInput
4042

41-
ParentType string
43+
ParentType TNetworkAddressParentType
4244
ParentId int64
4345
GuestId string
4446
GuestnetworkIndex int8

pkg/cloudcommon/db/opslog_const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ const (
252252
ACT_GUEST_CPUSET_REMOVE = "guest_cpuset_remove"
253253
ACT_GUEST_CPUSET_REMOVE_FAIL = "guest_cpuset_remove_fail"
254254

255+
ACT_CHANGE_IPADDR = "change_ipaddr"
255256
ACT_CHANGE_BANDWIDTH = "eip_change_bandwidth"
256257
ACT_EIP_CONVERT_FAIL = "eip_convert_fail"
257258

pkg/compute/models/guest_actions.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,11 +2942,14 @@ func (self *SGuest) PerformChangeIpaddr(
29422942

29432943
notes := gn.GetShortDesc(ctx)
29442944
if gn != nil {
2945-
notes.Add(jsonutils.NewString(gn.IpAddr), "prev_ip")
2946-
}
2947-
if ngn != nil {
2948-
notes.Add(jsonutils.NewString(ngn.IpAddr), "ip")
2945+
if gn.IpAddr != ngn.IpAddr {
2946+
notes.Add(jsonutils.NewString(gn.IpAddr), "prev_ip")
2947+
}
2948+
if gn.Ip6Addr != ngn.Ip6Addr {
2949+
notes.Add(jsonutils.NewString(gn.Ip6Addr), "prev_ip6")
2950+
}
29492951
}
2952+
db.OpsLog.LogEvent(self, db.ACT_CHANGE_IPADDR, notes, userCred)
29502953
logclient.AddActionLogWithContext(ctx, self, logclient.ACT_VM_CHANGE_NIC, notes, userCred, true)
29512954

29522955
restartNetwork := (input.RestartNetwork != nil && *input.RestartNetwork)
@@ -3328,15 +3331,18 @@ func (guest *SGuest) PerformChangeBandwidth(
33283331
}
33293332

33303333
if guestnic.BwLimit != int(bandwidth) {
3331-
diff, err := db.Update(guestnic, func() error {
3334+
oldBw := guestnic.BwLimit
3335+
_, err := db.Update(guestnic, func() error {
33323336
guestnic.BwLimit = int(bandwidth)
33333337
return nil
33343338
})
33353339
if err != nil {
33363340
return nil, err
33373341
}
3338-
db.OpsLog.LogEvent(guest, db.ACT_CHANGE_BANDWIDTH, diff, userCred)
3339-
logclient.AddActionLogWithContext(ctx, guest, logclient.ACT_VM_CHANGE_BANDWIDTH, diff, userCred, true)
3342+
eventDesc := guestnic.GetShortDesc(ctx)
3343+
eventDesc.Add(jsonutils.NewInt(int64(oldBw)), "old_bw_limit_mbps")
3344+
db.OpsLog.LogEvent(guest, db.ACT_CHANGE_BANDWIDTH, eventDesc, userCred)
3345+
logclient.AddActionLogWithContext(ctx, guest, logclient.ACT_VM_CHANGE_BANDWIDTH, eventDesc, userCred, true)
33403346
if guest.Status == api.VM_READY || (input.NoSync != nil && *input.NoSync) {
33413347
// if no sync, just update db
33423348
return nil, nil

pkg/compute/models/guest_queries.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,7 @@ func fetchGuestNICs(ctx context.Context, guestIds []string, virtual tristate.Tri
560560
netq := NetworkManager.Query().SubQuery()
561561
wirq := WireManager.Query().SubQuery()
562562

563-
subIPQ := NetworkAddressManager.Query("parent_id").Equals("parent_type", api.NetworkAddressParentTypeGuestnetwork)
564-
subIPQ = subIPQ.AppendField(sqlchemy.GROUP_CONCAT("sub_ips", subIPQ.Field("ip_addr")))
565-
subIPQ = subIPQ.GroupBy(subIPQ.Field("parent_id"))
563+
subIPQ := NetworkAddressManager.fetchSubIpsQuery(api.NetworkAddressParentTypeGuestnetwork)
566564
subIP := subIPQ.SubQuery()
567565

568566
gnwq := GuestnetworkManager.Query()

pkg/compute/models/guestnetworks.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,13 @@ func (gn *SGuestnetwork) GetShortDesc(ctx context.Context) *jsonutils.JSONDict {
12751275
if len(gn.IpAddr) > 0 {
12761276
desc.IpAddr = gn.IpAddr
12771277
desc.IsExit = gn.IsExit()
1278+
if desc.IsExit {
1279+
desc.NicType = "exit"
1280+
} else {
1281+
desc.NicType = "internal"
1282+
}
1283+
} else {
1284+
desc.NicType = "unused"
12781285
}
12791286
if len(gn.Ip6Addr) > 0 {
12801287
desc.Ip6Addr = gn.Ip6Addr
@@ -1287,9 +1294,33 @@ func (gn *SGuestnetwork) GetShortDesc(ctx context.Context) *jsonutils.JSONDict {
12871294
desc.Ifname = gn.Ifname
12881295
desc.IsDefault = gn.IsDefault
12891296
desc.ChargeType = gn.ChargeType
1297+
desc.GuestId = gn.GuestId
1298+
desc.NetworkId = gn.NetworkId
1299+
wire, _ := gn.GetWire()
1300+
if wire != nil {
1301+
desc.VpcId = wire.VpcId
1302+
}
1303+
desc.PortMappings = gn.PortMappings
1304+
desc.SubIps = gn.GetSubIps()
12901305
return jsonutils.Marshal(desc).(*jsonutils.JSONDict)
12911306
}
12921307

1308+
func (gn *SGuestnetwork) GetWire() (*SWire, error) {
1309+
net, err := gn.GetNetwork()
1310+
if err != nil {
1311+
return nil, errors.Wrap(err, "GetNetwork")
1312+
}
1313+
return net.GetWire()
1314+
}
1315+
1316+
func (gn *SGuestnetwork) GetVpc() (*SVpc, error) {
1317+
net, err := gn.GetNetwork()
1318+
if err != nil {
1319+
return nil, errors.Wrap(err, "GetNetwork")
1320+
}
1321+
return net.GetVpc()
1322+
}
1323+
12931324
func (gn *SGuestnetwork) ToNetworkConfig() *api.NetworkConfig {
12941325
net, err := gn.GetNetwork()
12951326
if err != nil {
@@ -1464,3 +1495,17 @@ func (guest *SGuest) IsStrictIpv6() (bool, error) {
14641495
}
14651496
return cnt > 0, nil
14661497
}
1498+
1499+
func (gn *SGuestnetwork) GetSubIps() string {
1500+
subIPQ := NetworkAddressManager.fetchSubIpsQuery(api.NetworkAddressParentTypeGuestnetwork)
1501+
subIPQ = subIPQ.Equals("parent_id", gn.RowId)
1502+
result := struct {
1503+
SubIps string `json:"sub_ips"`
1504+
}{}
1505+
err := subIPQ.First(&result)
1506+
if err != nil {
1507+
log.Errorf("Guestnetwork %s/%s/%s GetSubIps fail %s", gn.GuestId, gn.NetworkId, gn.MacAddr, err)
1508+
return ""
1509+
}
1510+
return result.SubIps
1511+
}

pkg/compute/models/networkaddresses.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ func init() {
6464
type SNetworkAddress struct {
6565
db.SStandaloneAnonResourceBase
6666

67-
Type string `width:"16" charset:"ascii" list:"user" create:"required"`
68-
ParentType string `width:"16" charset:"ascii" list:"user" create:"required"`
69-
ParentId string `width:"16" charset:"ascii" list:"user" create:"optional"`
67+
Type string `width:"16" charset:"ascii" list:"user" create:"required"`
68+
ParentType api.TNetworkAddressParentType `width:"16" charset:"ascii" list:"user" create:"required"`
69+
ParentId string `width:"16" charset:"ascii" list:"user" create:"optional"`
7070
SNetworkResourceBase
7171
IpAddr string `width:"16" charset:"ascii" list:"user" create:"optional"`
7272

@@ -78,9 +78,9 @@ func (man *SNetworkAddressManager) InitializeData() error {
7878
return nil
7979
}
8080

81-
func (man *SNetworkAddressManager) queryByParentTypeId(typ string, id string) *sqlchemy.SQuery {
81+
func (man *SNetworkAddressManager) queryByParentTypeId(typ api.TNetworkAddressParentType, id string) *sqlchemy.SQuery {
8282
q := NetworkAddressManager.Query().
83-
Equals("parent_type", typ).
83+
Equals("parent_type", string(typ)).
8484
Equals("parent_id", id)
8585
return q
8686
}
@@ -90,7 +90,7 @@ func (man *SNetworkAddressManager) queryByGuestnetworkId(rowid int64) *sqlchemy.
9090
return man.queryByParentTypeId(api.NetworkAddressParentTypeGuestnetwork, id)
9191
}
9292

93-
func (man *SNetworkAddressManager) fetchByParentTypeId(typ string, id string) ([]SNetworkAddress, error) {
93+
func (man *SNetworkAddressManager) fetchByParentTypeId(typ api.TNetworkAddressParentType, id string) ([]SNetworkAddress, error) {
9494
var (
9595
q = man.queryByParentTypeId(typ, id)
9696
nas []SNetworkAddress
@@ -691,3 +691,10 @@ func (g *SGuest) getICloudNic(ctx context.Context, gn *SGuestnetwork) (cloudprov
691691
}
692692
return nil, errors.Wrapf(errors.ErrNotFound, "no nic of ip %s mac %s", gn.IpAddr, gn.MacAddr)
693693
}
694+
695+
func (manager *SNetworkAddressManager) fetchSubIpsQuery(parentType api.TNetworkAddressParentType) *sqlchemy.SQuery {
696+
subIPQ := manager.Query("parent_id").Equals("parent_type", string(parentType))
697+
subIPQ = subIPQ.AppendField(sqlchemy.GROUP_CONCAT("sub_ips", subIPQ.Field("ip_addr")))
698+
subIPQ = subIPQ.GroupBy(subIPQ.Field("parent_id"))
699+
return subIPQ
700+
}

0 commit comments

Comments
 (0)