Skip to content

Commit 08c19ea

Browse files
committed
Only fetch required ips per switch describe instead of all
1 parent f0628c1 commit 08c19ea

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

cmd/metal-api/internal/datastore/ip.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ func (rs *RethinkStore) SearchIPs(q *IPSearchQuery, ips *metal.IPs) error {
9797
return rs.searchEntities(q.generateTerm(rs), ips)
9898
}
9999

100+
// FindIPsByProjects returns all ips of given projects.
101+
// FIXME no test present to check if the query actually works
102+
// according to https://stackoverflow.com/questions/44424377/rethinkdb-query-filter-against-multiple-values-for-single-key it should :-)
103+
func (rs *RethinkStore) FindIPsByProjects(projects []string) (metal.IPs, error) {
104+
q := *rs.ipTable()
105+
q = q.Filter(func(row r.Term) r.Term {
106+
return row.Field("projectid").Contains(func(nw r.Term) r.Term {
107+
return nw.Field("projectid").Contains(r.Expr(projects))
108+
})
109+
})
110+
111+
ips := make([]metal.IP, 0)
112+
err := rs.searchEntities(&q, ips)
113+
return ips, err
114+
}
115+
100116
// ListIPs returns all ips.
101117
func (rs *RethinkStore) ListIPs() (metal.IPs, error) {
102118
ips := make([]metal.IP, 0)

cmd/metal-api/internal/service/switch-service.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -997,23 +997,30 @@ func makeSwitchCons(s *metal.Switch) []v1.SwitchConnection {
997997
}
998998

999999
func findSwitchReferencedEntities(s *metal.Switch, ds *datastore.RethinkStore) (*metal.Partition, metal.IPsMap, metal.Machines, *metal.SwitchStatus, error) {
1000-
var err error
1001-
var p *metal.Partition
1002-
var m metal.Machines
1000+
var (
1001+
err error
1002+
p *metal.Partition
1003+
ms metal.Machines
1004+
projects []string
1005+
)
10031006

10041007
if s.PartitionID != "" {
10051008
p, err = ds.FindPartition(s.PartitionID)
10061009
if err != nil {
10071010
return nil, nil, nil, nil, fmt.Errorf("switch %q references partition, but partition %q cannot be found in database: %w", s.ID, s.PartitionID, err)
10081011
}
10091012

1010-
err = ds.SearchMachines(&datastore.MachineSearchQuery{PartitionID: &s.PartitionID}, &m)
1013+
err = ds.SearchMachines(&datastore.MachineSearchQuery{PartitionID: &s.PartitionID}, &ms)
10111014
if err != nil {
10121015
return nil, nil, nil, nil, fmt.Errorf("could not search machines of partition %q for switch %q: %w", s.PartitionID, s.ID, err)
10131016
}
10141017
}
10151018

1016-
ips, err := ds.ListIPs()
1019+
for project := range ms.ByProjectID() {
1020+
projects = append(projects, project)
1021+
}
1022+
1023+
ips, err := ds.FindIPsByProjects(projects)
10171024
if err != nil {
10181025
return nil, nil, nil, nil, fmt.Errorf("ips could not be listed: %w", err)
10191026
}
@@ -1023,7 +1030,7 @@ func findSwitchReferencedEntities(s *metal.Switch, ds *datastore.RethinkStore) (
10231030
return nil, nil, nil, nil, fmt.Errorf("switchStatus could not be listed: %w", err)
10241031
}
10251032

1026-
return p, ips.ByProjectID(), m, ss, nil
1033+
return p, ips.ByProjectID(), ms, ss, nil
10271034
}
10281035

10291036
func makeSwitchResponseList(ss metal.Switches, ds *datastore.RethinkStore) ([]*v1.SwitchResponse, error) {

0 commit comments

Comments
 (0)