From 15066284611b9e224d3c87adb38bead980b0aa79 Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Thu, 4 Oct 2018 21:38:23 -0700 Subject: [PATCH] Update cloud provider interface to take in context With the context object populated through the call stack, the operation could be cancelled properly --- pkg/cloud/cloud.go | 68 +++++++++--------- pkg/cloud/cloud_test.go | 36 ++++++---- pkg/cloud/fakes.go | 15 ++-- pkg/cloud/mocks/mock_ec2.go | 107 ++++++++++++++++++---------- pkg/cloud/mocks/mock_ec2metadata.go | 3 +- pkg/driver/controller.go | 16 ++--- 6 files changed, 143 insertions(+), 102 deletions(-) diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index bd899caffd..54a3e68ce5 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -17,6 +17,7 @@ limitations under the License. package cloud import ( + "context" "errors" "fmt" @@ -25,6 +26,7 @@ import ( "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds" "github.com/aws/aws-sdk-go/aws/ec2metadata" + "github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" "github.com/golang/glog" @@ -92,23 +94,23 @@ type DiskOptions struct { // EC2 abstracts aws.EC2 to facilitate its mocking. type EC2 interface { - DescribeVolumes(input *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) - CreateVolume(input *ec2.CreateVolumeInput) (*ec2.Volume, error) - DeleteVolume(input *ec2.DeleteVolumeInput) (*ec2.DeleteVolumeOutput, error) - DetachVolume(input *ec2.DetachVolumeInput) (*ec2.VolumeAttachment, error) - AttachVolume(input *ec2.AttachVolumeInput) (*ec2.VolumeAttachment, error) - DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) + DescribeVolumesWithContext(ctx aws.Context, input *ec2.DescribeVolumesInput, opts ...request.Option) (*ec2.DescribeVolumesOutput, error) + CreateVolumeWithContext(ctx aws.Context, input *ec2.CreateVolumeInput, opts ...request.Option) (*ec2.Volume, error) + DeleteVolumeWithContext(ctx aws.Context, input *ec2.DeleteVolumeInput, opts ...request.Option) (*ec2.DeleteVolumeOutput, error) + DetachVolumeWithContext(ctx aws.Context, input *ec2.DetachVolumeInput, opts ...request.Option) (*ec2.VolumeAttachment, error) + AttachVolumeWithContext(ctx aws.Context, input *ec2.AttachVolumeInput, opts ...request.Option) (*ec2.VolumeAttachment, error) + DescribeInstancesWithContext(ctx aws.Context, input *ec2.DescribeInstancesInput, opts ...request.Option) (*ec2.DescribeInstancesOutput, error) } type Cloud interface { GetMetadata() MetadataService - CreateDisk(volumeName string, diskOptions *DiskOptions) (disk *Disk, err error) - DeleteDisk(volumeID string) (success bool, err error) - AttachDisk(volumeID string, nodeID string) (devicePath string, err error) - DetachDisk(volumeID string, nodeID string) (err error) - GetDiskByName(name string, capacityBytes int64) (disk *Disk, err error) - GetDiskByID(volumeID string) (disk *Disk, err error) - IsExistInstance(nodeID string) (sucess bool) + CreateDisk(ctx context.Context, volumeName string, diskOptions *DiskOptions) (disk *Disk, err error) + DeleteDisk(ctx context.Context, volumeID string) (success bool, err error) + AttachDisk(ctx context.Context, volumeID string, nodeID string) (devicePath string, err error) + DetachDisk(ctx context.Context, volumeID string, nodeID string) (err error) + GetDiskByName(ctx context.Context, name string, capacityBytes int64) (disk *Disk, err error) + GetDiskByID(ctx context.Context, volumeID string) (disk *Disk, err error) + IsExistInstance(ctx context.Context, nodeID string) (sucess bool) } type cloud struct { @@ -155,7 +157,7 @@ func (c *cloud) GetMetadata() MetadataService { return c.metadata } -func (c *cloud) CreateDisk(volumeName string, diskOptions *DiskOptions) (*Disk, error) { +func (c *cloud) CreateDisk(ctx context.Context, volumeName string, diskOptions *DiskOptions) (*Disk, error) { var createType string var iops int64 capacityGiB := util.BytesToGiB(diskOptions.CapacityBytes) @@ -198,7 +200,7 @@ func (c *cloud) CreateDisk(volumeName string, diskOptions *DiskOptions) (*Disk, request.Iops = aws.Int64(iops) } - response, err := c.ec2.CreateVolume(request) + response, err := c.ec2.CreateVolumeWithContext(ctx, request) if err != nil { return nil, fmt.Errorf("could not create volume in EC2: %v", err) } @@ -216,9 +218,9 @@ func (c *cloud) CreateDisk(volumeName string, diskOptions *DiskOptions) (*Disk, return &Disk{CapacityGiB: size, VolumeID: volumeID}, nil } -func (c *cloud) DeleteDisk(volumeID string) (bool, error) { +func (c *cloud) DeleteDisk(ctx context.Context, volumeID string) (bool, error) { request := &ec2.DeleteVolumeInput{VolumeId: &volumeID} - if _, err := c.ec2.DeleteVolume(request); err != nil { + if _, err := c.ec2.DeleteVolumeWithContext(ctx, request); err != nil { if awsErr, ok := err.(awserr.Error); ok { if awsErr.Code() == "InvalidVolume.NotFound" { return false, ErrNotFound @@ -229,8 +231,8 @@ func (c *cloud) DeleteDisk(volumeID string) (bool, error) { return true, nil } -func (c *cloud) AttachDisk(volumeID, nodeID string) (string, error) { - instance, err := c.getInstance(nodeID) +func (c *cloud) AttachDisk(ctx context.Context, volumeID, nodeID string) (string, error) { + instance, err := c.getInstance(ctx, nodeID) if err != nil { return "", err } @@ -248,7 +250,7 @@ func (c *cloud) AttachDisk(volumeID, nodeID string) (string, error) { VolumeId: aws.String(volumeID), } - resp, err := c.ec2.AttachVolume(request) + resp, err := c.ec2.AttachVolumeWithContext(ctx, request) if err != nil { if awsErr, ok := err.(awserr.Error); ok { if awsErr.Code() == "VolumeInUse" { @@ -291,8 +293,8 @@ func (c *cloud) AttachDisk(volumeID, nodeID string) (string, error) { return device.Path, nil } -func (c *cloud) DetachDisk(volumeID, nodeID string) error { - instance, err := c.getInstance(nodeID) +func (c *cloud) DetachDisk(ctx context.Context, volumeID, nodeID string) error { + instance, err := c.getInstance(ctx, nodeID) if err != nil { return err } @@ -313,7 +315,7 @@ func (c *cloud) DetachDisk(volumeID, nodeID string) error { VolumeId: aws.String(volumeID), } - _, err = c.ec2.DetachVolume(request) + _, err = c.ec2.DetachVolumeWithContext(ctx, request) if err != nil { return fmt.Errorf("could not detach volume %q from node %q: %v", volumeID, nodeID, err) } @@ -321,7 +323,7 @@ func (c *cloud) DetachDisk(volumeID, nodeID string) error { return nil } -func (c *cloud) GetDiskByName(name string, capacityBytes int64) (*Disk, error) { +func (c *cloud) GetDiskByName(ctx context.Context, name string, capacityBytes int64) (*Disk, error) { request := &ec2.DescribeVolumesInput{ Filters: []*ec2.Filter{ { @@ -331,7 +333,7 @@ func (c *cloud) GetDiskByName(name string, capacityBytes int64) (*Disk, error) { }, } - volume, err := c.getVolume(request) + volume, err := c.getVolume(ctx, request) if err != nil { return nil, err } @@ -347,14 +349,14 @@ func (c *cloud) GetDiskByName(name string, capacityBytes int64) (*Disk, error) { }, nil } -func (c *cloud) GetDiskByID(volumeID string) (*Disk, error) { +func (c *cloud) GetDiskByID(ctx context.Context, volumeID string) (*Disk, error) { request := &ec2.DescribeVolumesInput{ VolumeIds: []*string{ aws.String(volumeID), }, } - volume, err := c.getVolume(request) + volume, err := c.getVolume(ctx, request) if err != nil { return nil, err } @@ -365,20 +367,20 @@ func (c *cloud) GetDiskByID(volumeID string) (*Disk, error) { }, nil } -func (c *cloud) IsExistInstance(nodeID string) bool { - instance, err := c.getInstance(nodeID) +func (c *cloud) IsExistInstance(ctx context.Context, nodeID string) bool { + instance, err := c.getInstance(ctx, nodeID) if err != nil || instance == nil { return false } return true } -func (c *cloud) getVolume(request *ec2.DescribeVolumesInput) (*ec2.Volume, error) { +func (c *cloud) getVolume(ctx context.Context, request *ec2.DescribeVolumesInput) (*ec2.Volume, error) { var volumes []*ec2.Volume var nextToken *string for { - response, err := c.ec2.DescribeVolumes(request) + response, err := c.ec2.DescribeVolumesWithContext(ctx, request) if err != nil { return nil, err } @@ -401,7 +403,7 @@ func (c *cloud) getVolume(request *ec2.DescribeVolumesInput) (*ec2.Volume, error return volumes[0], nil } -func (c *cloud) getInstance(nodeID string) (*ec2.Instance, error) { +func (c *cloud) getInstance(ctx context.Context, nodeID string) (*ec2.Instance, error) { instances := []*ec2.Instance{} request := &ec2.DescribeInstancesInput{ InstanceIds: []*string{&nodeID}, @@ -409,7 +411,7 @@ func (c *cloud) getInstance(nodeID string) (*ec2.Instance, error) { var nextToken *string for { - response, err := c.ec2.DescribeInstances(request) + response, err := c.ec2.DescribeInstancesWithContext(ctx, request) if err != nil { return nil, fmt.Errorf("error listing AWS instances: %q", err) } diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index fd7e53b71f..346fcc9db5 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -17,6 +17,7 @@ limitations under the License. package cloud import ( + "context" "fmt" "strings" "testing" @@ -76,9 +77,10 @@ func TestCreateDisk(t *testing.T) { } } - mockEC2.EXPECT().CreateVolume(gomock.Any()).Return(vol, tc.expErr) + ctx := context.Background() + mockEC2.EXPECT().CreateVolumeWithContext(gomock.Eq(ctx), gomock.Any()).Return(vol, tc.expErr) - disk, err := c.CreateDisk(tc.volumeName, tc.diskOptions) + disk, err := c.CreateDisk(ctx, tc.volumeName, tc.diskOptions) if err != nil { if tc.expErr == nil { t.Fatalf("CreateDisk() failed: expected no error, got: %v", err) @@ -134,9 +136,10 @@ func TestDeleteDisk(t *testing.T) { mockEC2 := mocks.NewMockEC2(mockCtrl) c := newCloud(mockEC2) - mockEC2.EXPECT().DeleteVolume(gomock.Any()).Return(&ec2.DeleteVolumeOutput{}, tc.expErr) + ctx := context.Background() + mockEC2.EXPECT().DeleteVolumeWithContext(gomock.Eq(ctx), gomock.Any()).Return(&ec2.DeleteVolumeOutput{}, tc.expErr) - ok, err := c.DeleteDisk(tc.volumeID) + ok, err := c.DeleteDisk(ctx, tc.volumeID) if err != nil && tc.expErr == nil { t.Fatalf("DeleteDisk() failed: expected no error, got: %v", err) } @@ -180,10 +183,11 @@ func TestAttachDisk(t *testing.T) { mockEC2 := mocks.NewMockEC2(mockCtrl) c := newCloud(mockEC2) - mockEC2.EXPECT().DescribeInstances(gomock.Any()).Return(newDescribeInstancesOutput(tc.nodeID), nil) - mockEC2.EXPECT().AttachVolume(gomock.Any()).Return(&ec2.VolumeAttachment{}, tc.expErr) + ctx := context.Background() + mockEC2.EXPECT().DescribeInstancesWithContext(gomock.Eq(ctx), gomock.Any()).Return(newDescribeInstancesOutput(tc.nodeID), nil) + mockEC2.EXPECT().AttachVolumeWithContext(gomock.Eq(ctx), gomock.Any()).Return(&ec2.VolumeAttachment{}, tc.expErr) - devicePath, err := c.AttachDisk(tc.volumeID, tc.nodeID) + devicePath, err := c.AttachDisk(ctx, tc.volumeID, tc.nodeID) if err != nil { if tc.expErr == nil { t.Fatalf("AttachDisk() failed: expected no error, got: %v", err) @@ -228,10 +232,11 @@ func TestDetachDisk(t *testing.T) { mockEC2 := mocks.NewMockEC2(mockCtrl) c := newCloud(mockEC2) - mockEC2.EXPECT().DescribeInstances(gomock.Any()).Return(newDescribeInstancesOutput(tc.nodeID), nil) - mockEC2.EXPECT().DetachVolume(gomock.Any()).Return(&ec2.VolumeAttachment{}, tc.expErr) + ctx := context.Background() + mockEC2.EXPECT().DescribeInstancesWithContext(gomock.Eq(ctx), gomock.Any()).Return(newDescribeInstancesOutput(tc.nodeID), nil) + mockEC2.EXPECT().DetachVolumeWithContext(gomock.Eq(ctx), gomock.Any()).Return(&ec2.VolumeAttachment{}, tc.expErr) - err := c.DetachDisk(tc.volumeID, tc.nodeID) + err := c.DetachDisk(ctx, tc.volumeID, tc.nodeID) if err != nil { if tc.expErr == nil { t.Fatalf("DetachDisk() failed: expected no error, got: %v", err) @@ -277,9 +282,11 @@ func TestGetDiskByName(t *testing.T) { VolumeId: aws.String(tc.volumeName), Size: aws.Int64(util.BytesToGiB(tc.volumeCapacity)), } - mockEC2.EXPECT().DescribeVolumes(gomock.Any()).Return(&ec2.DescribeVolumesOutput{Volumes: []*ec2.Volume{vol}}, tc.expErr) - disk, err := c.GetDiskByName(tc.volumeName, tc.volumeCapacity) + ctx := context.Background() + mockEC2.EXPECT().DescribeVolumesWithContext(gomock.Eq(ctx), gomock.Any()).Return(&ec2.DescribeVolumesOutput{Volumes: []*ec2.Volume{vol}}, tc.expErr) + + disk, err := c.GetDiskByName(ctx, tc.volumeName, tc.volumeCapacity) if err != nil { if tc.expErr == nil { t.Fatalf("GetDiskByName() failed: expected no error, got: %v", err) @@ -321,7 +328,8 @@ func TestGetDiskByID(t *testing.T) { mockEC2 := mocks.NewMockEC2(mockCtrl) c := newCloud(mockEC2) - mockEC2.EXPECT().DescribeVolumes(gomock.Any()).Return( + ctx := context.Background() + mockEC2.EXPECT().DescribeVolumesWithContext(gomock.Eq(ctx), gomock.Any()).Return( &ec2.DescribeVolumesOutput{ Volumes: []*ec2.Volume{ {VolumeId: aws.String(tc.volumeID)}, @@ -330,7 +338,7 @@ func TestGetDiskByID(t *testing.T) { tc.expErr, ) - disk, err := c.GetDiskByID(tc.volumeID) + disk, err := c.GetDiskByID(ctx, tc.volumeID) if err != nil { if tc.expErr == nil { t.Fatalf("GetDisk() failed: expected no error, got: %v", err) diff --git a/pkg/cloud/fakes.go b/pkg/cloud/fakes.go index 41e611c91a..fc9b383fa3 100644 --- a/pkg/cloud/fakes.go +++ b/pkg/cloud/fakes.go @@ -17,6 +17,7 @@ limitations under the License. package cloud import ( + "context" "fmt" "math/rand" "time" @@ -47,7 +48,7 @@ func (c *FakeCloudProvider) GetMetadata() MetadataService { return c.m } -func (c *FakeCloudProvider) CreateDisk(volumeName string, diskOptions *DiskOptions) (*Disk, error) { +func (c *FakeCloudProvider) CreateDisk(ctx context.Context, volumeName string, diskOptions *DiskOptions) (*Disk, error) { r1 := rand.New(rand.NewSource(time.Now().UnixNano())) d := &fakeDisk{ Disk: &Disk{ @@ -60,7 +61,7 @@ func (c *FakeCloudProvider) CreateDisk(volumeName string, diskOptions *DiskOptio return d.Disk, nil } -func (c *FakeCloudProvider) DeleteDisk(volumeID string) (bool, error) { +func (c *FakeCloudProvider) DeleteDisk(ctx context.Context, volumeID string) (bool, error) { for volName, f := range c.disks { if f.Disk.VolumeID == volumeID { delete(c.disks, volName) @@ -69,7 +70,7 @@ func (c *FakeCloudProvider) DeleteDisk(volumeID string) (bool, error) { return true, nil } -func (c *FakeCloudProvider) AttachDisk(volumeID, nodeID string) (string, error) { +func (c *FakeCloudProvider) AttachDisk(ctx context.Context, volumeID, nodeID string) (string, error) { if _, ok := c.pub[volumeID]; ok { return "", ErrAlreadyExists } @@ -77,11 +78,11 @@ func (c *FakeCloudProvider) AttachDisk(volumeID, nodeID string) (string, error) return "/dev/xvdbc", nil } -func (c *FakeCloudProvider) DetachDisk(volumeID, nodeID string) error { +func (c *FakeCloudProvider) DetachDisk(ctx context.Context, volumeID, nodeID string) error { return nil } -func (c *FakeCloudProvider) GetDiskByName(name string, capacityBytes int64) (*Disk, error) { +func (c *FakeCloudProvider) GetDiskByName(ctx context.Context, name string, capacityBytes int64) (*Disk, error) { var disks []*fakeDisk for _, d := range c.disks { for key, value := range d.tags { @@ -101,7 +102,7 @@ func (c *FakeCloudProvider) GetDiskByName(name string, capacityBytes int64) (*Di return nil, nil } -func (c *FakeCloudProvider) GetDiskByID(volumeID string) (*Disk, error) { +func (c *FakeCloudProvider) GetDiskByID(ctx context.Context, volumeID string) (*Disk, error) { for _, f := range c.disks { if f.Disk.VolumeID == volumeID { return f.Disk, nil @@ -110,7 +111,7 @@ func (c *FakeCloudProvider) GetDiskByID(volumeID string) (*Disk, error) { return nil, ErrNotFound } -func (c *FakeCloudProvider) IsExistInstance(nodeID string) bool { +func (c *FakeCloudProvider) IsExistInstance(ctx context.Context, nodeID string) bool { if nodeID != c.m.GetInstanceID() { return false } diff --git a/pkg/cloud/mocks/mock_ec2.go b/pkg/cloud/mocks/mock_ec2.go index 4559ba77cd..3816ead486 100644 --- a/pkg/cloud/mocks/mock_ec2.go +++ b/pkg/cloud/mocks/mock_ec2.go @@ -5,10 +5,11 @@ package mocks import ( - reflect "reflect" - + aws "github.com/aws/aws-sdk-go/aws" + request "github.com/aws/aws-sdk-go/aws/request" ec2 "github.com/aws/aws-sdk-go/service/ec2" gomock "github.com/golang/mock/gomock" + reflect "reflect" ) // MockEC2 is a mock of EC2 interface @@ -34,80 +35,110 @@ func (m *MockEC2) EXPECT() *MockEC2MockRecorder { return m.recorder } -// AttachVolume mocks base method -func (m *MockEC2) AttachVolume(arg0 *ec2.AttachVolumeInput) (*ec2.VolumeAttachment, error) { - ret := m.ctrl.Call(m, "AttachVolume", arg0) +// AttachVolumeWithContext mocks base method +func (m *MockEC2) AttachVolumeWithContext(arg0 aws.Context, arg1 *ec2.AttachVolumeInput, arg2 ...request.Option) (*ec2.VolumeAttachment, error) { + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "AttachVolumeWithContext", varargs...) ret0, _ := ret[0].(*ec2.VolumeAttachment) ret1, _ := ret[1].(error) return ret0, ret1 } -// AttachVolume indicates an expected call of AttachVolume -func (mr *MockEC2MockRecorder) AttachVolume(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AttachVolume", reflect.TypeOf((*MockEC2)(nil).AttachVolume), arg0) +// AttachVolumeWithContext indicates an expected call of AttachVolumeWithContext +func (mr *MockEC2MockRecorder) AttachVolumeWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AttachVolumeWithContext", reflect.TypeOf((*MockEC2)(nil).AttachVolumeWithContext), varargs...) } -// CreateVolume mocks base method -func (m *MockEC2) CreateVolume(arg0 *ec2.CreateVolumeInput) (*ec2.Volume, error) { - ret := m.ctrl.Call(m, "CreateVolume", arg0) +// CreateVolumeWithContext mocks base method +func (m *MockEC2) CreateVolumeWithContext(arg0 aws.Context, arg1 *ec2.CreateVolumeInput, arg2 ...request.Option) (*ec2.Volume, error) { + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "CreateVolumeWithContext", varargs...) ret0, _ := ret[0].(*ec2.Volume) ret1, _ := ret[1].(error) return ret0, ret1 } -// CreateVolume indicates an expected call of CreateVolume -func (mr *MockEC2MockRecorder) CreateVolume(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateVolume", reflect.TypeOf((*MockEC2)(nil).CreateVolume), arg0) +// CreateVolumeWithContext indicates an expected call of CreateVolumeWithContext +func (mr *MockEC2MockRecorder) CreateVolumeWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateVolumeWithContext", reflect.TypeOf((*MockEC2)(nil).CreateVolumeWithContext), varargs...) } -// DeleteVolume mocks base method -func (m *MockEC2) DeleteVolume(arg0 *ec2.DeleteVolumeInput) (*ec2.DeleteVolumeOutput, error) { - ret := m.ctrl.Call(m, "DeleteVolume", arg0) +// DeleteVolumeWithContext mocks base method +func (m *MockEC2) DeleteVolumeWithContext(arg0 aws.Context, arg1 *ec2.DeleteVolumeInput, arg2 ...request.Option) (*ec2.DeleteVolumeOutput, error) { + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "DeleteVolumeWithContext", varargs...) ret0, _ := ret[0].(*ec2.DeleteVolumeOutput) ret1, _ := ret[1].(error) return ret0, ret1 } -// DeleteVolume indicates an expected call of DeleteVolume -func (mr *MockEC2MockRecorder) DeleteVolume(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteVolume", reflect.TypeOf((*MockEC2)(nil).DeleteVolume), arg0) +// DeleteVolumeWithContext indicates an expected call of DeleteVolumeWithContext +func (mr *MockEC2MockRecorder) DeleteVolumeWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteVolumeWithContext", reflect.TypeOf((*MockEC2)(nil).DeleteVolumeWithContext), varargs...) } -// DescribeInstances mocks base method -func (m *MockEC2) DescribeInstances(arg0 *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) { - ret := m.ctrl.Call(m, "DescribeInstances", arg0) +// DescribeInstancesWithContext mocks base method +func (m *MockEC2) DescribeInstancesWithContext(arg0 aws.Context, arg1 *ec2.DescribeInstancesInput, arg2 ...request.Option) (*ec2.DescribeInstancesOutput, error) { + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "DescribeInstancesWithContext", varargs...) ret0, _ := ret[0].(*ec2.DescribeInstancesOutput) ret1, _ := ret[1].(error) return ret0, ret1 } -// DescribeInstances indicates an expected call of DescribeInstances -func (mr *MockEC2MockRecorder) DescribeInstances(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DescribeInstances", reflect.TypeOf((*MockEC2)(nil).DescribeInstances), arg0) +// DescribeInstancesWithContext indicates an expected call of DescribeInstancesWithContext +func (mr *MockEC2MockRecorder) DescribeInstancesWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DescribeInstancesWithContext", reflect.TypeOf((*MockEC2)(nil).DescribeInstancesWithContext), varargs...) } -// DescribeVolumes mocks base method -func (m *MockEC2) DescribeVolumes(arg0 *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) { - ret := m.ctrl.Call(m, "DescribeVolumes", arg0) +// DescribeVolumesWithContext mocks base method +func (m *MockEC2) DescribeVolumesWithContext(arg0 aws.Context, arg1 *ec2.DescribeVolumesInput, arg2 ...request.Option) (*ec2.DescribeVolumesOutput, error) { + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "DescribeVolumesWithContext", varargs...) ret0, _ := ret[0].(*ec2.DescribeVolumesOutput) ret1, _ := ret[1].(error) return ret0, ret1 } -// DescribeVolumes indicates an expected call of DescribeVolumes -func (mr *MockEC2MockRecorder) DescribeVolumes(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DescribeVolumes", reflect.TypeOf((*MockEC2)(nil).DescribeVolumes), arg0) +// DescribeVolumesWithContext indicates an expected call of DescribeVolumesWithContext +func (mr *MockEC2MockRecorder) DescribeVolumesWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DescribeVolumesWithContext", reflect.TypeOf((*MockEC2)(nil).DescribeVolumesWithContext), varargs...) } -// DetachVolume mocks base method -func (m *MockEC2) DetachVolume(arg0 *ec2.DetachVolumeInput) (*ec2.VolumeAttachment, error) { - ret := m.ctrl.Call(m, "DetachVolume", arg0) +// DetachVolumeWithContext mocks base method +func (m *MockEC2) DetachVolumeWithContext(arg0 aws.Context, arg1 *ec2.DetachVolumeInput, arg2 ...request.Option) (*ec2.VolumeAttachment, error) { + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "DetachVolumeWithContext", varargs...) ret0, _ := ret[0].(*ec2.VolumeAttachment) ret1, _ := ret[1].(error) return ret0, ret1 } -// DetachVolume indicates an expected call of DetachVolume -func (mr *MockEC2MockRecorder) DetachVolume(arg0 interface{}) *gomock.Call { - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DetachVolume", reflect.TypeOf((*MockEC2)(nil).DetachVolume), arg0) +// DetachVolumeWithContext indicates an expected call of DetachVolumeWithContext +func (mr *MockEC2MockRecorder) DetachVolumeWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DetachVolumeWithContext", reflect.TypeOf((*MockEC2)(nil).DetachVolumeWithContext), varargs...) } diff --git a/pkg/cloud/mocks/mock_ec2metadata.go b/pkg/cloud/mocks/mock_ec2metadata.go index 4e2e57353e..f54a7098cb 100644 --- a/pkg/cloud/mocks/mock_ec2metadata.go +++ b/pkg/cloud/mocks/mock_ec2metadata.go @@ -5,10 +5,9 @@ package mocks import ( - reflect "reflect" - ec2metadata "github.com/aws/aws-sdk-go/aws/ec2metadata" gomock "github.com/golang/mock/gomock" + reflect "reflect" ) // MockEC2Metadata is a mock of EC2Metadata interface diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index 4fdcfb4bbd..fab890b807 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -55,7 +55,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) return nil, status.Error(codes.InvalidArgument, "Volume capabilities not supported") } - disk, err := d.cloud.GetDiskByName(volName, volSizeBytes) + disk, err := d.cloud.GetDiskByName(ctx, volName, volSizeBytes) if err != nil { switch err { case cloud.ErrNotFound: @@ -73,7 +73,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) CapacityBytes: volSizeBytes, Tags: map[string]string{cloud.VolumeNameTagKey: volName}, } - newDisk, err := d.cloud.CreateDisk(volName, opts) + newDisk, err := d.cloud.CreateDisk(ctx, volName, opts) if err != nil { return nil, status.Errorf(codes.Internal, "Could not create volume %q: %v", volName, err) } @@ -95,7 +95,7 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) return nil, status.Error(codes.InvalidArgument, "Volume ID not provided") } - if _, err := d.cloud.DeleteDisk(volumeID); err != nil { + if _, err := d.cloud.DeleteDisk(ctx, volumeID); err != nil { if err == cloud.ErrNotFound { glog.V(4).Info("DeleteVolume: volume not found, returning with success") return &csi.DeleteVolumeResponse{}, nil @@ -128,18 +128,18 @@ func (d *Driver) ControllerPublishVolume(ctx context.Context, req *csi.Controlle return nil, status.Error(codes.InvalidArgument, "Volume capability not supported") } - if !d.cloud.IsExistInstance(nodeID) { + if !d.cloud.IsExistInstance(ctx, nodeID) { return nil, status.Errorf(codes.NotFound, "Instance %q not found", nodeID) } - if _, err := d.cloud.GetDiskByID(volumeID); err != nil { + if _, err := d.cloud.GetDiskByID(ctx, volumeID); err != nil { if err == cloud.ErrNotFound { return nil, status.Error(codes.NotFound, "Volume not found") } return nil, status.Errorf(codes.Internal, "Could not get volume with ID %q: %v", volumeID, err) } - devicePath, err := d.cloud.AttachDisk(volumeID, nodeID) + devicePath, err := d.cloud.AttachDisk(ctx, volumeID, nodeID) if err != nil { if err == cloud.ErrAlreadyExists { return nil, status.Error(codes.AlreadyExists, err.Error()) @@ -164,7 +164,7 @@ func (d *Driver) ControllerUnpublishVolume(ctx context.Context, req *csi.Control return nil, status.Error(codes.InvalidArgument, "Node ID not provided") } - if err := d.cloud.DetachDisk(volumeID, nodeID); err != nil { + if err := d.cloud.DetachDisk(ctx, volumeID, nodeID); err != nil { return nil, status.Errorf(codes.Internal, "Could not detach volume %q from node %q: %v", volumeID, nodeID, err) } glog.V(5).Infof("ControllerUnpublishVolume: volume %s detached from node %s", volumeID, nodeID) @@ -210,7 +210,7 @@ func (d *Driver) ValidateVolumeCapabilities(ctx context.Context, req *csi.Valida return nil, status.Error(codes.InvalidArgument, "Volume capabilities not provided") } - if _, err := d.cloud.GetDiskByID(volumeID); err != nil { + if _, err := d.cloud.GetDiskByID(ctx, volumeID); err != nil { if err == cloud.ErrNotFound { return nil, status.Error(codes.NotFound, "Volume not found") }