Skip to content

Commit 894396b

Browse files
authored
Modify volumebroker to filter volumes before aggregation (#1407)
1 parent 5b080a9 commit 894396b

File tree

8 files changed

+128
-63
lines changed

8 files changed

+128
-63
lines changed

broker/volumebroker/cmd/volumebroker/app/app.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,9 @@ func Run(ctx context.Context, opts Options) error {
140140
if err := grpcSrv.Serve(l); err != nil {
141141
return fmt.Errorf("error serving: %w", err)
142142
}
143+
144+
if err := srv.SetVolumeUIDLabelToAllVolumes(ctx, log); err != nil {
145+
return fmt.Errorf("failed to set volume uid label to all brokered volumes: %w", err)
146+
}
143147
return nil
144148
}

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/server.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99

1010
"github.com/go-logr/logr"
11+
"github.com/ironcore-dev/controller-utils/metautils"
1112
computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
1213
ipamv1alpha1 "github.com/ironcore-dev/ironcore/api/ipam/v1alpha1"
1314
networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
@@ -17,6 +18,7 @@ import (
1718
volumebrokerv1alpha1 "github.com/ironcore-dev/ironcore/broker/volumebroker/api/v1alpha1"
1819
"github.com/ironcore-dev/ironcore/broker/volumebroker/apiutils"
1920
iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1"
21+
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"
2022
corev1 "k8s.io/api/core/v1"
2123
apierrors "k8s.io/apimachinery/pkg/api/errors"
2224
"k8s.io/apimachinery/pkg/runtime"
@@ -140,3 +142,36 @@ func (s *Server) getManagedAndCreated(ctx context.Context, name string, obj clie
140142
}
141143
return nil
142144
}
145+
146+
func (s *Server) SetVolumeUIDLabelToAllVolumes(ctx context.Context, log logr.Logger) error {
147+
volumeList := &storagev1alpha1.VolumeList{}
148+
log.V(2).Info("Listing brokered volumes")
149+
if err := s.listManagedAndCreated(ctx, volumeList, nil); err != nil {
150+
return fmt.Errorf("error listing ironcore volumes: %w", err)
151+
}
152+
153+
for i := range volumeList.Items {
154+
volume := &volumeList.Items[i]
155+
labels, err := apiutils.GetLabelsAnnotation(volume)
156+
if err != nil {
157+
return fmt.Errorf("failed to get labels annotation: %w", err)
158+
}
159+
if labels == nil {
160+
log.V(2).Info("Labels are nil", "name", volume.Name, "namespace", volume.Namespace)
161+
continue
162+
}
163+
164+
volumeUid := labels[volumepoolletv1alpha1.VolumeUIDLabel]
165+
if volumeUid == "" {
166+
log.V(2).Info("Volume uid label is empty", "name", volume.Name, "namespace", volume.Namespace)
167+
continue
168+
}
169+
log.V(2).Info("Setting volume uid label for", "name", volume.Name, "namespace", volume.Namespace, "labelKey", volumepoolletv1alpha1.VolumeUIDLabel, "labelValue", volumeUid)
170+
base := volume.DeepCopy()
171+
metautils.SetLabel(volume, volumepoolletv1alpha1.VolumeUIDLabel, volumeUid)
172+
if err := s.client.Patch(ctx, volume, client.MergeFrom(base)); err != nil {
173+
return fmt.Errorf("error patching volume uid label: %w", err)
174+
}
175+
}
176+
return nil
177+
}

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

broker/volumebroker/server/volume_list_test.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
package server_test
55

66
import (
7+
storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1"
8+
volumebrokerv1alpha1 "github.com/ironcore-dev/ironcore/broker/volumebroker/api/v1alpha1"
79
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
810
iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1"
11+
poolletutils "github.com/ironcore-dev/ironcore/poollet/common/utils"
912
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"
1013
. "github.com/onsi/ginkgo/v2"
1114
. "github.com/onsi/gomega"
15+
ctrl "sigs.k8s.io/controller-runtime"
16+
"sigs.k8s.io/controller-runtime/pkg/client"
1217
)
1318

1419
var _ = Describe("ListVolumes", func() {
15-
_, srv := SetupTest()
20+
ns, srv := SetupTest()
1621
volumeClass := SetupVolumeClass()
1722

1823
It("should correctly list volumes", func(ctx SpecContext) {
@@ -44,4 +49,59 @@ var _ = Describe("ListVolumes", func() {
4449
By("listing the Volumes")
4550
Expect(srv.ListVolumes(ctx, &iri.ListVolumesRequest{})).To(HaveField("Volumes", ConsistOf(Volumes...)))
4651
})
52+
53+
It("should set volume uid label to all existing volumes", func(ctx SpecContext) {
54+
By("creating multiple volumes")
55+
res, err := srv.CreateVolume(ctx, &iri.CreateVolumeRequest{
56+
Volume: &iri.Volume{
57+
Metadata: &irimeta.ObjectMetadata{
58+
Labels: map[string]string{
59+
volumepoolletv1alpha1.VolumeUIDLabel: "foobar",
60+
},
61+
},
62+
Spec: &iri.VolumeSpec{
63+
Class: volumeClass.Name,
64+
Resources: &iri.VolumeResources{
65+
StorageBytes: 100,
66+
},
67+
},
68+
},
69+
})
70+
Expect(err).NotTo(HaveOccurred())
71+
Expect(res).NotTo(BeNil())
72+
73+
ironcoreVolume := &storagev1alpha1.Volume{}
74+
ironcoreVolumeKey := client.ObjectKey{Namespace: ns.Name, Name: res.Volume.Metadata.Id}
75+
Expect(k8sClient.Get(ctx, ironcoreVolumeKey, ironcoreVolume)).To(Succeed())
76+
Expect(ironcoreVolume.Labels).To(Equal(map[string]string{
77+
poolletutils.DownwardAPILabel(volumepoolletv1alpha1.VolumeDownwardAPIPrefix, "root-volume-uid"): "foobar",
78+
volumebrokerv1alpha1.CreatedLabel: "true",
79+
volumebrokerv1alpha1.ManagerLabel: volumebrokerv1alpha1.VolumeBrokerManager,
80+
volumepoolletv1alpha1.VolumeUIDLabel: "foobar",
81+
}))
82+
83+
base := ironcoreVolume.DeepCopy()
84+
delete(ironcoreVolume.Labels, volumepoolletv1alpha1.VolumeUIDLabel)
85+
Expect(k8sClient.Patch(ctx, ironcoreVolume, client.MergeFrom(base))).To(Succeed())
86+
Expect(k8sClient.Get(ctx, ironcoreVolumeKey, ironcoreVolume)).To(Succeed())
87+
Expect(ironcoreVolume.Labels).To(Equal(map[string]string{
88+
poolletutils.DownwardAPILabel(volumepoolletv1alpha1.VolumeDownwardAPIPrefix, "root-volume-uid"): "foobar",
89+
volumebrokerv1alpha1.CreatedLabel: "true",
90+
volumebrokerv1alpha1.ManagerLabel: volumebrokerv1alpha1.VolumeBrokerManager,
91+
}))
92+
93+
log := ctrl.LoggerFrom(ctx)
94+
Expect(srv.SetVolumeUIDLabelToAllVolumes(ctx, log)).NotTo(HaveOccurred())
95+
96+
By("getting the ironcore volume")
97+
Expect(k8sClient.Get(ctx, ironcoreVolumeKey, ironcoreVolume)).To(Succeed())
98+
99+
By("inspecting the ironcore volume")
100+
Expect(ironcoreVolume.Labels).To(Equal(map[string]string{
101+
poolletutils.DownwardAPILabel(volumepoolletv1alpha1.VolumeDownwardAPIPrefix, "root-volume-uid"): "foobar",
102+
volumebrokerv1alpha1.CreatedLabel: "true",
103+
volumebrokerv1alpha1.ManagerLabel: volumebrokerv1alpha1.VolumeBrokerManager,
104+
volumepoolletv1alpha1.VolumeUIDLabel: "foobar",
105+
}))
106+
})
47107
})

poollet/volumepoollet/controllers/volume_controller.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -484,22 +484,16 @@ func (r *VolumeReconciler) reconcile(ctx context.Context, log logr.Logger, volum
484484
}
485485

486486
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-
})
487+
volumes, err := r.listIRIVolumesByUID(ctx, volume.UID)
494488
if err != nil {
495489
return ctrl.Result{}, fmt.Errorf("error listing volumes: %w", err)
496490
}
497491

498-
switch len(res.Volumes) {
492+
switch len(volumes) {
499493
case 0:
500494
return r.create(ctx, log, volume)
501495
case 1:
502-
iriVolume := res.Volumes[0]
496+
iriVolume := volumes[0]
503497
if err := r.update(ctx, log, volume, iriVolume); err != nil {
504498
return ctrl.Result{}, fmt.Errorf("error updating volume: %w", err)
505499
}

0 commit comments

Comments
 (0)