Skip to content

Commit

Permalink
incusd/network/acl: Properly run instance counting outside of ACL loop
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
  • Loading branch information
stgraber committed May 2, 2024
1 parent 410ecc8 commit 0f798e9
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions internal/server/network/acl/acl_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ func UsedBy(s *state.State, aclProjectName string, usageFunc func(ctx context.Co
return err
}

for _, aclName := range aclNames {
err := s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
err = s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
for _, aclName := range aclNames {
_, aclInfo, err := tx.GetNetworkACL(ctx, aclProjectName, aclName)
if err != nil {
return err
Expand Down Expand Up @@ -233,42 +233,42 @@ func UsedBy(s *state.State, aclProjectName string, usageFunc func(ctx context.Co
return err
}
}
}

// Find instances using the ACLs. Most expensive to do.
err = tx.InstanceList(ctx, func(inst db.InstanceArgs, p api.Project) error {
// Get the instance's effective network project name.
instNetworkProject := project.NetworkProjectFromRecord(&p)
// Find instances using the ACLs. Most expensive to do.
err = tx.InstanceList(ctx, func(inst db.InstanceArgs, p api.Project) error {
// Get the instance's effective network project name.
instNetworkProject := project.NetworkProjectFromRecord(&p)

// Skip instances who's effective network project doesn't match this Network ACL's project.
if instNetworkProject != aclProjectName {
return nil
}
// Skip instances who's effective network project doesn't match this Network ACL's project.
if instNetworkProject != aclProjectName {
return nil
}

devices := db.ExpandInstanceDevices(inst.Devices.Clone(), inst.Profiles)

devices := db.ExpandInstanceDevices(inst.Devices.Clone(), inst.Profiles)

// Iterate through each of the instance's devices, looking for NICs that are using any of the ACLs.
for devName, devConfig := range devices {
matchedACLNames := isInUseByDevice(devConfig, matchACLNames...)
if len(matchedACLNames) > 0 {
// Call usageFunc with a list of matched ACLs and info about the instance NIC.
err := usageFunc(ctx, tx, matchedACLNames, inst, devName, devConfig)
if err != nil {
return err
}
// Iterate through each of the instance's devices, looking for NICs that are using any of the ACLs.
for devName, devConfig := range devices {
matchedACLNames := isInUseByDevice(devConfig, matchACLNames...)
if len(matchedACLNames) > 0 {
// Call usageFunc with a list of matched ACLs and info about the instance NIC.
err := usageFunc(ctx, tx, matchedACLNames, inst, devName, devConfig)
if err != nil {
return err
}
}

return nil
})
if err != nil {
return err
}

return nil
})
if err != nil {
return err
}

return nil
})
if err != nil {
return err
}

return nil
Expand Down

0 comments on commit 0f798e9

Please sign in to comment.