Skip to content

Commit f8dbd4e

Browse files
committed
modify volumebroker to filter volumes before aggregation
1 parent 9b1af90 commit f8dbd4e

File tree

5 files changed

+37
-92
lines changed

5 files changed

+37
-92
lines changed

broker/volumebroker/server/event_list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func (s *Server) listEvents(ctx context.Context) ([]*irievent.Event, error) {
4040

4141
var iriEvents []*irievent.Event
4242
for _, volumeEvent := range volumeEventList.Items {
43-
ironcoreVolume, err := s.getIronCoreVolume(ctx, volumeEvent.InvolvedObject.Name)
44-
if err != nil {
43+
ironcoreVolume := &storagev1alpha1.Volume{}
44+
if err := s.getManagedAndCreated(ctx, volumeEvent.InvolvedObject.Name, ironcoreVolume); err != nil {
4545
log.V(1).Info("Unable to get ironcore volume", "VolumeName", volumeEvent.InvolvedObject.Name)
4646
continue
4747
}

broker/volumebroker/server/volume_create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (s *Server) getIronCoreVolumeConfig(_ context.Context, volume *iri.Volume)
6565
s.brokerDownwardAPILabels,
6666
volumepoolletv1alpha1.VolumeDownwardAPIPrefix,
6767
)
68+
labels[volumepoolletv1alpha1.VolumeUIDLabel] = volume.GetMetadata().GetLabels()[volumepoolletv1alpha1.VolumeUIDLabel]
6869

6970
var image string
7071
var volumeSnapshotRef *corev1.LocalObjectReference

broker/volumebroker/server/volume_create_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ var _ = Describe("CreateVolume", func() {
5252
By("inspecting the ironcore volume")
5353
Expect(ironcoreVolume.Labels).To(Equal(map[string]string{
5454
poolletutils.DownwardAPILabel(volumepoolletv1alpha1.VolumeDownwardAPIPrefix, "root-volume-uid"): "foobar",
55-
volumebrokerv1alpha1.CreatedLabel: "true",
56-
volumebrokerv1alpha1.ManagerLabel: volumebrokerv1alpha1.VolumeBrokerManager,
55+
volumebrokerv1alpha1.CreatedLabel: "true",
56+
volumebrokerv1alpha1.ManagerLabel: volumebrokerv1alpha1.VolumeBrokerManager,
57+
volumepoolletv1alpha1.VolumeUIDLabel: "foobar",
5758
}))
5859
encodedIRIAnnotations, err := apiutils.EncodeAnnotationsAnnotation(nil)
5960
Expect(err).NotTo(HaveOccurred())

broker/volumebroker/server/volume_list.go

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,35 @@ import (
1010
storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1"
1111
"github.com/ironcore-dev/ironcore/broker/common"
1212
volumebrokerv1alpha1 "github.com/ironcore-dev/ironcore/broker/volumebroker/api/v1alpha1"
13-
"github.com/ironcore-dev/ironcore/broker/volumebroker/apiutils"
1413
iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1"
1514
"google.golang.org/grpc/codes"
1615
"google.golang.org/grpc/status"
1716
corev1 "k8s.io/api/core/v1"
1817
apierrors "k8s.io/apimachinery/pkg/api/errors"
19-
"k8s.io/apimachinery/pkg/labels"
2018
"sigs.k8s.io/controller-runtime/pkg/client"
2119
)
2220

23-
func (s *Server) listManagedAndCreated(ctx context.Context, list client.ObjectList) error {
24-
return s.client.List(ctx, list,
21+
func (s *Server) listManagedAndCreated(ctx context.Context, ironcoreVolumeList *storagev1alpha1.VolumeList, filter *iri.VolumeFilter) error {
22+
matchingLabels := client.MatchingLabels{
23+
volumebrokerv1alpha1.ManagerLabel: volumebrokerv1alpha1.VolumeBrokerManager,
24+
volumebrokerv1alpha1.CreatedLabel: "true",
25+
}
26+
27+
if filter != nil && filter.LabelSelector != nil {
28+
for k := range filter.LabelSelector {
29+
matchingLabels[k] = filter.LabelSelector[k]
30+
}
31+
}
32+
33+
return s.client.List(ctx, ironcoreVolumeList,
2534
client.InNamespace(s.namespace),
26-
client.MatchingLabels{
27-
volumebrokerv1alpha1.ManagerLabel: volumebrokerv1alpha1.VolumeBrokerManager,
28-
volumebrokerv1alpha1.CreatedLabel: "true",
29-
},
35+
matchingLabels,
3036
)
3137
}
3238

33-
func (s *Server) listAggregateIronCoreVolumes(ctx context.Context) ([]AggregateIronCoreVolume, error) {
39+
func (s *Server) listAggregateIronCoreVolumes(ctx context.Context, filter *iri.VolumeFilter) ([]AggregateIronCoreVolume, error) {
3440
ironcoreVolumeList := &storagev1alpha1.VolumeList{}
35-
if err := s.listManagedAndCreated(ctx, ironcoreVolumeList); err != nil {
41+
if err := s.listManagedAndCreated(ctx, ironcoreVolumeList, filter); err != nil {
3642
return nil, fmt.Errorf("error listing ironcore volumes: %w", err)
3743
}
3844

@@ -132,21 +138,6 @@ func (s *Server) aggregateIronCoreVolume(
132138
}, nil
133139
}
134140

135-
func (s *Server) getIronCoreVolume(ctx context.Context, id string) (*storagev1alpha1.Volume, error) {
136-
ironcoreVolume := &storagev1alpha1.Volume{}
137-
ironcoreVolumeKey := client.ObjectKey{Namespace: s.namespace, Name: id}
138-
if err := s.client.Get(ctx, ironcoreVolumeKey, ironcoreVolume); err != nil {
139-
if !apierrors.IsNotFound(err) {
140-
return nil, fmt.Errorf("error getting ironcore volume %s: %w", id, err)
141-
}
142-
return nil, status.Errorf(codes.NotFound, "volume %s not found", id)
143-
}
144-
if !apiutils.IsManagedBy(ironcoreVolume, volumebrokerv1alpha1.VolumeBrokerManager) || !apiutils.IsCreated(ironcoreVolume) {
145-
return nil, status.Errorf(codes.NotFound, "volume %s not found", id)
146-
}
147-
return ironcoreVolume, nil
148-
}
149-
150141
func (s *Server) getAggregateIronCoreVolume(ctx context.Context, id string) (*AggregateIronCoreVolume, error) {
151142
ironcoreVolume := &storagev1alpha1.Volume{}
152143
if err := s.getManagedAndCreated(ctx, id, ironcoreVolume); err != nil {
@@ -159,8 +150,8 @@ func (s *Server) getAggregateIronCoreVolume(ctx context.Context, id string) (*Ag
159150
return s.aggregateIronCoreVolume(ironcoreVolume, s.clientGetSecretFunc(ctx))
160151
}
161152

162-
func (s *Server) listVolumes(ctx context.Context) ([]*iri.Volume, error) {
163-
ironcoreVolumes, err := s.listAggregateIronCoreVolumes(ctx)
153+
func (s *Server) listVolumes(ctx context.Context, filter *iri.VolumeFilter) ([]*iri.Volume, error) {
154+
ironcoreVolumes, err := s.listAggregateIronCoreVolumes(ctx, filter)
164155
if err != nil {
165156
return nil, fmt.Errorf("error listing volumes: %w", err)
166157
}
@@ -177,25 +168,6 @@ func (s *Server) listVolumes(ctx context.Context) ([]*iri.Volume, error) {
177168
return res, nil
178169
}
179170

180-
func (s *Server) filterVolumes(volumes []*iri.Volume, filter *iri.VolumeFilter) []*iri.Volume {
181-
if filter == nil {
182-
return volumes
183-
}
184-
185-
var (
186-
res []*iri.Volume
187-
sel = labels.SelectorFromSet(filter.LabelSelector)
188-
)
189-
for _, iriVolume := range volumes {
190-
if !sel.Matches(labels.Set(iriVolume.Metadata.Labels)) {
191-
continue
192-
}
193-
194-
res = append(res, iriVolume)
195-
}
196-
return res
197-
}
198-
199171
func (s *Server) getVolume(ctx context.Context, id string) (*iri.Volume, error) {
200172
ironcoreVolume, err := s.getAggregateIronCoreVolume(ctx, id)
201173
if err != nil {
@@ -222,13 +194,11 @@ func (s *Server) ListVolumes(ctx context.Context, req *iri.ListVolumesRequest) (
222194
}, nil
223195
}
224196

225-
volumes, err := s.listVolumes(ctx)
197+
volumes, err := s.listVolumes(ctx, req.Filter)
226198
if err != nil {
227199
return nil, err
228200
}
229201

230-
volumes = s.filterVolumes(volumes, req.Filter)
231-
232202
return &iri.ListVolumesResponse{
233203
Volumes: volumes,
234204
}, nil

poollet/volumepoollet/controllers/volume_controller.go

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,6 @@ func (r *VolumeReconciler) iriVolumeAnnotations(_ *storagev1alpha1.Volume) map[s
8383
return map[string]string{}
8484
}
8585

86-
func (r *VolumeReconciler) listIRIVolumesByKey(ctx context.Context, volumeKey client.ObjectKey) ([]*iri.Volume, error) {
87-
res, err := r.VolumeRuntime.ListVolumes(ctx, &iri.ListVolumesRequest{
88-
Filter: &iri.VolumeFilter{
89-
LabelSelector: map[string]string{
90-
volumepoolletv1alpha1.VolumeNamespaceLabel: volumeKey.Namespace,
91-
volumepoolletv1alpha1.VolumeNameLabel: volumeKey.Name,
92-
},
93-
},
94-
})
95-
if err != nil {
96-
return nil, fmt.Errorf("error listing volumes by key: %w", err)
97-
}
98-
volumes := res.Volumes
99-
return volumes, nil
100-
}
101-
10286
func (r *VolumeReconciler) listIRIVolumesByUID(ctx context.Context, volumeUID types.UID) ([]*iri.Volume, error) {
10387
res, err := r.VolumeRuntime.ListVolumes(ctx, &iri.ListVolumesRequest{
10488
Filter: &iri.VolumeFilter{
@@ -126,18 +110,18 @@ func (r *VolumeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
126110
if !apierrors.IsNotFound(err) {
127111
return ctrl.Result{}, fmt.Errorf("error getting volume %s: %w", req.NamespacedName, err)
128112
}
129-
return r.deleteGone(ctx, log, req.NamespacedName)
113+
return r.deleteGone(ctx, log, volume)
130114
}
131115
return r.reconcileExists(ctx, log, volume)
132116
}
133117

134-
func (r *VolumeReconciler) deleteGone(ctx context.Context, log logr.Logger, volumeKey client.ObjectKey) (ctrl.Result, error) {
118+
func (r *VolumeReconciler) deleteGone(ctx context.Context, log logr.Logger, volume *storagev1alpha1.Volume) (ctrl.Result, error) {
135119
log.V(1).Info("Delete gone")
136120

137-
log.V(1).Info("Listing iri volumes by key")
138-
volumes, err := r.listIRIVolumesByKey(ctx, volumeKey)
121+
log.V(1).Info("Listing iri volumes by uid")
122+
volumes, err := r.listIRIVolumesByUID(ctx, volume.UID)
139123
if err != nil {
140-
return ctrl.Result{}, fmt.Errorf("error listing iri volumes by key: %w", err)
124+
return ctrl.Result{}, fmt.Errorf("error listing iri volumes by uid: %w", err)
141125
}
142126

143127
ok, err := r.deleteIRIVolumes(ctx, log, volumes)
@@ -207,19 +191,14 @@ func (r *VolumeReconciler) delete(ctx context.Context, log logr.Logger, volume *
207191

208192
log.V(1).Info("Finalizer present")
209193

210-
log.V(1).Info("Listing volumes")
211-
volumes, err := r.listIRIVolumesByUID(ctx, volume.UID)
212-
if err != nil {
213-
return ctrl.Result{}, fmt.Errorf("error listing volumes by uid: %w", err)
214-
}
215-
216-
ok, err := r.deleteIRIVolumes(ctx, log, volumes)
194+
log.V(1).Info("Deleting IRI volumes for volume")
195+
res, err := r.deleteGone(ctx, log, volume)
217196
if err != nil {
218197
return ctrl.Result{}, fmt.Errorf("error deleting iri volumes: %w", err)
219198
}
220-
if !ok {
199+
if !res.IsZero() {
221200
log.V(1).Info("Not all iri volumes are gone, requeueing")
222-
return ctrl.Result{Requeue: true}, nil
201+
return res, nil
223202
}
224203

225204
log.V(1).Info("Deleted all iri volumes, removing finalizer")
@@ -484,22 +463,16 @@ func (r *VolumeReconciler) reconcile(ctx context.Context, log logr.Logger, volum
484463
}
485464

486465
log.V(1).Info("Listing volumes")
487-
res, err := r.VolumeRuntime.ListVolumes(ctx, &iri.ListVolumesRequest{
488-
Filter: &iri.VolumeFilter{
489-
LabelSelector: map[string]string{
490-
volumepoolletv1alpha1.VolumeUIDLabel: string(volume.UID),
491-
},
492-
},
493-
})
466+
volumes, err := r.listIRIVolumesByUID(ctx, volume.UID)
494467
if err != nil {
495468
return ctrl.Result{}, fmt.Errorf("error listing volumes: %w", err)
496469
}
497470

498-
switch len(res.Volumes) {
471+
switch len(volumes) {
499472
case 0:
500473
return r.create(ctx, log, volume)
501474
case 1:
502-
iriVolume := res.Volumes[0]
475+
iriVolume := volumes[0]
503476
if err := r.update(ctx, log, volume, iriVolume); err != nil {
504477
return ctrl.Result{}, fmt.Errorf("error updating volume: %w", err)
505478
}

0 commit comments

Comments
 (0)