Skip to content

Commit 6e0ac4c

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 6e0ac4c

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

pkg/apis/volumesnapshot/v1alpha1/types.go

+1-3
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

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/framework_test.go

+2-2
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

+32-6
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 > 0 {
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)

0 commit comments

Comments
 (0)