From bf9175d306015513abf7bd91f60a118daa55cbd1 Mon Sep 17 00:00:00 2001 From: mengze zhu <1500011096@pku.edu.cn> Date: Wed, 4 Sep 2024 10:59:52 +0800 Subject: [PATCH] cleanup: fix golint errors --- .github/workflows/static.yaml | 2 +- pkg/blob/blob.go | 2 +- pkg/blob/controllerserver.go | 22 +++++++++++----------- pkg/blob/controllerserver_test.go | 26 +++++++++++++------------- pkg/blob/nodeserver.go | 12 ++++++------ pkg/csi-common/utils.go | 4 ++-- pkg/csi-common/utils_test.go | 2 +- test/e2e/dynamic_provisioning_test.go | 4 ++-- test/e2e/suite_test.go | 6 +++--- 9 files changed, 40 insertions(+), 40 deletions(-) diff --git a/.github/workflows/static.yaml b/.github/workflows/static.yaml index 20a0524c4..85af7d16b 100644 --- a/.github/workflows/static.yaml +++ b/.github/workflows/static.yaml @@ -15,5 +15,5 @@ jobs: - name: Run linter uses: golangci/golangci-lint-action@v4 with: - version: v1.54 + version: v1.60 args: -E=gofmt,unused,ineffassign,revive,misspell,exportloopref,asciicheck,bodyclose,depguard,dogsled,durationcheck,errname,forbidigo -D=staticcheck --timeout=30m0s diff --git a/pkg/blob/blob.go b/pkg/blob/blob.go index 609f53bd4..409a3903e 100644 --- a/pkg/blob/blob.go +++ b/pkg/blob/blob.go @@ -290,7 +290,7 @@ func NewDriver(options *DriverOptions, kubeClient kubernetes.Interface, cloud *p } var err error - getter := func(key string) (interface{}, error) { return nil, nil } + getter := func(_ string) (interface{}, error) { return nil, nil } if d.accountSearchCache, err = azcache.NewTimedCache(time.Minute, getter, false); err != nil { klog.Fatalf("%v", err) } diff --git a/pkg/blob/controllerserver.go b/pkg/blob/controllerserver.go index 4542c6210..99496410c 100644 --- a/pkg/blob/controllerserver.go +++ b/pkg/blob/controllerserver.go @@ -207,7 +207,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) // only do validations here, used in NodeStageVolume, NodePublishVolume if v != "" { if _, err := strconv.ParseUint(v, 8, 32); err != nil { - return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid mountPermissions %s in storage class", v)) + return nil, status.Errorf(codes.InvalidArgument, "invalid mountPermissions %s in storage class", v) } } case useDataPlaneAPIField: @@ -217,7 +217,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) case tagValueDelimiterField: tagValueDelimiter = v default: - return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid parameter %q in storage class", k)) + return nil, status.Errorf(codes.InvalidArgument, "invalid parameter %q in storage class", k) } } @@ -232,7 +232,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) } if matchTags && account != "" { - return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("matchTags must set as false when storageAccount(%s) is provided", account)) + return nil, status.Errorf(codes.InvalidArgument, "matchTags must set as false when storageAccount(%s) is provided", account) } if resourceGroup == "" { @@ -292,13 +292,13 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) if IsAzureStackCloud(d.cloud) { accountKind = string(armstorage.KindStorage) if storageAccountType != "" && storageAccountType != string(armstorage.SKUNameStandardLRS) && storageAccountType != string(armstorage.SKUNamePremiumLRS) { - return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("Invalid skuName value: %s, as Azure Stack only supports %s and %s Storage Account types.", storageAccountType, storage.SkuNamePremiumLRS, storage.SkuNameStandardLRS)) + return nil, status.Errorf(codes.InvalidArgument, "Invalid skuName value: %s, as Azure Stack only supports %s and %s Storage Account types.", storageAccountType, storage.SkuNamePremiumLRS, storage.SkuNameStandardLRS) } } tags, err := util.ConvertTagsToMap(customTags, tagValueDelimiter) if err != nil { - return nil, status.Errorf(codes.InvalidArgument, err.Error()) + return nil, status.Errorf(codes.InvalidArgument, "%v", err) } if strings.TrimSpace(storageEndpointSuffix) == "" { @@ -386,7 +386,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) // search in cache first cache, err := d.accountSearchCache.Get(lockKey, azcache.CacheReadTypeDefault) if err != nil { - return nil, status.Errorf(codes.Internal, err.Error()) + return nil, status.Errorf(codes.Internal, "%v", err) } if cache != nil { accountName = cache.(string) @@ -863,7 +863,7 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) { authAzcopyEnv = append(authAzcopyEnv, fmt.Sprintf("%s=%s", azcopySPAApplicationID, azureAuthConfig.AADClientID)) authAzcopyEnv = append(authAzcopyEnv, fmt.Sprintf("%s=%s", azcopySPAClientSecret, azureAuthConfig.AADClientSecret)) authAzcopyEnv = append(authAzcopyEnv, fmt.Sprintf("%s=%s", azcopyTenantID, azureAuthConfig.TenantID)) - klog.V(2).Infof(fmt.Sprintf("set AZCOPY_SPA_APPLICATION_ID=%s, AZCOPY_TENANT_ID=%s successfully", azureAuthConfig.AADClientID, azureAuthConfig.TenantID)) + klog.V(2).Infof("set AZCOPY_SPA_APPLICATION_ID=%s, AZCOPY_TENANT_ID=%s successfully", azureAuthConfig.AADClientID, azureAuthConfig.TenantID) return authAzcopyEnv, nil } @@ -919,10 +919,10 @@ func isValidVolumeCapabilities(volCaps []*csi.VolumeCapability) error { func parseDays(dayStr string) (int32, error) { days, err := strconv.Atoi(dayStr) if err != nil { - return 0, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid %s:%s in storage class", softDeleteBlobsField, dayStr)) + return 0, status.Errorf(codes.InvalidArgument, "invalid %s:%s in storage class", softDeleteBlobsField, dayStr) } if days <= 0 || days > 365 { - return 0, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid %s:%s in storage class, should be in range [1, 365]", softDeleteBlobsField, dayStr)) + return 0, status.Errorf(codes.InvalidArgument, "invalid %s:%s in storage class, should be in range [1, 365]", softDeleteBlobsField, dayStr) } return int32(days), nil @@ -942,13 +942,13 @@ func (d *Driver) generateSASToken(accountName, accountKey, storageEndpointSuffix credential, err := azblob.NewSharedKeyCredential(accountName, accountKey) if err != nil { - return "", status.Errorf(codes.Internal, fmt.Sprintf("failed to generate sas token in creating new shared key credential, accountName: %s, err: %s", accountName, err.Error())) + return "", status.Errorf(codes.Internal, "failed to generate sas token in creating new shared key credential, accountName: %s, err: %v", accountName, err) } clientOptions := service.ClientOptions{} clientOptions.InsecureAllowCredentialWithHTTP = true serviceClient, err := service.NewClientWithSharedKeyCredential(fmt.Sprintf("https://%s.blob.%s/", accountName, storageEndpointSuffix), credential, &clientOptions) if err != nil { - return "", status.Errorf(codes.Internal, fmt.Sprintf("failed to generate sas token in creating new client with shared key credential, accountName: %s, err: %s", accountName, err.Error())) + return "", status.Errorf(codes.Internal, "failed to generate sas token in creating new client with shared key credential, accountName: %s, err: %v", accountName, err) } sasURL, err := serviceClient.GetSASURL( sas.AccountResourceTypes{Object: true, Service: false, Container: true}, diff --git a/pkg/blob/controllerserver_test.go b/pkg/blob/controllerserver_test.go index f1df87696..1ea2f4441 100644 --- a/pkg/blob/controllerserver_test.go +++ b/pkg/blob/controllerserver_test.go @@ -378,7 +378,7 @@ func TestCreateVolume(t *testing.T) { controllerServiceCapability, } - expectedErr := status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid parameter %q in storage class", "invalidparameter")) + expectedErr := status.Errorf(codes.InvalidArgument, "invalid parameter %q in storage class", "invalidparameter") _, err := d.CreateVolume(context.Background(), req) if !reflect.DeepEqual(err, expectedErr) { t.Errorf("Unexpected error: %v", err) @@ -400,7 +400,7 @@ func TestCreateVolume(t *testing.T) { controllerServiceCapability, } - expectedErr := status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid %s %s in storage class", "mountPermissions", "0abc")) + expectedErr := status.Errorf(codes.InvalidArgument, "invalid %s %s in storage class", "mountPermissions", "0abc") _, err := d.CreateVolume(context.Background(), req) if !reflect.DeepEqual(err, expectedErr) { t.Errorf("Unexpected error: %v", err) @@ -496,7 +496,7 @@ func TestCreateVolume(t *testing.T) { controllerServiceCapability, } - expectedErr := status.Errorf(codes.InvalidArgument, fmt.Sprintf("Invalid skuName value: %s, as Azure Stack only supports %s and %s Storage Account types.", "unit-test", storage.SkuNamePremiumLRS, storage.SkuNameStandardLRS)) + expectedErr := status.Errorf(codes.InvalidArgument, "Invalid skuName value: %s, as Azure Stack only supports %s and %s Storage Account types.", "unit-test", storage.SkuNamePremiumLRS, storage.SkuNameStandardLRS) _, err := d.CreateVolume(context.Background(), req) if !reflect.DeepEqual(err, expectedErr) { t.Errorf("Unexpected error: %v", err) @@ -1059,7 +1059,7 @@ func TestValidateVolumeCapabilities(t *testing.T) { clientErr: DATAPLANE, containerProp: &armstorage.ContainerProperties{}, expectedRes: nil, - expectedErr: status.Errorf(codes.Internal, fmt.Errorf(containerBeingDeletedDataplaneAPIError).Error()), + expectedErr: status.Errorf(codes.Internal, "%v", containerBeingDeletedDataplaneAPIError), }, { name: "Requested Volume does not exist", @@ -1071,7 +1071,7 @@ func TestValidateVolumeCapabilities(t *testing.T) { clientErr: NULL, containerProp: &armstorage.ContainerProperties{}, expectedRes: nil, - expectedErr: status.Errorf(codes.NotFound, fmt.Sprintf("requested volume(%s) does not exist", "unit#test#test")), + expectedErr: status.Errorf(codes.NotFound, "requested volume(%s) does not exist", "unit#test#test"), }, /*{ //Volume being shown as not existing. ContainerProperties.Deleted not setting correctly?? name: "Successful I/O", @@ -1117,7 +1117,7 @@ func TestValidateVolumeCapabilities(t *testing.T) { blobClientMock := mock_blobcontainerclient.NewMockInterface(ctrl) clientFactoryMock.EXPECT().GetBlobContainerClientForSub(gomock.Any()).Return(blobClientMock, nil).AnyTimes() blobClientMock.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( - func(ctx context.Context, resourceGroupName string, parentResourceName string, resourceName string) (result *armstorage.BlobContainer, rerr error) { + func(_ context.Context, _ string, _ string, _ string) (result *armstorage.BlobContainer, _ error) { switch test.clientErr { case DATAPLANE: return nil, fmt.Errorf(containerBeingDeletedDataplaneAPIError) @@ -1357,7 +1357,7 @@ func TestCreateBlobContainer(t *testing.T) { clientFactoryMock.EXPECT().GetBlobContainerClientForSub(gomock.Any()).Return(blobClientMock, nil).AnyTimes() d.clientFactory = clientFactoryMock blobClientMock.EXPECT().CreateContainer(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( - func(ctx context.Context, resourceGroupName, accountName, containerName string, parameters armstorage.BlobContainer) (*armstorage.BlobContainer, error) { + func(_ context.Context, _, _, _ string, _ armstorage.BlobContainer) (*armstorage.BlobContainer, error) { if test.clientErr == DATAPLANE { return nil, fmt.Errorf(containerBeingDeletedDataplaneAPIError) } @@ -1365,7 +1365,7 @@ func TestCreateBlobContainer(t *testing.T) { return nil, fmt.Errorf(containerBeingDeletedManagementAPIError) } if test.clientErr == CUSTOM { - return nil, fmt.Errorf(test.customErrStr) + return nil, fmt.Errorf("%v", test.customErrStr) } return nil, nil }).AnyTimes() @@ -1447,7 +1447,7 @@ func TestDeleteBlobContainer(t *testing.T) { clientFactoryMock.EXPECT().GetBlobContainerClientForSub(gomock.Any()).Return(blobClientMock, nil).AnyTimes() d.clientFactory = clientFactoryMock blobClientMock.EXPECT().DeleteContainer(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( - func(ctx context.Context, resourceGroupName, accountName, containerName string) error { + func(_ context.Context, _, _, _ string) error { if test.clientErr == DATAPLANE { return fmt.Errorf(containerBeingDeletedDataplaneAPIError) } @@ -1455,7 +1455,7 @@ func TestDeleteBlobContainer(t *testing.T) { return fmt.Errorf(containerBeingDeletedManagementAPIError) } if test.clientErr == CUSTOM { - return fmt.Errorf(test.customErrStr) + return fmt.Errorf("%v", test.customErrStr) } return nil }).AnyTimes() @@ -1793,7 +1793,7 @@ func TestGenerateSASToken(t *testing.T) { accountName: "unit-test", accountKey: "fakeValue", want: "", - expectedErr: status.Errorf(codes.Internal, fmt.Sprintf("failed to generate sas token in creating new shared key credential, accountName: %s, err: %s", "unit-test", "decode account key: illegal base64 data at input byte 8")), + expectedErr: status.Errorf(codes.Internal, "failed to generate sas token in creating new shared key credential, accountName: %s, err: %s", "unit-test", "decode account key: illegal base64 data at input byte 8"), }, } for _, tt := range tests { @@ -2008,7 +2008,7 @@ func TestGetAzcopyAuth(t *testing.T) { } expectedAccountSASToken := "" - expectedErr := status.Errorf(codes.Internal, fmt.Sprintf("failed to generate sas token in creating new shared key credential, accountName: %s, err: %s", "accountName", "decode account key: illegal base64 data at input byte 8")) + expectedErr := status.Errorf(codes.Internal, "failed to generate sas token in creating new shared key credential, accountName: %s, err: %s", "accountName", "decode account key: illegal base64 data at input byte 8") accountSASToken, _, err := d.getAzcopyAuth(context.Background(), "accountName", "", "core.windows.net", &azure.AccountOptions{}, secrets, "secretsName", "secretsNamespace", false) if !reflect.DeepEqual(err, expectedErr) || !reflect.DeepEqual(accountSASToken, expectedAccountSASToken) { t.Errorf("Unexpected accountSASToken: %s, Unexpected error: %v", accountSASToken, err) @@ -2029,7 +2029,7 @@ func TestGetAzcopyAuth(t *testing.T) { ctx := context.Background() expectedAccountSASToken := "" - expectedErr := status.Errorf(codes.Internal, fmt.Sprintf("failed to generate sas token in creating new shared key credential, accountName: %s, err: %s", "accountName", "decode account key: illegal base64 data at input byte 8")) + expectedErr := status.Errorf(codes.Internal, "failed to generate sas token in creating new shared key credential, accountName: %s, err: %s", "accountName", "decode account key: illegal base64 data at input byte 8") accountSASToken, _, err := d.getAzcopyAuth(ctx, "accountName", "", "core.windows.net", &azure.AccountOptions{}, secrets, "secretsName", "secretsNamespace", false) if !reflect.DeepEqual(err, expectedErr) || !reflect.DeepEqual(accountSASToken, expectedAccountSASToken) { t.Errorf("Unexpected accountSASToken: %s, Unexpected error: %v", accountSASToken, err) diff --git a/pkg/blob/nodeserver.go b/pkg/blob/nodeserver.go index 80706649a..a8b231b49 100644 --- a/pkg/blob/nodeserver.go +++ b/pkg/blob/nodeserver.go @@ -112,7 +112,7 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu if perm := getValueInMap(context, mountPermissionsField); perm != "" { var err error if mountPermissions, err = strconv.ParseUint(perm, 8, 32); err != nil { - return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid mountPermissions %s", perm)) + return nil, status.Errorf(codes.InvalidArgument, "invalid mountPermissions %s", perm) } } } @@ -141,7 +141,7 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu klog.Warningf("NodePublishVolume: mock mount on volumeID(%s), this is only for TESTING!!!", volumeID) if err := volumehelper.MakeDir(target, os.FileMode(mountPermissions)); err != nil { klog.Errorf("MakeDir failed on target: %s (%v)", target, err) - return nil, status.Errorf(codes.Internal, err.Error()) + return nil, status.Errorf(codes.Internal, "%v", err) } return &csi.NodePublishVolumeResponse{}, nil } @@ -297,7 +297,7 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe var err error var perm uint64 if perm, err = strconv.ParseUint(v, 8, 32); err != nil { - return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid mountPermissions %s", v)) + return nil, status.Errorf(codes.InvalidArgument, "invalid mountPermissions %s", v) } if perm == 0 { performChmodOp = false @@ -325,7 +325,7 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe _, accountName, _, containerName, authEnv, err := d.GetAuthEnv(ctx, volumeID, protocol, attrib, secrets) if err != nil { - return nil, status.Errorf(codes.Internal, err.Error()) + return nil, status.Errorf(codes.Internal, "%v", err) } // replace pv/pvc name namespace metadata in subDir @@ -429,7 +429,7 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe klog.Warningf("NodeStageVolume: mock mount on volumeID(%s), this is only for TESTING!!!", volumeID) if err := volumehelper.MakeDir(targetPath, os.FileMode(mountPermissions)); err != nil { klog.Errorf("MakeDir failed on target: %s (%v)", targetPath, err) - return nil, status.Errorf(codes.Internal, err.Error()) + return nil, status.Errorf(codes.Internal, "%v", err) } return &csi.NodeStageVolumeResponse{}, nil } @@ -549,7 +549,7 @@ func (d *Driver) NodeGetVolumeStats(_ context.Context, req *csi.NodeGetVolumeSta // check if the volume stats is cached cache, err := d.volStatsCache.Get(req.VolumeId, azcache.CacheReadTypeDefault) if err != nil { - return nil, status.Errorf(codes.Internal, err.Error()) + return nil, status.Errorf(codes.Internal, "%v", err) } if cache != nil { resp := cache.(csi.NodeGetVolumeStatsResponse) diff --git a/pkg/csi-common/utils.go b/pkg/csi-common/utils.go index 957a26fc7..21f5889c9 100644 --- a/pkg/csi-common/utils.go +++ b/pkg/csi-common/utils.go @@ -44,7 +44,7 @@ func ParseEndpoint(ep string) (string, string, error) { func Listen(ctx context.Context, endpoint string) (net.Listener, error) { proto, addr, err := ParseEndpoint(endpoint) if err != nil { - klog.Errorf(err.Error()) + klog.Errorf("%v", err) return nil, err } @@ -53,7 +53,7 @@ func Listen(ctx context.Context, endpoint string) (net.Listener, error) { addr = "/" + addr } if err := os.Remove(addr); err != nil && !os.IsNotExist(err) { - klog.Errorf("Failed to remove %s, error: %s", addr, err.Error()) + klog.Errorf("Failed to remove %s, error: %v", addr, err) return nil, err } } diff --git a/pkg/csi-common/utils_test.go b/pkg/csi-common/utils_test.go index 04a8f18ce..720e5f1e9 100644 --- a/pkg/csi-common/utils_test.go +++ b/pkg/csi-common/utils_test.go @@ -90,7 +90,7 @@ func TestLogGRPC(t *testing.T) { klog.SetOutput(buf) defer klog.SetOutput(io.Discard) - handler := func(ctx context.Context, req interface{}) (interface{}, error) { return nil, nil } + handler := func(_ context.Context, _ interface{}) (interface{}, error) { return nil, nil } info := grpc.UnaryServerInfo{ FullMethod: "fake", } diff --git a/test/e2e/dynamic_provisioning_test.go b/test/e2e/dynamic_provisioning_test.go index 3544368cb..9057a3eb3 100644 --- a/test/e2e/dynamic_provisioning_test.go +++ b/test/e2e/dynamic_provisioning_test.go @@ -40,7 +40,7 @@ var _ = ginkgo.Describe("[blob-csi-e2e] Dynamic Provisioning", func() { testDriver driver.PVTestDriver ) - ginkgo.BeforeEach(func(ctx ginkgo.SpecContext) { + ginkgo.BeforeEach(func(_ ginkgo.SpecContext) { checkPodsRestart := testCmd{ command: "sh", args: []string{"test/utils/check_driver_pods_restart.sh"}, @@ -891,7 +891,7 @@ var _ = ginkgo.Describe("[blob-csi-e2e] Dynamic Provisioning", func() { test.Run(ctx, cs, ns) }) - ginkgo.It("[blob.csi.azure.com] verify examples", ginkgo.Label("flaky"), func(ctx ginkgo.SpecContext) { + ginkgo.It("[blob.csi.azure.com] verify examples", ginkgo.Label("flaky"), func(_ ginkgo.SpecContext) { createExampleDeployment := testCmd{ command: "bash", args: []string{"hack/verify-examples.sh"}, diff --git a/test/e2e/suite_test.go b/test/e2e/suite_test.go index 2a5f8c7db..5ae4cfd44 100644 --- a/test/e2e/suite_test.go +++ b/test/e2e/suite_test.go @@ -109,7 +109,7 @@ var _ = ginkgo.SynchronizedBeforeSuite(func(ctx ginkgo.SpecContext) []byte { } execTestCmd([]testCmd{e2eBootstrap, createMetricsSVC}) return nil -}, func(ctx ginkgo.SpecContext, data []byte) { +}, func(_ ginkgo.SpecContext, _ []byte) { // k8s.io/kubernetes/test/e2e/framework requires env KUBECONFIG to be set // it does not fall back to defaults if os.Getenv(kubeconfigEnvVar) == "" { @@ -140,7 +140,7 @@ var _ = ginkgo.SynchronizedBeforeSuite(func(ctx ginkgo.SpecContext) []byte { }() }) -var _ = ginkgo.SynchronizedAfterSuite(func(ctx ginkgo.SpecContext) {}, +var _ = ginkgo.SynchronizedAfterSuite(func(_ ginkgo.SpecContext) {}, func(ctx ginkgo.SpecContext) { blobLog := testCmd{ command: "bash", @@ -188,7 +188,7 @@ func execTestCmd(cmds []testCmd) { cmdSh.Stderr = os.Stderr err := cmdSh.Run() if err != nil { - log.Printf("Failed to run command: %s %s, Error: %s\n", cmd.command, strings.Join(cmd.args, " "), err.Error()) + log.Printf("Failed to run command: %s %s, Error: %v\n", cmd.command, strings.Join(cmd.args, " "), err) } gomega.Expect(err).NotTo(gomega.HaveOccurred()) log.Println(cmd.endLog)