Skip to content

Commit

Permalink
Return Capacity in Create Volume Request
Browse files Browse the repository at this point in the history
After PR [1], volume capacity needs to be returned by csi plugin
This commit returns volume capacity in create volume request

[1]kubernetes-csi/external-provisioner#76
  • Loading branch information
adisky committed Aug 21, 2018
1 parent 127ed03 commit af49aec
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
8 changes: 5 additions & 3 deletions pkg/csi/cinder/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol

resID := ""
resAvailability := ""
resSize := 0

if len(volumes) == 1 {
resID = volumes[0].ID
Expand All @@ -77,19 +78,20 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
return nil, errors.New("multiple volumes reported by Cinder with same name")
} else {
// Volume Create
resID, resAvailability, err = cloud.CreateVolume(volName, volSizeGB, volType, volAvailability, nil)
resID, resAvailability, resSize, err = cloud.CreateVolume(volName, volSizeGB, volType, volAvailability, nil)
if err != nil {
glog.V(3).Infof("Failed to CreateVolume: %v", err)
return nil, err
}

glog.V(4).Infof("Create volume %s in Availability Zone: %s", resID, resAvailability)
glog.V(4).Infof("Create volume %s in Availability Zone: %s of size %s GiB", resID, resAvailability, resSize)

}

return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
Id: resID,
Id: resID,
CapacityBytes: int64(resSize * 1024 * 1024 * 1024),
Attributes: map[string]string{
"availability": resAvailability,
},
Expand Down
9 changes: 6 additions & 3 deletions pkg/csi/cinder/controllerserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func TestCreateVolume(t *testing.T) {

// mock OpenStack
osmock := new(openstack.OpenStackMock)
// CreateVolume(name string, size int, vtype, availability string, tags *map[string]string) (string, string, error)
osmock.On("CreateVolume", fakeVolName, mock.AnythingOfType("int"), fakeVolType, fakeAvailability, (*map[string]string)(nil)).Return(fakeVolID, fakeAvailability, nil)
// CreateVolume(name string, size int, vtype, availability string, tags *map[string]string) (string, string, int, error)
osmock.On("CreateVolume", fakeVolName, mock.AnythingOfType("int"), fakeVolType, fakeAvailability, (*map[string]string)(nil)).Return(fakeVolID, fakeAvailability, fakeCapacityGiB, nil)
openstack.OsInstance = osmock

// Init assert
Expand All @@ -62,17 +62,20 @@ func TestCreateVolume(t *testing.T) {
// Assert
assert.NotNil(actualRes.Volume)

assert.NotNil(actualRes.Volume.CapacityBytes)

assert.NotEqual(0, len(actualRes.Volume.Id), "Volume Id is nil")

assert.Equal(fakeAvailability, actualRes.Volume.Attributes["availability"])

}

// Test CreateVolumeDuplicate
func TestCreateVolumeDuplicate(t *testing.T) {

// mock OpenStack
osmock := new(openstack.OpenStackMock)
osmock.On("CreateVolume", fakeVolName, mock.AnythingOfType("int"), fakeVolType, fakeAvailability, (*map[string]string)(nil)).Return(fakeVolID, fakeAvailability, nil)
osmock.On("CreateVolume", fakeVolName, mock.AnythingOfType("int"), fakeVolType, fakeAvailability, (*map[string]string)(nil)).Return(fakeVolID, fakeAvailability, fakeCapacityGiB, nil)
openstack.OsInstance = osmock

// Init assert
Expand Down
1 change: 1 addition & 0 deletions pkg/csi/cinder/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var fakeConfig = "/etc/cloud.conf"
var fakeCtx = context.Background()
var fakeVolName = "CSIVolumeName"
var fakeVolID = "CSIVolumeID"
var fakeCapacityGiB = 1
var fakeVolType = ""
var fakeAvailability = ""
var fakeDevicePath = "/dev/xxx"
Expand Down
2 changes: 1 addition & 1 deletion pkg/csi/cinder/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

type IOpenStack interface {
CreateVolume(name string, size int, vtype, availability string, tags *map[string]string) (string, string, error)
CreateVolume(name string, size int, vtype, availability string, tags *map[string]string) (string, string, int, error)
DeleteVolume(volumeID string) error
AttachVolume(instanceID, volumeID string) (string, error)
WaitDiskAttached(instanceID string, volumeID string) error
Expand Down
17 changes: 12 additions & 5 deletions pkg/csi/cinder/openstack/openstack_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (_m *OpenStackMock) AttachVolume(instanceID string, volumeID string) (strin
}

// CreateVolume provides a mock function with given fields: name, size, vtype, availability, tags
func (_m *OpenStackMock) CreateVolume(name string, size int, vtype string, availability string, tags *map[string]string) (string, string, error) {
func (_m *OpenStackMock) CreateVolume(name string, size int, vtype string, availability string, tags *map[string]string) (string, string, int, error) {
ret := _m.Called(name, size, vtype, availability, tags)

var r0 string
Expand All @@ -91,14 +91,21 @@ func (_m *OpenStackMock) CreateVolume(name string, size int, vtype string, avail
r1 = ret.Get(1).(string)
}

var r2 error
if rf, ok := ret.Get(2).(func(string, int, string, string, *map[string]string) error); ok {
var r2 int
if rf, ok := ret.Get(2).(func(string, int, string, string, *map[string]string) int); ok {
r2 = rf(name, size, vtype, availability, tags)
} else {
r2 = ret.Error(2)
r2 = ret.Get(2).(int)
}

return r0, r1, r2
var r3 error
if rf, ok := ret.Get(3).(func(string, int, string, string, *map[string]string) error); ok {
r3 = rf(name, size, vtype, availability, tags)
} else {
r3 = ret.Error(3)
}

return r0, r1, r2, r3
}

// DeleteVolume provides a mock function with given fields: volumeID
Expand Down
6 changes: 3 additions & 3 deletions pkg/csi/cinder/openstack/openstack_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Volume struct {
}

// CreateVolume creates a volume of given size
func (os *OpenStack) CreateVolume(name string, size int, vtype, availability string, tags *map[string]string) (string, string, error) {
func (os *OpenStack) CreateVolume(name string, size int, vtype, availability string, tags *map[string]string) (string, string, int, error) {
opts := &volumes.CreateOpts{
Name: name,
Size: size,
Expand All @@ -74,10 +74,10 @@ func (os *OpenStack) CreateVolume(name string, size int, vtype, availability str

vol, err := volumes.Create(os.blockstorage, opts).Extract()
if err != nil {
return "", "", err
return "", "", 0, err
}

return vol.ID, vol.AvailabilityZone, nil
return vol.ID, vol.AvailabilityZone, vol.Size, nil
}

// GetVolumesByName is a wrapper around ListVolumes that creates a Name filter to act as a GetByName
Expand Down

0 comments on commit af49aec

Please sign in to comment.