Skip to content

Commit

Permalink
Only fetch required ips per switch describe instead of all
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Aug 16, 2024
1 parent f0628c1 commit 08c19ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
16 changes: 16 additions & 0 deletions cmd/metal-api/internal/datastore/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ func (rs *RethinkStore) SearchIPs(q *IPSearchQuery, ips *metal.IPs) error {
return rs.searchEntities(q.generateTerm(rs), ips)
}

// FindIPsByProjects returns all ips of given projects.
// FIXME no test present to check if the query actually works
// according to https://stackoverflow.com/questions/44424377/rethinkdb-query-filter-against-multiple-values-for-single-key it should :-)
func (rs *RethinkStore) FindIPsByProjects(projects []string) (metal.IPs, error) {
q := *rs.ipTable()
q = q.Filter(func(row r.Term) r.Term {
return row.Field("projectid").Contains(func(nw r.Term) r.Term {
return nw.Field("projectid").Contains(r.Expr(projects))
})
})

ips := make([]metal.IP, 0)
err := rs.searchEntities(&q, ips)
return ips, err
}

// ListIPs returns all ips.
func (rs *RethinkStore) ListIPs() (metal.IPs, error) {
ips := make([]metal.IP, 0)
Expand Down
19 changes: 13 additions & 6 deletions cmd/metal-api/internal/service/switch-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,23 +997,30 @@ func makeSwitchCons(s *metal.Switch) []v1.SwitchConnection {
}

func findSwitchReferencedEntities(s *metal.Switch, ds *datastore.RethinkStore) (*metal.Partition, metal.IPsMap, metal.Machines, *metal.SwitchStatus, error) {
var err error
var p *metal.Partition
var m metal.Machines
var (
err error
p *metal.Partition
ms metal.Machines
projects []string
)

if s.PartitionID != "" {
p, err = ds.FindPartition(s.PartitionID)
if err != nil {
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)
}

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

ips, err := ds.ListIPs()
for project := range ms.ByProjectID() {
projects = append(projects, project)
}

ips, err := ds.FindIPsByProjects(projects)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("ips could not be listed: %w", err)
}
Expand All @@ -1023,7 +1030,7 @@ func findSwitchReferencedEntities(s *metal.Switch, ds *datastore.RethinkStore) (
return nil, nil, nil, nil, fmt.Errorf("switchStatus could not be listed: %w", err)
}

return p, ips.ByProjectID(), m, ss, nil
return p, ips.ByProjectID(), ms, ss, nil
}

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

0 comments on commit 08c19ea

Please sign in to comment.