Skip to content

Commit

Permalink
Merge pull request #148 from Madhu-1/add-parent-check
Browse files Browse the repository at this point in the history
Add check for  volumecontent source in CreateVolume
  • Loading branch information
k8s-ci-robot authored Feb 13, 2020
2 parents aba849b + 366d752 commit b9f759c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
60 changes: 41 additions & 19 deletions pkg/hostpath/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,36 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
// Since err is nil, it means the volume with the same name already exists
// need to check if the size of existing volume is the same as in new
// request
if exVol.VolSize >= int64(req.GetCapacityRange().GetRequiredBytes()) {
// existing volume is compatible with new request and should be reused.
// TODO (sbezverk) Do I need to make sure that volume still exists?
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: exVol.VolID,
CapacityBytes: int64(exVol.VolSize),
VolumeContext: req.GetParameters(),
ContentSource: req.GetVolumeContentSource(),
},
}, nil
if exVol.VolSize < capacity {
return nil, status.Errorf(codes.AlreadyExists, "Volume with the same name: %s but with different size already exist", req.GetName())
}
if req.GetVolumeContentSource() != nil {
volumeSource := req.VolumeContentSource
switch volumeSource.Type.(type) {
case *csi.VolumeContentSource_Snapshot:
if volumeSource.GetSnapshot() != nil && exVol.ParentSnapID != volumeSource.GetSnapshot().GetSnapshotId() {
return nil, status.Error(codes.AlreadyExists, "existing volume source snapshot id not matching")
}
case *csi.VolumeContentSource_Volume:
if volumeSource.GetVolume() != nil && exVol.ParentVolID != volumeSource.GetVolume().GetVolumeId() {
return nil, status.Error(codes.AlreadyExists, "existing volume source volume id not matching")
}
default:
return nil, status.Errorf(codes.InvalidArgument, "%v not a proper volume source", volumeSource)
}
}
return nil, status.Errorf(codes.AlreadyExists, "Volume with the same name: %s but with different size already exist", req.GetName())
// TODO (sbezverk) Do I need to make sure that volume still exists?
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: exVol.VolID,
CapacityBytes: int64(exVol.VolSize),
VolumeContext: req.GetParameters(),
ContentSource: req.GetVolumeContentSource(),
},
}, nil
}

volumeID := uuid.NewUUID().String()
path := getVolumePath(volumeID)

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

if req.GetVolumeContentSource() != nil {
contentSource := req.GetVolumeContentSource()
if snapshot := contentSource.GetSnapshot(); snapshot != nil {
err = loadFromSnapshot(capacity, snapshot.GetSnapshotId(), path)
}
if srcVolume := contentSource.GetVolume(); srcVolume != nil {
err = loadFromVolume(capacity, srcVolume.GetVolumeId(), path)
path := getVolumePath(volumeID)
volumeSource := req.VolumeContentSource
switch volumeSource.Type.(type) {
case *csi.VolumeContentSource_Snapshot:
if snapshot := volumeSource.GetSnapshot(); snapshot != nil {
err = loadFromSnapshot(capacity, snapshot.GetSnapshotId(), path)
vol.ParentSnapID = snapshot.GetSnapshotId()
}
case *csi.VolumeContentSource_Volume:
if srcVolume := volumeSource.GetVolume(); srcVolume != nil {
err = loadFromVolume(capacity, srcVolume.GetVolumeId(), path)
vol.ParentVolID = srcVolume.GetVolumeId()
}
default:
err = status.Errorf(codes.InvalidArgument, "%v not a proper volume source", volumeSource)
}
if err != nil {
if delErr := deleteHostpathVolume(volumeID); delErr != nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package hostpath

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -59,6 +60,8 @@ type hostPathVolume struct {
VolSize int64 `json:"volSize"`
VolPath string `json:"volPath"`
VolAccessType accessType `json:"volAccessType"`
ParentVolID string `json:"parentVolID,omitempty"`
ParentSnapID string `json:"parentSnapID,omitempty"`
Ephemeral bool `json:"ephemeral"`
}

Expand Down Expand Up @@ -93,15 +96,15 @@ func init() {

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

if nodeID == "" {
return nil, fmt.Errorf("No node id provided")
return nil, errors.New("no node id provided")
}

if endpoint == "" {
return nil, fmt.Errorf("No driver endpoint provided")
return nil, errors.New("no driver endpoint provided")
}
if version != "" {
vendorVersion = version
Expand Down

0 comments on commit b9f759c

Please sign in to comment.