Skip to content

Commit daaee08

Browse files
committed
Fix restore size type in snapshot content
Change the restore size to int64 for snapshot content. Also add the code to update the restore size for snapshot content.
1 parent 631b9dc commit daaee08

File tree

5 files changed

+43
-19
lines changed

5 files changed

+43
-19
lines changed

pkg/apis/volumesnapshot/v1alpha1/types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,8 @@ type VolumeSnapshotStatus struct {
114114
// TODO: After TypedLocalObjectReference is merged into the in-tree core API, this will be replaced.
115115
type TypedLocalObjectReference struct {
116116
// Name of the referent.
117-
// +optional
118117
Name string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"`
119118
// Kind of the referent.
120-
// +optional
121119
Kind string `json:"kind,omitempty" protobuf:"bytes,2,opt,name=kind"`
122120
}
123121

@@ -241,5 +239,5 @@ type CSIVolumeSnapshotSource struct {
241239
// larger than the RestoreSize if it is specified. If RestoreSize is set to nil, it means
242240
// that the storage plugin does not have this information available.
243241
// +optional
244-
RestoreSize *resource.Quantity `json:"restoreSize" protobuf:"bytes,4,opt,name=restoreSize"`
242+
RestoreSize *int64 `json:"restoreSize,omitempty" protobuf:"bytes,4,opt,name=restoreSize"`
245243
}

pkg/apis/volumesnapshot/v1alpha1/zz_generated.deepcopy.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/framework_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ func newTestController(kubeClient kubernetes.Interface, clientset clientset.Inte
743743
}
744744

745745
// newContent returns a new content with given attributes
746-
func newContent(name, className, snapshotHandle, volumeUID, volumeName, boundToSnapshotUID, boundToSnapshotName string, size *resource.Quantity, creationTime *int64) *crdv1.VolumeSnapshotContent {
746+
func newContent(name, className, snapshotHandle, volumeUID, volumeName, boundToSnapshotUID, boundToSnapshotName string, size *int64, creationTime *int64) *crdv1.VolumeSnapshotContent {
747747
content := crdv1.VolumeSnapshotContent{
748748
ObjectMeta: metav1.ObjectMeta{
749749
Name: name,
@@ -780,7 +780,7 @@ func newContent(name, className, snapshotHandle, volumeUID, volumeName, boundToS
780780
return &content
781781
}
782782

783-
func newContentArray(name, className, snapshotHandle, volumeUID, volumeName, boundToSnapshotUID, boundToSnapshotName string, size *resource.Quantity, creationTime *int64) []*crdv1.VolumeSnapshotContent {
783+
func newContentArray(name, className, snapshotHandle, volumeUID, volumeName, boundToSnapshotUID, boundToSnapshotName string, size *int64, creationTime *int64) []*crdv1.VolumeSnapshotContent {
784784
return []*crdv1.VolumeSnapshotContent{
785785
newContent(name, className, snapshotHandle, volumeUID, volumeName, boundToSnapshotUID, boundToSnapshotName, size, creationTime),
786786
}

pkg/controller/snapshot_controller.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (ctrl *csiSnapshotController) syncReadySnapshot(snapshot *crdv1.VolumeSnaps
170170
return fmt.Errorf("Cannot convert object from snapshot content store to VolumeSnapshotContent %q!?: %#v", snapshot.Spec.SnapshotContentName, obj)
171171
}
172172

173-
glog.V(5).Infof("syncCompleteSnapshot[%s]: VolumeSnapshotContent %q found", snapshotKey(snapshot), content.Name)
173+
glog.V(5).Infof("syncReadySnapshot[%s]: VolumeSnapshotContent %q found", snapshotKey(snapshot), content.Name)
174174
if !IsSnapshotBound(snapshot, content) {
175175
// snapshot is bound but content is not bound to snapshot correctly
176176
if err = ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotMisbound", "VolumeSnapshotContent is not bound to the VolumeSnapshot correctly"); err != nil {
@@ -212,7 +212,7 @@ func (ctrl *csiSnapshotController) syncUnreadySnapshot(snapshot *crdv1.VolumeSna
212212

213213
// snapshot is already bound correctly, check the status and update if it is ready.
214214
glog.V(5).Infof("Check and update snapshot %s status", uniqueSnapshotName)
215-
if err = ctrl.checkandUpdateSnapshotStatus(snapshot, content); err != nil {
215+
if err = ctrl.checkandUpdateBoundSnapshotStatus(snapshot, content); err != nil {
216216
return err
217217
}
218218
return nil
@@ -321,11 +321,11 @@ func (ctrl *csiSnapshotController) createSnapshot(snapshot *crdv1.VolumeSnapshot
321321
return nil
322322
}
323323

324-
func (ctrl *csiSnapshotController) checkandUpdateSnapshotStatus(snapshot *crdv1.VolumeSnapshot, content *crdv1.VolumeSnapshotContent) error {
324+
func (ctrl *csiSnapshotController) checkandUpdateBoundSnapshotStatus(snapshot *crdv1.VolumeSnapshot, content *crdv1.VolumeSnapshotContent) error {
325325
glog.V(5).Infof("checkandUpdateSnapshotStatus[%s] started", snapshotKey(snapshot))
326326
opName := fmt.Sprintf("check-%s[%s]", snapshotKey(snapshot), string(snapshot.UID))
327327
ctrl.scheduleOperation(opName, func() error {
328-
snapshotObj, err := ctrl.checkandUpdateSnapshotStatusOperation(snapshot, content)
328+
snapshotObj, err := ctrl.checkandUpdateBoundSnapshotStatusOperation(snapshot, content)
329329
if err != nil {
330330
ctrl.updateSnapshotErrorStatusWithEvent(snapshot, v1.EventTypeWarning, "SnapshotCheckandUpdateFailed", fmt.Sprintf("Failed to check and update snapshot: %v", err))
331331
glog.Errorf("checkandUpdateSnapshotStatus [%s]: error occured %v", snapshotKey(snapshot), err)
@@ -420,7 +420,7 @@ func (ctrl *csiSnapshotController) checkandBindSnapshotContent(snapshot *crdv1.V
420420
return nil
421421
}
422422

423-
func (ctrl *csiSnapshotController) checkandUpdateSnapshotStatusOperation(snapshot *crdv1.VolumeSnapshot, content *crdv1.VolumeSnapshotContent) (*crdv1.VolumeSnapshot, error) {
423+
func (ctrl *csiSnapshotController) checkandUpdateBoundSnapshotStatusOperation(snapshot *crdv1.VolumeSnapshot, content *crdv1.VolumeSnapshotContent) (*crdv1.VolumeSnapshot, error) {
424424
status, _, size, err := ctrl.handler.GetSnapshotStatus(content)
425425
if err != nil {
426426
return nil, fmt.Errorf("failed to check snapshot status %s with error %v", snapshot.Name, err)
@@ -430,6 +430,10 @@ func (ctrl *csiSnapshotController) checkandUpdateSnapshotStatusOperation(snapsho
430430
if err != nil {
431431
return nil, err
432432
}
433+
err = ctrl.updateSnapshotContentSize(content, size)
434+
if err != nil {
435+
return nil, err
436+
}
433437
return newSnapshot, nil
434438
}
435439

@@ -521,7 +525,7 @@ func (ctrl *csiSnapshotController) createSnapshotOperation(snapshot *crdv1.Volum
521525
Driver: driverName,
522526
SnapshotHandle: snapshotID,
523527
CreationTime: &timestamp,
524-
RestoreSize: resource.NewQuantity(size, resource.BinarySI),
528+
RestoreSize: &size,
525529
},
526530
},
527531
VolumeSnapshotClassName: &(class.Name),
@@ -637,6 +641,28 @@ func (ctrl *csiSnapshotController) bindandUpdateVolumeSnapshot(snapshotContent *
637641
return snapshotCopy, nil
638642
}
639643

644+
// updateSnapshotContentSize update the restore size for snapshot content
645+
func (ctrl *csiSnapshotController) updateSnapshotContentSize(content *crdv1.VolumeSnapshotContent, size int64) error {
646+
if content.Spec.VolumeSnapshotSource.CSI == nil || size <= 0 {
647+
return nil
648+
}
649+
if content.Spec.VolumeSnapshotSource.CSI.RestoreSize != nil && *content.Spec.VolumeSnapshotSource.CSI.RestoreSize == size {
650+
return nil
651+
}
652+
contentClone := content.DeepCopy()
653+
contentClone.Spec.VolumeSnapshotSource.CSI.RestoreSize = &size
654+
_, err := ctrl.clientset.VolumesnapshotV1alpha1().VolumeSnapshotContents().Update(contentClone)
655+
if err != nil {
656+
return newControllerUpdateError(content.Name, err.Error())
657+
}
658+
659+
_, err = ctrl.storeContentUpdate(contentClone)
660+
if err != nil {
661+
glog.Errorf("failed to update content store %v", err)
662+
}
663+
return nil
664+
}
665+
640666
// UpdateSnapshotStatus converts snapshot status to crdv1.VolumeSnapshotCondition
641667
func (ctrl *csiSnapshotController) updateSnapshotStatus(snapshot *crdv1.VolumeSnapshot, csistatus *csi.SnapshotStatus, createdAt, size int64, bound bool) (*crdv1.VolumeSnapshot, error) {
642668
glog.V(5).Infof("updating VolumeSnapshot[]%s, set status %v, timestamp %v", snapshotKey(snapshot), csistatus, createdAt)

pkg/controller/snapshot_create_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestCreateSnapshotSync(t *testing.T) {
6666
{
6767
name: "6-1 - successful create snapshot with snapshot class gold",
6868
initialContents: nocontents,
69-
expectedContents: newContentArray("snapcontent-snapuid6-1", classGold, "sid6-1", "pv-uid6-1", "volume6-1", "snapuid6-1", "snap6-1", getSize(defaultSize), &timeNow),
69+
expectedContents: newContentArray("snapcontent-snapuid6-1", classGold, "sid6-1", "pv-uid6-1", "volume6-1", "snapuid6-1", "snap6-1", &defaultSize, &timeNow),
7070
initialSnapshots: newSnapshotArray("snap6-1", classGold, "", "snapuid6-1", "claim6-1", false, nil, nil, nil),
7171
expectedSnapshots: newSnapshotArray("snap6-1", classGold, "snapcontent-snapuid6-1", "snapuid6-1", "claim6-1", false, nil, metaTimeNowUnix, getSize(defaultSize)),
7272
initialClaims: newClaimArray("claim6-1", "pvc-uid6-1", "1Gi", "volume6-1", v1.ClaimBound, &classEmpty),
@@ -93,7 +93,7 @@ func TestCreateSnapshotSync(t *testing.T) {
9393
{
9494
name: "6-2 - successful create snapshot with snapshot class silver",
9595
initialContents: nocontents,
96-
expectedContents: newContentArray("snapcontent-snapuid6-2", classSilver, "sid6-2", "pv-uid6-2", "volume6-2", "snapuid6-2", "snap6-2", getSize(defaultSize), &timeNow),
96+
expectedContents: newContentArray("snapcontent-snapuid6-2", classSilver, "sid6-2", "pv-uid6-2", "volume6-2", "snapuid6-2", "snap6-2", &defaultSize, &timeNow),
9797
initialSnapshots: newSnapshotArray("snap6-2", classSilver, "", "snapuid6-2", "claim6-2", false, nil, nil, nil),
9898
expectedSnapshots: newSnapshotArray("snap6-2", classSilver, "snapcontent-snapuid6-2", "snapuid6-2", "claim6-2", false, nil, metaTimeNowUnix, getSize(defaultSize)),
9999
initialClaims: newClaimArray("claim6-2", "pvc-uid6-2", "1Gi", "volume6-2", v1.ClaimBound, &classEmpty),
@@ -120,7 +120,7 @@ func TestCreateSnapshotSync(t *testing.T) {
120120
{
121121
name: "6-3 - successful create snapshot with snapshot class valid-secret-class",
122122
initialContents: nocontents,
123-
expectedContents: newContentArray("snapcontent-snapuid6-3", validSecretClass, "sid6-3", "pv-uid6-3", "volume6-3", "snapuid6-3", "snap6-3", getSize(defaultSize), &timeNow),
123+
expectedContents: newContentArray("snapcontent-snapuid6-3", validSecretClass, "sid6-3", "pv-uid6-3", "volume6-3", "snapuid6-3", "snap6-3", &defaultSize, &timeNow),
124124
initialSnapshots: newSnapshotArray("snap6-3", validSecretClass, "", "snapuid6-3", "claim6-3", false, nil, nil, nil),
125125
expectedSnapshots: newSnapshotArray("snap6-3", validSecretClass, "snapcontent-snapuid6-3", "snapuid6-3", "claim6-3", false, nil, metaTimeNowUnix, getSize(defaultSize)),
126126
initialClaims: newClaimArray("claim6-3", "pvc-uid6-3", "1Gi", "volume6-3", v1.ClaimBound, &classEmpty),
@@ -149,7 +149,7 @@ func TestCreateSnapshotSync(t *testing.T) {
149149
{
150150
name: "6-4 - successful create snapshot with snapshot class empty-secret-class",
151151
initialContents: nocontents,
152-
expectedContents: newContentArray("snapcontent-snapuid6-4", emptySecretClass, "sid6-4", "pv-uid6-4", "volume6-4", "snapuid6-4", "snap6-4", getSize(defaultSize), &timeNow),
152+
expectedContents: newContentArray("snapcontent-snapuid6-4", emptySecretClass, "sid6-4", "pv-uid6-4", "volume6-4", "snapuid6-4", "snap6-4", &defaultSize, &timeNow),
153153
initialSnapshots: newSnapshotArray("snap6-4", emptySecretClass, "", "snapuid6-4", "claim6-4", false, nil, nil, nil),
154154
expectedSnapshots: newSnapshotArray("snap6-4", emptySecretClass, "snapcontent-snapuid6-4", "snapuid6-4", "claim6-4", false, nil, metaTimeNowUnix, getSize(defaultSize)),
155155
initialClaims: newClaimArray("claim6-4", "pvc-uid6-4", "1Gi", "volume6-4", v1.ClaimBound, &classEmpty),
@@ -178,7 +178,7 @@ func TestCreateSnapshotSync(t *testing.T) {
178178
{
179179
name: "6-5 - successful create snapshot with status uploading",
180180
initialContents: nocontents,
181-
expectedContents: newContentArray("snapcontent-snapuid6-5", classGold, "sid6-5", "pv-uid6-5", "volume6-5", "snapuid6-5", "snap6-5", getSize(defaultSize), &timeNow),
181+
expectedContents: newContentArray("snapcontent-snapuid6-5", classGold, "sid6-5", "pv-uid6-5", "volume6-5", "snapuid6-5", "snap6-5", &defaultSize, &timeNow),
182182
initialSnapshots: newSnapshotArray("snap6-5", classGold, "", "snapuid6-5", "claim6-5", false, nil, nil, nil),
183183
expectedSnapshots: newSnapshotArray("snap6-5", classGold, "snapcontent-snapuid6-5", "snapuid6-5", "claim6-5", false, nil, metaTimeNowUnix, getSize(defaultSize)),
184184
initialClaims: newClaimArray("claim6-5", "pvc-uid6-5", "1Gi", "volume6-5", v1.ClaimBound, &classEmpty),
@@ -205,7 +205,7 @@ func TestCreateSnapshotSync(t *testing.T) {
205205
{
206206
name: "6-6 - successful create snapshot with status error uploading",
207207
initialContents: nocontents,
208-
expectedContents: newContentArray("snapcontent-snapuid6-6", classGold, "sid6-6", "pv-uid6-6", "volume6-6", "snapuid6-6", "snap6-6", getSize(defaultSize), &timeNow),
208+
expectedContents: newContentArray("snapcontent-snapuid6-6", classGold, "sid6-6", "pv-uid6-6", "volume6-6", "snapuid6-6", "snap6-6", &defaultSize, &timeNow),
209209
initialSnapshots: newSnapshotArray("snap6-6", classGold, "", "snapuid6-6", "claim6-6", false, nil, nil, nil),
210210
expectedSnapshots: newSnapshotArray("snap6-6", classGold, "snapcontent-snapuid6-6", "snapuid6-6", "claim6-6", false, nil, metaTimeNowUnix, getSize(defaultSize)),
211211
initialClaims: newClaimArray("claim6-6", "pvc-uid6-6", "1Gi", "volume6-6", v1.ClaimBound, &classEmpty),

0 commit comments

Comments
 (0)