Skip to content

Commit b9f759c

Browse files
authored
Merge pull request #148 from Madhu-1/add-parent-check
Add check for volumecontent source in CreateVolume
2 parents aba849b + 366d752 commit b9f759c

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

pkg/hostpath/controllerserver.go

+41-19
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,36 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
127127
// Since err is nil, it means the volume with the same name already exists
128128
// need to check if the size of existing volume is the same as in new
129129
// request
130-
if exVol.VolSize >= int64(req.GetCapacityRange().GetRequiredBytes()) {
131-
// existing volume is compatible with new request and should be reused.
132-
// TODO (sbezverk) Do I need to make sure that volume still exists?
133-
return &csi.CreateVolumeResponse{
134-
Volume: &csi.Volume{
135-
VolumeId: exVol.VolID,
136-
CapacityBytes: int64(exVol.VolSize),
137-
VolumeContext: req.GetParameters(),
138-
ContentSource: req.GetVolumeContentSource(),
139-
},
140-
}, nil
130+
if exVol.VolSize < capacity {
131+
return nil, status.Errorf(codes.AlreadyExists, "Volume with the same name: %s but with different size already exist", req.GetName())
132+
}
133+
if req.GetVolumeContentSource() != nil {
134+
volumeSource := req.VolumeContentSource
135+
switch volumeSource.Type.(type) {
136+
case *csi.VolumeContentSource_Snapshot:
137+
if volumeSource.GetSnapshot() != nil && exVol.ParentSnapID != volumeSource.GetSnapshot().GetSnapshotId() {
138+
return nil, status.Error(codes.AlreadyExists, "existing volume source snapshot id not matching")
139+
}
140+
case *csi.VolumeContentSource_Volume:
141+
if volumeSource.GetVolume() != nil && exVol.ParentVolID != volumeSource.GetVolume().GetVolumeId() {
142+
return nil, status.Error(codes.AlreadyExists, "existing volume source volume id not matching")
143+
}
144+
default:
145+
return nil, status.Errorf(codes.InvalidArgument, "%v not a proper volume source", volumeSource)
146+
}
141147
}
142-
return nil, status.Errorf(codes.AlreadyExists, "Volume with the same name: %s but with different size already exist", req.GetName())
148+
// TODO (sbezverk) Do I need to make sure that volume still exists?
149+
return &csi.CreateVolumeResponse{
150+
Volume: &csi.Volume{
151+
VolumeId: exVol.VolID,
152+
CapacityBytes: int64(exVol.VolSize),
153+
VolumeContext: req.GetParameters(),
154+
ContentSource: req.GetVolumeContentSource(),
155+
},
156+
}, nil
143157
}
144158

145159
volumeID := uuid.NewUUID().String()
146-
path := getVolumePath(volumeID)
147160

148161
vol, err := createHostpathVolume(volumeID, req.GetName(), capacity, requestedAccessType, false /* ephemeral */)
149162
if err != nil {
@@ -152,12 +165,21 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
152165
glog.V(4).Infof("created volume %s at path %s", vol.VolID, vol.VolPath)
153166

154167
if req.GetVolumeContentSource() != nil {
155-
contentSource := req.GetVolumeContentSource()
156-
if snapshot := contentSource.GetSnapshot(); snapshot != nil {
157-
err = loadFromSnapshot(capacity, snapshot.GetSnapshotId(), path)
158-
}
159-
if srcVolume := contentSource.GetVolume(); srcVolume != nil {
160-
err = loadFromVolume(capacity, srcVolume.GetVolumeId(), path)
168+
path := getVolumePath(volumeID)
169+
volumeSource := req.VolumeContentSource
170+
switch volumeSource.Type.(type) {
171+
case *csi.VolumeContentSource_Snapshot:
172+
if snapshot := volumeSource.GetSnapshot(); snapshot != nil {
173+
err = loadFromSnapshot(capacity, snapshot.GetSnapshotId(), path)
174+
vol.ParentSnapID = snapshot.GetSnapshotId()
175+
}
176+
case *csi.VolumeContentSource_Volume:
177+
if srcVolume := volumeSource.GetVolume(); srcVolume != nil {
178+
err = loadFromVolume(capacity, srcVolume.GetVolumeId(), path)
179+
vol.ParentVolID = srcVolume.GetVolumeId()
180+
}
181+
default:
182+
err = status.Errorf(codes.InvalidArgument, "%v not a proper volume source", volumeSource)
161183
}
162184
if err != nil {
163185
if delErr := deleteHostpathVolume(volumeID); delErr != nil {

pkg/hostpath/hostpath.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package hostpath
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"io"
2223
"os"
@@ -59,6 +60,8 @@ type hostPathVolume struct {
5960
VolSize int64 `json:"volSize"`
6061
VolPath string `json:"volPath"`
6162
VolAccessType accessType `json:"volAccessType"`
63+
ParentVolID string `json:"parentVolID,omitempty"`
64+
ParentSnapID string `json:"parentSnapID,omitempty"`
6265
Ephemeral bool `json:"ephemeral"`
6366
}
6467

@@ -93,15 +96,15 @@ func init() {
9396

9497
func NewHostPathDriver(driverName, nodeID, endpoint string, ephemeral bool, maxVolumesPerNode int64, version string) (*hostPath, error) {
9598
if driverName == "" {
96-
return nil, fmt.Errorf("No driver name provided")
99+
return nil, errors.New("no driver name provided")
97100
}
98101

99102
if nodeID == "" {
100-
return nil, fmt.Errorf("No node id provided")
103+
return nil, errors.New("no node id provided")
101104
}
102105

103106
if endpoint == "" {
104-
return nil, fmt.Errorf("No driver endpoint provided")
107+
return nil, errors.New("no driver endpoint provided")
105108
}
106109
if version != "" {
107110
vendorVersion = version

0 commit comments

Comments
 (0)