fix golint issue for pkg/volume/flexvolume package

pull/58/head
Cheng Pan 2018-09-19 03:32:55 +00:00
parent 7bfd0d358c
commit aa297d1adf
12 changed files with 30 additions and 26 deletions

View File

@ -409,7 +409,6 @@ pkg/volume/configmap
pkg/volume/csi/fake pkg/volume/csi/fake
pkg/volume/empty_dir pkg/volume/empty_dir
pkg/volume/fc pkg/volume/fc
pkg/volume/flexvolume
pkg/volume/flocker pkg/volume/flocker
pkg/volume/gce_pd pkg/volume/gce_pd
pkg/volume/git_repo pkg/volume/git_repo

View File

@ -91,9 +91,8 @@ func (a *flexVolumeAttacher) MountDevice(spec *volume.Spec, devicePath string, d
// plugin does not implement attach interface. // plugin does not implement attach interface.
if devicePath != "" { if devicePath != "" {
return (*attacherDefaults)(a).MountDevice(spec, devicePath, deviceMountPath, a.plugin.host.GetMounter(a.plugin.GetPluginName())) return (*attacherDefaults)(a).MountDevice(spec, devicePath, deviceMountPath, a.plugin.host.GetMounter(a.plugin.GetPluginName()))
} else {
return nil
} }
return nil
} }
return err return err
} }

View File

