From 62fe5fe7b874e33a09503aa15bc945bb393be579 Mon Sep 17 00:00:00 2001 From: caiweidong Date: Sat, 4 May 2019 11:08:28 +0800 Subject: [PATCH] add common func for NewAttacher and NewDetacher --- pkg/volume/csi/csi_plugin.go | 38 +++++++++++++++--------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/pkg/volume/csi/csi_plugin.go b/pkg/volume/csi/csi_plugin.go index 4c73484f02b..ac50ac1c427 100644 --- a/pkg/volume/csi/csi_plugin.go +++ b/pkg/volume/csi/csi_plugin.go @@ -550,17 +550,7 @@ var _ volume.AttachableVolumePlugin = &csiPlugin{} var _ volume.DeviceMountableVolumePlugin = &csiPlugin{} func (p *csiPlugin) NewAttacher() (volume.Attacher, error) { - k8s := p.host.GetKubeClient() - if k8s == nil { - klog.Error(log("unable to get kubernetes client from host")) - return nil, errors.New("unable to get Kubernetes client") - } - - return &csiAttacher{ - plugin: p, - k8s: k8s, - waitSleepTime: 1 * time.Second, - }, nil + return p.newAttacherDetacher() } func (p *csiPlugin) NewDeviceMounter() (volume.DeviceMounter, error) { @@ -568,17 +558,7 @@ func (p *csiPlugin) NewDeviceMounter() (volume.DeviceMounter, error) { } func (p *csiPlugin) NewDetacher() (volume.Detacher, error) { - k8s := p.host.GetKubeClient() - if k8s == nil { - klog.Error(log("unable to get kubernetes client from host")) - return nil, errors.New("unable to get Kubernetes client") - } - - return &csiAttacher{ - plugin: p, - k8s: k8s, - waitSleepTime: 1 * time.Second, - }, nil + return p.newAttacherDetacher() } func (p *csiPlugin) CanAttach(spec *volume.Spec) (bool, error) { @@ -837,6 +817,20 @@ func (p *csiPlugin) getPublishContext(client clientset.Interface, handle, driver return attachment.Status.AttachmentMetadata, nil } +func (p *csiPlugin) newAttacherDetacher() (*csiAttacher, error) { + k8s := p.host.GetKubeClient() + if k8s == nil { + klog.Error(log("unable to get kubernetes client from host")) + return nil, errors.New("unable to get Kubernetes client") + } + + return &csiAttacher{ + plugin: p, + k8s: k8s, + waitSleepTime: 1 * time.Second, + }, nil +} + func unregisterDriver(driverName string) error { csiDrivers.Delete(driverName)