Skip to content

Commit c276001

Browse files
committed
Filter in store List calls instead of post-fetch loops
Signed-off-by: Daniel Gonzalez Nothnagel <daniel.gonzalez.nothnagel@sap.com>
1 parent aafb38c commit c276001

5 files changed

Lines changed: 20 additions & 17 deletions

File tree

cmd/libvirt-provider/app/app.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"time"
1818

1919
claim "github.com/ironcore-dev/provider-utils/claimutils/claim"
20+
"github.com/ironcore-dev/provider-utils/storeutils/store"
2021

2122
"github.com/go-logr/logr"
2223
"github.com/ironcore-dev/ironcore-image/oci/remote"
@@ -296,6 +297,11 @@ func Run(ctx context.Context, opts Options) error {
296297
NewFunc: func() *api.Machine { return &api.Machine{} },
297298
CreateStrategy: strategy.MachineStrategy,
298299
Dir: providerHost.MachineStoreDir(),
300+
FieldIndexers: map[string]store.IndexerFunc[*api.Machine]{
301+
api.MachineMetadataDeletedField: api.SetupMachineMetadataDeletedFieldIndexer,
302+
api.MachineSpecImageField: api.SetupMachineSpecImageFieldIndexer,
303+
api.MachineSpecHasGpuField: api.SetupMachineSpecHasGpuFieldIndexer,
304+
},
299305
})
300306
if err != nil {
301307
setupLog.Error(err, "failed to initialize machine store")

internal/controllers/machine_controller.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,16 @@ func (r *MachineReconciler) Start(ctx context.Context) error {
157157

158158
r.imageCache.AddListener(ociutils.ListenerFuncs{
159159
HandlePullDoneFunc: func(evt ociutils.PullDoneEvent) {
160-
machines, err := r.machines.List(ctx)
160+
machines, err := r.machines.List(ctx, store.MatchingFields{api.MachineSpecImageField: evt.Ref})
161161
if err != nil {
162162
log.Error(err, "failed to list machine")
163163
return
164164
}
165165

166166
for _, machine := range machines {
167-
if api.IsImageReferenced(machine, evt.Ref) {
168-
r.eventRecorder.Eventf(machine.Metadata, corev1.EventTypeNormal, "ImagePullSucceeded", "PullImage", "Pulled image %s", evt.Ref)
169-
log.V(1).Info("Image pulled: Requeue machines", "Image", evt.Ref, "Machine", machine.ID)
170-
r.queue.Add(machine.ID)
171-
}
167+
r.eventRecorder.Eventf(machine.Metadata, corev1.EventTypeNormal, "ImagePullSucceeded", "PullImage", "Pulled image %s", evt.Ref)
168+
log.V(1).Info("Image pulled: Requeue machines", "Image", evt.Ref, "Machine", machine.ID)
169+
r.queue.Add(machine.ID)
172170
}
173171
},
174172
})
@@ -262,14 +260,14 @@ func (r *MachineReconciler) startEnqueueMachineByLibvirtEvent(ctx context.Contex
262260
func (r *MachineReconciler) startGarbageCollector(ctx context.Context, log logr.Logger) {
263261
wait.UntilWithContext(ctx, func(ctx context.Context) {
264262
log.V(1).Info("starting garbage-collector loop")
265-
machines, err := r.machines.List(ctx)
263+
machines, err := r.machines.List(ctx, store.MatchingFields{api.MachineMetadataDeletedField: "true"})
266264
if err != nil {
267265
log.Error(err, "failed to list machines")
268266
return
269267
}
270268

271269
for _, machine := range machines {
272-
if !slices.Contains(machine.Finalizers, MachineFinalizer) || machine.DeletedAt == nil {
270+
if !slices.Contains(machine.Finalizers, MachineFinalizer) {
273271
continue
274272
}
275273

internal/server/machine_list.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,13 @@ func (s *Server) getLibvirtMachine(ctx context.Context, id string) (*api.Machine
3232
}
3333

3434
func (s *Server) listMachines(ctx context.Context, log logr.Logger) ([]*iri.Machine, error) {
35-
machines, err := s.machineStore.List(ctx)
35+
machines, err := s.machineStore.List(ctx, store.MatchingLabels{api.ManagerLabel: api.MachineManager})
3636
if err != nil {
3737
return nil, fmt.Errorf("error listing machines: %w", err)
3838
}
3939

4040
var res []*iri.Machine
4141
for _, machine := range machines {
42-
if !api.IsManagedBy(machine, api.MachineManager) {
43-
continue
44-
}
45-
4642
iriMachine, err := s.convertMachineToIRIMachine(ctx, log, machine)
4743
if err != nil {
4844
return nil, err

internal/utils/claims.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ import (
99
"github.com/ironcore-dev/libvirt-provider/api"
1010
"github.com/ironcore-dev/provider-utils/claimutils/pci"
1111
hostutils "github.com/ironcore-dev/provider-utils/storeutils/host"
12+
"github.com/ironcore-dev/provider-utils/storeutils/store"
1213
)
1314

1415
func GetClaimedPCIAddressesFromMachineStore(ctx context.Context, machineStore *hostutils.Store[*api.Machine]) ([]pci.Address, error) {
15-
machines, err := machineStore.List(ctx)
16+
machines, err := machineStore.List(ctx, store.MatchingFields{api.MachineSpecHasGpuField: "true"})
1617
if err != nil {
1718
return nil, err
1819
}
1920

2021
var pciAddrs []pci.Address
2122
for _, machine := range machines {
22-
if machine.Spec.Gpu != nil {
23-
pciAddrs = append(pciAddrs, machine.Spec.Gpu...)
24-
}
23+
pciAddrs = append(pciAddrs, machine.Spec.Gpu...)
2524
}
2625
return pciAddrs, nil
2726
}

internal/utils/claims_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
apiutils "github.com/ironcore-dev/provider-utils/apiutils/api"
1313
"github.com/ironcore-dev/provider-utils/claimutils/pci"
1414
hostutils "github.com/ironcore-dev/provider-utils/storeutils/host"
15+
"github.com/ironcore-dev/provider-utils/storeutils/store"
1516
. "github.com/onsi/ginkgo/v2"
1617
. "github.com/onsi/gomega"
1718
)
@@ -25,6 +26,9 @@ var _ = Describe("Claims Utils", func() {
2526
NewFunc: func() *api.Machine { return &api.Machine{} },
2627
CreateStrategy: strategy.MachineStrategy,
2728
Dir: filepath.Join(tempDir, "store", "machines"),
29+
FieldIndexers: map[string]store.IndexerFunc[*api.Machine]{
30+
api.MachineSpecHasGpuField: api.SetupMachineSpecHasGpuFieldIndexer,
31+
},
2832
})
2933
Expect(err).NotTo(HaveOccurred())
3034

0 commit comments

Comments
 (0)