Skip to content

Commit df70ce4

Browse files
committed
Refactor snapshot processing & streamline data flow
1 parent 7882603 commit df70ce4

17 files changed

+164
-663
lines changed

api/storage/v1alpha1/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import corev1 "k8s.io/api/core/v1"
88
const (
99
VolumeVolumePoolRefNameField = "spec.volumePoolRef.name"
1010
VolumeVolumeClassRefNameField = "spec.volumeClassRef.name"
11-
VolumeVolumeSnapshotRefNameField = "spec.volumeDataSource.volumeSnapshotRef.name"
11+
VolumeVolumeSnapshotRefNameField = "spec.volumeSnapshotRef.name"
1212

1313
BucketBucketPoolRefNameField = "spec.bucketPoolRef.name"
1414
BucketBucketClassRefNameField = "spec.bucketClassRef.name"

broker/volumebroker/server/volume_create.go

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"
1919

2020
utilsmaps "github.com/ironcore-dev/ironcore/utils/maps"
21-
"google.golang.org/grpc/codes"
22-
"google.golang.org/grpc/status"
2321
corev1 "k8s.io/api/core/v1"
2422
"k8s.io/apimachinery/pkg/api/resource"
2523
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -32,7 +30,7 @@ type AggregateIronCoreVolume struct {
3230
AccessSecret *corev1.Secret
3331
}
3432

35-
func (s *Server) getIronCoreVolumeConfig(ctx context.Context, volume *iri.Volume) (*AggregateIronCoreVolume, error) {
33+
func (s *Server) getIronCoreVolumeConfig(_ context.Context, volume *iri.Volume) (*AggregateIronCoreVolume, error) {
3634
var volumePoolRef *corev1.LocalObjectReference
3735
if s.volumePoolName != "" {
3836
volumePoolRef = &corev1.LocalObjectReference{
@@ -69,31 +67,18 @@ func (s *Server) getIronCoreVolumeConfig(ctx context.Context, volume *iri.Volume
6967
)
7068

7169
var image string
72-
image = volume.Spec.Image
73-
if dataSource := volume.Spec.VolumeDataSource; dataSource != nil && dataSource.ImageDataSource != nil {
74-
image = dataSource.ImageDataSource.Image
75-
}
76-
7770
var volumeSnapshotRef *corev1.LocalObjectReference
78-
if dataSource := volume.Spec.VolumeDataSource; dataSource != nil && dataSource.SnapshotDataSource != nil {
79-
volumeSnapshot, err := s.findVolumeSnapshotBySnapshotID(ctx, dataSource.SnapshotDataSource.SnapshotId)
80-
if err != nil {
81-
return nil, fmt.Errorf("error finding volume snapshot by snapshot ID %s: %w", dataSource.SnapshotDataSource.SnapshotId, err)
82-
}
83-
if volumeSnapshot == nil {
84-
return nil, status.Errorf(codes.NotFound, "volume snapshot with ID %s not found", dataSource.SnapshotDataSource.SnapshotId)
85-
}
86-
if volumeSnapshot.Status.State != storagev1alpha1.VolumeSnapshotStateReady {
87-
switch volumeSnapshot.Status.State {
88-
case storagev1alpha1.VolumeSnapshotStatePending:
89-
return nil, status.Errorf(codes.FailedPrecondition, "volume snapshot %s is not ready (state: %s)", volumeSnapshot.Name, volumeSnapshot.Status.State)
90-
case storagev1alpha1.VolumeSnapshotStateFailed:
91-
return nil, status.Errorf(codes.Internal, "volume snapshot %s has failed (state: %s)", volumeSnapshot.Name, volumeSnapshot.Status.State)
92-
default:
93-
return nil, status.Errorf(codes.FailedPrecondition, "volume snapshot %s is not ready (state: %s)", volumeSnapshot.Name, volumeSnapshot.Status.State)
94-
}
71+
72+
image = volume.Spec.Image // TODO: Remove this once volume.Spec.Image is deprecated
73+
74+
if dataSource := volume.Spec.VolumeDataSource; dataSource != nil {
75+
switch {
76+
case dataSource.SnapshotDataSource != nil:
77+
volumeSnapshotRef = &corev1.LocalObjectReference{Name: dataSource.SnapshotDataSource.SnapshotId}
78+
image = "" // TODO: Remove this once volume.Spec.Image is deprecated
79+
case dataSource.ImageDataSource != nil:
80+
image = dataSource.ImageDataSource.Image
9581
}
96-
volumeSnapshotRef = &corev1.LocalObjectReference{Name: volumeSnapshot.Name}
9782
}
9883

9984
ironcoreVolume := &storagev1alpha1.Volume{
@@ -111,7 +96,7 @@ func (s *Server) getIronCoreVolumeConfig(ctx context.Context, volume *iri.Volume
11196
Resources: corev1alpha1.ResourceList{
11297
corev1alpha1.ResourceStorage: *resource.NewQuantity(volume.Spec.Resources.StorageBytes, resource.DecimalSI),
11398
},
114-
Image: image, // TODO: Remove this once the image field is deprecated
99+
Image: image, // TODO: Remove this once volume.Spec.Image is deprecated
115100
ImagePullSecretRef: nil, // TODO: Fill if necessary
116101
Encryption: encryption,
117102
VolumeDataSource: storagev1alpha1.VolumeDataSource{
@@ -137,21 +122,6 @@ func getOSImageIfPresent(image string) *string {
137122
return &image
138123
}
139124

140-
func (s *Server) findVolumeSnapshotBySnapshotID(ctx context.Context, snapshotID string) (*storagev1alpha1.VolumeSnapshot, error) {
141-
volumeSnapshotList := &storagev1alpha1.VolumeSnapshotList{}
142-
if err := s.client.List(ctx, volumeSnapshotList, client.InNamespace(s.namespace)); err != nil {
143-
return nil, fmt.Errorf("error listing volume snapshots: %w", err)
144-
}
145-
146-
for _, volumeSnapshot := range volumeSnapshotList.Items {
147-
if volumeSnapshot.Status.SnapshotID == snapshotID {
148-
return &volumeSnapshot, nil
149-
}
150-
}
151-
152-
return nil, nil
153-
}
154-
155125
func (s *Server) createIronCoreVolume(ctx context.Context, log logr.Logger, volume *AggregateIronCoreVolume) (retErr error) {
156126
c, cleanup := s.setupCleaner(ctx, log, &retErr)
157127
defer cleanup()

broker/volumebroker/server/volume_create_test.go

Lines changed: 1 addition & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ import (
1414

1515
. "github.com/onsi/ginkgo/v2"
1616
. "github.com/onsi/gomega"
17-
"google.golang.org/grpc/codes"
18-
"google.golang.org/grpc/status"
1917
corev1 "k8s.io/api/core/v1"
2018
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21-
2219
"sigs.k8s.io/controller-runtime/pkg/client"
2320
)
2421

@@ -104,7 +101,7 @@ var _ = Describe("CreateVolume", func() {
104101
},
105102
VolumeDataSource: &iri.VolumeDataSource{
106103
SnapshotDataSource: &iri.SnapshotDataSource{
107-
SnapshotId: "test-snapshot-id",
104+
SnapshotId: "test-snapshot",
108105
},
109106
},
110107
},
@@ -124,119 +121,4 @@ var _ = Describe("CreateVolume", func() {
124121
Expect(ironcoreVolume.Spec.VolumeDataSource.VolumeSnapshotRef.Name).To(Equal("test-snapshot"))
125122
})
126123

127-
It("should return error if snapshot is not found", func(ctx SpecContext) {
128-
By("creating a volume with non-existent snapshot data source")
129-
res, err := srv.CreateVolume(ctx, &iri.CreateVolumeRequest{
130-
Volume: &iri.Volume{
131-
Metadata: &irimeta.ObjectMetadata{
132-
Labels: map[string]string{
133-
volumepoolletv1alpha1.VolumeUIDLabel: "foobar",
134-
},
135-
},
136-
Spec: &iri.VolumeSpec{
137-
Class: volumeClass.Name,
138-
Resources: &iri.VolumeResources{
139-
StorageBytes: 100,
140-
},
141-
VolumeDataSource: &iri.VolumeDataSource{
142-
SnapshotDataSource: &iri.SnapshotDataSource{
143-
SnapshotId: "non-existent-snapshot-id",
144-
},
145-
},
146-
},
147-
},
148-
})
149-
150-
Expect(err).To(HaveOccurred())
151-
Expect(res).To(BeNil())
152-
Expect(status.Code(err)).To(Equal(codes.NotFound))
153-
})
154-
155-
It("should return error if snapshot is in pending state", func(ctx SpecContext) {
156-
By("creating a volume snapshot in pending state")
157-
volumeSnapshot := &storagev1alpha1.VolumeSnapshot{
158-
ObjectMeta: metav1.ObjectMeta{
159-
Namespace: ns.Name,
160-
Name: "pending-snapshot",
161-
},
162-
Spec: storagev1alpha1.VolumeSnapshotSpec{
163-
VolumeRef: &corev1.LocalObjectReference{Name: "source-volume"},
164-
},
165-
Status: storagev1alpha1.VolumeSnapshotStatus{
166-
State: storagev1alpha1.VolumeSnapshotStatePending,
167-
SnapshotID: "pending-snapshot-id",
168-
},
169-
}
170-
Expect(k8sClient.Create(ctx, volumeSnapshot)).To(Succeed())
171-
172-
By("creating a volume with pending snapshot data source")
173-
res, err := srv.CreateVolume(ctx, &iri.CreateVolumeRequest{
174-
Volume: &iri.Volume{
175-
Metadata: &irimeta.ObjectMetadata{
176-
Labels: map[string]string{
177-
volumepoolletv1alpha1.VolumeUIDLabel: "foobar",
178-
},
179-
},
180-
Spec: &iri.VolumeSpec{
181-
Class: volumeClass.Name,
182-
Resources: &iri.VolumeResources{
183-
StorageBytes: 100,
184-
},
185-
VolumeDataSource: &iri.VolumeDataSource{
186-
SnapshotDataSource: &iri.SnapshotDataSource{
187-
SnapshotId: "pending-snapshot-id",
188-
},
189-
},
190-
},
191-
},
192-
})
193-
194-
Expect(err).To(HaveOccurred())
195-
Expect(res).To(BeNil())
196-
Expect(status.Code(err)).To(Equal(codes.FailedPrecondition))
197-
})
198-
199-
It("should return error if snapshot is in failed state", func(ctx SpecContext) {
200-
By("creating a volume snapshot in failed state")
201-
volumeSnapshot := &storagev1alpha1.VolumeSnapshot{
202-
ObjectMeta: metav1.ObjectMeta{
203-
Namespace: ns.Name,
204-
Name: "failed-snapshot",
205-
},
206-
Spec: storagev1alpha1.VolumeSnapshotSpec{
207-
VolumeRef: &corev1.LocalObjectReference{Name: "source-volume"},
208-
},
209-
Status: storagev1alpha1.VolumeSnapshotStatus{
210-
State: storagev1alpha1.VolumeSnapshotStateFailed,
211-
SnapshotID: "failed-snapshot-id",
212-
},
213-
}
214-
Expect(k8sClient.Create(ctx, volumeSnapshot)).To(Succeed())
215-
216-
By("creating a volume with failed snapshot data source")
217-
res, err := srv.CreateVolume(ctx, &iri.CreateVolumeRequest{
218-
Volume: &iri.Volume{
219-
Metadata: &irimeta.ObjectMetadata{
220-
Labels: map[string]string{
221-
volumepoolletv1alpha1.VolumeUIDLabel: "foobar",
222-
},
223-
},
224-
Spec: &iri.VolumeSpec{
225-
Class: volumeClass.Name,
226-
Resources: &iri.VolumeResources{
227-
StorageBytes: 100,
228-
},
229-
VolumeDataSource: &iri.VolumeDataSource{
230-
SnapshotDataSource: &iri.SnapshotDataSource{
231-
SnapshotId: "failed-snapshot-id",
232-
},
233-
},
234-
},
235-
},
236-
})
237-
238-
Expect(err).To(HaveOccurred())
239-
Expect(res).To(BeNil())
240-
Expect(status.Code(err)).To(Equal(codes.Internal))
241-
})
242124
})

broker/volumebroker/server/volume_snapshot_delete_test.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)