Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Automated cherry pick of #21607: fix(region): add lb backend group count and listener count for lb details #21608

Open
wants to merge 1 commit into
base: release/3.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/apis/compute/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ type LoadbalancerDetails struct {

// 关联安全组列表
Secgroups []SimpleSecurityGroup `json:"secgroups"`
LoadbalancerUsage
}

type LoadbalancerUsage struct {
BackendGroupCount int `json:"backend_group_count"`
ListenerCount int `json:"listener_count"`
}

type SimpleSecurityGroup struct {
Expand Down
60 changes: 60 additions & 0 deletions pkg/compute/models/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,59 @@ func (lb *SLoadbalancer) ValidateUpdateData(ctx context.Context, userCred mcclie
return data, nil
}

type SLoadbalancerUsageCount struct {
Id string
api.LoadbalancerUsage
}

func (lm *SLoadbalancerManager) query(manager db.IModelManager, field string, lbIds []string, filter func(*sqlchemy.SQuery) *sqlchemy.SQuery) *sqlchemy.SSubQuery {
q := manager.Query()

if filter != nil {
q = filter(q)
}

sq := q.SubQuery()

return sq.Query(
sq.Field("loadbalancer_id"),
sqlchemy.COUNT(field),
).In("loadbalancer_id", lbIds).GroupBy(sq.Field("loadbalancer_id")).SubQuery()
}

func (manager *SLoadbalancerManager) TotalResourceCount(lbIds []string) (map[string]api.LoadbalancerUsage, error) {
// backendGroup
lbgSQ := manager.query(LoadbalancerBackendGroupManager, "backend_group_cnt", lbIds, nil)
// listener
lisSQ := manager.query(LoadbalancerListenerManager, "listener_cnt", lbIds, nil)

lb := manager.Query().SubQuery()
lbQ := lb.Query(
sqlchemy.SUM("backend_group_count", lbgSQ.Field("backend_group_cnt")),
sqlchemy.SUM("listener_count", lisSQ.Field("listener_cnt")),
)

lbQ.AppendField(lbQ.Field("id"))

lbQ = lbQ.LeftJoin(lbgSQ, sqlchemy.Equals(lbQ.Field("id"), lbgSQ.Field("loadbalancer_id")))
lbQ = lbQ.LeftJoin(lisSQ, sqlchemy.Equals(lbQ.Field("id"), lisSQ.Field("loadbalancer_id")))

lbQ = lbQ.Filter(sqlchemy.In(lbQ.Field("id"), lbIds)).GroupBy(lbQ.Field("id"))

lbCount := []SLoadbalancerUsageCount{}
err := lbQ.All(&lbCount)
if err != nil {
return nil, errors.Wrapf(err, "lbQ.All")
}

result := map[string]api.LoadbalancerUsage{}
for i := range lbCount {
result[lbCount[i].Id] = lbCount[i].LoadbalancerUsage
}

return result, nil
}

func (man *SLoadbalancerManager) FetchCustomizeColumns(
ctx context.Context,
userCred mcclient.TokenCredential,
Expand Down Expand Up @@ -838,6 +891,12 @@ func (man *SLoadbalancerManager) FetchCustomizeColumns(
})
}

usage, err := man.TotalResourceCount(lbIds)
if err != nil {
log.Errorf("TotalResourceCount error: %v", err)
return rows
}

for i := range rows {
eip, ok := eipMap[lbIds[i]]
if ok {
Expand All @@ -850,6 +909,7 @@ func (man *SLoadbalancerManager) FetchCustomizeColumns(
rows[i].BackendGroup = bg
}
rows[i].Secgroups, _ = groups[lbIds[i]]
rows[i].LoadbalancerUsage, _ = usage[lbIds[i]]
}

return rows
Expand Down
Loading