diff --git a/.golanci.yml b/.golangci.yml similarity index 81% rename from .golanci.yml rename to .golangci.yml index b13b9daf..86227631 100644 --- a/.golanci.yml +++ b/.golangci.yml @@ -16,7 +16,7 @@ linters-settings: - default - blank - dot - - prefix(github.com/linode) + - prefix(github.com/linode/linode-blockstorage-csi-driver) govet: check-shadowing: false @@ -28,7 +28,7 @@ linters-settings: simplify: true goimports: - local-prefixes: github.com/linode/ + local-prefixes: github.com/linode/linode-blockstorage-csi-driver maligned: suggest-new: true @@ -70,10 +70,10 @@ linters: enable: - asasalint - asciicheck - - bidicheck - bodyclose - containedctx - contextcheck + - copyloopvar - decorder - dogsled - dupl @@ -83,9 +83,7 @@ linters: - errname - errorlint - errcheck - - exportloopref - exhaustive - - exportloopref - forbidigo - forcetypeassert - gci @@ -103,13 +101,11 @@ linters: - maintidx - makezero - misspell - - mnd - nestif - nilerr - nilnil - noctx - nolintlint - - parlalleltest - prealloc - predeclared - reassign @@ -131,11 +127,23 @@ linters: issues: exclude-rules: - - path: _test\.go + # Exclude some linters from running on tests files. + - path: _test(ing)?\.go linters: + - gocyclo - maintidx + - errcheck - dupl - - exportloopref + - gosec + - copyloopvar + - unparam + - varnamelen + + # Ease some gocritic warnings on test files. + - path: _test\.go + text: "(unnamedResult|exitAfterDefer)" + linters: + - gocritic - text: "G101:" linters: diff --git a/internal/driver/controllerserver.go b/internal/driver/controllerserver.go index 9ebaa14c..72bc39fa 100644 --- a/internal/driver/controllerserver.go +++ b/internal/driver/controllerserver.go @@ -158,12 +158,12 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol log.V(4).Info("CreateVolume details", "storage_size_giga_bytes", targetSizeGB, "volume_name", volumeName) volumeContext := make(map[string]string) - if req.Parameters[LuksEncryptedAttribute] == "true" { + if req.GetParameters()[LuksEncryptedAttribute] == "true" { // if luks encryption is enabled add a volume context volumeContext[LuksEncryptedAttribute] = "true" volumeContext[PublishInfoVolumeName] = volumeName - volumeContext[LuksCipherAttribute] = req.Parameters[LuksCipherAttribute] - volumeContext[LuksKeySizeAttribute] = req.Parameters[LuksKeySizeAttribute] + volumeContext[LuksCipherAttribute] = req.GetParameters()[LuksCipherAttribute] + volumeContext[LuksKeySizeAttribute] = req.GetParameters()[LuksKeySizeAttribute] } // Attempt to get info about the source volume for @@ -182,7 +182,7 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol ctx, volumeName, targetSizeGB, - req.Parameters[VolumeTags], + req.GetParameters()[VolumeTags], sourceVolumeInfo, ) if err != nil { @@ -497,7 +497,6 @@ func (cs *ControllerServer) ValidateVolumeCapabilities(ctx context.Context, req return &csi.ValidateVolumeCapabilitiesResponse{}, errInternal("get volume: %v", err) } - resp := &csi.ValidateVolumeCapabilitiesResponse{} if validVolumeCapabilities(volumeCapabilities) { resp.Confirmed = &csi.ValidateVolumeCapabilitiesResponse_Confirmed{VolumeCapabilities: volumeCapabilities} @@ -532,7 +531,6 @@ func (cs *ControllerServer) ListVolumes(ctx context.Context, req *csi.ListVolume nextToken = strconv.Itoa(listOpts.Page + 1) } - // List all volumes log.V(4).Info("Listing volumes", "list_opts", listOpts) volumes, err := cs.client.ListVolumes(ctx, listOpts) @@ -606,7 +604,7 @@ func (cs *ControllerServer) ControllerGetCapabilities(ctx context.Context, req * func (cs *ControllerServer) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) { log, ctx, done := logger.GetLogger(ctx).WithMethod("ControllerExpandVolume") defer done() - + log.V(2).Info("Processing request", "req", req) volumeID, statusErr := linodevolumes.VolumeIdAsInt("ControllerExpandVolume", req) diff --git a/internal/driver/controllerserver_test.go b/internal/driver/controllerserver_test.go index fbbd4be5..2dbd0629 100644 --- a/internal/driver/controllerserver_test.go +++ b/internal/driver/controllerserver_test.go @@ -7,9 +7,10 @@ import ( "testing" "github.com/container-storage-interface/spec/lib/go/csi" + "github.com/linode/linodego" + linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client" linodevolumes "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-volumes" - "github.com/linode/linodego" ) func TestListVolumes(t *testing.T) { @@ -101,7 +102,7 @@ func TestListVolumes(t *testing.T) { var linodeVolume *linodego.Volume for _, v := range tt.volumes { key := linodevolumes.CreateLinodeVolumeKey(v.ID, v.Label) - if volume.VolumeId == key.GetVolumeKey() { + if volume.GetVolumeId() == key.GetVolumeKey() { v := v linodeVolume = &v break @@ -111,11 +112,11 @@ func TestListVolumes(t *testing.T) { t.Fatalf("no matching linode volume for %#v", volume) } - if want, got := int64(linodeVolume.Size<<30), volume.CapacityBytes; want != got { + if want, got := int64(linodeVolume.Size<<30), volume.GetCapacityBytes(); want != got { t.Errorf("mismatched volume size: want=%d got=%d", want, got) } for _, topology := range volume.GetAccessibleTopology() { - region, ok := topology.Segments[VolumeTopologyRegion] + region, ok := topology.GetSegments()[VolumeTopologyRegion] if !ok { t.Error("region not set in volume topology") } @@ -129,7 +130,7 @@ func TestListVolumes(t *testing.T) { t.Error("nil status") continue } - if status.VolumeCondition.Abnormal { + if status.GetVolumeCondition().GetAbnormal() { t.Error("abnormal volume condition") } diff --git a/internal/driver/driver.go b/internal/driver/driver.go index 931980af..e72b7d72 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -23,13 +23,13 @@ import ( "sync" "github.com/container-storage-interface/spec/lib/go/csi" - linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client" - - "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" - mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "k8s.io/mount-utils" + + linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client" + "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" + mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" ) // Name is the name of the driver provided by this package. @@ -141,7 +141,7 @@ func (linodeDriver *LinodeDriver) ValidateControllerServiceRequest(ctx context.C } for _, cap := range linodeDriver.cscap { - if c == cap.GetRpc().Type { + if c == cap.GetRpc().GetType() { log.V(4).Info("Controller service request validated successfully") return nil } diff --git a/internal/driver/driver_test.go b/internal/driver/driver_test.go index 33d1127d..c6625770 100644 --- a/internal/driver/driver_test.go +++ b/internal/driver/driver_test.go @@ -7,14 +7,13 @@ import ( "os" "testing" + "github.com/linode/linodego" + "go.uber.org/mock/gomock" + "k8s.io/mount-utils" + "github.com/linode/linode-blockstorage-csi-driver/mocks" drivertest "github.com/linode/linode-blockstorage-csi-driver/pkg/driver-test" linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client" - "k8s.io/mount-utils" - - "github.com/linode/linodego" - - "go.uber.org/mock/gomock" ) var ( diff --git a/internal/driver/identityserver.go b/internal/driver/identityserver.go index 46017430..332271bc 100644 --- a/internal/driver/identityserver.go +++ b/internal/driver/identityserver.go @@ -18,11 +18,12 @@ import ( "fmt" csi "github.com/container-storage-interface/spec/lib/go/csi" - "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" "golang.org/x/net/context" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/wrapperspb" + + "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" ) // IdentityServer implements the CSI Identity service for the Linode Block Storage CSI Driver. diff --git a/internal/driver/luks.go b/internal/driver/luks.go index a1e3dee0..d0bd4e26 100644 --- a/internal/driver/luks.go +++ b/internal/driver/luks.go @@ -30,7 +30,7 @@ import ( "k8s.io/klog/v2" utilexec "k8s.io/utils/exec" - "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" + mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" ) type LuksContext struct { @@ -243,7 +243,6 @@ func (e *Encryption) isLuksMapping(volume string) (bool, string, error) { return false, mappingName, nil } } - } return false, "", nil } diff --git a/internal/driver/metadata.go b/internal/driver/metadata.go index e8dfd938..319aeb22 100644 --- a/internal/driver/metadata.go +++ b/internal/driver/metadata.go @@ -9,8 +9,9 @@ import ( "strconv" metadata "github.com/linode/go-metadata" - "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" "github.com/linode/linodego" + + "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" ) // Metadata contains metadata about the node/instance the CSI node plugin @@ -45,7 +46,7 @@ func GetMetadata(ctx context.Context) (Metadata, error) { return Metadata{}, fmt.Errorf("get instance data: %w", err) } - log.V(4).Info("Successfully retrieved metadata", + log.V(4).Info("Successfully retrieved metadata", "instanceID", data.ID, "instanceLabel", data.Label, "region", data.Region, @@ -140,7 +141,7 @@ func GetMetadataFromAPI(ctx context.Context, client *linodego.Client) (Metadata, Memory: memory, } - log.V(4).Info("Successfully retrieved metadata", + log.V(4).Info("Successfully retrieved metadata", "instanceID", metadata.ID, "instanceLabel", metadata.Label, "region", metadata.Region, diff --git a/internal/driver/nodeserver.go b/internal/driver/nodeserver.go index b82f9e27..3ad982e9 100644 --- a/internal/driver/nodeserver.go +++ b/internal/driver/nodeserver.go @@ -21,13 +21,14 @@ import ( "sync" "github.com/container-storage-interface/spec/lib/go/csi" + "github.com/linode/linodego" + "golang.org/x/net/context" + "k8s.io/mount-utils" + linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client" linodevolumes "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-volumes" "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" - "github.com/linode/linodego" - "golang.org/x/net/context" - "k8s.io/mount-utils" ) type NodeServer struct { @@ -227,7 +228,7 @@ func (ns *NodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol // Check if the volume mode is set to 'Block' // Do nothing else with the mount point for stage - if blk := req.VolumeCapability.GetBlock(); blk != nil { + if blk := req.GetVolumeCapability().GetBlock(); blk != nil { log.V(4).Info("Volume is a block volume", "volumeID", volumeID) return &csi.NodeStageVolumeResponse{}, nil } @@ -307,7 +308,7 @@ func (ns *NodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandV log.V(2).Info("Successfully completed", "volumeID", volumeID) return &csi.NodeExpandVolumeResponse{ - CapacityBytes: req.CapacityRange.RequiredBytes, + CapacityBytes: req.GetCapacityRange().GetRequiredBytes(), }, nil } diff --git a/internal/driver/nodeserver_all.go b/internal/driver/nodeserver_all.go index 63bbb749..c3a6485d 100644 --- a/internal/driver/nodeserver_all.go +++ b/internal/driver/nodeserver_all.go @@ -8,22 +8,23 @@ import ( "fmt" "github.com/container-storage-interface/spec/lib/go/csi" - "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" "golang.org/x/sys/unix" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + + "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" ) func nodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) { log := logger.GetLogger(ctx) - if req.VolumeId == "" || req.VolumePath == "" { + if req.GetVolumeId() == "" || req.GetVolumePath() == "" { return nil, status.Error(codes.InvalidArgument, "volume ID or path empty") } var statfs unix.Statfs_t // See http://man7.org/linux/man-pages/man2/statfs.2.html for details. - err := unix.Statfs(req.VolumePath, &statfs) + err := unix.Statfs(req.GetVolumePath(), &statfs) if err != nil && !errors.Is(err, unix.EIO) { if errors.Is(err, unix.ENOENT) { return nil, status.Errorf(codes.NotFound, "volume path not found: %v", err.Error()) @@ -58,6 +59,6 @@ func nodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) }, } - log.V(2).Info("Successfully retrieved volume stats", "volumeID", req.VolumeId, "volumePath", req.VolumePath, "response", response) + log.V(2).Info("Successfully retrieved volume stats", "volumeID", req.GetVolumeId(), "volumePath", req.GetVolumePath(), "response", response) return response, nil } diff --git a/internal/driver/nodeserver_helpers.go b/internal/driver/nodeserver_helpers.go index 7c1832c2..0fc94f7d 100644 --- a/internal/driver/nodeserver_helpers.go +++ b/internal/driver/nodeserver_helpers.go @@ -23,12 +23,13 @@ import ( "strings" "github.com/container-storage-interface/spec/lib/go/csi" - linodevolumes "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-volumes" - "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" - mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" "k8s.io/klog/v2" "k8s.io/mount-utils" utilexec "k8s.io/utils/exec" + + linodevolumes "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-volumes" + "github.com/linode/linode-blockstorage-csi-driver/pkg/logger" + mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" ) const ( @@ -157,12 +158,12 @@ func getFSTypeAndMountOptions(ctx context.Context, volumeCapability *csi.VolumeC if mnt := volumeCapability.GetMount(); mnt != nil { // Use file system type from volume capability if specified - if mnt.FsType != "" { - fsType = mnt.FsType + if mnt.GetFsType() != "" { + fsType = mnt.GetFsType() } // Use mount options from volume capability if specified if mnt.MountFlags != nil { - mountOptions = mnt.MountFlags + mountOptions = mnt.GetMountFlags() } } @@ -240,7 +241,7 @@ func (ns *NodeServer) nodePublishVolumeBlock(ctx context.Context, req *csi.NodeP targetPathDir := filepath.Dir(targetPath) // Get the device path from the request - devicePath := req.PublishContext["devicePath"] + devicePath := req.GetPublishContext()["devicePath"] if devicePath == "" { return nil, errInternal("devicePath cannot be found") } @@ -296,7 +297,7 @@ func (ns *NodeServer) mountVolume(ctx context.Context, devicePath string, req *c fmtAndMountSource := devicePath // Check if LUKS encryption is enabled and prepare the LUKS volume if needed - luksContext := getLuksContext(req.Secrets, req.VolumeContext, VolumeLifecycleNodeStageVolume) + luksContext := getLuksContext(req.GetSecrets(), req.GetVolumeContext(), VolumeLifecycleNodeStageVolume) if luksContext.EncryptionEnabled { var err error log.V(4).Info("preparing luks volume", "devicePath", devicePath) diff --git a/internal/driver/nodeserver_helpers_test.go b/internal/driver/nodeserver_helpers_test.go index c8aa8ddf..47d54a8f 100644 --- a/internal/driver/nodeserver_helpers_test.go +++ b/internal/driver/nodeserver_helpers_test.go @@ -1,6 +1,7 @@ package driver import ( + "context" "fmt" "os" osexec "os/exec" @@ -8,15 +9,14 @@ import ( "runtime" "testing" - "context" - "github.com/container-storage-interface/spec/lib/go/csi" - "github.com/linode/linode-blockstorage-csi-driver/mocks" - linodevolumes "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-volumes" "go.uber.org/mock/gomock" "google.golang.org/grpc/status" "k8s.io/mount-utils" "k8s.io/utils/exec" + + "github.com/linode/linode-blockstorage-csi-driver/mocks" + linodevolumes "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-volumes" ) // compareGRPCErrors compares two gRPC errors for equality. @@ -246,7 +246,6 @@ func TestNodeServer_findDevicePath(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Create gomock controller ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -345,7 +344,6 @@ func TestNodeServer_ensureMountPoint(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -448,7 +446,6 @@ func TestNodeServer_prepareLUKSVolume(t *testing.T) { m.EXPECT().Command(gomock.Any(), gomock.Any()).Return(c) c.EXPECT().SetStdin(gomock.Any()) c.EXPECT().CombinedOutput().Return(nil, fmt.Errorf("failed test")) - }, devicePath: "/dev/test", luksContext: LuksContext{ @@ -494,7 +491,6 @@ func TestNodeServer_prepareLUKSVolume(t *testing.T) { m.EXPECT().Command(gomock.Any(), gomock.Any()).Return(c) c.EXPECT().SetStdin(gomock.Any()) c.EXPECT().CombinedOutput().Return(nil, fmt.Errorf("failed test")) - }, devicePath: "/dev/test", luksContext: LuksContext{ @@ -540,7 +536,6 @@ func TestNodeServer_prepareLUKSVolume(t *testing.T) { m.EXPECT().Command(gomock.Any(), gomock.Any()).Return(c) c.EXPECT().SetStdin(gomock.Any()) c.EXPECT().CombinedOutput().Return([]byte("test"), nil) - }, devicePath: "/dev/test", luksContext: LuksContext{ @@ -630,7 +625,6 @@ func TestNodeServer_prepareLUKSVolume(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -758,7 +752,6 @@ func TestNodeServer_mountVolume(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Skip this test on Linux. Linux supported alternative test case can be found in nodeserver_helpers_linux_test.go if runtime.GOOS == "linux" { t.Skipf("Skipping test on Linux") @@ -892,7 +885,6 @@ func TestNodeServer_closeLuksMountSources(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) defer ctrl.Finish() diff --git a/internal/driver/nodeserver_test.go b/internal/driver/nodeserver_test.go index cbf506e1..e903a762 100644 --- a/internal/driver/nodeserver_test.go +++ b/internal/driver/nodeserver_test.go @@ -5,10 +5,11 @@ import ( "reflect" "testing" - linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client" - mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" "github.com/linode/linodego" "k8s.io/mount-utils" + + linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client" + mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager" ) func TestNewNodeServer(t *testing.T) { diff --git a/internal/driver/server.go b/internal/driver/server.go index 837490ca..01a7d24d 100644 --- a/internal/driver/server.go +++ b/internal/driver/server.go @@ -20,10 +20,9 @@ import ( "os" "sync" + csi "github.com/container-storage-interface/spec/lib/go/csi" "google.golang.org/grpc" "k8s.io/klog/v2" - - csi "github.com/container-storage-interface/spec/lib/go/csi" ) // Defines Non blocking GRPC server interfaces @@ -112,5 +111,4 @@ func (s *nonBlockingGRPCServer) serve(endpoint string, ids csi.IdentityServer, c if err := server.Serve(listener); err != nil { klog.Fatalf("Failed to serve: %v", err) } - } diff --git a/pkg/driver-test/fake-api.go b/pkg/driver-test/fake-api.go index d4e3c3a0..bab8e6c0 100644 --- a/pkg/driver-test/fake-api.go +++ b/pkg/driver-test/fake-api.go @@ -55,7 +55,6 @@ func (f *FakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { } for _, vol := range f.Volumes { - if filters["label"] != "" && filters["label"] != vol.Label { continue } @@ -104,7 +103,6 @@ func (f *FakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { rr, _ := json.Marshal(resp) _, _ = w.Write(rr) return - } case "POST": @@ -138,7 +136,6 @@ func (f *FakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { } vol.LinodeID = &v.LinodeID f.Volumes[parts[2]] = vol - } else if tp == "detach" { parts := strings.Split(r.URL.Path, "/") if len(parts) != 4 { @@ -183,7 +180,6 @@ func (f *FakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { } f.Volumes[strconv.Itoa(id)] = vol - } resp, err := json.Marshal(vol)