Skip to content

Commit 56e5d1f

Browse files
committed
set volume uid label to migrate existing volumes on broker startup
1 parent fde4399 commit 56e5d1f

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
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); 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/volume_list.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ import (
77
"context"
88
"fmt"
99

10+
"github.com/ironcore-dev/controller-utils/metautils"
1011
storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1"
1112
"github.com/ironcore-dev/ironcore/broker/common"
1213
volumebrokerv1alpha1 "github.com/ironcore-dev/ironcore/broker/volumebroker/api/v1alpha1"
14+
"github.com/ironcore-dev/ironcore/broker/volumebroker/apiutils"
1315
iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1"
16+
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"
1417
"google.golang.org/grpc/codes"
1518
"google.golang.org/grpc/status"
1619
corev1 "k8s.io/api/core/v1"
1720
apierrors "k8s.io/apimachinery/pkg/api/errors"
21+
ctrl "sigs.k8s.io/controller-runtime"
1822
"sigs.k8s.io/controller-runtime/pkg/client"
1923
)
2024

@@ -203,3 +207,37 @@ func (s *Server) ListVolumes(ctx context.Context, req *iri.ListVolumesRequest) (
203207
Volumes: volumes,
204208
}, nil
205209
}
210+
211+
func (s *Server) SetVolumeUIDLabelToAllVolumes(ctx context.Context) error {
212+
volumeList := &storagev1alpha1.VolumeList{}
213+
log := ctrl.LoggerFrom(ctx)
214+
log.Info("Listing brokered volumes")
215+
if err := s.listManagedAndCreated(ctx, volumeList, nil); err != nil {
216+
return fmt.Errorf("error listing ironcore volumes: %w", err)
217+
}
218+
219+
for i := range volumeList.Items {
220+
volume := &volumeList.Items[i]
221+
labels, err := apiutils.GetLabelsAnnotation(volume)
222+
if err != nil {
223+
return fmt.Errorf("failed to get labels annotation: %w", err)
224+
}
225+
if labels == nil {
226+
log.Info("Labels are nil", "name", volume.Name, "namespace", volume.Namespace)
227+
continue
228+
}
229+
230+
volumeUid := labels[volumepoolletv1alpha1.VolumeUIDLabel]
231+
if volumeUid == "" {
232+
log.Info("Volume uid label is empty", "name", volume.Name, "namespace", volume.Namespace)
233+
continue
234+
}
235+
log.Info("Setting volume uid label for", "name", volume.Name, "namespace", volume.Namespace, "labelKey", volumepoolletv1alpha1.VolumeUIDLabel, "labelValue", volumeUid)
236+
base := volume.DeepCopy()
237+
metautils.SetLabel(volume, volumepoolletv1alpha1.VolumeUIDLabel, volumeUid)
238+
if err := s.client.Patch(ctx, volume, client.MergeFrom(base)); err != nil {
239+
return fmt.Errorf("error patching volume uid label: %w", err)
240+
}
241+
}
242+
return nil
243+
}

broker/volumebroker/server/volume_list_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
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+
"sigs.k8s.io/controller-runtime/pkg/client"
1216
)
1317

1418
var _ = Describe("ListVolumes", func() {
15-
_, srv := SetupTest()
19+
ns, srv := SetupTest()
1620
volumeClass := SetupVolumeClass()
1721

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

0 commit comments

Comments
 (0)