Skip to content

Commit c5003cd

Browse files
author
adisky
committed
Return Capacity in Create Volume Request
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
1 parent 13e6dd0 commit c5003cd

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

pkg/csi/cinder/controllerserver.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
6868

6969
resID := ""
7070
resAvailability := ""
71+
resSize := 0
7172

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

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

8889
}
8990

9091
return &csi.CreateVolumeResponse{
9192
Volume: &csi.Volume{
92-
Id: resID,
93+
Id: resID,
94+
CapacityBytes: int64(resSize * 1024 * 1024 * 1024),
9395
Attributes: map[string]string{
9496
"availability": resAvailability,
9597
},

pkg/csi/cinder/controllerserver_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func TestCreateVolume(t *testing.T) {
4040

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

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

65+
assert.NotNil(actualRes.Volume.CapacityBytes)
66+
6567
assert.NotEqual(0, len(actualRes.Volume.Id), "Volume Id is nil")
6668

6769
assert.Equal(fakeAvailability, actualRes.Volume.Attributes["availability"])
70+
6871
}
6972

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

7376
// mock OpenStack
7477
osmock := new(openstack.OpenStackMock)
75-
osmock.On("CreateVolume", fakeVolName, mock.AnythingOfType("int"), fakeVolType, fakeAvailability, (*map[string]string)(nil)).Return(fakeVolID, fakeAvailability, nil)
78+
osmock.On("CreateVolume", fakeVolName, mock.AnythingOfType("int"), fakeVolType, fakeAvailability, (*map[string]string)(nil)).Return(fakeVolID, fakeAvailability, fakeCapacityGiB, nil)
7679
openstack.OsInstance = osmock
7780

7881
// Init assert

pkg/csi/cinder/fake.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var fakeConfig = "/etc/cloud.conf"
2727
var fakeCtx = context.Background()
2828
var fakeVolName = "CSIVolumeName"
2929
var fakeVolID = "CSIVolumeID"
30+
var fakeCapacityGiB = 1
3031
var fakeVolType = ""
3132
var fakeAvailability = ""
3233
var fakeDevicePath = "/dev/xxx"

pkg/csi/cinder/openstack/openstack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
)
2828

2929
type IOpenStack interface {
30-
CreateVolume(name string, size int, vtype, availability string, tags *map[string]string) (string, string, error)
30+
CreateVolume(name string, size int, vtype, availability string, tags *map[string]string) (string, string, int, error)
3131
DeleteVolume(volumeID string) error
3232
AttachVolume(instanceID, volumeID string) (string, error)
3333
WaitDiskAttached(instanceID string, volumeID string) error

pkg/csi/cinder/openstack/openstack_mock.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (_m *OpenStackMock) AttachVolume(instanceID string, volumeID string) (strin
7474
}
7575

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

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

94-
var r2 error
95-
if rf, ok := ret.Get(2).(func(string, int, string, string, *map[string]string) error); ok {
94+
var r2 int
95+
if rf, ok := ret.Get(2).(func(string, int, string, string, *map[string]string) int); ok {
9696
r2 = rf(name, size, vtype, availability, tags)
9797
} else {
98-
r2 = ret.Error(2)
98+
r2 = ret.Get(2).(int)
9999
}
100100

101-
return r0, r1, r2
101+
var r3 error
102+
if rf, ok := ret.Get(3).(func(string, int, string, string, *map[string]string) error); ok {
103+
r3 = rf(name, size, vtype, availability, tags)
104+
} else {
105+
r3 = ret.Error(3)
106+
}
107+
108+
return r0, r1, r2, r3
102109
}
103110

104111
// DeleteVolume provides a mock function with given fields: volumeID

pkg/csi/cinder/openstack/openstack_volumes.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type Volume struct {
6161
}
6262

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

7575
vol, err := volumes.Create(os.blockstorage, opts).Extract()
7676
if err != nil {
77-
return "", "", err
77+
return "", "", 0, err
7878
}
7979

80-
return vol.ID, vol.AvailabilityZone, nil
80+
return vol.ID, vol.AvailabilityZone, vol.Size, nil
8181
}
8282

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

0 commit comments

Comments
 (0)