Skip to content

Commit 4028d30

Browse files
committed
remove diskoffering id when creating volume from snapshot
1 parent 88f4ef5 commit 4028d30

5 files changed

Lines changed: 54 additions & 21 deletions

File tree

pkg/cloud/cloud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Interface interface {
2424
DetachVolume(ctx context.Context, volumeID string) error
2525
ExpandVolume(ctx context.Context, volumeID string, newSizeInGB int64) error
2626

27-
CreateVolumeFromSnapshot(ctx context.Context, diskOfferingID, zoneID, name, domainID, projectID, snapshotID string, sizeInGB int64) (string, error)
27+
CreateVolumeFromSnapshot(ctx context.Context, zoneID, name, domainID, projectID, snapshotID string, sizeInGB int64) (*Volume, error)
2828
GetSnapshotByID(ctx context.Context, snapshotID ...string) (*Snapshot, error)
2929
CreateSnapshot(ctx context.Context, volumeID string) (*Snapshot, error)
3030
DeleteSnapshot(ctx context.Context, snapshotID string) error

pkg/cloud/fake/fake.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ func (f *fakeConnector) ExpandVolume(_ context.Context, volumeID string, newSize
137137
return cloud.ErrNotFound
138138
}
139139

140-
func (f *fakeConnector) CreateVolumeFromSnapshot(ctx context.Context, diskOfferingID, zoneID, name, domainID, projectID, snapshotID string, sizeInGB int64) (string, error) {
141-
return "1", nil
140+
func (f *fakeConnector) CreateVolumeFromSnapshot(ctx context.Context, zoneID, name, domainID, projectID, snapshotID string, sizeInGB int64) (*cloud.Volume, error) {
141+
return nil, nil
142142
}
143143

144144
func (f *fakeConnector) GetSnapshotByID(ctx context.Context, snapshotID ...string) (*cloud.Snapshot, error) {

pkg/cloud/volumes.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,14 @@ func (c *client) ExpandVolume(ctx context.Context, volumeID string, newSizeInGB
156156
return nil
157157
}
158158

159-
func (c *client) CreateVolumeFromSnapshot(ctx context.Context, diskOfferingID, zoneID, name, domainID, projectID, snapshotID string, sizeInGB int64) (string, error) {
159+
func (c *client) CreateVolumeFromSnapshot(ctx context.Context, zoneID, name, domainID, projectID, snapshotID string, sizeInGB int64) (*Volume, error) {
160160
logger := klog.FromContext(ctx)
161161
snapshot, _, err := c.Snapshot.GetSnapshotByID(snapshotID)
162162
if err != nil {
163-
return "", fmt.Errorf("failed to retrieve snapshot '%s': %w", snapshotID, err)
163+
return nil, fmt.Errorf("failed to retrieve snapshot '%s': %w", snapshotID, err)
164164
}
165165

166166
p := c.Volume.NewCreateVolumeParams()
167-
p.SetDiskofferingid(diskOfferingID)
168167
p.SetZoneid(zoneID)
169168
if projectID != "" {
170169
p.SetProjectid(projectID)
@@ -181,8 +180,20 @@ func (c *client) CreateVolumeFromSnapshot(ctx context.Context, diskOfferingID, z
181180
vol, err := c.Volume.CreateVolume(p)
182181
if err != nil {
183182
// Handle the error accordingly
184-
return "", fmt.Errorf("failed to create volume from snapshot'%s': %w", snapshotID, err)
183+
return nil, fmt.Errorf("failed to create volume from snapshot'%s': %w", snapshotID, err)
184+
}
185+
186+
v := Volume{
187+
ID: vol.Id,
188+
Name: vol.Name,
189+
Size: vol.Size,
190+
DiskOfferingID: vol.Diskofferingid,
191+
DomainID: vol.Domainid,
192+
ProjectID: vol.Projectid,
193+
ZoneID: vol.Zoneid,
194+
VirtualMachineID: vol.Virtualmachineid,
195+
DeviceID: strconv.FormatInt(vol.Deviceid, 10),
185196
}
186197

187-
return vol.Id, err
198+
return &v, nil
188199
}

pkg/driver/controller.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package driver
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"math/rand"
@@ -118,34 +119,50 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
118119
}
119120
}
120121

122+
// We have to create the volume.
123+
124+
// Determine volume size using requested capacity range.
125+
sizeInGB, err := determineSize(req)
126+
if err != nil {
127+
return nil, status.Error(codes.InvalidArgument, err.Error())
128+
}
129+
121130
if snapshotID != "" {
122131
logger.Info("Creating volume from snapshot", "snapshotID", snapshotID)
123132
// Call the cloud connector's CreateVolumeFromSnapshot if implemented
124-
volID, err := cs.connector.CreateVolumeFromSnapshot(ctx, diskOfferingID, vol.ZoneID, name, vol.DomainID, vol.ProjectID, snapshotID, vol.Size)
133+
printVolumeAsJSON(req)
134+
snapshot, err := cs.connector.GetSnapshotByID(ctx, snapshotID)
135+
if errors.Is(err, cloud.ErrNotFound) {
136+
return nil, status.Errorf(codes.NotFound, "Snapshot %v not found", snapshotID)
137+
} else if err != nil {
138+
// Error with CloudStack
139+
return nil, status.Errorf(codes.Internal, "Error %v", err)
140+
}
141+
logger.Info("Disk offering ID", "diskOfferingID: ", diskOfferingID)
142+
logger.Info("Zone ID", "ZoneID", snapshot.ZoneID)
143+
logger.Info("Name", "name", name)
144+
logger.Info("Domain ID", "DomainID", snapshot.DomainID)
145+
logger.Info("Project ID", "ProjectID", snapshot.ProjectID)
146+
logger.Info("Snapshot ID", "snapshotID", snapshotID)
147+
logger.Info("Volume size", "Size", sizeInGB)
148+
volFromSnapshot, err := cs.connector.CreateVolumeFromSnapshot(ctx, snapshot.ZoneID, name, snapshot.DomainID, snapshot.ProjectID, snapshotID, sizeInGB)
125149
if err != nil {
126150
return nil, status.Errorf(codes.Internal, "Cannot create volume from snapshot %s: %v", snapshotID, err.Error())
127151
}
128152
resp := &csi.CreateVolumeResponse{
129153
Volume: &csi.Volume{
130-
VolumeId: volID,
131-
CapacityBytes: vol.Size,
154+
VolumeId: volFromSnapshot.ID,
155+
CapacityBytes: volFromSnapshot.Size,
132156
VolumeContext: req.GetParameters(),
157+
ContentSource: req.GetVolumeContentSource(),
133158
AccessibleTopology: []*csi.Topology{
134-
Topology{ZoneID: vol.ZoneID}.ToCSI(),
159+
Topology{ZoneID: volFromSnapshot.ZoneID}.ToCSI(),
135160
},
136161
},
137162
}
138163
return resp, nil
139164
}
140165

141-
// We have to create the volume.
142-
143-
// Determine volume size using requested capacity range.
144-
sizeInGB, err := determineSize(req)
145-
if err != nil {
146-
return nil, status.Error(codes.InvalidArgument, err.Error())
147-
}
148-
149166
// Determine zone using topology constraints.
150167
var zoneID string
151168
topologyRequirement := req.GetAccessibilityRequirements()
@@ -189,7 +206,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
189206
VolumeId: volID,
190207
CapacityBytes: util.GigaBytesToBytes(sizeInGB),
191208
VolumeContext: req.GetParameters(),
192-
// ContentSource: req.GetVolumeContentSource(), TODO: snapshot support.
209+
ContentSource: req.GetVolumeContentSource(),
193210
AccessibleTopology: []*csi.Topology{
194211
Topology{ZoneID: zoneID}.ToCSI(),
195212
},
@@ -199,6 +216,11 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
199216
return resp, nil
200217
}
201218

219+
func printVolumeAsJSON(vol *csi.CreateVolumeRequest) {
220+
b, _ := json.MarshalIndent(vol, "", " ")
221+
fmt.Println(string(b))
222+
}
223+
202224
func checkVolumeSuitable(vol *cloud.Volume,
203225
diskOfferingID string, capRange *csi.CapacityRange, topologyRequirement *csi.TopologyRequirement,
204226
) (bool, string) {

0 commit comments

Comments
 (0)