Enable all static-checks in golangci-lint

* Enable all static-checks in golangci-lint
* Update golangci-lint version
* Fix issue found in golangci-lint

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna 2019-06-10 12:18:41 +05:30
parent 47d4e44af0
commit a38986fce0
21 changed files with 85 additions and 87 deletions

View File

@ -21,7 +21,7 @@ go: 1.11.x
env: env:
global: global:
- GOLANGCI_VERSION="v1.16.0" - GOLANGCI_VERSION="v1.17.0"
- TEST_COVERAGE=stdout - TEST_COVERAGE=stdout
- GO_METALINTER_THREADS=1 - GO_METALINTER_THREADS=1
- GO_COVER_DIR=_output - GO_COVER_DIR=_output

View File

@ -63,7 +63,7 @@ func init() {
} }
func getType() string { func getType() string {
if vtype == nil || len(*vtype) == 0 { if vtype == nil || *vtype == "" {
a0 := path.Base(os.Args[0]) a0 := path.Base(os.Args[0])
if strings.Contains(a0, rbdType) { if strings.Contains(a0, rbdType) {
return rbdType return rbdType
@ -78,7 +78,7 @@ func getType() string {
func getDriverName() string { func getDriverName() string {
// was explicitly passed a driver name // was explicitly passed a driver name
if driverName != nil && len(*driverName) != 0 { if driverName != nil && *driverName != "" {
return *driverName return *driverName
} }
// select driver name based on volume type // select driver name based on volume type
@ -96,7 +96,7 @@ func main() {
var cp util.CachePersister var cp util.CachePersister
driverType := getType() driverType := getType()
if len(driverType) == 0 { if driverType == "" {
klog.Fatalln("driver type not specified") klog.Fatalln("driver type not specified")
} }
@ -108,12 +108,12 @@ func main() {
klog.Infof("Starting driver type: %v with name: %v", driverType, dname) klog.Infof("Starting driver type: %v with name: %v", driverType, dname)
switch driverType { switch driverType {
case rbdType: case rbdType:
rbd.PluginFolder = rbd.PluginFolder + dname rbd.PluginFolder += dname
driver := rbd.NewDriver() driver := rbd.NewDriver()
driver.Run(dname, *nodeID, *endpoint, *instanceID, *containerized) driver.Run(dname, *nodeID, *endpoint, *instanceID, *containerized)
case cephfsType: case cephfsType:
cephfs.PluginFolder = cephfs.PluginFolder + dname cephfs.PluginFolder += dname
if *metadataStorage != "" { if *metadataStorage != "" {
cp, err = util.CreatePersistanceStorage( cp, err = util.CreatePersistanceStorage(
cephfs.PluginFolder, *metadataStorage, dname) cephfs.PluginFolder, *metadataStorage, dname)

View File

@ -36,7 +36,7 @@ func deployCommon() {
func createFileSystem(c kubernetes.Interface) { func createFileSystem(c kubernetes.Interface) {
commonPath := fmt.Sprintf("%s/%s", rookURL, "filesystem-test.yaml") commonPath := fmt.Sprintf("%s/%s", rookURL, "filesystem-test.yaml")
framework.RunKubectlOrDie("create", "-f", commonPath) framework.RunKubectlOrDie("create", "-f", commonPath)
opt := metav1.ListOptions{ opt := &metav1.ListOptions{
LabelSelector: "app=rook-ceph-mds", LabelSelector: "app=rook-ceph-mds",
} }
err := checkCephPods(rookNS, c, 1, deployTimeout, opt) err := checkCephPods(rookNS, c, 1, deployTimeout, opt)
@ -72,7 +72,7 @@ func deployOperator(c kubernetes.Interface) {
func deployCluster(c kubernetes.Interface) { func deployCluster(c kubernetes.Interface) {
opPath := fmt.Sprintf("%s/%s", rookURL, "cluster-test.yaml") opPath := fmt.Sprintf("%s/%s", rookURL, "cluster-test.yaml")
framework.RunKubectlOrDie("create", "-f", opPath) framework.RunKubectlOrDie("create", "-f", opPath)
opt := metav1.ListOptions{ opt := &metav1.ListOptions{
LabelSelector: "app=rook-ceph-mon", LabelSelector: "app=rook-ceph-mon",
} }
err := checkCephPods(rookNS, c, 1, deployTimeout, opt) err := checkCephPods(rookNS, c, 1, deployTimeout, opt)
@ -82,7 +82,7 @@ func deployCluster(c kubernetes.Interface) {
func deployToolBox(c kubernetes.Interface) { func deployToolBox(c kubernetes.Interface) {
opPath := fmt.Sprintf("%s/%s", rookURL, "toolbox.yaml") opPath := fmt.Sprintf("%s/%s", rookURL, "toolbox.yaml")
framework.RunKubectlOrDie("create", "-f", opPath) framework.RunKubectlOrDie("create", "-f", opPath)
opt := metav1.ListOptions{ opt := &metav1.ListOptions{
LabelSelector: "app=rook-ceph-tools", LabelSelector: "app=rook-ceph-tools",
} }

View File

@ -137,8 +137,8 @@ func getMons(ns string, c kubernetes.Interface) []string {
svcList, err := c.CoreV1().Services(ns).List(opt) svcList, err := c.CoreV1().Services(ns).List(opt)
Expect(err).Should(BeNil()) Expect(err).Should(BeNil())
services := make([]string, 0) services := make([]string, 0)
for _, svc := range svcList.Items { for i := range svcList.Items {
s := fmt.Sprintf("%s.%s.svc.cluster.local:%d", svc.Name, svc.Namespace, svc.Spec.Ports[0].Port) s := fmt.Sprintf("%s.%s.svc.cluster.local:%d", svcList.Items[i].Name, svcList.Items[i].Namespace, svcList.Items[i].Spec.Ports[0].Port)
services = append(services, s) services = append(services, s)
} }
return services return services
@ -367,11 +367,11 @@ func createApp(c kubernetes.Interface, app *v1.Pod, timeout int) error {
return waitForPodInRunningState(app.Name, app.Namespace, c, timeout) return waitForPodInRunningState(app.Name, app.Namespace, c, timeout)
} }
func getPodName(ns string, c kubernetes.Interface, opt metav1.ListOptions) string { func getPodName(ns string, c kubernetes.Interface, opt *metav1.ListOptions) string {
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(1 * time.Second)
// TODO add stop logic // TODO add stop logic
for range ticker.C { for range ticker.C {
podList, err := c.CoreV1().Pods(ns).List(opt) podList, err := c.CoreV1().Pods(ns).List(*opt)
framework.ExpectNoError(err) framework.ExpectNoError(err)
Expect(podList.Items).NotTo(BeNil()) Expect(podList.Items).NotTo(BeNil())
Expect(err).Should(BeNil()) Expect(err).Should(BeNil())
@ -439,12 +439,12 @@ func unmarshal(fileName string, obj interface{}) error {
return err return err
} }
func checkCephPods(ns string, c kubernetes.Interface, count int, t int, opt metav1.ListOptions) error { func checkCephPods(ns string, c kubernetes.Interface, count, t int, opt *metav1.ListOptions) error {
timeout := time.Duration(t) * time.Minute timeout := time.Duration(t) * time.Minute
start := time.Now() start := time.Now()
return wait.PollImmediate(poll, timeout, func() (bool, error) { return wait.PollImmediate(poll, timeout, func() (bool, error) {
podList, err := c.CoreV1().Pods(ns).List(opt) podList, err := c.CoreV1().Pods(ns).List(*opt)
if err != nil { if err != nil {
return false, err return false, err
} }

View File

@ -34,7 +34,7 @@ var (
volumeMountCacheMtx sync.Mutex volumeMountCacheMtx sync.Mutex
) )
func initVolumeMountCache(driverName string, mountCacheDir string) { func initVolumeMountCache(driverName, mountCacheDir string) {
volumeMountCache.volumes = make(map[string]volumeMountCacheEntry) volumeMountCache.volumes = make(map[string]volumeMountCacheEntry)
volumeMountCache.nodeCacheStore.BasePath = mountCacheDir volumeMountCache.nodeCacheStore.BasePath = mountCacheDir
@ -246,7 +246,7 @@ func (mc *volumeMountCacheMap) nodeUnStageVolume(volID string) error {
return mc.nodeCacheStore.Delete(genVolumeMountCacheFileName(volID)) return mc.nodeCacheStore.Delete(genVolumeMountCacheFileName(volID))
} }
func (mc *volumeMountCacheMap) nodePublishVolume(volID string, targetPath string, readOnly bool) error { func (mc *volumeMountCacheMap) nodePublishVolume(volID, targetPath string, readOnly bool) error {
if !mc.isEnable() { if !mc.isEnable() {
return nil return nil
} }
@ -261,7 +261,7 @@ func (mc *volumeMountCacheMap) nodePublishVolume(volID string, targetPath string
return mc.updateNodeCache(volID) return mc.updateNodeCache(volID)
} }
func (mc *volumeMountCacheMap) nodeUnPublishVolume(volID string, targetPath string) error { func (mc *volumeMountCacheMap) nodeUnPublishVolume(volID, targetPath string) error {
if !mc.isEnable() { if !mc.isEnable() {
return nil return nil
} }

View File

@ -178,7 +178,7 @@ func mountKernel(mountPoint string, cr *credentials, volOptions *volumeOptions)
} }
optionsStr := fmt.Sprintf("name=%s,secret=%s", cr.id, cr.key) optionsStr := fmt.Sprintf("name=%s,secret=%s", cr.id, cr.key)
if volOptions.FsName != "" { if volOptions.FsName != "" {
optionsStr = optionsStr + fmt.Sprintf(",mds_namespace=%s", volOptions.FsName) optionsStr += fmt.Sprintf(",mds_namespace=%s", volOptions.FsName)
} }
args = append(args, "-o", optionsStr) args = append(args, "-o", optionsStr)
@ -202,7 +202,7 @@ func bindMount(from, to string, readOnly bool, mntOptions []string) error {
} }
if readOnly { if readOnly {
mntOptionSli = mntOptionSli + ",remount" mntOptionSli += ",remount"
if err := execCommandErr("mount", "-o", mntOptionSli, to); err != nil { if err := execCommandErr("mount", "-o", mntOptionSli, to); err != nil {
return fmt.Errorf("failed read-only remount of %s: %v", to, err) return fmt.Errorf("failed read-only remount of %s: %v", to, err)
} }

View File

@ -37,7 +37,7 @@ type CSIDriver struct {
// NewCSIDriver Creates a NewCSIDriver object. Assumes vendor // NewCSIDriver Creates a NewCSIDriver object. Assumes vendor
// version is equal to driver version & does not support optional // version is equal to driver version & does not support optional
// driver plugin info manifest field. Refer to CSI spec for more details. // driver plugin info manifest field. Refer to CSI spec for more details.
func NewCSIDriver(name string, v string, nodeID string) *CSIDriver { func NewCSIDriver(name, v, nodeID string) *CSIDriver {
if name == "" { if name == "" {
klog.Errorf("Driver name missing") klog.Errorf("Driver name missing")
return nil return nil
@ -48,7 +48,7 @@ func NewCSIDriver(name string, v string, nodeID string) *CSIDriver {
return nil return nil
} }
// TODO version format and validation // TODO version format and validation
if len(v) == 0 { if v == "" {
klog.Errorf("Version argument missing") klog.Errorf("Version argument missing")
return nil return nil
} }

View File

@ -64,11 +64,11 @@ func NewDefaultControllerServer(d *CSIDriver) *DefaultControllerServer {
} }
// NewControllerServiceCapability returns controller capabilities // NewControllerServiceCapability returns controller capabilities
func NewControllerServiceCapability(cap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability { func NewControllerServiceCapability(ctrlCap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
return &csi.ControllerServiceCapability{ return &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{ Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{ Rpc: &csi.ControllerServiceCapability_RPC{
Type: cap, Type: ctrlCap,
}, },
}, },
} }

View File

@ -46,17 +46,17 @@ func (cs *ControllerServer) validateVolumeReq(req *csi.CreateVolumeRequest) erro
return err return err
} }
// Check sanity of request Name, Volume Capabilities // Check sanity of request Name, Volume Capabilities
if len(req.Name) == 0 { if req.Name == "" {
return status.Error(codes.InvalidArgument, "volume Name cannot be empty") return status.Error(codes.InvalidArgument, "volume Name cannot be empty")
} }
if req.VolumeCapabilities == nil { if req.VolumeCapabilities == nil {
return status.Error(codes.InvalidArgument, "volume Capabilities cannot be empty") return status.Error(codes.InvalidArgument, "volume Capabilities cannot be empty")
} }
options := req.GetParameters() options := req.GetParameters()
if value, ok := options["clusterID"]; !ok || len(value) == 0 { if value, ok := options["clusterID"]; !ok || value == "" {
return status.Error(codes.InvalidArgument, "missing or empty cluster ID to provision volume from") return status.Error(codes.InvalidArgument, "missing or empty cluster ID to provision volume from")
} }
if value, ok := options["pool"]; !ok || len(value) == 0 { if value, ok := options["pool"]; !ok || value == "" {
return status.Error(codes.InvalidArgument, "missing or empty pool name to provision volume from") return status.Error(codes.InvalidArgument, "missing or empty pool name to provision volume from")
} }
return nil return nil
@ -194,7 +194,7 @@ func (cs *ControllerServer) checkSnapshot(req *csi.CreateVolumeRequest, rbdVol *
} }
snapshotID := snapshot.GetSnapshotId() snapshotID := snapshot.GetSnapshotId()
if len(snapshotID) == 0 { if snapshotID == "" {
return status.Error(codes.InvalidArgument, "volume Snapshot ID cannot be empty") return status.Error(codes.InvalidArgument, "volume Snapshot ID cannot be empty")
} }
@ -401,10 +401,10 @@ func (cs *ControllerServer) validateSnapshotReq(req *csi.CreateSnapshotRequest)
} }
// Check sanity of request Snapshot Name, Source Volume Id // Check sanity of request Snapshot Name, Source Volume Id
if len(req.Name) == 0 { if req.Name == "" {
return status.Error(codes.InvalidArgument, "snapshot Name cannot be empty") return status.Error(codes.InvalidArgument, "snapshot Name cannot be empty")
} }
if len(req.SourceVolumeId) == 0 { if req.SourceVolumeId == "" {
return status.Error(codes.InvalidArgument, "source Volume ID cannot be empty") return status.Error(codes.InvalidArgument, "source Volume ID cannot be empty")
} }
@ -466,7 +466,7 @@ func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
} }
snapshotID := req.GetSnapshotId() snapshotID := req.GetSnapshotId()
if len(snapshotID) == 0 { if snapshotID == "" {
return nil, status.Error(codes.InvalidArgument, "snapshot ID cannot be empty") return nil, status.Error(codes.InvalidArgument, "snapshot ID cannot be empty")
} }

View File

@ -50,7 +50,7 @@ func init() {
} }
// Search /sys/bus for rbd device that matches given pool and image. // Search /sys/bus for rbd device that matches given pool and image.
func getRbdDevFromImageAndPool(pool string, image string) (string, bool) { func getRbdDevFromImageAndPool(pool, image string) (string, bool) {
// /sys/bus/rbd/devices/X/name and /sys/bus/rbd/devices/X/pool // /sys/bus/rbd/devices/X/name and /sys/bus/rbd/devices/X/pool
sysPath := "/sys/bus/rbd/devices" sysPath := "/sys/bus/rbd/devices"
if dirs, err := ioutil.ReadDir(sysPath); err == nil { if dirs, err := ioutil.ReadDir(sysPath); err == nil {
@ -123,7 +123,7 @@ func getMaxNbds() (int, error) {
// but older versions of list-mapped don't. // but older versions of list-mapped don't.
// The implementation below peeks at the command line of nbd bound processes // The implementation below peeks at the command line of nbd bound processes
// to figure out any mapped images. // to figure out any mapped images.
func getNbdDevFromImageAndPool(pool string, image string) (string, bool) { func getNbdDevFromImageAndPool(pool, image string) (string, bool) {
// nbd module exports the pid of serving process in sysfs // nbd module exports the pid of serving process in sysfs
basePath := "/sys/block/nbd" basePath := "/sys/block/nbd"
// Do not change imgPath format - some tools like rbd-nbd are strict about it. // Do not change imgPath format - some tools like rbd-nbd are strict about it.

View File

@ -55,7 +55,7 @@ type cephStoragePoolSummary struct {
// GetPoolID searches a list of pools in a cluster and returns the ID of the pool that matches // GetPoolID searches a list of pools in a cluster and returns the ID of the pool that matches
// the passed in poolName parameter // the passed in poolName parameter
func GetPoolID(monitors string, adminID string, key string, poolName string) (int64, error) { func GetPoolID(monitors, adminID, key, poolName string) (int64, error) {
// ceph <options> -f json osd lspools // ceph <options> -f json osd lspools
// JSON out: [{"poolnum":<int64>,"poolname":<string>}] // JSON out: [{"poolnum":<int64>,"poolname":<string>}]
@ -90,7 +90,7 @@ func GetPoolID(monitors string, adminID string, key string, poolName string) (in
// GetPoolName lists all pools in a ceph cluster, and matches the pool whose pool ID is equal to // GetPoolName lists all pools in a ceph cluster, and matches the pool whose pool ID is equal to
// the requested poolID parameter // the requested poolID parameter
func GetPoolName(monitors string, adminID string, key string, poolID int64) (string, error) { func GetPoolName(monitors, adminID, key string, poolID int64) (string, error) {
// ceph <options> -f json osd lspools // ceph <options> -f json osd lspools
// [{"poolnum":1,"poolname":"replicapool"}] // [{"poolnum":1,"poolname":"replicapool"}]

View File

@ -100,9 +100,9 @@ func (k8scm *K8sCMCache) ForAll(pattern string, destObj interface{}, f ForAllFun
return errors.Wrap(err, "k8s-cm-cache: failed to list metadata configmaps") return errors.Wrap(err, "k8s-cm-cache: failed to list metadata configmaps")
} }
for _, cm := range cms.Items { for i := range cms.Items {
data := cm.Data[cmDataKey] data := cms.Items[i].Data[cmDataKey]
match, err := regexp.MatchString(pattern, cm.ObjectMeta.Name) match, err := regexp.MatchString(pattern, cms.Items[i].ObjectMeta.Name)
if err != nil { if err != nil {
continue continue
} }
@ -110,9 +110,9 @@ func (k8scm *K8sCMCache) ForAll(pattern string, destObj interface{}, f ForAllFun
continue continue
} }
if err = json.Unmarshal([]byte(data), destObj); err != nil { if err = json.Unmarshal([]byte(data), destObj); err != nil {
return errors.Wrapf(err, "k8s-cm-cache: JSON unmarshaling failed for configmap %s", cm.ObjectMeta.Name) return errors.Wrapf(err, "k8s-cm-cache: JSON unmarshaling failed for configmap %s", cms.Items[i].ObjectMeta.Name)
} }
if err = f(cm.ObjectMeta.Name); err != nil { if err = f(cms.Items[i].ObjectMeta.Name); err != nil {
return err return err
} }
} }

View File

@ -59,9 +59,9 @@ func (nc *NodeCache) ForAll(pattern string, destObj interface{}, f ForAllFunc) e
if err != nil { if err != nil {
return errors.Wrapf(err, "node-cache: failed to read %s folder", nc.BasePath) return errors.Wrapf(err, "node-cache: failed to read %s folder", nc.BasePath)
} }
path := path.Join(nc.BasePath, nc.CacheDir) cachePath := path.Join(nc.BasePath, nc.CacheDir)
for _, file := range files { for _, file := range files {
err = decodeObj(path, pattern, file, destObj) err = decodeObj(cachePath, pattern, file, destObj)
if err == errDec { if err == errDec {
continue continue
} else if err == nil { } else if err == nil {
@ -75,7 +75,7 @@ func (nc *NodeCache) ForAll(pattern string, destObj interface{}, f ForAllFunc) e
return nil return nil
} }
func decodeObj(filepath, pattern string, file os.FileInfo, destObj interface{}) error { func decodeObj(fpath, pattern string, file os.FileInfo, destObj interface{}) error {
match, err := regexp.MatchString(pattern, file.Name()) match, err := regexp.MatchString(pattern, file.Name())
if err != nil || !match { if err != nil || !match {
return errDec return errDec
@ -84,7 +84,7 @@ func decodeObj(filepath, pattern string, file os.FileInfo, destObj interface{})
return errDec return errDec
} }
// #nosec // #nosec
fp, err := os.Open(path.Join(filepath, file.Name())) fp, err := os.Open(path.Join(fpath, file.Name()))
if err != nil { if err != nil {
klog.Infof("node-cache: open file: %s err %v", file.Name(), err) klog.Infof("node-cache: open file: %s err %v", file.Name(), err)
return errDec return errDec

View File

@ -39,7 +39,7 @@ func RoundUpToMiB(size int64) int64 {
return roundUpSize(requestBytes, MiB) return roundUpSize(requestBytes, MiB)
} }
func roundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 { func roundUpSize(volumeSizeBytes, allocationUnitBytes int64) int64 {
roundedUp := volumeSizeBytes / allocationUnitBytes roundedUp := volumeSizeBytes / allocationUnitBytes
if volumeSizeBytes%allocationUnitBytes > 0 { if volumeSizeBytes%allocationUnitBytes > 0 {
roundedUp++ roundedUp++
@ -74,7 +74,7 @@ func createPersistentStorage(persistentStoragePath string) error {
// ValidateDriverName validates the driver name // ValidateDriverName validates the driver name
func ValidateDriverName(driverName string) error { func ValidateDriverName(driverName string) error {
if len(driverName) == 0 { if driverName == "" {
return errors.New("driver name is empty") return errors.New("driver name is empty")
} }

View File

@ -139,7 +139,7 @@ func (ci *CSIIdentifier) DecomposeCSIID(composedCSIID string) (err error) {
ci.LocationID = int64(binary.BigEndian.Uint64(buf64)) ci.LocationID = int64(binary.BigEndian.Uint64(buf64))
// 16 for poolID encoding and 1 for '-' separator // 16 for poolID encoding and 1 for '-' separator
bytesToProcess -= 17 bytesToProcess -= 17
nextFieldStartIdx = nextFieldStartIdx + 17 nextFieldStartIdx += 17
// has to be an exact match // has to be an exact match
if bytesToProcess != uuidSize { if bytesToProcess != uuidSize {

View File

@ -107,6 +107,18 @@ linters-settings:
line-length: 180 line-length: 180
# tab width in spaces. Default to 1. # tab width in spaces. Default to 1.
tab-width: 1 tab-width: 1
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- sloppyReassign
- elseif
- unslice
- wrapperFunc
- unnamedResult
- dupImport # https://github.com/go-critic/go-critic/issues/845
unused: unused:
# treat code as a program (not a library) and report unused exported # treat code as a program (not a library) and report unused exported
# identifiers; default is false. # identifiers; default is false.
@ -131,23 +143,9 @@ linters-settings:
max-func-lines: 30 max-func-lines: 30
linters: linters:
enable: enable-all: true
- megacheck
- govet
- golint
- stylecheck
- interfacer
- unconvert
- gofmt
- gocyclo
- maligned
- lll
- nakedret
enable-all: false
disable: disable:
- prealloc - prealloc
disable-all: false - dupl
presets: - gochecknoinits
- bugs - gochecknoglobals
- unused
fast: false