Fix issues found in gometalinter

Signed-off-by: Madhu Rajanna <mrajanna@redhat.com>
This commit is contained in:
Madhu Rajanna
2019-01-28 19:29:16 +05:30
parent 008c82c1e7
commit 7a0c233c27
13 changed files with 136 additions and 38 deletions

View File

@ -21,6 +21,8 @@ import (
"os"
"path"
"text/template"
"github.com/golang/glog"
)
const cephConfig = `[global]
@ -89,7 +91,11 @@ func writeCephTemplate(fileName string, m os.FileMode, t *template.Template, dat
return err
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
glog.Errorf("failed to close file %s with error %s", f.Name(), err)
}
}()
return t.Execute(f, data)
}

View File

@ -21,6 +21,8 @@ import (
"encoding/json"
"fmt"
"os"
"github.com/golang/glog"
)
const (
@ -111,12 +113,20 @@ func deleteCephUser(adminCr *credentials, volID volumeID) error {
"auth", "rm", cephEntityClientPrefix + userID,
}
if err := execCommandAndValidate("ceph", args[:]...); err != nil {
var err error
if err = execCommandAndValidate("ceph", args[:]...); err != nil {
return err
}
os.Remove(getCephKeyringPath(volID, userID))
os.Remove(getCephSecretPath(volID, userID))
keyringPath := getCephKeyringPath(volID, userID)
if err = os.Remove(keyringPath); err != nil {
glog.Errorf("failed to remove keyring file %s with error %s", keyringPath, err)
}
secretPath := getCephSecretPath(volID, userID)
if err = os.Remove(secretPath); err != nil {
glog.Errorf("failed to remove secret file %s with error %s", secretPath, err)
}
return nil
}

View File

@ -132,7 +132,8 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
// mons may have changed since create volume,
// retrieve the latest mons and override old mons
secret := req.GetSecrets()
if mon, err := getMonValFromSecret(secret); err == nil && len(mon) > 0 {
mon := ""
if mon, err = getMonValFromSecret(secret); err == nil && len(mon) > 0 {
glog.Infof("override old mons [%q] with [%q]", ce.VolOptions.Monitors, mon)
ce.VolOptions.Monitors = mon
}

View File

@ -197,18 +197,21 @@ func (ns *NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
// NodeUnpublishVolume unmounts the volume from the target path
func (ns *NodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
if err := validateNodeUnpublishVolumeRequest(req); err != nil {
var err error
if err = validateNodeUnpublishVolumeRequest(req); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
targetPath := req.GetTargetPath()
// Unmount the bind-mount
if err := unmountVolume(targetPath); err != nil {
if err = unmountVolume(targetPath); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
os.Remove(targetPath)
if err = os.Remove(targetPath); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
glog.Infof("cephfs: successfully unbinded volume %s from %s", req.GetVolumeId(), targetPath)
@ -217,18 +220,21 @@ func (ns *NodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
// NodeUnstageVolume unstages the volume from the staging path
func (ns *NodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
if err := validateNodeUnstageVolumeRequest(req); err != nil {
var err error
if err = validateNodeUnstageVolumeRequest(req); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
stagingTargetPath := req.GetStagingTargetPath()
// Unmount the volume
if err := unmountVolume(stagingTargetPath); err != nil {
if err = unmountVolume(stagingTargetPath); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
os.Remove(stagingTargetPath)
if err = os.Remove(stagingTargetPath); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
glog.Infof("cephfs: successfully umounted volume %s from %s", req.GetVolumeId(), stagingTargetPath)

View File

@ -20,6 +20,8 @@ import (
"fmt"
"os"
"path"
"github.com/golang/glog"
)
const (
@ -70,8 +72,7 @@ func createVolume(volOptions *volumeOptions, adminCr *credentials, volID volumeI
}
defer func() {
unmountVolume(cephRoot)
os.Remove(cephRoot)
umountAndRemove(cephRoot)
}()
volOptions.RootPath = getVolumeRootPathCeph(volID)
@ -123,8 +124,7 @@ func purgeVolume(volID volumeID, adminCr *credentials, volOptions *volumeOptions
}
defer func() {
unmountVolume(volRoot)
os.Remove(volRoot)
umountAndRemove(volRoot)
}()
if err := os.Rename(volRoot, volRootDeleting); err != nil {
@ -137,3 +137,14 @@ func purgeVolume(volID volumeID, adminCr *credentials, volOptions *volumeOptions
return nil
}
func umountAndRemove(mountPoint string) {
var err error
if err = unmountVolume(mountPoint); err != nil {
glog.Errorf("failed to unmount %s with error %s", mountPoint, err)
}
if err = os.Remove(mountPoint); err != nil {
glog.Errorf("failed to remove %s with error %s", mountPoint, err)
}
}

View File

@ -102,7 +102,8 @@ func newVolumeOptions(volOptions, secret map[string]string) (*volumeOptions, err
// extract mon from secret first
if err = extractOption(&opts.MonValueFromSecret, "monValueFromSecret", volOptions); err == nil {
if mon, err := getMonValFromSecret(secret); err == nil && len(mon) > 0 {
mon := ""
if mon, err = getMonValFromSecret(secret); err == nil && len(mon) > 0 {
opts.Monitors = mon
}
}
@ -131,6 +132,8 @@ func newVolumeOptions(volOptions, secret map[string]string) (*volumeOptions, err
}
// This field is optional, don't check for its presence
// nolint: errcheck
// (skip errcheck as this is optional)
extractOption(&opts.Mounter, "mounter", volOptions)
if err = opts.validate(); err != nil {