Skip to content

Commit 35b3f46

Browse files
committed
fixes to allow restoration to volume created from snapshot
1 parent 4028d30 commit 35b3f46

6 files changed

Lines changed: 33 additions & 8 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: restore-pod
5+
spec:
6+
containers:
7+
- name: app
8+
image: busybox
9+
command: [ "sleep", "3600" ]
10+
volumeMounts:
11+
- mountPath: /data
12+
name: data
13+
volumes:
14+
- name: data
15+
persistentVolumeClaim:
16+
claimName: snapshot-pvc-1

examples/k8s/snapshot/snapshot.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ metadata:
55
spec:
66
volumeSnapshotClassName: cloudstack-snapshot
77
source:
8-
persistentVolumeClaimName: my-pvc
8+
persistentVolumeClaimName: example-pvc

pkg/cloud/cloud.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Volume struct {
5050
type Snapshot struct {
5151
ID string
5252
Name string
53+
Size int64
5354

5455
DomainID string
5556
ProjectID string

pkg/cloud/snapshots.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (c *client) CreateSnapshot(ctx context.Context, volumeID string) (*Snapshot
4646
snap := Snapshot{
4747
ID: snapshot.Id,
4848
Name: snapshot.Name,
49+
Size: snapshot.Virtualsize,
4950
DomainID: snapshot.Domainid,
5051
ProjectID: snapshot.Projectid,
5152
ZoneID: snapshot.Zoneid,

pkg/cloud/volumes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,15 @@ func (c *client) CreateVolumeFromSnapshot(ctx context.Context, zoneID, name, dom
169169
p.SetProjectid(projectID)
170170
}
171171
p.SetName(name)
172+
p.SetSize(sizeInGB)
172173
p.SetSnapshotid(snapshot.Id)
173174

174175
logger.V(2).Info("CloudStack API call", "command", "CreateVolume", "params", map[string]string{
175176
"name": name,
177+
"size": strconv.FormatInt(sizeInGB, 10),
176178
"snapshotid": snapshotID,
177179
"projectid": projectID,
180+
"zoneid": zoneID,
178181
})
179182
// Execute the API call to create volume from snapshot
180183
vol, err := c.Volume.CreateVolume(p)

pkg/driver/controller.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
127127
return nil, status.Error(codes.InvalidArgument, err.Error())
128128
}
129129

130+
// If creating from snapshot, get the snapshot size
131+
var snapshotSizeGiB int64
130132
if snapshotID != "" {
131133
logger.Info("Creating volume from snapshot", "snapshotID", snapshotID)
132134
// Call the cloud connector's CreateVolumeFromSnapshot if implemented
@@ -138,17 +140,19 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
138140
// Error with CloudStack
139141
return nil, status.Errorf(codes.Internal, "Error %v", err)
140142
}
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)
143+
144+
logger.Info("PVC created with", "size", sizeInGB)
145+
snapshotSizeGiB = util.RoundUpBytesToGB(snapshot.Size)
146+
if snapshotSizeGiB > sizeInGB {
147+
logger.Info("Snapshot size is greater than the request PVC, creating volume from snapshot of size", "snapshot size:", snapshotSizeGiB)
148+
sizeInGB = snapshotSizeGiB
149+
}
150+
148151
volFromSnapshot, err := cs.connector.CreateVolumeFromSnapshot(ctx, snapshot.ZoneID, name, snapshot.DomainID, snapshot.ProjectID, snapshotID, sizeInGB)
149152
if err != nil {
150153
return nil, status.Errorf(codes.Internal, "Cannot create volume from snapshot %s: %v", snapshotID, err.Error())
151154
}
155+
152156
resp := &csi.CreateVolumeResponse{
153157
Volume: &csi.Volume{
154158
VolumeId: volFromSnapshot.ID,

0 commit comments

Comments
 (0)