@ -30,7 +30,7 @@ func TestAttach(t *testing.T) {
plugin, _ := testPlugin() plugin, _ := testPlugin()
plugin.runner = fakeRunner( plugin.runner = fakeRunner(
assertDriverCall(t, notSupportedOutput(), attachCmd, assertDriverCall(t, notSupportedOutput(), attachCmd,
specJson(plugin, spec, nil), "localhost"), specJSON(plugin, spec, nil), "localhost"),
) )
a, _ := plugin.NewAttacher() a, _ := plugin.NewAttacher()
@ -43,7 +43,7 @@ func TestWaitForAttach(t *testing.T) {
plugin, _ := testPlugin() plugin, _ := testPlugin()
plugin.runner = fakeRunner( plugin.runner = fakeRunner(
assertDriverCall(t, notSupportedOutput(), waitForAttachCmd, "/dev/sdx", assertDriverCall(t, notSupportedOutput(), waitForAttachCmd, "/dev/sdx",
specJson(plugin, spec, nil)), specJSON(plugin, spec, nil)),
) )
a, _ := plugin.NewAttacher() a, _ := plugin.NewAttacher()
@ -56,7 +56,7 @@ func TestMountDevice(t *testing.T) {
plugin, rootDir := testPlugin() plugin, rootDir := testPlugin()
plugin.runner = fakeRunner( plugin.runner = fakeRunner(
assertDriverCall(t, notSupportedOutput(), mountDeviceCmd, rootDir+"/mount-dir", "/dev/sdx", assertDriverCall(t, notSupportedOutput(), mountDeviceCmd, rootDir+"/mount-dir", "/dev/sdx",
specJson(plugin, spec, nil)), specJSON(plugin, spec, nil)),
) )
a, _ := plugin.NewAttacher() a, _ := plugin.NewAttacher()
@ -68,7 +68,7 @@ func TestIsVolumeAttached(t *testing.T) {
plugin, _ := testPlugin() plugin, _ := testPlugin()
plugin.runner = fakeRunner( plugin.runner = fakeRunner(
assertDriverCall(t, notSupportedOutput(), isAttached, specJson(plugin, spec, nil), "localhost"), assertDriverCall(t, notSupportedOutput(), isAttached, specJSON(plugin, spec, nil), "localhost"),
) )
a, _ := plugin.NewAttacher() a, _ := plugin.NewAttacher()
specs := []*volume.Spec{spec} specs := []*volume.Spec{spec}

View File

@ -129,7 +129,7 @@ func fakePersistentVolumeSpec() *volume.Spec {
return volume.NewSpecFromPersistentVolume(vol, false) return volume.NewSpecFromPersistentVolume(vol, false)
} }
func specJson(plugin *flexVolumeAttachablePlugin, spec *volume.Spec, extraOptions map[string]string) string { func specJSON(plugin *flexVolumeAttachablePlugin, spec *volume.Spec, extraOptions map[string]string) string {
o, err := NewOptionsForDriver(spec, plugin.host, extraOptions) o, err := NewOptionsForDriver(spec, plugin.host, extraOptions)
if err != nil { if err != nil {
panic("Failed to convert spec: " + err.Error()) panic("Failed to convert spec: " + err.Error())

View File

@ -67,7 +67,7 @@ const (
) )
var ( var (
TimeoutError = fmt.Errorf("Timeout") errTimeout = fmt.Errorf("Timeout")
) )
// DriverCall implements the basic contract between FlexVolume and its driver. // DriverCall implements the basic contract between FlexVolume and its driver.
@ -92,10 +92,12 @@ func (plugin *flexVolumePlugin) NewDriverCallWithTimeout(command string, timeout
} }
} }
// Append appends arg into driver call argument list
func (dc *DriverCall) Append(arg string) { func (dc *DriverCall) Append(arg string) {
dc.args = append(dc.args, arg) dc.args = append(dc.args, arg)
} }
// AppendSpec appends volume spec to driver call argument list
func (dc *DriverCall) AppendSpec(spec *volume.Spec, host volume.VolumeHost, extraOptions map[string]string) error { func (dc *DriverCall) AppendSpec(spec *volume.Spec, host volume.VolumeHost, extraOptions map[string]string) error {
optionsForDriver, err := NewOptionsForDriver(spec, host, extraOptions) optionsForDriver, err := NewOptionsForDriver(spec, host, extraOptions)
if err != nil { if err != nil {
@ -111,6 +113,7 @@ func (dc *DriverCall) AppendSpec(spec *volume.Spec, host volume.VolumeHost, extr
return nil return nil
} }
// Run executes the driver call
func (dc *DriverCall) Run() (*DriverStatus, error) { func (dc *DriverCall) Run() (*DriverStatus, error) {
if dc.plugin.isUnsupported(dc.Command) { if dc.plugin.isUnsupported(dc.Command) {
return nil, errors.New(StatusNotSupported) return nil, errors.New(StatusNotSupported)
@ -131,7 +134,7 @@ func (dc *DriverCall) Run() (*DriverStatus, error) {
output, execErr := cmd.CombinedOutput() output, execErr := cmd.CombinedOutput()
if execErr != nil { if execErr != nil {
if timeout { if timeout {
return nil, TimeoutError return nil, errTimeout
} }
_, err := handleCmdResponse(dc.Command, output) _, err := handleCmdResponse(dc.Command, output)
if err == nil { if err == nil {
@ -160,6 +163,7 @@ func (dc *DriverCall) Run() (*DriverStatus, error) {
// OptionsForDriver represents the spec given to the driver. // OptionsForDriver represents the spec given to the driver.
type OptionsForDriver map[string]string type OptionsForDriver map[string]string
// NewOptionsForDriver create driver options given volume spec
func NewOptionsForDriver(spec *volume.Spec, host volume.VolumeHost, extraOptions map[string]string) (OptionsForDriver, error) { func NewOptionsForDriver(spec *volume.Spec, host volume.VolumeHost, extraOptions map[string]string) (OptionsForDriver, error) {
volSourceFSType, err := getFSType(spec) volSourceFSType, err := getFSType(spec)
@ -219,6 +223,7 @@ type DriverStatus struct {
Capabilities *DriverCapabilities `json:",omitempty"` Capabilities *DriverCapabilities `json:",omitempty"`
} }
// DriverCapabilities represents what driver can do
type DriverCapabilities struct { type DriverCapabilities struct {
Attach bool `json:"attach"` Attach bool `json:"attach"`
SELinuxRelabel bool `json:"selinuxRelabel"` SELinuxRelabel bool `json:"selinuxRelabel"`

View File

@ -29,7 +29,7 @@ type fakeWatcher struct {
var _ utilfs.FSWatcher = &fakeWatcher{} var _ utilfs.FSWatcher = &fakeWatcher{}
func NewFakeWatcher() *fakeWatcher { func newFakeWatcher() *fakeWatcher {
return &fakeWatcher{ return &fakeWatcher{
watches: nil, watches: nil,
} }

View File

@ -44,7 +44,7 @@ func TestSetUpAt(t *testing.T) {
plugin.runner = fakeRunner( plugin.runner = fakeRunner(
// first call without fsGroup // first call without fsGroup
assertDriverCall(t, successOutput(), mountCmd, rootDir+"/mount-dir", assertDriverCall(t, successOutput(), mountCmd, rootDir+"/mount-dir",
specJson(plugin, spec, map[string]string{ specJSON(plugin, spec, map[string]string{
optionKeyPodName: "my-pod", optionKeyPodName: "my-pod",
optionKeyPodNamespace: "my-ns", optionKeyPodNamespace: "my-ns",
optionKeyPodUID: "my-uid", optionKeyPodUID: "my-uid",
@ -53,7 +53,7 @@ func TestSetUpAt(t *testing.T) {
// second test has fsGroup // second test has fsGroup
assertDriverCall(t, notSupportedOutput(), mountCmd, rootDir+"/mount-dir", assertDriverCall(t, notSupportedOutput(), mountCmd, rootDir+"/mount-dir",
specJson(plugin, spec, map[string]string{ specJSON(plugin, spec, map[string]string{
optionFSGroup: "42", optionFSGroup: "42",
optionKeyPodName: "my-pod", optionKeyPodName: "my-pod",
optionKeyPodNamespace: "my-ns", optionKeyPodNamespace: "my-ns",
@ -61,7 +61,7 @@ func TestSetUpAt(t *testing.T) {
optionKeyServiceAccountName: "my-sa", optionKeyServiceAccountName: "my-sa",
})), })),
assertDriverCall(t, fakeVolumeNameOutput("sdx"), getVolumeNameCmd, assertDriverCall(t, fakeVolumeNameOutput("sdx"), getVolumeNameCmd,
specJson(plugin, spec, nil)), specJSON(plugin, spec, nil)),
) )
m, _ := plugin.newMounterInternal(spec, pod, mounter, plugin.runner) m, _ := plugin.newMounterInternal(spec, pod, mounter, plugin.runner)

View File

@ -60,6 +60,7 @@ var _ volume.PersistentVolumePlugin = &flexVolumePlugin{}
var _ volume.DeviceMountableVolumePlugin = &flexVolumeAttachablePlugin{} var _ volume.DeviceMountableVolumePlugin = &flexVolumeAttachablePlugin{}
// PluginFactory create flex volume plugin
type PluginFactory interface { type PluginFactory interface {
NewFlexVolumePlugin(pluginDir, driverName string, runner exec.Interface) (volume.VolumePlugin, error) NewFlexVolumePlugin(pluginDir, driverName string, runner exec.Interface) (volume.VolumePlugin, error)
} }
@ -89,9 +90,8 @@ func (pluginFactory) NewFlexVolumePlugin(pluginDir, name string, runner exec.Int
if flexPlugin.capabilities.Attach { if flexPlugin.capabilities.Attach {
// Plugin supports attach/detach, so return flexVolumeAttachablePlugin // Plugin supports attach/detach, so return flexVolumeAttachablePlugin
return &flexVolumeAttachablePlugin{flexVolumePlugin: flexPlugin}, nil return &flexVolumeAttachablePlugin{flexVolumePlugin: flexPlugin}, nil
} else {
return flexPlugin, nil
} }
return flexPlugin, nil
} }
// Init is part of the volume.VolumePlugin interface. // Init is part of the volume.VolumePlugin interface.

View File

@ -42,7 +42,7 @@ func TestGetVolumeName(t *testing.T) {
plugin, _ := testPlugin() plugin, _ := testPlugin()
plugin.runner = fakeRunner( plugin.runner = fakeRunner(
assertDriverCall(t, fakeVolumeNameOutput(spec.Name()), getVolumeNameCmd, assertDriverCall(t, fakeVolumeNameOutput(spec.Name()), getVolumeNameCmd,
specJson(plugin, spec, nil)), specJSON(plugin, spec, nil)),
) )
name, err := plugin.GetVolumeName(spec) name, err := plugin.GetVolumeName(spec)

View File

@ -46,6 +46,7 @@ type flexVolumeProber struct {
eventsMap map[string]volume.ProbeOperation // the key is the driver directory path, the value is the coresponding operation eventsMap map[string]volume.ProbeOperation // the key is the driver directory path, the value is the coresponding operation
} }
// GetDynamicPluginProber creates dynamic plugin prober
func GetDynamicPluginProber(pluginDir string, runner exec.Interface) volume.DynamicPluginProber { func GetDynamicPluginProber(pluginDir string, runner exec.Interface) volume.DynamicPluginProber {
return &flexVolumeProber{ return &flexVolumeProber{
pluginDir: pluginDir, pluginDir: pluginDir,

View File

@ -174,7 +174,7 @@ func TestProberAddRemoveDriver(t *testing.T) {
func TestEmptyPluginDir(t *testing.T) { func TestEmptyPluginDir(t *testing.T) {
// Arrange // Arrange
fs := utilfs.NewFakeFs() fs := utilfs.NewFakeFs()
watcher := NewFakeWatcher() watcher := newFakeWatcher()
prober := &flexVolumeProber{ prober := &flexVolumeProber{
pluginDir: pluginDir, pluginDir: pluginDir,
watcher: watcher, watcher: watcher,
@ -268,7 +268,7 @@ func TestProberMultipleEvents(t *testing.T) {
func TestProberError(t *testing.T) { func TestProberError(t *testing.T) {
fs := utilfs.NewFakeFs() fs := utilfs.NewFakeFs()
watcher := NewFakeWatcher() watcher := newFakeWatcher()
prober := &flexVolumeProber{ prober := &flexVolumeProber{
pluginDir: pluginDir, pluginDir: pluginDir,
watcher: watcher, watcher: watcher,
@ -296,7 +296,7 @@ func initTestEnvironment(t *testing.T) (
watcher *fakeWatcher, watcher *fakeWatcher,
prober volume.DynamicPluginProber) { prober volume.DynamicPluginProber) {
fs = utilfs.NewFakeFs() fs = utilfs.NewFakeFs()
watcher = NewFakeWatcher() watcher = newFakeWatcher()
prober = &flexVolumeProber{ prober = &flexVolumeProber{
pluginDir: pluginDir, pluginDir: pluginDir,
watcher: watcher, watcher: watcher,

View File

@ -55,7 +55,7 @@ func addSecretsToOptions(options map[string]string, spec *volume.Spec, namespace
return nil return nil
} }
var notFlexVolume = fmt.Errorf("not a flex volume") var errNotFlexVolume = fmt.Errorf("not a flex volume")
func getDriver(spec *volume.Spec) (string, error) { func getDriver(spec *volume.Spec) (string, error) {
if spec.Volume != nil && spec.Volume.FlexVolume != nil { if spec.Volume != nil && spec.Volume.FlexVolume != nil {
@ -64,7 +64,7 @@ func getDriver(spec *volume.Spec) (string, error) {
if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil { if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil {
return spec.PersistentVolume.Spec.FlexVolume.Driver, nil return spec.PersistentVolume.Spec.FlexVolume.Driver, nil
} }
return "", notFlexVolume return "", errNotFlexVolume
} }
func getFSType(spec *volume.Spec) (string, error) { func getFSType(spec *volume.Spec) (string, error) {
@ -74,7 +74,7 @@ func getFSType(spec *volume.Spec) (string, error) {
if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil { if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil {
return spec.PersistentVolume.Spec.FlexVolume.FSType, nil return spec.PersistentVolume.Spec.FlexVolume.FSType, nil
} }
return "", notFlexVolume return "", errNotFlexVolume
} }
func getSecretNameAndNamespace(spec *volume.Spec, podNamespace string) (string, string, error) { func getSecretNameAndNamespace(spec *volume.Spec, podNamespace string) (string, string, error) {
@ -95,7 +95,7 @@ func getSecretNameAndNamespace(spec *volume.Spec, podNamespace string) (string,
} }
return secretName, secretNamespace, nil return secretName, secretNamespace, nil
} }
return "", "", notFlexVolume return "", "", errNotFlexVolume
} }
func getReadOnly(spec *volume.Spec) (bool, error) { func getReadOnly(spec *volume.Spec) (bool, error) {
@ -106,7 +106,7 @@ func getReadOnly(spec *volume.Spec) (bool, error) {
// ReadOnly is specified at the PV level // ReadOnly is specified at the PV level
return spec.ReadOnly, nil return spec.ReadOnly, nil
} }
return false, notFlexVolume return false, errNotFlexVolume
} }
func getOptions(spec *volume.Spec) (map[string]string, error) { func getOptions(spec *volume.Spec) (map[string]string, error) {
@ -116,7 +116,7 @@ func getOptions(spec *volume.Spec) (map[string]string, error) {
if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil { if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.FlexVolume != nil {
return spec.PersistentVolume.Spec.FlexVolume.Options, nil return spec.PersistentVolume.Spec.FlexVolume.Options, nil
} }
return nil, notFlexVolume return nil, errNotFlexVolume
} }
func prepareForMount(mounter mount.Interface, deviceMountPath string) (bool, error) { func prepareForMount(mounter mount.Interface, deviceMountPath string) (bool, error) {