From 2049ad3287fccddc13a43ca73c19956354804a38 Mon Sep 17 00:00:00 2001 From: adisky Date: Fri, 17 Aug 2018 08:50:48 +0000 Subject: [PATCH] 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]https://github.com/kubernetes-csi/external-provisioner/pull/76 --- pkg/csi/cinder/controllerserver.go | 8 +++++--- pkg/csi/cinder/controllerserver_test.go | 9 ++++++--- pkg/csi/cinder/fake.go | 1 + pkg/csi/cinder/openstack/openstack.go | 2 +- pkg/csi/cinder/openstack/openstack_mock.go | 17 ++++++++++++----- pkg/csi/cinder/openstack/openstack_volumes.go | 6 +++--- 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/pkg/csi/cinder/controllerserver.go b/pkg/csi/cinder/controllerserver.go index 56aadd1276..bc76ce9353 100644 --- a/pkg/csi/cinder/controllerserver.go +++ b/pkg/csi/cinder/controllerserver.go @@ -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 @@ -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, }, diff --git a/pkg/csi/cinder/controllerserver_test.go b/pkg/csi/cinder/controllerserver_test.go index 02876f755e..76d41c5807 100644 --- a/pkg/csi/cinder/controllerserver_test.go +++ b/pkg/csi/cinder/controllerserver_test.go @@ -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 @@ -62,9 +62,12 @@ 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 @@ -72,7 +75,7 @@ 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 diff --git a/pkg/csi/cinder/fake.go b/pkg/csi/cinder/fake.go index a1b76c0400..777deeab0f 100644 --- a/pkg/csi/cinder/fake.go +++ b/pkg/csi/cinder/fake.go @@ -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" diff --git a/pkg/csi/cinder/openstack/openstack.go b/pkg/csi/cinder/openstack/openstack.go index 77e69af5b6..d7e915c471 100644 --- a/pkg/csi/cinder/openstack/openstack.go +++ b/pkg/csi/cinder/openstack/openstack.go @@ -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 diff --git a/pkg/csi/cinder/openstack/openstack_mock.go b/pkg/csi/cinder/openstack/openstack_mock.go index 686936bd5d..6092d5493a 100644 --- a/pkg/csi/cinder/openstack/openstack_mock.go +++ b/pkg/csi/cinder/openstack/openstack_mock.go @@ -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 @@ -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 diff --git a/pkg/csi/cinder/openstack/openstack_volumes.go b/pkg/csi/cinder/openstack/openstack_volumes.go index 05932bc8f9..c1801d51dc 100644 --- a/pkg/csi/cinder/openstack/openstack_volumes.go +++ b/pkg/csi/cinder/openstack/openstack_volumes.go @@ -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, @@ -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