From bc0d1c07e2d3e92e39235415718414ad2c03f5db Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 12:57:40 -0400 Subject: [PATCH 01/24] Enable default golangci linters --- .golangci.yml | 366 ------------------------ pkg/cloud/devicemanager/manager_test.go | 4 - pkg/driver/node_test.go | 4 - pkg/mounter/mount_unsupported.go | 29 +- 4 files changed, 13 insertions(+), 390 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 16d3e83ba4..c1a55bb08d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -12,371 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -# This file contains all available configuration options -# with their default values. - -# options for analysis running run: - # default concurrency is a available CPU number - concurrency: 4 - - # timeout for analysis, e.g. 30s, 5m, default is 1m - timeout: 1m - - # exit code when at least one issue was found, default is 1 - issues-exit-code: 1 - - # include test files or not, default is true - tests: true - - # list of build tags, all linters use it. Default is empty list. - build-tags: - - mytag - - # which dirs to skip: issues from them won't be reported; - # can use regexp here: generated.*, regexp is applied on full path; - # default value is empty list, but default dirs are skipped independently - # from this option's value (see skip-dirs-use-default). - # "/" will be replaced by current OS file path separator to properly work - # on Windows. - skip-dirs: - - src/external_libs - - autogenerated_by_my_lib - - # default is true. Enables skipping of directories: - # vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ - skip-dirs-use-default: true - - # which files to skip: they will be analyzed, but issues from them - # won't be reported. Default value is empty list, but there is - # no need to include all autogenerated files, we confidently recognize - # autogenerated files. If it's not please let us know. - # "/" will be replaced by current OS file path separator to properly work - # on Windows. - skip-files: - - ".*\\.my\\.go$" - - lib/bad.go - - # by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules": - # If invoked with -mod=readonly, the go command is disallowed from the implicit - # automatic updating of go.mod described above. Instead, it fails when any changes - # to go.mod are needed. This setting is most useful to check that go.mod does - # not need updates, such as in a continuous integration and testing system. - # If invoked with -mod=vendor, the go command assumes that the vendor - # directory holds the correct copies of dependencies and ignores - # the dependency descriptions in go.mod. modules-download-mode: readonly - - # Allow multiple parallel golangci-lint instances running. - # If false (default) - golangci-lint acquires file lock on start. - allow-parallel-runners: false - - -# output configuration options -output: - # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" - format: colored-line-number - - # print lines of code with issue, default is true - print-issued-lines: true - - # print linter name in the end of issue text, default is true - print-linter-name: true - - # make issues output unique by line, default is true - uniq-by-line: true - - # add a prefix to the output file references; default is no prefix - path-prefix: "" - - -# all available settings of specific linters -linters-settings: - dogsled: - # checks assignments with too many blank identifiers; default is 2 - max-blank-identifiers: 2 - dupl: - # tokens count to trigger issue, 150 by default - threshold: 100 - errcheck: - # report about not checking of errors in type assertions: `a := b.(MyStruct)`; - # default is false: such cases aren't reported by default. - check-type-assertions: false - - # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`; - # default is false: such cases aren't reported by default. - check-blank: false - - # [deprecated] comma-separated list of pairs of the form pkg:regex - # the regex is used to ignore names within pkg. (default "fmt:.*"). - # see https://github.com/kisielk/errcheck#the-deprecated-method for details - ignore: fmt:.*,io/ioutil:^Read.* - - # path to a file containing a list of functions to exclude from checking - # see https://github.com/kisielk/errcheck#excluding-functions for details - #exclude: - exhaustive: - # indicates that switch statements are to be considered exhaustive if a - # 'default' case is present, even if all enum members aren't listed in the - # switch - default-signifies-exhaustive: false - funlen: - lines: 60 - statements: 40 - gocognit: - # minimal code complexity to report, 30 by default (but we recommend 10-20) - min-complexity: 10 - nestif: - # minimal complexity of if statements to report, 5 by default - min-complexity: 4 - goconst: - # minimal length of string constant, 3 by default - min-len: 3 - # minimal occurrences count to trigger, 3 by default - min-occurrences: 3 - gocritic: - # Which checks should be enabled; can't be combined with 'disabled-checks'; - # See https://go-critic.github.io/overview#checks-overview - # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run` - # By default list of stable checks is used. - #enabled-checks: - # - rangeValCopy - - # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty - disabled-checks: - - regexpMust - - # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks. - # Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags". - enabled-tags: - - performance - disabled-tags: - - experimental - - settings: # settings passed to gocritic - captLocal: # must be valid enabled check name - paramsOnly: true - # rangeValCopy: - # sizeThreshold: 32 - gocyclo: - # minimal code complexity to report, 30 by default (but we recommend 10-20) - min-complexity: 10 - godot: - # check all top-level comments, not only declarations - check-all: false - godox: - # report any comments starting with keywords, this is useful for TODO or FIXME comments that - # might be left in the code accidentally and should be resolved before merging - keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting - - NOTE - - OPTIMIZE # marks code that should be optimized before merging - - HACK # marks hack-arounds that should be removed before merging - gofmt: - # simplify code: gofmt with `-s` option, true by default - simplify: true - goheader: - values: - const: - # define here const type values in format k:v, for example: - # YEAR: 2020 - # COMPANY: MY COMPANY - regexp: - # define here regexp type values, for example - # AUTHOR: .*@mycompany\.com - template: - # put here copyright header template for source code files, for example: - # {{ AUTHOR }} {{ COMPANY }} {{ YEAR }} - # SPDX-License-Identifier: Apache-2.0 - # - # Licensed under the Apache License, Version 2.0 (the "License"); - # you may not use this file except in compliance with the License. - # You may obtain a copy of the License at: - # - # http://www.apache.org/licenses/LICENSE-2.0 - # - # Unless required by applicable law or agreed to in writing, software - # distributed under the License is distributed on an "AS IS" BASIS, - # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - # See the License for the specific language governing permissions and - # limitations under the License. - template-path: - # also as alternative of directive 'template' you may put the path to file with the template source - goimports: - # put imports beginning with prefix after 3rd-party packages; - # it's a comma-separated list of prefixes - local-prefixes: github.com/org/project - golint: - # minimal confidence for issues, default is 0.8 - min-confidence: 0.8 - gomnd: - settings: - mnd: - # the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description. - checks: argument,case,condition,operation,return,assign - gomodguard: - allowed: - modules: # List of allowed modules - # - gopkg.in/yaml.v2 - domains: # List of allowed module domains - # - golang.org - blocked: - modules: # List of blocked modules - # - github.com/uudashr/go-module: # Blocked module - # recommendations: # Recommended modules that should be used instead (Optional) - # - golang.org/x/mod - # reason: "`mod` is the official go.mod parser library." # Reason why the recommended module should be used (Optional) - versions: # List of blocked module version constraints - # - github.com/mitchellh/go-homedir: # Blocked module with version constraint - # version: "< 1.1.0" # Version constraint, see https://github.com/Masterminds/semver#basic-comparisons - # reason: "testing if blocked version constraint works." # Reason why the version constraint exists. (Optional) - govet: - # report about shadowed variables - check-shadowing: true - - # settings per analyzer - settings: - printf: # analyzer name, run `go tool vet help` to see all analyzers - funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf - - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf - - # enable or disable analyzers by name - enable: - - atomicalign - enable-all: false - disable: - - shadow - disable-all: false - depguard: - list-type: blacklist - include-go-root: false - packages: - - github.com/sirupsen/logrus - packages-with-error-message: - # specify an error message to output when a blacklisted package is used - - github.com/sirupsen/logrus: "logging is allowed only by logutils.Log" - lll: - # max line length, lines longer will be reported. Default is 120. - # '\t' is counted as 1 character by default, and can be changed with the tab-width option - line-length: 120 - # tab width in spaces. Default to 1. - tab-width: 1 - maligned: - # print struct with more effective memory layout or not, false by default - suggest-new: true - misspell: - # Correct spellings using locale preferences for US or UK. - # Default is to use a neutral variety of English. - # Setting locale to US will correct the British spelling of 'colour' to 'color'. - locale: US - ignore-words: - - someword - nakedret: - # make an issue if func has more lines of code than this setting and it has naked returns; default is 30 - max-func-lines: 30 - prealloc: - # XXX: we don't recommend using this linter before doing performance profiling. - # For most programs usage of prealloc will be a premature optimization. - - # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. - # True by default. - simple: true - range-loops: true # Report preallocation suggestions on range loops, true by default - for-loops: false # Report preallocation suggestions on for loops, false by default - nolintlint: - # Enable to ensure that nolint directives are all used. Default is true. - allow-unused: false - # Disable to ensure that nolint directives don't have a leading space. Default is true. - allow-leading-space: true - # Exclude following linters from requiring an explanation. Default is []. - allow-no-explanation: [] - # Enable to require an explanation of nonzero length after each nolint directive. Default is false. - require-explanation: true - # Enable to require nolint directives to mention the specific linter being suppressed. Default is false. - require-specific: true - rowserrcheck: - packages: - - github.com/jmoiron/sqlx - testpackage: - # regexp pattern to skip files - skip-regexp: (export|internal)_test\.go - unparam: - # Inspect exported functions, default is false. Set to true if no external program/library imports your code. - # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: - # if it's called for subdir of a project it can't find external interfaces. All text editor integrations - # with golangci-lint call it on a directory with the changed file. - check-exported: false - unused: - # treat code as a program (not a library) and report unused exported identifiers; default is false. - # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: - # if it's called for subdir of a project it can't find funcs usages. All text editor integrations - # with golangci-lint call it on a directory with the changed file. - check-exported: false - whitespace: - multi-if: false # Enforces newlines (or comments) after every multi-line if statement - multi-func: false # Enforces newlines (or comments) after every multi-line function signature - wsl: - # If true append is only allowed to be cuddled if appending value is - # matching variables, fields or types on line above. Default is true. - strict-append: true - # Allow calls and assignments to be cuddled as long as the lines have any - # matching variables, fields or types. Default is true. - allow-assign-and-call: true - # Allow multiline assignments to be cuddled. Default is true. - allow-multiline-assign: true - # Allow declarations (var) to be cuddled. - allow-cuddle-declarations: false - # Allow trailing comments in ending of blocks - allow-trailing-comment: false - # Force newlines in end of case at this limit (0 = never). - force-case-trailing-whitespace: 0 - # Force cuddling of err checks with err var assignment - force-err-cuddling: false - # Allow leading comments to be separated with empty liens - allow-separated-leading-comment: false - gofumpt: - # Choose whether or not to use the extra rules that are disabled - # by default - extra-rules: false - -linters: - enable: - - megacheck - - govet - disable: - - maligned - - prealloc - - scopelint - - gosec - disable-all: false - presets: - - bugs - - unused - fast: false - -severity: - # Default value is empty string. - # Set the default severity for issues. If severity rules are defined and the issues - # do not match or no severity is provided to the rule this will be the default - # severity applied. Severities should match the supported severity names of the - # selected out format. - # - Code climate: https://docs.codeclimate.com/docs/issues#issue-severity - # - Checkstyle: https://checkstyle.sourceforge.io/property_types.html#severity - # - Github: https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message - default-severity: error - - # The default value is false. - # If set to true severity-rules regular expressions become case sensitive. - case-sensitive: false - - # Default value is empty list. - # When a list of severity rules are provided, severity information will be added to lint - # issues. Severity rules have the same filtering capability as exclude rules except you - # are allowed to specify one matcher per severity rule. - # Only affects out formats that support setting severity information. - rules: - - linters: - - dupl - severity: info diff --git a/pkg/cloud/devicemanager/manager_test.go b/pkg/cloud/devicemanager/manager_test.go index 88b16e24d0..1504d319a5 100644 --- a/pkg/cloud/devicemanager/manager_test.go +++ b/pkg/cloud/devicemanager/manager_test.go @@ -243,10 +243,6 @@ func assertDevice(t *testing.T, d *Device, assigned bool, err error) { t.Fatalf("Expected no error, got %v", err) } - if d == nil { - t.Fatalf("Expected valid device, got nil") - } - if d.IsAlreadyAssigned != assigned { t.Fatalf("Expected IsAlreadyAssigned to be %v, got %v", assigned, d.IsAlreadyAssigned) } diff --git a/pkg/driver/node_test.go b/pkg/driver/node_test.go index a4da70b3fd..2d77b37eab 100644 --- a/pkg/driver/node_test.go +++ b/pkg/driver/node_test.go @@ -57,10 +57,6 @@ func TestNewNodeService(t *testing.T) { nodeService := NewNodeService(options, mockMetadataService, mockMounter, mockKubernetesClient) - if nodeService == nil { - t.Fatal("Expected NewNodeService to return a non-nil NodeService") - } - if nodeService.metadata != mockMetadataService { t.Error("Expected NodeService.metadata to be set to the mock MetadataService") } diff --git a/pkg/mounter/mount_unsupported.go b/pkg/mounter/mount_unsupported.go index 394f72df2d..79fbaceb85 100644 --- a/pkg/mounter/mount_unsupported.go +++ b/pkg/mounter/mount_unsupported.go @@ -20,6 +20,7 @@ limitations under the License. package mounter import ( + "errors" "fmt" mountutils "k8s.io/mount-utils" ) @@ -42,27 +43,23 @@ func NewSafeMounterV2() (*mountutils.SafeFormatAndMount, error) { } func (m *NodeMounter) FindDevicePath(devicePath, volumeID, partition, region string) (string, error) { - return stubMessage, fmt.Errorf(stubMessage) + return stubMessage, errors.New(stubMessage) } func (m *NodeMounter) PreparePublishTarget(target string) error { - return fmt.Errorf(stubMessage) + return errors.New(stubMessage) } func (m *NodeMounter) IsBlockDevice(fullPath string) (bool, error) { - return false, fmt.Errorf(stubMessage) + return false, errors.New(stubMessage) } func (m *NodeMounter) GetBlockSizeBytes(devicePath string) (int64, error) { - return 1, fmt.Errorf(stubMessage) -} - -func (m *NodeMounter) appendPartition(devicePath, partition string) string { - return stubMessage + return 1, errors.New(stubMessage) } func (m NodeMounter) GetDeviceNameFromMount(mountPath string) (string, int, error) { - return stubMessage, 0, fmt.Errorf(stubMessage) + return stubMessage, 0, errors.New(stubMessage) } func (m NodeMounter) IsCorruptedMnt(err error) bool { @@ -70,29 +67,29 @@ func (m NodeMounter) IsCorruptedMnt(err error) bool { } func (m *NodeMounter) MakeFile(path string) error { - return fmt.Errorf(stubMessage) + return errors.New(stubMessage) } func (m *NodeMounter) MakeDir(path string) error { - return fmt.Errorf(stubMessage) + return errors.New(stubMessage) } func (m *NodeMounter) PathExists(path string) (bool, error) { - return false, fmt.Errorf(stubMessage) + return false, errors.New(stubMessage) } func (m *NodeMounter) Resize(devicePath, deviceMountPath string) (bool, error) { - return false, fmt.Errorf(stubMessage) + return false, errors.New(stubMessage) } func (m *NodeMounter) NeedResize(devicePath string, deviceMountPath string) (bool, error) { - return false, fmt.Errorf(stubMessage) + return false, errors.New(stubMessage) } func (m *NodeMounter) Unpublish(path string) error { - return fmt.Errorf(stubMessage) + return errors.New(stubMessage) } func (m *NodeMounter) Unstage(path string) error { - return fmt.Errorf(stubMessage) + return errors.New(stubMessage) } From a8e784822599449d575972e27d8412b6b0116d84 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:10:18 -0400 Subject: [PATCH 02/24] Enable whitespace linter --- .golangci.yml | 12 ++++++++++++ cmd/hooks/prestop_test.go | 6 ------ pkg/cloud/cloud_test.go | 2 +- pkg/driver/controller.go | 2 -- pkg/driver/controller_test.go | 2 -- pkg/driver/internal/inflight_test.go | 2 -- pkg/driver/node.go | 2 -- pkg/util/template/template_test.go | 2 -- .../dynamically_provisioned_collocated_pod_tester.go | 1 - .../pre_provisioned_snapshot_volume_tester.go | 1 - tests/e2e/testsuites/testsuites.go | 2 -- 11 files changed, 13 insertions(+), 21 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index c1a55bb08d..d157ee6a44 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -14,3 +14,15 @@ run: modules-download-mode: readonly +linters: + enable: + - asasalint + - asciicheck + - bidichk + - bodyclose + - canonicalheader + - containedctx + - contextcheck + - whitespace + disable: + - govet # We already run with `make verify/govet` diff --git a/cmd/hooks/prestop_test.go b/cmd/hooks/prestop_test.go index 9b72ee1b26..cb8aa17962 100644 --- a/cmd/hooks/prestop_test.go +++ b/cmd/hooks/prestop_test.go @@ -77,7 +77,6 @@ func TestPreStopHook(t *testing.T) { nodeName: "test-node", expErr: nil, mockFunc: func(nodeName string, mockClient *driver.MockKubernetesClient, mockCoreV1 *driver.MockCoreV1Interface, mockNode *driver.MockNodeInterface, mockVolumeAttachments *driver.MockVolumeAttachmentInterface, mockStorageV1Interface *driver.MockStorageV1Interface) error { - fakeNode := &v1.Node{ Spec: v1.NodeSpec{ Taints: []v1.Taint{ @@ -109,7 +108,6 @@ func TestPreStopHook(t *testing.T) { nodeName: "test-node", expErr: nil, mockFunc: func(nodeName string, mockClient *driver.MockKubernetesClient, mockCoreV1 *driver.MockCoreV1Interface, mockNode *driver.MockNodeInterface, mockVolumeAttachments *driver.MockVolumeAttachmentInterface, mockStorageV1Interface *driver.MockStorageV1Interface) error { - fakeNode := &v1.Node{ Spec: v1.NodeSpec{ Taints: []v1.Taint{ @@ -149,7 +147,6 @@ func TestPreStopHook(t *testing.T) { nodeName: "test-node", expErr: nil, mockFunc: func(nodeName string, mockClient *driver.MockKubernetesClient, mockCoreV1 *driver.MockCoreV1Interface, mockNode *driver.MockNodeInterface, mockVolumeAttachments *driver.MockVolumeAttachmentInterface, mockStorageV1Interface *driver.MockStorageV1Interface) error { - fakeNode := &v1.Node{ Spec: v1.NodeSpec{ Taints: []v1.Taint{ @@ -206,7 +203,6 @@ func TestPreStopHook(t *testing.T) { nodeName: "test-karpenter-node", expErr: nil, mockFunc: func(nodeName string, mockClient *driver.MockKubernetesClient, mockCoreV1 *driver.MockCoreV1Interface, mockNode *driver.MockNodeInterface, mockVolumeAttachments *driver.MockVolumeAttachmentInterface, mockStorageV1Interface *driver.MockStorageV1Interface) error { - fakeNode := &v1.Node{ Spec: v1.NodeSpec{ Taints: []v1.Taint{ @@ -238,7 +234,6 @@ func TestPreStopHook(t *testing.T) { nodeName: "test-karpenter-node", expErr: nil, mockFunc: func(nodeName string, mockClient *driver.MockKubernetesClient, mockCoreV1 *driver.MockCoreV1Interface, mockNode *driver.MockNodeInterface, mockVolumeAttachments *driver.MockVolumeAttachmentInterface, mockStorageV1Interface *driver.MockStorageV1Interface) error { - fakeNode := &v1.Node{ Spec: v1.NodeSpec{ Taints: []v1.Taint{ @@ -278,7 +273,6 @@ func TestPreStopHook(t *testing.T) { nodeName: "test-karpenter-node", expErr: nil, mockFunc: func(nodeName string, mockClient *driver.MockKubernetesClient, mockCoreV1 *driver.MockCoreV1Interface, mockNode *driver.MockNodeInterface, mockVolumeAttachments *driver.MockVolumeAttachmentInterface, mockStorageV1Interface *driver.MockStorageV1Interface) error { - fakeNode := &v1.Node{ Spec: v1.NodeSpec{ Taints: []v1.Taint{ diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index 84100c8562..1ee7b2285f 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -82,8 +82,8 @@ func extractVolumeIdentifiers(volumes []types.Volume) (volumeIDs []string, volum } return volumeIDs, volumeNames } -func TestNewCloud(t *testing.T) { +func TestNewCloud(t *testing.T) { testCases := []struct { name string region string diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index 5af42fba46..9aa0b0c90a 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -975,7 +975,6 @@ func newCreateSnapshotResponse(snapshot *cloud.Snapshot) *csi.CreateSnapshotResp } func newListSnapshotsResponse(cloudResponse *cloud.ListSnapshotsResponse) *csi.ListSnapshotsResponse { - var entries []*csi.ListSnapshotsResponse_Entry for _, snapshot := range cloudResponse.Snapshots { snapshotResponseEntry := newListSnapshotsResponseEntry(snapshot) @@ -1018,7 +1017,6 @@ func getVolSizeBytes(req *csi.CreateVolumeRequest) (int64, error) { // BuildOutpostArn returns the string representation of the outpost ARN from the given csi.TopologyRequirement.segments func BuildOutpostArn(segments map[string]string) string { - if len(segments[AwsPartitionKey]) <= 0 { return "" } diff --git a/pkg/driver/controller_test.go b/pkg/driver/controller_test.go index 63fc630c1a..3918040f1b 100644 --- a/pkg/driver/controller_test.go +++ b/pkg/driver/controller_test.go @@ -2487,7 +2487,6 @@ func TestCreateSnapshot(t *testing.T) { } _, err := awsDriver.CreateSnapshot(context.Background(), req) checkExpectedErrorCode(t, err, codes.InvalidArgument) - }, }, { @@ -3519,7 +3518,6 @@ func TestControllerUnpublishVolume(t *testing.T) { errorCode: codes.OK, mockDetach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { mockCloud.EXPECT().DetachDisk(gomock.Eq(ctx), volumeId, nodeId).Return(nil) - }, expResp: &csi.ControllerUnpublishVolumeResponse{}, }, diff --git a/pkg/driver/internal/inflight_test.go b/pkg/driver/internal/inflight_test.go index 4373c59da0..eaaeb9d6ed 100644 --- a/pkg/driver/internal/inflight_test.go +++ b/pkg/driver/internal/inflight_test.go @@ -68,7 +68,6 @@ func TestInFlight(t *testing.T) { }, }, }, - { name: "success add, delete, add copy", requests: []testRequest{ @@ -107,6 +106,5 @@ func TestInFlight(t *testing.T) { } } }) - } } diff --git a/pkg/driver/node.go b/pkg/driver/node.go index 750fa6eee8..bcb9cb4124 100644 --- a/pkg/driver/node.go +++ b/pkg/driver/node.go @@ -555,7 +555,6 @@ func (d *NodeService) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVo }, }, }, nil - } func (d *NodeService) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) { @@ -765,7 +764,6 @@ func (d *NodeService) nodePublishVolumeForFileSystem(req *csi.NodePublishVolumeR // getVolumesLimit returns the limit of volumes that the node supports func (d *NodeService) getVolumesLimit() int64 { - if d.options.VolumeAttachLimit >= 0 { return d.options.VolumeAttachLimit } diff --git a/pkg/util/template/template_test.go b/pkg/util/template/template_test.go index a292e9a685..d866acb413 100644 --- a/pkg/util/template/template_test.go +++ b/pkg/util/template/template_test.go @@ -202,7 +202,6 @@ func TestEvaluate(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - props := &PVProps{ PVCName: tc.pvcName, PVCNamespace: tc.pvcNamespace, @@ -343,7 +342,6 @@ func TestEvaluateVolumeSnapshotTemplate(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - props := &VolumeSnapshotProps{ VolumeSnapshotName: tc.volumeSnapshotName, VolumeSnapshotNamespace: tc.volumeSnapshotNamespace, diff --git a/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go b/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go index d8e5b77f2b..d85a2e0b53 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go @@ -52,5 +52,4 @@ func (t *DynamicallyProvisionedCollocatedPodTest) Run(client clientset.Interface tpod.WaitForRunning() nodeName = tpod.pod.Spec.NodeName } - } diff --git a/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go b/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go index 81f10cd738..5dfa6dc6eb 100644 --- a/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go @@ -33,7 +33,6 @@ type PreProvisionedVolumeSnapshotTest struct { } func (t *PreProvisionedVolumeSnapshotTest) Run(client clientset.Interface, restclient k8srestclient.Interface, namespace *v1.Namespace, snapshotId string) { - By("taking snapshots") tvsc, cleanup := CreateVolumeSnapshotClass(restclient, namespace, t.CSIDriver, nil) defer cleanup() diff --git a/tests/e2e/testsuites/testsuites.go b/tests/e2e/testsuites/testsuites.go index d4ac74feca..ee87045c64 100644 --- a/tests/e2e/testsuites/testsuites.go +++ b/tests/e2e/testsuites/testsuites.go @@ -185,7 +185,6 @@ func (t *TestVolumeSnapshotClass) UpdateStaticVolumeSnapshotContent(volumeSnapsh volumeSnapshotContent.Spec.VolumeSnapshotRef.Name = volumeSnapshot.Name _, err := snapshotclientset.New(t.client).SnapshotV1().VolumeSnapshotContents().Update(context.Background(), volumeSnapshotContent, metav1.UpdateOptions{}) framework.ExpectNoError(err) - } func (t *TestVolumeSnapshotClass) ReadyToUse(snapshot *volumesnapshotv1.VolumeSnapshot) { By("waiting for VolumeSnapshot to be ready to use - " + snapshot.Name) @@ -389,7 +388,6 @@ func (t *TestPersistentVolumeClaim) ValidateProvisionedPersistentVolume() { for _, v := range t.persistentVolume.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions[0].Values { Expect(t.storageClass.AllowedTopologies[0].MatchLabelExpressions[0].Values).To(ContainElement(v)) } - } } } From 3d3840b561b59c19b2c68e84bbbd9bee1edf6fc7 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:22:38 -0400 Subject: [PATCH 03/24] cleanup: Delete copying of loop variables (Go 1.22+) --- .golangci.yml | 1 + pkg/batcher/batcher_test.go | 1 - pkg/cloud/cloud.go | 2 -- pkg/cloud/cloud_test.go | 5 ----- pkg/driver/request_coalescing_test.go | 1 - tests/e2e/format_options.go | 1 - tests/e2e/modify_volume.go | 1 - 7 files changed, 1 insertion(+), 11 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d157ee6a44..b54dfe3349 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -23,6 +23,7 @@ linters: - canonicalheader - containedctx - contextcheck + - copyloopvar - whitespace disable: - govet # We already run with `make verify/govet` diff --git a/pkg/batcher/batcher_test.go b/pkg/batcher/batcher_test.go index 8b1b9af9bd..72319fdc3f 100644 --- a/pkg/batcher/batcher_test.go +++ b/pkg/batcher/batcher_test.go @@ -127,7 +127,6 @@ func TestBatcher(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 3b8d93c6ef..d26430ea37 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -1057,7 +1057,6 @@ func (c *cloud) WaitForAttachmentState(ctx context.Context, volumeID, expectedSt attachmentState := "" for _, a := range volume.Attachments { - a := a if a.InstanceId != nil { if aws.ToString(a.InstanceId) == expectedInstance { attachmentState = string(a.State) @@ -1202,7 +1201,6 @@ func execBatchDescribeSnapshots(svc EC2API, input []string, batcher snapshotBatc result := make(map[string]*types.Snapshot) for _, snapshot := range resp { - snapshot := snapshot key, err := extractSnapshotKey(&snapshot, batcher) if err != nil { klog.Warningf("execBatchDescribeSnapshots: skipping snapshot: %v, reason: %v", snapshot, err) diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index 1ee7b2285f..cba3a2a060 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -217,7 +217,6 @@ func TestBatchDescribeVolumes(t *testing.T) { } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() mockCtrl := gomock.NewController(t) @@ -334,7 +333,6 @@ func TestBatchDescribeInstances(t *testing.T) { } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() mockCtrl := gomock.NewController(t) @@ -509,7 +507,6 @@ func TestBatchDescribeSnapshots(t *testing.T) { } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() mockCtrl := gomock.NewController(t) @@ -723,7 +720,6 @@ func TestBatchDescribeVolumesModifications(t *testing.T) { } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() mockCtrl := gomock.NewController(t) @@ -1349,7 +1345,6 @@ func TestCreateDisk(t *testing.T) { }, } for _, tc := range testCases { - tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() mockCtrl := gomock.NewController(t) diff --git a/pkg/driver/request_coalescing_test.go b/pkg/driver/request_coalescing_test.go index 8e700b7354..6b41aca5f0 100644 --- a/pkg/driver/request_coalescing_test.go +++ b/pkg/driver/request_coalescing_test.go @@ -84,7 +84,6 @@ func TestVolumeModificationWithCoalescing(t *testing.T) { } for _, tc := range testCases { - tc := tc // Not strictly necessary but required by `go vet` t.Run(tc.name+": volume-modifier-for-k8s", func(t *testing.T) { t.Parallel() tc.testFunction(t, modifierForK8sModifyVolume) diff --git a/tests/e2e/format_options.go b/tests/e2e/format_options.go index 440cac8b04..c8170a525f 100644 --- a/tests/e2e/format_options.go +++ b/tests/e2e/format_options.go @@ -90,7 +90,6 @@ var _ = Describe("[ebs-csi-e2e] [single-az] [format-options] Formatting a volume Context(fmt.Sprintf("using an %s filesystem", fsType), func() { for testedParameter, formatOptionTestCase := range formatOptionTests { - formatOptionTestCase := formatOptionTestCase if fsTypeDoesNotSupportFormatOptionParameter(fsType, testedParameter) { continue } diff --git a/tests/e2e/modify_volume.go b/tests/e2e/modify_volume.go index 4083319ad1..b3fb0ec3b8 100644 --- a/tests/e2e/modify_volume.go +++ b/tests/e2e/modify_volume.go @@ -143,7 +143,6 @@ var _ = Describe("[ebs-csi-e2e] [single-az] [modify-volume] Modifying a PVC", fu }) for testName, modifyVolumeTest := range modifyVolumeTests { - modifyVolumeTest := modifyVolumeTest Context(testName, func() { It("will modify associated PV and EBS Volume via volume-modifier-for-k8s", func() { if modifyVolumeTest.ExternalResizerOnly { From 3d3f9a007ac13bf706508e44b4c4b35675ab1969 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:27:17 -0400 Subject: [PATCH 04/24] Enable dupword linter --- .golangci.yml | 7 +++++++ pkg/cloud/cloud_test.go | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index b54dfe3349..2389a9eddd 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -24,6 +24,13 @@ linters: - containedctx - contextcheck - copyloopvar + - decorder + - dogsled + - dupword + - durationcheck - whitespace disable: - govet # We already run with `make verify/govet` + - depguard # We don't use this + - cyclop # TODO FIXME (at least for non-tests) + - dupl # TODO FIXME (tests + non-tests) diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index cba3a2a060..a8f5f681b2 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -2534,7 +2534,7 @@ func TestResizeOrModifyDisk(t *testing.T) { expErr: fmt.Errorf("InvalidParameterCombination: The parameter iops is not supported for gp2 volumes"), }, { - name: "failure: returned returned ErrInvalidArgument when ModifyVolume returned UnknownVolumeType", + name: "failure: returned ErrInvalidArgument when ModifyVolume returned UnknownVolumeType", volumeID: "vol-test", modifyDiskOptions: &ModifyDiskOptions{ VolumeType: "GPFake", From e239fd8c2f5d9ee341b41b51525fd76e670c3a35 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:33:34 -0400 Subject: [PATCH 05/24] cleanup: Ensure all variable names conform to 'ErrXxx' format --- .golangci.yml | 24 +++++++++++++++++++++++- pkg/cloud/cloud.go | 14 +++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 2389a9eddd..defe2da999 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -28,9 +28,31 @@ linters: - dogsled - dupword - durationcheck + - errcheck + - errchkjson + - errname + - errorlint + - exhaustive - whitespace disable: - govet # We already run with `make verify/govet` - - depguard # We don't use this + - depguard # We don't use this today - cyclop # TODO FIXME (at least for non-tests) - dupl # TODO FIXME (tests + non-tests) + - err113 # TODO FIXME + - wsl # Doesn't fit with existing code style + - wrapcheck # Many false positives + - varnamelen # Many false positives + - mnd # Many false positives + - gomnd # Many false positives + - testpackage # We don't do this + - paralleltest # TODO FIXME + - gomoddirectives # We need `replace` in `go.mod` + - funlen # Many false positives + - gochecknoglobals # TODO FIXME + - interfacebloat # False positives on AWS SDK interfaces + - exhaustruct # TODO FIXME + - lll # TODO FIXME + - nestif # Many false positives + - gocognit # Many false positives + - cyclop # Many false positives diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index d26430ea37..c4391cd201 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -168,8 +168,8 @@ var ( // ErrInvalidMaxResults is returned when a MaxResults pagination parameter is between 1 and 4 ErrInvalidMaxResults = errors.New("MaxResults parameter must be 0 or greater than or equal to 5") - // VolumeNotBeingModified is returned if volume being described is not being modified - VolumeNotBeingModified = fmt.Errorf("volume is not being modified") + // ErrVolumeNotBeingModified is returned if volume being described is not being modified + ErrVolumeNotBeingModified = fmt.Errorf("volume is not being modified") // ErrInvalidArgument is returned if parameters were rejected by cloud provider ErrInvalidArgument = errors.New("invalid argument") @@ -1718,7 +1718,7 @@ func (c *cloud) waitForVolumeModification(ctx context.Context, volumeID string) waitErr := wait.ExponentialBackoff(c.vwp.modificationBackoff, func() (bool, error) { m, err := c.getLatestVolumeModification(ctx, volumeID, true) // Consider volumes that have never been modified as done - if err != nil && errors.Is(err, VolumeNotBeingModified) { + if err != nil && errors.Is(err, ErrVolumeNotBeingModified) { return true, nil } else if err != nil { return false, err @@ -1746,7 +1746,7 @@ func describeVolumesModifications(ctx context.Context, svc EC2API, request *ec2. response, err := svc.DescribeVolumesModifications(ctx, request) if err != nil { if isAWSErrorModificationNotFound(err) { - return nil, VolumeNotBeingModified + return nil, ErrVolumeNotBeingModified } return nil, fmt.Errorf("error describing volume modifications: %w", err) } @@ -1774,14 +1774,14 @@ func (c *cloud) getLatestVolumeModification(ctx context.Context, volumeID string }) if err != nil { if isAWSErrorModificationNotFound(err) { - return nil, VolumeNotBeingModified + return nil, ErrVolumeNotBeingModified } return nil, fmt.Errorf("error describing modifications in volume %q: %w", volumeID, err) } volumeMods := mod.VolumesModifications if len(volumeMods) == 0 { - return nil, VolumeNotBeingModified + return nil, ErrVolumeNotBeingModified } return &volumeMods[len(volumeMods)-1], nil @@ -1860,7 +1860,7 @@ func (c *cloud) validateModifyVolume(ctx context.Context, volumeID string, newSi // This call must NOT be batched because a missing volume modification will return client error latestMod, err := c.getLatestVolumeModification(ctx, volumeID, false) - if err != nil && !errors.Is(err, VolumeNotBeingModified) { + if err != nil && !errors.Is(err, ErrVolumeNotBeingModified) { return true, oldSizeGiB, fmt.Errorf("error fetching volume modifications for %q: %w", volumeID, err) } From 1dd1c11c5ab6a322b773da6f023d8bfcd35812cd Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:36:57 -0400 Subject: [PATCH 06/24] cleanup: Do not log with fmt.Println (forbidigo) --- .golangci.yml | 2 ++ cmd/main.go | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index defe2da999..806b2b7574 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -33,6 +33,8 @@ linters: - errname - errorlint - exhaustive + - fatcontext + - forbidigo - whitespace disable: - govet # We already run with `make verify/govet` diff --git a/cmd/main.go b/cmd/main.go index 1428a69dc1..adfa4293fa 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -18,7 +18,6 @@ package main import ( "context" - "fmt" "os" "strings" "time" @@ -108,7 +107,7 @@ func main() { klog.ErrorS(err, "failed to get version") klog.FlushAndExit(klog.ExitFlushTimeout, 1) } - fmt.Println(versionInfo) + klog.Info(versionInfo) os.Exit(0) } From 9dd6e71d572f895496799074836681c037539b79 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:11:22 -0400 Subject: [PATCH 07/24] Enable forcetypeassert linter --- .golangci.yml | 1 + cmd/hooks/prestop.go | 10 ++++++++-- pkg/cloud/cloud_test.go | 38 ++++++++++++++++++++++++++++++-------- pkg/driver/node_test.go | 1 + pkg/metrics/metrics.go | 14 ++++++++++++-- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 806b2b7574..02af859042 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -35,6 +35,7 @@ linters: - exhaustive - fatcontext - forbidigo + - forcetypeassert - whitespace disable: - govet # We already run with `make verify/govet` diff --git a/cmd/hooks/prestop.go b/cmd/hooks/prestop.go index 2f84424822..34eee66b05 100644 --- a/cmd/hooks/prestop.go +++ b/cmd/hooks/prestop.go @@ -104,7 +104,10 @@ func waitForVolumeAttachments(clientset kubernetes.Interface, nodeName string) e _, err := informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ DeleteFunc: func(obj interface{}) { klog.V(5).InfoS("DeleteFunc: VolumeAttachment deleted", "node", nodeName) - va := obj.(*storagev1.VolumeAttachment) + va, ok := obj.(*storagev1.VolumeAttachment) + if !ok { + klog.Error("DeleteFunc: error asserting object as type VolumeAttachment", "obj", va) + } if va.Spec.NodeName == nodeName { if err := checkVolumeAttachments(clientset, nodeName, allAttachmentsDeleted); err != nil { klog.ErrorS(err, "DeleteFunc: error checking VolumeAttachments") @@ -113,7 +116,10 @@ func waitForVolumeAttachments(clientset kubernetes.Interface, nodeName string) e }, UpdateFunc: func(oldObj, newObj interface{}) { klog.V(5).InfoS("UpdateFunc: VolumeAttachment updated", "node", nodeName) - va := newObj.(*storagev1.VolumeAttachment) + va, ok := newObj.(*storagev1.VolumeAttachment) + if !ok { + klog.Error("UpdateFunc: error asserting object as type VolumeAttachment", "obj", va) + } if va.Spec.NodeName == nodeName { if err := checkVolumeAttachments(clientset, nodeName, allAttachmentsDeleted); err != nil { klog.ErrorS(err, "UpdateFunc: error checking VolumeAttachments") diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index a8f5f681b2..eefcdf7dc0 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -114,7 +114,10 @@ func TestNewCloud(t *testing.T) { if err != nil { t.Fatalf("error %v", err) } - ec2CloudAscloud := ec2Cloud.(*cloud) + ec2CloudAscloud, ok := ec2Cloud.(*cloud) + if !ok { + t.Fatalf("could not assert object ec2Cloud as cloud type, %v", ec2Cloud) + } assert.Equal(t, ec2CloudAscloud.region, tc.region) if tc.batchingEnabled { assert.NotNil(t, ec2CloudAscloud.bm) @@ -224,7 +227,10 @@ func TestBatchDescribeVolumes(t *testing.T) { mockEC2 := NewMockEC2API(mockCtrl) c := newCloud(mockEC2) - cloudInstance := c.(*cloud) + cloudInstance, ok := c.(*cloud) + if !ok { + t.Fatalf("could not assert cloudInstance as type cloud, %v", cloudInstance) + } cloudInstance.bm = newBatcherManager(cloudInstance.ec2) tc.mockFunc(mockEC2, tc.expErr, tc.volumes) @@ -340,7 +346,10 @@ func TestBatchDescribeInstances(t *testing.T) { mockEC2 := NewMockEC2API(mockCtrl) c := newCloud(mockEC2) - cloudInstance := c.(*cloud) + cloudInstance, ok := c.(*cloud) + if !ok { + t.Fatalf("could not assert cloudInstance as type cloud, %v", cloudInstance) + } cloudInstance.bm = newBatcherManager(cloudInstance.ec2) // Setup mocks @@ -514,7 +523,10 @@ func TestBatchDescribeSnapshots(t *testing.T) { mockEC2 := NewMockEC2API(mockCtrl) c := newCloud(mockEC2) - cloudInstance := c.(*cloud) + cloudInstance, ok := c.(*cloud) + if !ok { + t.Fatalf("could not assert cloudInstance as type cloud, %v", cloudInstance) + } cloudInstance.bm = newBatcherManager(cloudInstance.ec2) tc.mockFunc(mockEC2, tc.expErr, tc.snapshots) @@ -658,7 +670,10 @@ func TestCheckDesiredState(t *testing.T) { defer mockCtrl.Finish() mockEC2 := NewMockEC2API(mockCtrl) c := newCloud(mockEC2) - cloudInstance := c.(*cloud) + cloudInstance, ok := c.(*cloud) + if !ok { + t.Fatalf("could not assert cloudInstance as type cloud, %v", cloudInstance) + } mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(&ec2.DescribeVolumesOutput{ Volumes: []types.Volume{ { @@ -727,7 +742,10 @@ func TestBatchDescribeVolumesModifications(t *testing.T) { mockEC2 := NewMockEC2API(mockCtrl) c := newCloud(mockEC2) - cloudInstance := c.(*cloud) + cloudInstance, ok := c.(*cloud) + if !ok { + t.Fatalf("could not assert cloudInstance as type cloud, %v", cloudInstance) + } cloudInstance.bm = newBatcherManager(cloudInstance.ec2) // Setup mocks @@ -1729,11 +1747,15 @@ func TestAttachDisk(t *testing.T) { mockCtrl := gomock.NewController(t) mockEC2 := NewMockEC2API(mockCtrl) c := newCloud(mockEC2) + cloudInstance, ok := c.(*cloud) + if !ok { + t.Fatalf("could not assert c as type cloud, %v", c) + } ctx := context.Background() - dm := c.(*cloud).dm + deviceManager := cloudInstance.dm - tc.mockFunc(mockEC2, ctx, tc.volumeID, tc.nodeID, tc.nodeID2, tc.path, dm) + tc.mockFunc(mockEC2, ctx, tc.volumeID, tc.nodeID, tc.nodeID2, tc.path, deviceManager) devicePath, err := c.AttachDisk(ctx, tc.volumeID, tc.nodeID) diff --git a/pkg/driver/node_test.go b/pkg/driver/node_test.go index 2d77b37eab..1fe9fb3bee 100644 --- a/pkg/driver/node_test.go +++ b/pkg/driver/node_test.go @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +//nolint:forcetypeassert package driver import ( diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 83fee738a1..85596ccc90 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -65,7 +65,12 @@ func (m *metricRecorder) IncreaseCount(name string, labels map[string]string) { return } - metric.(*metrics.CounterVec).With(metrics.Labels(labels)).Inc() + metricAsCounterVec, ok := metric.(*metrics.CounterVec) + if ok { + metricAsCounterVec.With(labels).Inc() + } else { + klog.V(4).InfoS("Could not assert metric as metrics.CounterVec. Metric increase may have been skipped") + } } // ObserveHistogram records the given value in the histogram metric. @@ -82,7 +87,12 @@ func (m *metricRecorder) ObserveHistogram(name string, value float64, labels map return } - metric.(*metrics.HistogramVec).With(metrics.Labels(labels)).Observe(value) + metricAsHistogramVec, ok := metric.(*metrics.HistogramVec) + if ok { + metricAsHistogramVec.With(labels).Observe(value) + } else { + klog.V(4).InfoS("Could not assert metric as metrics.HistogramVec. Metric observation may have been skipped") + } } // InitializeMetricsHandler starts a new HTTP server to expose the metrics. From 43d3f17882f36fbf1648bde6bfcfd8e3e8625589 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 20:19:58 -0400 Subject: [PATCH 08/24] cleanup: enable gci linter to make package import order deterministic --- .golangci.yml | 78 +++++++++++++++++++ pkg/cloud/cloud_test.go | 5 +- pkg/driver/request_coalescing_test.go | 6 +- pkg/mounter/mount_unsupported.go | 1 + tests/e2e/dynamic_provisioning.go | 16 ++-- tests/e2e/format_options.go | 1 + tests/e2e/pre_provsioning.go | 5 +- tests/e2e/requires_aws_api.go | 13 ++-- tests/e2e/suite_test.go | 1 - ...namically_provisioned_cmd_volume_tester.go | 3 +- ...cally_provisioned_collocated_pod_tester.go | 4 +- ...lly_provisioned_read_only_volume_tester.go | 8 +- ...cally_provisioned_reclaim_policy_tester.go | 1 - ...rovisioned_topology_aware_volume_tester.go | 4 +- ...ally_provisioned_volume_snapshot_tester.go | 6 +- ...pre_provisioned_read_only_volume_tester.go | 5 +- .../pre_provisioned_reclaim_policy_tester.go | 1 - .../pre_provisioned_snapshot_volume_tester.go | 3 +- .../pre_provisioned_volume_tester.go | 3 +- tests/e2e/testsuites/specs.go | 4 +- tests/e2e/testsuites/testsuites.go | 1 - 21 files changed, 110 insertions(+), 59 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 02af859042..2ecb90324a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -36,7 +36,85 @@ linters: - fatcontext - forbidigo - forcetypeassert + - gci + - ginkgolinter + - gocheckcompilerdirectives +# - gochecknoglobals +# - gochecknoinits +# - gochecksumtype +# - gocognit +# - goconst +# - gocritic +# - gocyclo +# - godot +# - godox +# - gofmt +# - gofumpt +# - goheader +# - goimports +# - gomoddirectives +# - gomodguard +# - goprintffuncname +# - gosec +# - gosimple +# - gosmopolitan +# - govet +# - grouper +# - importas +# - inamedparam +# - ineffassign +# - interfacebloat +# - intrange +# - ireturn +# - lll +# - loggercheck +# - maintidx +# - makezero +# - mirror +# - misspell +# - mnd +# - musttag +# - nakedret +# - nestif +# - nilerr +# - nilnil +# - nlreturn +# - noctx +# - nolintlint +# - nonamedreturns +# - nosprintfhostport +# - paralleltest +# - perfsprint +# - prealloc +# - predeclared +# - promlinter +# - protogetter +# - reassign +# - revive +# - rowserrcheck +# - sloglint +# - spancheck +# - sqlclosecheck +# - staticcheck +# - stylecheck +# - tagalign +# - tagliatelle +# - tenv +# - testableexamples +# - testifylint +# - testpackage +# - thelper +# - tparallel +# - unconvert +# - unparam +# - unused +# - usestdlibvars +# - varnamelen +# - wastedassign - whitespace +# - wrapcheck +# - wsl +# - zerologlint disable: - govet # We already run with `make verify/govet` - depguard # We don't use this today diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index eefcdf7dc0..772996ef15 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -26,20 +26,17 @@ import ( "testing" "time" - "k8s.io/apimachinery/pkg/util/wait" - "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" - "github.com/aws/smithy-go" - "github.com/golang/mock/gomock" dm "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud/devicemanager" "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/expiringcache" "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/util/wait" ) const ( diff --git a/pkg/driver/request_coalescing_test.go b/pkg/driver/request_coalescing_test.go index 6b41aca5f0..631e5b9f82 100644 --- a/pkg/driver/request_coalescing_test.go +++ b/pkg/driver/request_coalescing_test.go @@ -19,19 +19,17 @@ package driver import ( "context" "fmt" - "sync" "testing" "time" "github.com/awslabs/volume-modifier-for-k8s/pkg/rpc" "github.com/container-storage-interface/spec/lib/go/csi" + "github.com/golang/mock/gomock" + "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver/internal" "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util" "k8s.io/klog/v2" - - "github.com/golang/mock/gomock" - "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" ) type modifyVolumeExecutor func(ctx context.Context, driver ControllerService, name string, params map[string]string) error diff --git a/pkg/mounter/mount_unsupported.go b/pkg/mounter/mount_unsupported.go index 79fbaceb85..f04106961c 100644 --- a/pkg/mounter/mount_unsupported.go +++ b/pkg/mounter/mount_unsupported.go @@ -22,6 +22,7 @@ package mounter import ( "errors" "fmt" + mountutils "k8s.io/mount-utils" ) diff --git a/tests/e2e/dynamic_provisioning.go b/tests/e2e/dynamic_provisioning.go index c39de5b51c..bf17eb25dd 100644 --- a/tests/e2e/dynamic_provisioning.go +++ b/tests/e2e/dynamic_provisioning.go @@ -20,21 +20,19 @@ import ( "os" "strings" + awscloud "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" + ebscsidriver "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver" + "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" + "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/testsuites" . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" storagev1 "k8s.io/api/storage/v1" - clientset "k8s.io/client-go/kubernetes" - restclientset "k8s.io/client-go/rest" - "k8s.io/kubernetes/test/e2e/framework" - - "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/testsuites" - - awscloud "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" - ebscsidriver "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/serializer" + clientset "k8s.io/client-go/kubernetes" + restclientset "k8s.io/client-go/rest" + "k8s.io/kubernetes/test/e2e/framework" admissionapi "k8s.io/pod-security-admission/api" ) diff --git a/tests/e2e/format_options.go b/tests/e2e/format_options.go index c8170a525f..b098192856 100644 --- a/tests/e2e/format_options.go +++ b/tests/e2e/format_options.go @@ -16,6 +16,7 @@ package e2e import ( "fmt" + ebscsidriver "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/testsuites" diff --git a/tests/e2e/pre_provsioning.go b/tests/e2e/pre_provsioning.go index cd77e6e6ca..6a3c82dfbd 100644 --- a/tests/e2e/pre_provsioning.go +++ b/tests/e2e/pre_provsioning.go @@ -22,15 +22,14 @@ import ( "strings" "time" - ebscsidriver "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver" - k8srestclient "k8s.io/client-go/rest" - awscloud "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" + ebscsidriver "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/testsuites" . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" + k8srestclient "k8s.io/client-go/rest" "k8s.io/kubernetes/test/e2e/framework" admissionapi "k8s.io/pod-security-admission/api" ) diff --git a/tests/e2e/requires_aws_api.go b/tests/e2e/requires_aws_api.go index 2bb03435c6..682593e292 100644 --- a/tests/e2e/requires_aws_api.go +++ b/tests/e2e/requires_aws_api.go @@ -18,25 +18,22 @@ import ( "context" "fmt" - "github.com/google/uuid" - "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/google/uuid" + volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" + awscloud "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" + ebscsidriver "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/testsuites" - "k8s.io/kubernetes/test/e2e/framework" - - volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" restclientset "k8s.io/client-go/rest" + "k8s.io/kubernetes/test/e2e/framework" admissionapi "k8s.io/pod-security-admission/api" - - awscloud "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" - ebscsidriver "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver" ) const testTagNamePrefix = "testTag" diff --git a/tests/e2e/suite_test.go b/tests/e2e/suite_test.go index 209c8216ad..616c9ca2dd 100644 --- a/tests/e2e/suite_test.go +++ b/tests/e2e/suite_test.go @@ -25,7 +25,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "k8s.io/kubernetes/test/e2e/framework" frameworkconfig "k8s.io/kubernetes/test/e2e/framework/config" ) diff --git a/tests/e2e/testsuites/dynamically_provisioned_cmd_volume_tester.go b/tests/e2e/testsuites/dynamically_provisioned_cmd_volume_tester.go index 49f69ed004..d28f2a60a8 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_cmd_volume_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_cmd_volume_tester.go @@ -16,10 +16,9 @@ package testsuites import ( "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" + . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" - - . "github.com/onsi/ginkgo/v2" ) // DynamicallyProvisionedCmdVolumeTest will provision required StorageClass(es), PVC(s) and Pod(s) diff --git a/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go b/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go index d85a2e0b53..487ef47c3a 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go @@ -16,11 +16,9 @@ package testsuites import ( "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - + . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" - - . "github.com/onsi/ginkgo/v2" ) // DynamicallyProvisionedCollocatedPodTest will provision required StorageClass(es), PVC(s) and Pod(s) diff --git a/tests/e2e/testsuites/dynamically_provisioned_read_only_volume_tester.go b/tests/e2e/testsuites/dynamically_provisioned_read_only_volume_tester.go index 4cf48c944c..92de0c68f9 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_read_only_volume_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_read_only_volume_tester.go @@ -18,13 +18,11 @@ import ( "fmt" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - v1 "k8s.io/api/core/v1" - "k8s.io/kubernetes/test/e2e/framework" - - clientset "k8s.io/client-go/kubernetes" - . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + v1 "k8s.io/api/core/v1" + clientset "k8s.io/client-go/kubernetes" + "k8s.io/kubernetes/test/e2e/framework" ) const expectedReadOnlyLog = "Read-only file system" diff --git a/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go b/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go index e58e1210ae..7406ae771b 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go @@ -17,7 +17,6 @@ package testsuites import ( "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" ) diff --git a/tests/e2e/testsuites/dynamically_provisioned_topology_aware_volume_tester.go b/tests/e2e/testsuites/dynamically_provisioned_topology_aware_volume_tester.go index b1d32078f4..1d64f77c2b 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_topology_aware_volume_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_topology_aware_volume_tester.go @@ -18,11 +18,9 @@ import ( "fmt" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - + . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" - - . "github.com/onsi/ginkgo/v2" ) // DynamicallyProvisionedTopologyAwareVolumeTest will provision required StorageClass(es), PVC(s) and Pod(s) diff --git a/tests/e2e/testsuites/dynamically_provisioned_volume_snapshot_tester.go b/tests/e2e/testsuites/dynamically_provisioned_volume_snapshot_tester.go index ef2b3f7c5a..23880778bf 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_volume_snapshot_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_volume_snapshot_tester.go @@ -15,14 +15,12 @@ limitations under the License. package testsuites import ( - "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" + "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" + . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" restclientset "k8s.io/client-go/rest" - - . "github.com/onsi/ginkgo/v2" ) // DynamicallyProvisionedVolumeSnapshotTest will provision required StorageClass(es),VolumeSnapshotClass(es), PVC(s) and Pod(s) diff --git a/tests/e2e/testsuites/pre_provisioned_read_only_volume_tester.go b/tests/e2e/testsuites/pre_provisioned_read_only_volume_tester.go index 8fa5ec2963..04156914dd 100644 --- a/tests/e2e/testsuites/pre_provisioned_read_only_volume_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_read_only_volume_tester.go @@ -18,12 +18,11 @@ import ( "fmt" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" "k8s.io/kubernetes/test/e2e/framework" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" ) // PreProvisionedReadOnlyVolumeTest will provision required PV(s), PVC(s) and Pod(s) diff --git a/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go b/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go index 8fd7793a19..595442a27b 100644 --- a/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go @@ -16,7 +16,6 @@ package testsuites import ( "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" ) diff --git a/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go b/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go index 5dfa6dc6eb..c88d7f823a 100644 --- a/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go @@ -18,13 +18,12 @@ import ( "fmt" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" + . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" storagev1 "k8s.io/api/storage/v1" clientset "k8s.io/client-go/kubernetes" k8srestclient "k8s.io/client-go/rest" "k8s.io/kubernetes/test/e2e/framework" - - . "github.com/onsi/ginkgo/v2" ) type PreProvisionedVolumeSnapshotTest struct { diff --git a/tests/e2e/testsuites/pre_provisioned_volume_tester.go b/tests/e2e/testsuites/pre_provisioned_volume_tester.go index b3967fe72d..30143fd38f 100644 --- a/tests/e2e/testsuites/pre_provisioned_volume_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_volume_tester.go @@ -16,10 +16,9 @@ package testsuites import ( "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" + . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" - - . "github.com/onsi/ginkgo/v2" ) // PreProvisionedVolumeTest will provision required PV(s), PVC(s) and Pod(s) diff --git a/tests/e2e/testsuites/specs.go b/tests/e2e/testsuites/specs.go index 20a7d679a0..8aa03d72e8 100644 --- a/tests/e2e/testsuites/specs.go +++ b/tests/e2e/testsuites/specs.go @@ -18,13 +18,11 @@ import ( "fmt" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - + . "github.com/onsi/ginkgo/v2" v1 "k8s.io/api/core/v1" storagev1 "k8s.io/api/storage/v1" clientset "k8s.io/client-go/kubernetes" restclientset "k8s.io/client-go/rest" - - . "github.com/onsi/ginkgo/v2" ) type PodDetails struct { diff --git a/tests/e2e/testsuites/testsuites.go b/tests/e2e/testsuites/testsuites.go index ee87045c64..22eb8bb09d 100644 --- a/tests/e2e/testsuites/testsuites.go +++ b/tests/e2e/testsuites/testsuites.go @@ -21,7 +21,6 @@ import ( "time" "github.com/aws/aws-sdk-go-v2/aws" - volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" snapshotclientset "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned" awscloud "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" From 49aae916fdeb80e004877309cf077a64018f32ce Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 21:25:26 -0400 Subject: [PATCH 09/24] cleanup: enable gocritic linter and simplify elseIf chains --- .golangci.yml | 13 ++++----- cmd/hooks/prestop.go | 9 +++--- pkg/cloud/cloud.go | 11 ++++---- pkg/cloud/cloud_test.go | 38 ++++++++------------------ pkg/coalescer/coalescer_test.go | 7 +++-- pkg/driver/controller.go | 15 +++++----- pkg/driver/controller_modify_volume.go | 7 +++-- pkg/driver/controller_test.go | 6 ++-- pkg/driver/driver.go | 2 +- pkg/driver/node.go | 13 ++++----- pkg/driver/options.go | 11 ++++---- pkg/driver/request_coalescing_test.go | 7 +++-- pkg/util/util.go | 2 +- tests/e2e/testsuites/testsuites.go | 6 ++-- 14 files changed, 66 insertions(+), 81 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 2ecb90324a..b27524bb92 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -39,14 +39,8 @@ linters: - gci - ginkgolinter - gocheckcompilerdirectives -# - gochecknoglobals -# - gochecknoinits -# - gochecksumtype -# - gocognit -# - goconst -# - gocritic -# - gocyclo -# - godot + - gochecksumtype + - gocritic # - godox # - gofmt # - gofumpt @@ -137,3 +131,6 @@ linters: - nestif # Many false positives - gocognit # Many false positives - cyclop # Many false positives + - godot # TODO Ask team + - goconst # TODO FIXME + - gochecknoinits # TODO FIXME diff --git a/cmd/hooks/prestop.go b/cmd/hooks/prestop.go index 34eee66b05..786f4cd0a0 100644 --- a/cmd/hooks/prestop.go +++ b/cmd/hooks/prestop.go @@ -63,14 +63,15 @@ func PreStop(clientset kubernetes.Interface) error { } node, err := fetchNode(clientset, nodeName) - if k8serrors.IsNotFound(err) { + switch { + case k8serrors.IsNotFound(err): klog.InfoS("PreStop: node does not exist - assuming this is a termination event, checking for remaining VolumeAttachments", "node", nodeName) - } else if err != nil { + case err != nil: return err - } else if !isNodeBeingDrained(node) { + case !isNodeBeingDrained(node): klog.InfoS("PreStop: node is not being drained, skipping VolumeAttachments check", "node", nodeName) return nil - } else { + default: klog.InfoS("PreStop: node is being drained, checking for remaining VolumeAttachments", "node", nodeName) } diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index c4391cd201..c1da97e478 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -127,7 +127,7 @@ const ( KubernetesTagKeyPrefix = "kubernetes.io" // AWSTagKeyPrefix is the prefix of the key value that is reserved for AWS. AWSTagKeyPrefix = "aws:" - //AwsEbsDriverTagKey is the tag to identify if a volume/snapshot is managed by ebs csi driver + // AwsEbsDriverTagKey is the tag to identify if a volume/snapshot is managed by ebs csi driver AwsEbsDriverTagKey = "ebs.csi.aws.com/cluster" ) @@ -1700,13 +1700,14 @@ func (c *cloud) checkDesiredState(ctx context.Context, volumeID string, desiredS // Check if there is a mismatch between the requested modification and the current volume // If there is, the volume is still modifying and we should not return a success - if realSizeGiB < desiredSizeGiB { + switch { + case realSizeGiB < desiredSizeGiB: return realSizeGiB, fmt.Errorf("volume %q is still being expanded to %d size", volumeID, desiredSizeGiB) - } else if options.IOPS != 0 && (volume.Iops == nil || *volume.Iops != options.IOPS) { + case options.IOPS != 0 && (volume.Iops == nil || *volume.Iops != options.IOPS): return realSizeGiB, fmt.Errorf("volume %q is still being modified to iops %d", volumeID, options.IOPS) - } else if options.VolumeType != "" && !strings.EqualFold(string(volume.VolumeType), options.VolumeType) { + case options.VolumeType != "" && !strings.EqualFold(string(volume.VolumeType), options.VolumeType): return realSizeGiB, fmt.Errorf("volume %q is still being modified to type %q", volumeID, options.VolumeType) - } else if options.Throughput != 0 && (volume.Throughput == nil || *volume.Throughput != options.Throughput) { + case options.Throughput != 0 && (volume.Throughput == nil || *volume.Throughput != options.Throughput): return realSizeGiB, fmt.Errorf("volume %q is still being modified to throughput %d", volumeID, options.Throughput) } diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index 772996ef15..5d38d5eb12 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -2667,24 +2667,14 @@ func TestResizeOrModifyDisk(t *testing.T) { } newSize, err := c.ResizeOrModifyDisk(ctx, tc.volumeID, util.GiBToBytes(tc.reqSizeGiB), tc.modifyDiskOptions) - if err != nil { - if tc.expErr == nil { - t.Fatalf("ResizeOrModifyDisk() failed: expected no error, got: %v", err) - } else { - if errors.Is(tc.expErr, ErrInvalidArgument) { - if !errors.Is(err, ErrInvalidArgument) { - t.Fatalf("ResizeOrModifyDisk() failed: expected ErrInvalidArgument, got: %v", err) - } - } - } - } else { - if tc.expErr != nil { - t.Fatal("ResizeOrModifyDisk() failed: expected error, got nothing") - } else { - if tc.reqSizeGiB != newSize { - t.Fatalf("ResizeOrModifyDisk() failed: expected capacity %d, got %d", tc.reqSizeGiB, newSize) - } - } + switch { + case errors.Is(tc.expErr, ErrInvalidArgument): + require.ErrorIs(t, err, ErrInvalidArgument, "ResizeOrModifyDisk() should return ErrInvalidArgument") + case tc.expErr != nil: + require.Error(t, err, "ResizeOrModifyDisk() should return error") + default: + require.NoError(t, err, "ResizeOrModifyDisk() should not return error") + assert.Equal(t, tc.reqSizeGiB, newSize, "ResizeOrModifyDisk() returned unexpected capacity") } mockCtrl.Finish() @@ -2788,15 +2778,11 @@ func TestModifyTags(t *testing.T) { if err != nil { if tc.expErr == nil { t.Fatalf("ModifyTags() failed: expected no error, got: %v", err) - } else { - if !strings.Contains(err.Error(), tc.expErr.Error()) { - t.Fatalf("ModifyTags() failed: expected error %v, got: %v", tc.expErr, err) - } - } - } else { - if tc.expErr != nil { - t.Fatal("ModifyTags() failed: expected error, got nothing") + } else if !strings.Contains(err.Error(), tc.expErr.Error()) { + t.Fatalf("ModifyTags() failed: expected error %v, got: %v", tc.expErr, err) } + } else if tc.expErr != nil { + t.Fatal("ModifyTags() failed: expected error, got nothing") } mockCtrl.Finish() diff --git a/pkg/coalescer/coalescer_test.go b/pkg/coalescer/coalescer_test.go index be40167034..1cb4a96f0e 100644 --- a/pkg/coalescer/coalescer_test.go +++ b/pkg/coalescer/coalescer_test.go @@ -95,11 +95,12 @@ func TestCoalescer(t *testing.T) { for range tc.inputs { err := <-testChannel if err != nil { - if errors.Is(err, errFailedToMerge) { + switch { + case errors.Is(err, errFailedToMerge): mergeFailure = true - } else if errors.Is(err, errFailedToExecute) { + case errors.Is(err, errFailedToExecute): executeFailure = true - } else { + default: t.Fatalf("Unexpected error %v", err) } } diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index 9aa0b0c90a..fcd7e75d1c 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -645,8 +645,8 @@ func isBlock(cap *csi.VolumeCapability) bool { } func isValidVolumeContext(volContext map[string]string) bool { - //There could be multiple volume attributes in the volumeContext map - //Validate here case by case + // There could be multiple volume attributes in the volumeContext map + // Validate here case by case if partition, ok := volContext[VolumeAttributePartition]; ok { partitionInt, err := strconv.ParseInt(partition, 10, 64) if err != nil { @@ -1017,17 +1017,17 @@ func getVolSizeBytes(req *csi.CreateVolumeRequest) (int64, error) { // BuildOutpostArn returns the string representation of the outpost ARN from the given csi.TopologyRequirement.segments func BuildOutpostArn(segments map[string]string) string { - if len(segments[AwsPartitionKey]) <= 0 { + if len(segments[AwsPartitionKey]) == 0 { return "" } - if len(segments[AwsRegionKey]) <= 0 { + if len(segments[AwsRegionKey]) == 0 { return "" } - if len(segments[AwsOutpostIDKey]) <= 0 { + if len(segments[AwsOutpostIDKey]) == 0 { return "" } - if len(segments[AwsAccountIDKey]) <= 0 { + if len(segments[AwsAccountIDKey]) == 0 { return "" } @@ -1041,8 +1041,7 @@ func BuildOutpostArn(segments map[string]string) string { func validateFormattingOption(volumeCapabilities []*csi.VolumeCapability, paramName string, fsConfigs map[string]fileSystemConfig) error { for _, volCap := range volumeCapabilities { - switch volCap.GetAccessType().(type) { - case *csi.VolumeCapability_Block: + if _, isBlockVolCap := volCap.GetAccessType().(*csi.VolumeCapability_Block); isBlockVolCap { return status.Error(codes.InvalidArgument, fmt.Sprintf("Cannot use %s with block volume", paramName)) } diff --git a/pkg/driver/controller_modify_volume.go b/pkg/driver/controller_modify_volume.go index 09b8e1d4ed..e7ca742d02 100644 --- a/pkg/driver/controller_modify_volume.go +++ b/pkg/driver/controller_modify_volume.go @@ -192,15 +192,16 @@ func parseModifyVolumeParameters(params map[string]string) (*modifyVolumeRequest case ModificationKeyVolumeType: options.modifyDiskOptions.VolumeType = value default: - if strings.HasPrefix(key, ModificationAddTag) { + switch { + case strings.HasPrefix(key, ModificationAddTag): st := strings.SplitN(value, "=", 2) if len(st) < 2 { return nil, status.Errorf(codes.InvalidArgument, "Invalid tag specification: %v", st) } options.modifyTagsOptions.TagsToAdd[st[0]] = st[1] - } else if strings.HasPrefix(key, ModificationDeleteTag) { + case strings.HasPrefix(key, ModificationDeleteTag): options.modifyTagsOptions.TagsToDelete = append(options.modifyTagsOptions.TagsToDelete, value) - } else { + default: return nil, status.Errorf(codes.InvalidArgument, "Invalid mutable parameter key: %s", key) } } diff --git a/pkg/driver/controller_test.go b/pkg/driver/controller_test.go index 3918040f1b..d95e920164 100644 --- a/pkg/driver/controller_test.go +++ b/pkg/driver/controller_test.go @@ -3666,10 +3666,8 @@ func TestControllerExpandVolume(t *testing.T) { if !tc.expError { t.Fatalf("Unexpected error: %v", err) } - } else { - if tc.expError { - t.Fatalf("Expected error from ControllerExpandVolume, got nothing") - } + } else if tc.expError { + t.Fatalf("Expected error from ControllerExpandVolume, got nothing") } sizeGiB := util.BytesToGiB(resp.GetCapacityBytes()) diff --git a/pkg/driver/driver.go b/pkg/driver/driver.go index af74a919d0..c8eeb17cd6 100644 --- a/pkg/driver/driver.go +++ b/pkg/driver/driver.go @@ -52,7 +52,7 @@ const ( AwsRegionKey = "topology." + DriverName + "/region" AwsOutpostIDKey = "topology." + DriverName + "/outpost-id" WellKnownZoneTopologyKey = "topology.kubernetes.io/zone" - // DEPRECATED Use the WellKnownZoneTopologyKey instead + // Deprecated: Use the WellKnownZoneTopologyKey instead ZoneTopologyKey = "topology." + DriverName + "/zone" OSTopologyKey = "kubernetes.io/os" ) diff --git a/pkg/driver/node.go b/pkg/driver/node.go index bcb9cb4124..3c8a91fd41 100644 --- a/pkg/driver/node.go +++ b/pkg/driver/node.go @@ -136,8 +136,7 @@ func (d *NodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol } // If the access type is block, do nothing for stage - switch volCap.GetAccessType().(type) { - case *csi.VolumeCapability_Block: + if _, isAccessTypeBlock := volCap.GetAccessType().(*csi.VolumeCapability_Block); isAccessTypeBlock { return &csi.NodeStageVolumeResponse{}, nil } @@ -657,7 +656,7 @@ func (d *NodeService) nodePublishVolumeForBlock(req *csi.NodePublishVolumeReques return status.Errorf(codes.Internal, "Could not create file %q: %v", target, err) } - //Checking if the target file is already mounted with a device. + // Checking if the target file is already mounted with a device. mounted, err := d.isMounted(source, target) if err != nil { return status.Errorf(codes.Internal, "Could not check if %q is mounted: %v", target, err) @@ -689,14 +688,14 @@ func (d *NodeService) isMounted(_ string, target string) (bool, error) { */ notMnt, err := d.mounter.IsLikelyNotMountPoint(target) if err != nil && !os.IsNotExist(err) { - //Checking if the path exists and error is related to Corrupted Mount, in that case, the system could unmount and mount. + // Checking if the path exists and error is related to Corrupted Mount, in that case, the system could unmount and mount. _, pathErr := d.mounter.PathExists(target) if pathErr != nil && d.mounter.IsCorruptedMnt(pathErr) { klog.V(4).InfoS("NodePublishVolume: Target path is a corrupted mount. Trying to unmount.", "target", target) if mntErr := d.mounter.Unpublish(target); mntErr != nil { return false, status.Errorf(codes.Internal, "Unable to unmount the target %q : %v", target, mntErr) } - //After successful unmount, the device is ready to be mounted. + // After successful unmount, the device is ready to be mounted. return false, nil } return false, status.Errorf(codes.Internal, "Could not check if %q is a mount point: %v, %v", target, err, pathErr) @@ -735,7 +734,7 @@ func (d *NodeService) nodePublishVolumeForFileSystem(req *csi.NodePublishVolumeR return status.Errorf(codes.Internal, "%s", err.Error()) } - //Checking if the target directory is already mounted with a device. + // Checking if the target directory is already mounted with a device. mounted, err := d.isMounted(source, target) if err != nil { return status.Errorf(codes.Internal, "Could not check if %q is mounted: %v", target, err) @@ -799,7 +798,7 @@ func (d *NodeService) getVolumesLimit() int64 { availableAttachments = availableAttachments - enis - reservedSlots } } - availableAttachments = availableAttachments - reservedVolumeAttachments + availableAttachments -= reservedVolumeAttachments if availableAttachments <= 0 { availableAttachments = 1 } diff --git a/pkg/driver/options.go b/pkg/driver/options.go index 415e3ddc34..937ac68b0d 100644 --- a/pkg/driver/options.go +++ b/pkg/driver/options.go @@ -34,7 +34,7 @@ type Options struct { // #### Server options #### - //Endpoint is the endpoint for the CSI driver server + // Endpoint is the endpoint for the CSI driver server Endpoint string // HttpEndpoint is the TCP network address where the HTTP server for metrics will listen HttpEndpoint string @@ -52,7 +52,7 @@ type Options struct { ExtraTags map[string]string // ExtraVolumeTags is a map of tags that will be attached to each dynamically provisioned // volume. - // DEPRECATED: Use ExtraTags instead. + // Deprecated: Use ExtraTags instead. ExtraVolumeTags map[string]string // ID of the kubernetes cluster. KubernetesClusterID string @@ -129,11 +129,12 @@ func (o *Options) Validate() error { } if o.MetricsCertFile != "" || o.MetricsKeyFile != "" { - if o.HttpEndpoint == "" { + switch { + case o.HttpEndpoint == "": return fmt.Errorf("--http-endpoint MUST be specififed when using the metrics server with HTTPS") - } else if o.MetricsCertFile == "" { + case o.MetricsCertFile == "": return fmt.Errorf("--metrics-cert-file MUST be specififed when using the metrics server with HTTPS") - } else if o.MetricsKeyFile == "" { + case o.MetricsKeyFile == "": return fmt.Errorf("--metrics-key-file MUST be specififed when using the metrics server with HTTPS") } } diff --git a/pkg/driver/request_coalescing_test.go b/pkg/driver/request_coalescing_test.go index 631e5b9f82..e562d12412 100644 --- a/pkg/driver/request_coalescing_test.go +++ b/pkg/driver/request_coalescing_test.go @@ -287,21 +287,22 @@ func testPartialFail(t *testing.T, executor modifyVolumeExecutor) { wg.Wait() - if volumeTypeChosen == NewVolumeType1 { + switch volumeTypeChosen { + case NewVolumeType1: if volumeType1Err { t.Error("Controller chose", NewVolumeType1, "but errored request") } if !volumeType2Error { t.Error("Controller chose", NewVolumeType1, "but returned success to", NewVolumeType2, "request") } - } else if volumeTypeChosen == NewVolumeType2 { + case NewVolumeType2: if volumeType2Error { t.Error("Controller chose", NewVolumeType2, "but errored request") } if !volumeType1Err { t.Error("Controller chose", NewVolumeType2, "but returned success to", NewVolumeType1, "request") } - } else { + default: t.Error("No volume type chosen") } } diff --git a/pkg/util/util.go b/pkg/util/util.go index 3e0c5a6082..afb82fab44 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -147,7 +147,7 @@ func CountMACAddresses(s string) int { // NormalizeWindowsPath normalizes a Windows path func NormalizeWindowsPath(path string) string { - normalizedPath := strings.Replace(path, "/", "\\", -1) + normalizedPath := strings.ReplaceAll(path, "/", "\\") if strings.HasPrefix(normalizedPath, "\\") { normalizedPath = "c:" + normalizedPath } diff --git a/tests/e2e/testsuites/testsuites.go b/tests/e2e/testsuites/testsuites.go index 22eb8bb09d..92b9b8c708 100644 --- a/tests/e2e/testsuites/testsuites.go +++ b/tests/e2e/testsuites/testsuites.go @@ -601,16 +601,16 @@ func (t *TestDeployment) Logs() ([]byte, error) { } // waitForPersistentVolumeClaimDeleted waits for a PersistentVolumeClaim to be removed from the system until timeout occurs, whichever comes first. -func waitForPersistentVolumeClaimDeleted(c clientset.Interface, ns string, pvcName string, Poll, timeout time.Duration) error { +func waitForPersistentVolumeClaimDeleted(c clientset.Interface, ns string, pvcName string, poll, timeout time.Duration) error { framework.Logf("Waiting up to %v for PersistentVolumeClaim %s to be removed", timeout, pvcName) - for start := time.Now(); time.Since(start) < timeout; time.Sleep(Poll) { + for start := time.Now(); time.Since(start) < timeout; time.Sleep(poll) { _, err := c.CoreV1().PersistentVolumeClaims(ns).Get(context.Background(), pvcName, metav1.GetOptions{}) if err != nil { if apierrs.IsNotFound(err) { framework.Logf("Claim %q in namespace %q doesn't exist in the system", pvcName, ns) return nil } - framework.Logf("Failed to get claim %q in namespace %q, retrying in %v. Error: %v", pvcName, ns, Poll, err) + framework.Logf("Failed to get claim %q in namespace %q, retrying in %v. Error: %v", pvcName, ns, poll, err) } } return fmt.Errorf("PersistentVolumeClaim %s is not removed from the system within %v", pvcName, timeout) From 39efb4eecb52d7502e5106fde9b31db1e811ef21 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 21:31:58 -0400 Subject: [PATCH 10/24] cleanup: enable goimports linter --- .../testsuites/dynamically_provisioned_reclaim_policy_tester.go | 2 +- tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go b/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go index 7406ae771b..3211c18031 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go @@ -17,7 +17,7 @@ package testsuites import ( "github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" ) diff --git a/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go b/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go index 595442a27b..9a9538a713 100644 --- a/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go @@ -16,7 +16,7 @@ package testsuites import ( "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" clientset "k8s.io/client-go/kubernetes" ) From 2ffd949881cd351623cdf050a834f084ad587351 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 21:32:57 -0400 Subject: [PATCH 11/24] cleanup: Enable unconvert linter and remove unecessary type conversions --- .golangci.yml | 28 ++++++++++++---------------- pkg/cloud/cloud.go | 2 +- pkg/util/template/template.go | 2 +- tests/e2e/driver/ebs_csi_driver.go | 2 +- tests/e2e/testsuites/testsuites.go | 8 ++++---- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index b27524bb92..2678b1c074 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -41,13 +41,10 @@ linters: - gocheckcompilerdirectives - gochecksumtype - gocritic -# - godox -# - gofmt -# - gofumpt -# - goheader -# - goimports -# - gomoddirectives -# - gomodguard + - gofmt + - goheader + - goimports + - gomodguard # - goprintffuncname # - gosec # - gosimple @@ -99,16 +96,13 @@ linters: # - testpackage # - thelper # - tparallel -# - unconvert -# - unparam -# - unused -# - usestdlibvars -# - varnamelen -# - wastedassign + - unconvert + - unparam + - unused + - usestdlibvars + - wastedassign - whitespace -# - wrapcheck -# - wsl -# - zerologlint + - zerologlint disable: - govet # We already run with `make verify/govet` - depguard # We don't use this today @@ -134,3 +128,5 @@ linters: - godot # TODO Ask team - goconst # TODO FIXME - gochecknoinits # TODO FIXME + - godox # TODO FIXME + - gofumpt # TODO Ask team diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index c1da97e478..c085a37804 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -845,7 +845,7 @@ func (c *cloud) ResizeOrModifyDisk(ctx context.Context, volumeID string, newSize } } // Perform one final check on the volume - return c.checkDesiredState(ctx, volumeID, int32(newSizeGiB), options) + return c.checkDesiredState(ctx, volumeID, newSizeGiB, options) } func (c *cloud) DeleteDisk(ctx context.Context, volumeID string) (bool, error) { diff --git a/pkg/util/template/template.go b/pkg/util/template/template.go index c8b07c3a0a..ca7f21bb61 100644 --- a/pkg/util/template/template.go +++ b/pkg/util/template/template.go @@ -44,7 +44,7 @@ func Evaluate(tm []string, props interface{}, warnOnly bool) (map[string]string, key, value := st[0], st[1] - t := template.New("tmpl").Funcs(template.FuncMap(newFuncMap())) + t := template.New("tmpl").Funcs(newFuncMap()) val, err := execTemplate(value, props, t) if err != nil { if warnOnly { diff --git a/tests/e2e/driver/ebs_csi_driver.go b/tests/e2e/driver/ebs_csi_driver.go index ee0a038068..52bfb9fabb 100644 --- a/tests/e2e/driver/ebs_csi_driver.go +++ b/tests/e2e/driver/ebs_csi_driver.go @@ -92,7 +92,7 @@ func (d *ebsCSIDriver) GetPersistentVolume(volumeID string, fsType string, size Spec: v1.PersistentVolumeSpec{ AccessModes: []v1.PersistentVolumeAccessMode{accessMode}, Capacity: v1.ResourceList{ - v1.ResourceName(v1.ResourceStorage): resource.MustParse(size), + v1.ResourceStorage: resource.MustParse(size), }, PersistentVolumeReclaimPolicy: pvReclaimPolicy, PersistentVolumeSource: v1.PersistentVolumeSource{ diff --git a/tests/e2e/testsuites/testsuites.go b/tests/e2e/testsuites/testsuites.go index 92b9b8c708..2176f824f1 100644 --- a/tests/e2e/testsuites/testsuites.go +++ b/tests/e2e/testsuites/testsuites.go @@ -343,11 +343,11 @@ func (t *TestPersistentVolumeClaim) ValidateProvisionedPersistentVolume() { framework.ExpectNoError(err) // Check sizes - expectedCapacity := t.requestedPersistentVolumeClaim.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)] - claimCapacity := t.persistentVolumeClaim.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)] + expectedCapacity := t.requestedPersistentVolumeClaim.Spec.Resources.Requests[v1.ResourceStorage] + claimCapacity := t.persistentVolumeClaim.Spec.Resources.Requests[v1.ResourceStorage] Expect(claimCapacity.Value()).To(Equal(expectedCapacity.Value()), "claimCapacity is not equal to requestedCapacity") - pvCapacity := t.persistentVolume.Spec.Capacity[v1.ResourceName(v1.ResourceStorage)] + pvCapacity := t.persistentVolume.Spec.Capacity[v1.ResourceStorage] Expect(pvCapacity.Value()).To(Equal(expectedCapacity.Value()), "pvCapacity is not equal to requestedCapacity") // Check PV properties @@ -422,7 +422,7 @@ func generatePVC(namespace, storageClassName, claimSize string, volumeMode v1.Pe }, Resources: v1.VolumeResourceRequirements{ Requests: v1.ResourceList{ - v1.ResourceName(v1.ResourceStorage): resource.MustParse(claimSize), + v1.ResourceStorage: resource.MustParse(claimSize), }, }, VolumeMode: &volumeMode, From ef3bba52966b811a6a58dea8d3653da30d0d6956 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 21:43:00 -0400 Subject: [PATCH 12/24] cleanup: change for loops to use integer range (Go 1.22+) --- .golangci.yml | 20 +++++++++----------- pkg/batcher/batcher_test.go | 8 ++++---- pkg/cloud/cloud_test.go | 12 ++++++------ pkg/cloud/devicemanager/allocator_test.go | 2 +- pkg/driver/request_coalescing_test.go | 2 +- pkg/driver/validation_test.go | 2 +- 6 files changed, 22 insertions(+), 24 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 2678b1c074..71c874dacd 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -45,17 +45,14 @@ linters: - goheader - goimports - gomodguard -# - goprintffuncname -# - gosec -# - gosimple -# - gosmopolitan -# - govet -# - grouper -# - importas -# - inamedparam -# - ineffassign -# - interfacebloat -# - intrange + - goprintffuncname + - gosimple + - gosmopolitan + - grouper + - importas + - inamedparam + - ineffassign + - intrange # - ireturn # - lll # - loggercheck @@ -130,3 +127,4 @@ linters: - gochecknoinits # TODO FIXME - godox # TODO FIXME - gofumpt # TODO Ask team + - gosec # TODO Urgent not too bad diff --git a/pkg/batcher/batcher_test.go b/pkg/batcher/batcher_test.go index 72319fdc3f..0de9105d7c 100644 --- a/pkg/batcher/batcher_test.go +++ b/pkg/batcher/batcher_test.go @@ -135,7 +135,7 @@ func TestBatcher(t *testing.T) { var wg sync.WaitGroup - for i := 0; i < len(tc.tasks); i++ { + for i := range tc.tasks { wg.Add(1) go func(taskNum int) { defer wg.Done() @@ -147,7 +147,7 @@ func TestBatcher(t *testing.T) { wg.Wait() - for i := 0; i < len(tc.tasks); i++ { + for i := range tc.tasks { select { case r := <-resultChans[i]: task := fmt.Sprintf("task%d", i) @@ -174,7 +174,7 @@ func TestBatcherConcurrentTaskAdditions(t *testing.T) { b := New(numTasks, 1*time.Second, mockExecution) resultChans := make([]chan BatchResult[string], numTasks) - for i := 0; i < numTasks; i++ { + for i := range numTasks { wg.Add(1) go func(taskNum int) { defer wg.Done() @@ -186,7 +186,7 @@ func TestBatcherConcurrentTaskAdditions(t *testing.T) { wg.Wait() - for i := 0; i < numTasks; i++ { + for i := range numTasks { r := <-resultChans[i] task := fmt.Sprintf("task%d", i) if r.Err != nil { diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index 5d38d5eb12..be13e2c508 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -53,12 +53,12 @@ const ( func generateVolumes(volIdCount, volTagCount int) []types.Volume { volumes := make([]types.Volume, 0, volIdCount+volTagCount) - for i := 0; i < volIdCount; i++ { + for i := range volIdCount { volumeID := fmt.Sprintf("vol-%d", i) volumes = append(volumes, types.Volume{VolumeId: aws.String(volumeID)}) } - for i := 0; i < volTagCount; i++ { + for i := range volTagCount { volumeName := fmt.Sprintf("vol-name-%d", i) volumes = append(volumes, types.Volume{Tags: []types.Tag{{Key: aws.String(VolumeNameTagKey), Value: aws.String(volumeName)}}}) } @@ -418,12 +418,12 @@ func executeDescribeInstancesTest(t *testing.T, c *cloud, instanceIds []string, func generateSnapshots(snapIDCount, snapTagCount int) []types.Snapshot { snapshots := make([]types.Snapshot, 0, snapIDCount+snapTagCount) - for i := 0; i < snapIDCount; i++ { + for i := range snapIDCount { snapID := fmt.Sprintf("snap-%d", i) snapshots = append(snapshots, types.Snapshot{SnapshotId: aws.String(snapID)}) } - for i := 0; i < snapTagCount; i++ { + for i := range snapTagCount { snapshotName := fmt.Sprintf("snap-name-%d", i) snapshots = append(snapshots, types.Snapshot{Tags: []types.Tag{{Key: aws.String(SnapshotNameTagKey), Value: aws.String(snapshotName)}}}) } @@ -3115,7 +3115,7 @@ func TestListSnapshots(t *testing.T) { maxResults := 5 nextTokenValue := "nextTokenValue" var expSnapshots []*Snapshot - for i := 0; i < maxResults*2; i++ { + for i := range maxResults * 2 { expSnapshots = append(expSnapshots, &Snapshot{ SourceVolumeID: "snap-test-volume1", SnapshotID: fmt.Sprintf("snap-test-name%d", i), @@ -3125,7 +3125,7 @@ func TestListSnapshots(t *testing.T) { } var ec2Snapshots []types.Snapshot - for i := 0; i < maxResults*2; i++ { + for i := range maxResults * 2 { ec2Snapshots = append(ec2Snapshots, types.Snapshot{ SnapshotId: aws.String(expSnapshots[i].SnapshotID), VolumeId: aws.String(fmt.Sprintf("snap-test-volume%d", i)), diff --git a/pkg/cloud/devicemanager/allocator_test.go b/pkg/cloud/devicemanager/allocator_test.go index a0d68003b5..5b581246e0 100644 --- a/pkg/cloud/devicemanager/allocator_test.go +++ b/pkg/cloud/devicemanager/allocator_test.go @@ -90,7 +90,7 @@ func TestNameAllocatorError(t *testing.T) { allocator := nameAllocator{} existingNames := map[string]string{} - for i := 0; i < len(deviceNames); i++ { + for range deviceNames { name, _ := allocator.GetNext(existingNames, new(sync.Map)) existingNames[name] = "" } diff --git a/pkg/driver/request_coalescing_test.go b/pkg/driver/request_coalescing_test.go index e562d12412..7e515c103a 100644 --- a/pkg/driver/request_coalescing_test.go +++ b/pkg/driver/request_coalescing_test.go @@ -393,7 +393,7 @@ func testDuplicateRequest(t *testing.T, executor modifyVolumeExecutor) { num := 5 wg.Add(num * 2) - for j := 0; j < num; j++ { + for range num { go wrapTimeout(t, "ControllerExpandVolume timed out", func() { _, err := awsDriver.ControllerExpandVolume(context.Background(), &csi.ControllerExpandVolumeRequest{ VolumeId: volumeID, diff --git a/pkg/driver/validation_test.go b/pkg/driver/validation_test.go index 95aa81f16c..056266ee41 100644 --- a/pkg/driver/validation_test.go +++ b/pkg/driver/validation_test.go @@ -40,7 +40,7 @@ func randomString(n int) string { func randomStringMap(n int) map[string]string { result := map[string]string{} - for i := 0; i < n; i++ { + for i := range n { result[strconv.Itoa(i)] = randomString(10) } return result From 42f7b9d68e75f98e909f97283dd4ab2bbff97d06 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 21:58:54 -0400 Subject: [PATCH 13/24] cleanup: pre-allocate slices to increase performance --- .golangci.yml | 36 ++++++++++++++++-------------------- pkg/cloud/cloud.go | 8 +++++--- pkg/driver/controller.go | 5 +++-- pkg/driver/node.go | 2 +- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 71c874dacd..8e0c7f235b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -53,27 +53,19 @@ linters: - inamedparam - ineffassign - intrange -# - ireturn -# - lll -# - loggercheck -# - maintidx -# - makezero -# - mirror -# - misspell -# - mnd -# - musttag -# - nakedret -# - nestif -# - nilerr -# - nilnil -# - nlreturn -# - noctx -# - nolintlint -# - nonamedreturns -# - nosprintfhostport -# - paralleltest + - loggercheck + - makezero + - mirror + - misspell + - musttag + - nakedret + - nilerr + - nilnil + - noctx + - nolintlint + - nosprintfhostport # - perfsprint -# - prealloc + - prealloc # - predeclared # - promlinter # - protogetter @@ -128,3 +120,7 @@ linters: - godox # TODO FIXME - gofumpt # TODO Ask team - gosec # TODO Urgent not too bad + - ireturn # ask team + - maintidx # oof + - nlreturn # Meh, ask team + - nonamedreturns # meh ask team diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index c085a37804..98a497ac62 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -583,7 +583,7 @@ func (c *cloud) CreateDisk(ctx context.Context, volumeName string, diskOptions * iops = capIOPS(createType, capacityGiB, requestedIops, minIops, maxIops, maxIopsPerGb, diskOptions.AllowIOPSPerGBIncrease) } - var tags []types.Tag + tags := make([]types.Tag, 0, len(diskOptions.Tags)) for key, value := range diskOptions.Tags { tags = append(tags, types.Tag{Key: aws.String(key), Value: aws.String(value)}) } @@ -1274,8 +1274,9 @@ func extractSnapshotKey(s *types.Snapshot, batcher snapshotBatcherType) (string, func (c *cloud) CreateSnapshot(ctx context.Context, volumeID string, snapshotOptions *SnapshotOptions) (snapshot *Snapshot, err error) { descriptions := "Created by AWS EBS CSI driver for volume " + volumeID - var tags []types.Tag var request *ec2.CreateSnapshotInput + + tags := make([]types.Tag, 0, len(snapshotOptions.Tags)) for key, value := range snapshotOptions.Tags { tags = append(tags, types.Tag{Key: aws.String(key), Value: aws.String(value)}) } @@ -1384,7 +1385,8 @@ func (c *cloud) ListSnapshots(ctx context.Context, volumeID string, maxResults i if err != nil { return nil, err } - var snapshots []*Snapshot + + snapshots := make([]*Snapshot, 0, len(ec2SnapshotsResponse.Snapshots)) for _, ec2Snapshot := range ec2SnapshotsResponse.Snapshots { snapshots = append(snapshots, c.ec2SnapshotResponseToStruct(ec2Snapshot)) } diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index fcd7e75d1c..46824b13ad 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -489,7 +489,8 @@ func validateControllerUnpublishVolumeRequest(req *csi.ControllerUnpublishVolume func (d *ControllerService) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) { klog.V(4).InfoS("ControllerGetCapabilities: called", "args", req) - var caps []*csi.ControllerServiceCapability + + caps := make([]*csi.ControllerServiceCapability, 0, len(controllerCaps)) for _, cap := range controllerCaps { c := &csi.ControllerServiceCapability{ Type: &csi.ControllerServiceCapability_Rpc{ @@ -975,7 +976,7 @@ func newCreateSnapshotResponse(snapshot *cloud.Snapshot) *csi.CreateSnapshotResp } func newListSnapshotsResponse(cloudResponse *cloud.ListSnapshotsResponse) *csi.ListSnapshotsResponse { - var entries []*csi.ListSnapshotsResponse_Entry + entries := make([]*csi.ListSnapshotsResponse_Entry, 0, len(cloudResponse.Snapshots)) for _, snapshot := range cloudResponse.Snapshots { snapshotResponseEntry := newListSnapshotsResponseEntry(snapshot) entries = append(entries, snapshotResponseEntry) diff --git a/pkg/driver/node.go b/pkg/driver/node.go index 3c8a91fd41..fdba3ced22 100644 --- a/pkg/driver/node.go +++ b/pkg/driver/node.go @@ -558,7 +558,7 @@ func (d *NodeService) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVo func (d *NodeService) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) { klog.V(4).InfoS("NodeGetCapabilities: called", "args", req) - var caps []*csi.NodeServiceCapability + caps := make([]*csi.NodeServiceCapability, 0, len(nodeCaps)) for _, cap := range nodeCaps { c := &csi.NodeServiceCapability{ Type: &csi.NodeServiceCapability_Rpc{ From 0a6d0c05d7e4821d4adee1a28f352cd2d2eb4537 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 22:04:12 -0400 Subject: [PATCH 14/24] cleanup: Use t.Setenv() in unit tests instead of os.Setenv --- .golangci.yml | 28 ++++++++++++++-------------- pkg/cloud/metadata/metadata_test.go | 6 ++---- pkg/driver/node_test.go | 4 +--- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 8e0c7f235b..5f6ac5f3dc 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -64,23 +64,21 @@ linters: - noctx - nolintlint - nosprintfhostport -# - perfsprint - prealloc # - predeclared -# - promlinter -# - protogetter -# - reassign -# - revive -# - rowserrcheck -# - sloglint -# - spancheck -# - sqlclosecheck -# - staticcheck + - promlinter + - protogetter + - reassign + - rowserrcheck + - sloglint + - spancheck + - sqlclosecheck + - staticcheck # - stylecheck -# - tagalign -# - tagliatelle -# - tenv -# - testableexamples + - tagalign + - tagliatelle + - tenv + - testableexamples # - testifylint # - testpackage # - thelper @@ -124,3 +122,5 @@ linters: - maintidx # oof - nlreturn # Meh, ask team - nonamedreturns # meh ask team + - perfsprint # TODO FIXME + - revive # TODO diff --git a/pkg/cloud/metadata/metadata_test.go b/pkg/cloud/metadata/metadata_test.go index d5a4914ea6..23b055498a 100644 --- a/pkg/cloud/metadata/metadata_test.go +++ b/pkg/cloud/metadata/metadata_test.go @@ -19,7 +19,6 @@ package metadata import ( "errors" "io" - "os" "strings" "testing" @@ -103,7 +102,7 @@ func TestNewMetadataService(t *testing.T) { return fake.NewSimpleClientset(node), nil } - os.Setenv("CSI_NODE_NAME", "test-node") + t.Setenv("CSI_NODE_NAME", "test-node") if tc.ec2MetadataError == nil { mockEC2Metadata.EXPECT().GetInstanceIdentityDocument(gomock.Any(), &imds.GetInstanceIdentityDocumentInput{}).Return(&imds.GetInstanceIdentityDocumentOutput{ @@ -525,8 +524,7 @@ func TestKubernetesAPIInstanceInfo(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - os.Setenv("CSI_NODE_NAME", tc.nodeName) - defer os.Unsetenv("CSI_NODE_NAME") + t.Setenv("CSI_NODE_NAME", tc.nodeName) clientset := fake.NewSimpleClientset() if tc.node != nil { diff --git a/pkg/driver/node_test.go b/pkg/driver/node_test.go index 1fe9fb3bee..60ee7a0931 100644 --- a/pkg/driver/node_test.go +++ b/pkg/driver/node_test.go @@ -21,7 +21,6 @@ import ( "context" "errors" "fmt" - "os" "reflect" "runtime" "testing" @@ -51,8 +50,7 @@ func TestNewNodeService(t *testing.T) { mockMounter := mounter.NewMockMounter(ctrl) mockKubernetesClient := NewMockKubernetesClient(ctrl) - os.Setenv("AWS_REGION", "us-west-2") - defer os.Unsetenv("AWS_REGION") + t.Setenv("AWS_REGION", "us-west-2") options := &Options{} From 735a0344c1bae83d81b2aa5ce4085d820d4dc2f4 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Tue, 29 Oct 2024 22:16:33 -0400 Subject: [PATCH 15/24] cleanup: Add t.Parallel() to more unit tests --- .golangci.yml | 5 ++--- pkg/batcher/batcher_test.go | 1 + pkg/cloud/cloud_test.go | 5 +++++ pkg/coalescer/coalescer_test.go | 1 + pkg/driver/request_coalescing_test.go | 1 + 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 5f6ac5f3dc..2e91b758ca 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -79,10 +79,9 @@ linters: - tagliatelle - tenv - testableexamples -# - testifylint -# - testpackage + - testifylint # - thelper -# - tparallel + - tparallel - unconvert - unparam - unused diff --git a/pkg/batcher/batcher_test.go b/pkg/batcher/batcher_test.go index 0de9105d7c..05ee8d6e7f 100644 --- a/pkg/batcher/batcher_test.go +++ b/pkg/batcher/batcher_test.go @@ -43,6 +43,7 @@ func mockExecutionWithError(inputs []string) (map[string]string, error) { } func TestBatcher(t *testing.T) { + t.Parallel() type testCase struct { name string mockFunc func(inputs []string) (map[string]string, error) diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index be13e2c508..2cc609f513 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -124,6 +124,7 @@ func TestNewCloud(t *testing.T) { } } func TestBatchDescribeVolumes(t *testing.T) { + t.Parallel() testCases := []struct { name string volumes []types.Volume @@ -302,6 +303,7 @@ func executeDescribeVolumesTest(t *testing.T, c *cloud, volumeIDs, volumeNames [ } func TestBatchDescribeInstances(t *testing.T) { + t.Parallel() testCases := []struct { name string instanceIds []string @@ -446,6 +448,7 @@ func extractSnapshotIdentifiers(snapshots []types.Snapshot) (snapshotIDs []strin } func TestBatchDescribeSnapshots(t *testing.T) { + t.Parallel() testCases := []struct { name string snapshots []types.Snapshot @@ -698,6 +701,7 @@ func TestCheckDesiredState(t *testing.T) { } func TestBatchDescribeVolumesModifications(t *testing.T) { + t.Parallel() testCases := []struct { name string volumeIds []string @@ -810,6 +814,7 @@ func executeDescribeVolumesModificationsTest(t *testing.T, c *cloud, volumeIds [ } func TestCreateDisk(t *testing.T) { + t.Parallel() testCases := []struct { name string volumeName string diff --git a/pkg/coalescer/coalescer_test.go b/pkg/coalescer/coalescer_test.go index 1cb4a96f0e..2e6fdafaa8 100644 --- a/pkg/coalescer/coalescer_test.go +++ b/pkg/coalescer/coalescer_test.go @@ -51,6 +51,7 @@ func mockExecute(_ string, input int) (string, error) { } func TestCoalescer(t *testing.T) { + t.Parallel() testCases := []struct { name string inputs []int diff --git a/pkg/driver/request_coalescing_test.go b/pkg/driver/request_coalescing_test.go index 7e515c103a..1930d4a658 100644 --- a/pkg/driver/request_coalescing_test.go +++ b/pkg/driver/request_coalescing_test.go @@ -51,6 +51,7 @@ func modifierForK8sModifyVolume(ctx context.Context, driver ControllerService, n } func TestVolumeModificationWithCoalescing(t *testing.T) { + t.Parallel() testCases := []struct { name string testFunction func(t *testing.T, executor modifyVolumeExecutor) From 2673e24e901dad29db93b89ef421de5d2a517fb5 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:28:15 -0400 Subject: [PATCH 16/24] cleanup: Rename vars with same name as a predeclared identifier --- .golangci.yml | 2 +- pkg/cloud/cloud.go | 4 ++-- pkg/driver/controller.go | 18 +++++++++--------- pkg/driver/node.go | 7 ------- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 2e91b758ca..12fe649fb4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -65,7 +65,7 @@ linters: - nolintlint - nosprintfhostport - prealloc -# - predeclared + - predeclared - promlinter - protogetter - reassign diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 98a497ac62..52c868bd3a 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -694,8 +694,8 @@ func (c *cloud) CreateDisk(ctx context.Context, volumeName string, diskOptions * if err != nil { // To avoid leaking volume, we should delete the volume just created // TODO: Need to figure out how to handle DeleteDisk failed scenario instead of just log the error - if _, error := c.DeleteDisk(ctx, volumeID); err != nil { - klog.ErrorS(error, "failed to be deleted, this may cause volume leak", "volumeID", volumeID) + if _, deleteDiskErr := c.DeleteDisk(ctx, volumeID); deleteDiskErr != nil { + klog.ErrorS(deleteDiskErr, "failed to be deleted, this may cause volume leak", "volumeID", volumeID) } else { klog.V(5).InfoS("volume is deleted because there was an error while attaching the tags", "volumeID", volumeID) } diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index 46824b13ad..03965f9b36 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -569,8 +569,8 @@ func (d *ControllerService) ControllerExpandVolume(ctx context.Context, req *csi nodeExpansionRequired := true // if this is a raw block device, no expansion should be necessary on the node - cap := req.GetVolumeCapability() - if cap != nil && cap.GetBlock() != nil { + capability := req.GetVolumeCapability() + if capability != nil && capability.GetBlock() != nil { nodeExpansionRequired = false } @@ -640,9 +640,9 @@ func isValidCapability(c *csi.VolumeCapability) bool { } } -func isBlock(cap *csi.VolumeCapability) bool { - _, isBlock := cap.GetAccessType().(*csi.VolumeCapability_Block) - return isBlock +func isBlock(capability *csi.VolumeCapability) bool { + _, isBlk := capability.GetAccessType().(*csi.VolumeCapability_Block) + return isBlk } func isValidVolumeContext(volContext map[string]string) bool { @@ -755,9 +755,9 @@ func (d *ControllerService) CreateSnapshot(ctx context.Context, req *csi.CreateS // Check if the availability zone is supported for fast snapshot restore if len(fsrAvailabilityZones) > 0 { - zones, error := d.cloud.AvailabilityZones(ctx) - if error != nil { - klog.ErrorS(error, "failed to get availability zones") + zones, err := d.cloud.AvailabilityZones(ctx) + if err != nil { + klog.ErrorS(err, "failed to get availability zones") } else { klog.V(4).InfoS("Availability Zones", "zone", zones) for _, az := range fsrAvailabilityZones { @@ -1042,7 +1042,7 @@ func BuildOutpostArn(segments map[string]string) string { func validateFormattingOption(volumeCapabilities []*csi.VolumeCapability, paramName string, fsConfigs map[string]fileSystemConfig) error { for _, volCap := range volumeCapabilities { - if _, isBlockVolCap := volCap.GetAccessType().(*csi.VolumeCapability_Block); isBlockVolCap { + if isBlock(volCap) { return status.Error(codes.InvalidArgument, fmt.Sprintf("Cannot use %s with block volume", paramName)) } diff --git a/pkg/driver/node.go b/pkg/driver/node.go index fdba3ced22..fd1a9478f2 100644 --- a/pkg/driver/node.go +++ b/pkg/driver/node.go @@ -806,13 +806,6 @@ func (d *NodeService) getVolumesLimit() int64 { return int64(availableAttachments) } -func min(x, y int) int { - if x <= y { - return x - } - return y -} - // hasMountOption returns a boolean indicating whether the given // slice already contains a mount option. This is used to prevent // passing duplicate option to the mount command. From c18bef1880fc99c063abeab87180229bb2fbf577 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:44:13 -0400 Subject: [PATCH 17/24] Enable stylecheck linter; Fix ST1003 & ST1005 --- .golangci.yml | 32 +++-- cmd/main.go | 4 +- pkg/cloud/cloud.go | 16 +-- pkg/cloud/cloud_test.go | 68 +++++----- pkg/coalescer/coalescer_test.go | 4 +- pkg/driver/controller_modify_volume.go | 10 +- pkg/driver/controller_test.go | 124 +++++++++--------- pkg/driver/internal/inflight_test.go | 26 ++-- pkg/driver/node_test.go | 20 +-- pkg/driver/options.go | 8 +- pkg/driver/options_test.go | 6 +- pkg/driver/validation.go | 26 ++-- pkg/driver/validation_test.go | 26 ++-- tests/e2e/testsuites/e2e_utils.go | 4 +- tests/e2e/testsuites/modify_volume_tester.go | 6 +- .../pre_provisioned_snapshot_volume_tester.go | 4 +- tests/e2e/testsuites/testsuites.go | 6 +- 17 files changed, 201 insertions(+), 189 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 12fe649fb4..4f2c1ed449 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -14,6 +14,17 @@ run: modules-download-mode: readonly +linters-settings: + revive: + rules: + # Using += 1 instead of ++ is fine + - name: increment-decrement + disabled: true + stylecheck: + # dot importing ginkgo and gomega is standard + dot-import-whitelist: + - "github.com/onsi/gomega" + - "github.com/onsi/ginkgo/v2" linters: enable: - asasalint @@ -74,13 +85,13 @@ linters: - spancheck - sqlclosecheck - staticcheck -# - stylecheck + - stylecheck - tagalign - tagliatelle - tenv - testableexamples - testifylint -# - thelper + - thelper - tparallel - unconvert - unparam @@ -89,8 +100,14 @@ linters: - wastedassign - whitespace - zerologlint + - goconst + - gochecknoinits + - perfsprint + - revive # TODO disable: - govet # We already run with `make verify/govet` + # We do not use today + - gomoddirectives # We need `replace` in `go.mod` - depguard # We don't use this today - cyclop # TODO FIXME (at least for non-tests) - dupl # TODO FIXME (tests + non-tests) @@ -101,19 +118,16 @@ linters: - mnd # Many false positives - gomnd # Many false positives - testpackage # We don't do this - - paralleltest # TODO FIXME - - gomoddirectives # We need `replace` in `go.mod` + - paralleltest # TODO FIXME (Too many tests that aren't parallelized right now) - funlen # Many false positives - gochecknoglobals # TODO FIXME - interfacebloat # False positives on AWS SDK interfaces - exhaustruct # TODO FIXME - - lll # TODO FIXME - nestif # Many false positives - gocognit # Many false positives - - cyclop # Many false positives + # Ask team + - lll # Limit line length - godot # TODO Ask team - - goconst # TODO FIXME - - gochecknoinits # TODO FIXME - godox # TODO FIXME - gofumpt # TODO Ask team - gosec # TODO Urgent not too bad @@ -121,5 +135,3 @@ linters: - maintidx # oof - nlreturn # Meh, ask team - nonamedreturns # meh ask team - - perfsprint # TODO FIXME - - revive # TODO diff --git a/cmd/main.go b/cmd/main.go index adfa4293fa..f0fe09abbf 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -132,9 +132,9 @@ func main() { }() } - if options.HttpEndpoint != "" { + if options.HTTPEndpoint != "" { r := metrics.InitializeRecorder() - r.InitializeMetricsHandler(options.HttpEndpoint, "/metrics", options.MetricsCertFile, options.MetricsKeyFile) + r.InitializeMetricsHandler(options.HTTPEndpoint, "/metrics", options.MetricsCertFile, options.MetricsKeyFile) } cfg := metadata.MetadataServiceConfig{ diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 52c868bd3a..965aaa7736 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -146,27 +146,27 @@ const ( var ( // ErrMultiDisks is an error that is returned when multiple // disks are found with the same volume name. - ErrMultiDisks = errors.New("Multiple disks with same name") + ErrMultiDisks = errors.New("multiple disks with same name") // ErrDiskExistsDiffSize is an error that is returned if a disk with a given // name, but different size, is found. - ErrDiskExistsDiffSize = errors.New("There is already a disk with same name and different size") + ErrDiskExistsDiffSize = errors.New("there is already a disk with same name and different size") // ErrNotFound is returned when a resource is not found. - ErrNotFound = errors.New("Resource was not found") + ErrNotFound = errors.New("resource was not found") - // ErrIdempotent is returned when another request with same idempotent token is in-flight. - ErrIdempotentParameterMismatch = errors.New("Parameters on this idempotent request are inconsistent with parameters used in previous request(s)") + // ErrIdempotentParameterMismatch is returned when another request with same idempotent token is in-flight. + ErrIdempotentParameterMismatch = errors.New("parameters on this idempotent request are inconsistent with parameters used in previous request(s)") // ErrAlreadyExists is returned when a resource is already existent. - ErrAlreadyExists = errors.New("Resource already exists") + ErrAlreadyExists = errors.New("resource already exists") // ErrMultiSnapshots is returned when multiple snapshots are found // with the same ID - ErrMultiSnapshots = errors.New("Multiple snapshots with the same name found") + ErrMultiSnapshots = errors.New("multiple snapshots with the same name found") // ErrInvalidMaxResults is returned when a MaxResults pagination parameter is between 1 and 4 - ErrInvalidMaxResults = errors.New("MaxResults parameter must be 0 or greater than or equal to 5") + ErrInvalidMaxResults = errors.New("maxResults parameter must be 0 or greater than or equal to 5") // ErrVolumeNotBeingModified is returned if volume being described is not being modified ErrVolumeNotBeingModified = fmt.Errorf("volume is not being modified") diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index 2cc609f513..1141ce9bc1 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -50,10 +50,10 @@ const ( defaultCreateDiskDeadline = time.Second * 5 ) -func generateVolumes(volIdCount, volTagCount int) []types.Volume { - volumes := make([]types.Volume, 0, volIdCount+volTagCount) +func generateVolumes(volIDCount, volTagCount int) []types.Volume { + volumes := make([]types.Volume, 0, volIDCount+volTagCount) - for i := range volIdCount { + for i := range volIDCount { volumeID := fmt.Sprintf("vol-%d", i) volumes = append(volumes, types.Volume{VolumeId: aws.String(volumeID)}) } @@ -306,13 +306,13 @@ func TestBatchDescribeInstances(t *testing.T) { t.Parallel() testCases := []struct { name string - instanceIds []string + instanceIDs []string mockFunc func(mockEC2 *MockEC2API, expErr error, reservations []types.Reservation) expErr error }{ { name: "success: instance by ID", - instanceIds: []string{"i-001", "i-002", "i-003"}, + instanceIDs: []string{"i-001", "i-002", "i-003"}, mockFunc: func(mockEC2 *MockEC2API, expErr error, reservations []types.Reservation) { reservationOutput := &ec2.DescribeInstancesOutput{Reservations: reservations} mockEC2.EXPECT().DescribeInstances(gomock.Any(), gomock.Any()).Return(reservationOutput, expErr).Times(1) @@ -321,7 +321,7 @@ func TestBatchDescribeInstances(t *testing.T) { }, { name: "fail: EC2 API generic error", - instanceIds: []string{"i-001", "i-002", "i-003"}, + instanceIDs: []string{"i-001", "i-002", "i-003"}, mockFunc: func(mockEC2 *MockEC2API, expErr error, reservations []types.Reservation) { mockEC2.EXPECT().DescribeInstances(gomock.Any(), gomock.Any()).Return(nil, expErr).Times(1) }, @@ -329,7 +329,7 @@ func TestBatchDescribeInstances(t *testing.T) { }, { name: "fail: invalid request", - instanceIds: []string{""}, + instanceIDs: []string{""}, mockFunc: func(mockEC2 *MockEC2API, expErr error, reservations []types.Reservation) { mockEC2.EXPECT().DescribeInstances(gomock.Any(), gomock.Any()).Return(nil, nil).Times(0) }, @@ -353,27 +353,27 @@ func TestBatchDescribeInstances(t *testing.T) { // Setup mocks var instances []types.Instance - for _, instanceId := range tc.instanceIds { - instances = append(instances, types.Instance{InstanceId: aws.String(instanceId)}) + for _, instanceID := range tc.instanceIDs { + instances = append(instances, types.Instance{InstanceId: aws.String(instanceID)}) } reservation := types.Reservation{Instances: instances} reservations := []types.Reservation{reservation} tc.mockFunc(mockEC2, tc.expErr, reservations) - executeDescribeInstancesTest(t, cloudInstance, tc.instanceIds, tc.expErr) + executeDescribeInstancesTest(t, cloudInstance, tc.instanceIDs, tc.expErr) }) } } -func executeDescribeInstancesTest(t *testing.T, c *cloud, instanceIds []string, expErr error) { +func executeDescribeInstancesTest(t *testing.T, c *cloud, instanceIDs []string, expErr error) { var wg sync.WaitGroup getRequestForID := func(id string) *ec2.DescribeInstancesInput { return &ec2.DescribeInstancesInput{InstanceIds: []string{id}} } - requests := make([]*ec2.DescribeInstancesInput, 0, len(instanceIds)) - for _, instanceID := range instanceIds { + requests := make([]*ec2.DescribeInstancesInput, 0, len(instanceIDs)) + for _, instanceID := range instanceIDs { requests = append(requests, getRequestForID(instanceID)) } @@ -605,14 +605,14 @@ func executeDescribeSnapshotsTest(t *testing.T, c *cloud, snapshotIDs, snapshotN func TestCheckDesiredState(t *testing.T) { testCases := []struct { name string - volumeId string + volumeID string desiredSizeGiB int32 options *ModifyDiskOptions expErr error }{ { name: "success: normal path", - volumeId: "vol-001", + volumeID: "vol-001", desiredSizeGiB: 5, options: &ModifyDiskOptions{ VolumeType: VolumeTypeGP2, @@ -622,7 +622,7 @@ func TestCheckDesiredState(t *testing.T) { }, { name: "failure: volume is still being expanded", - volumeId: "vol-001", + volumeID: "vol-001", desiredSizeGiB: 500, options: &ModifyDiskOptions{ VolumeType: VolumeTypeGP2, @@ -633,7 +633,7 @@ func TestCheckDesiredState(t *testing.T) { }, { name: "failure: volume is still being modified to iops", - volumeId: "vol-001", + volumeID: "vol-001", desiredSizeGiB: 50, options: &ModifyDiskOptions{ VolumeType: VolumeTypeGP2, @@ -644,7 +644,7 @@ func TestCheckDesiredState(t *testing.T) { }, { name: "failure: volume is still being modifed to type", - volumeId: "vol-001", + volumeID: "vol-001", desiredSizeGiB: 50, options: &ModifyDiskOptions{ VolumeType: VolumeTypeGP3, @@ -655,7 +655,7 @@ func TestCheckDesiredState(t *testing.T) { }, { name: "failure: volume is still being modified to throughput", - volumeId: "vol-001", + volumeID: "vol-001", desiredSizeGiB: 5, options: &ModifyDiskOptions{ VolumeType: VolumeTypeGP2, @@ -685,7 +685,7 @@ func TestCheckDesiredState(t *testing.T) { }, }, }, nil) - _, err := cloudInstance.checkDesiredState(context.Background(), tc.volumeId, tc.desiredSizeGiB, tc.options) + _, err := cloudInstance.checkDesiredState(context.Background(), tc.volumeID, tc.desiredSizeGiB, tc.options) if err != nil { if tc.expErr == nil { t.Fatalf("Did not expect to get an error but got %q", err) @@ -704,13 +704,13 @@ func TestBatchDescribeVolumesModifications(t *testing.T) { t.Parallel() testCases := []struct { name string - volumeIds []string + volumeIDs []string mockFunc func(mockEC2 *MockEC2API, expErr error, volumeModifications []types.VolumeModification) expErr error }{ { name: "success: volumeModification by ID", - volumeIds: []string{"vol-001", "vol-002", "vol-003"}, + volumeIDs: []string{"vol-001", "vol-002", "vol-003"}, mockFunc: func(mockEC2 *MockEC2API, expErr error, volumeModifications []types.VolumeModification) { volumeModificationsOutput := &ec2.DescribeVolumesModificationsOutput{VolumesModifications: volumeModifications} mockEC2.EXPECT().DescribeVolumesModifications(gomock.Any(), gomock.Any()).Return(volumeModificationsOutput, expErr).Times(1) @@ -719,7 +719,7 @@ func TestBatchDescribeVolumesModifications(t *testing.T) { }, { name: "fail: EC2 API generic error", - volumeIds: []string{"vol-001", "vol-002", "vol-003"}, + volumeIDs: []string{"vol-001", "vol-002", "vol-003"}, mockFunc: func(mockEC2 *MockEC2API, expErr error, volumeModifications []types.VolumeModification) { mockEC2.EXPECT().DescribeVolumesModifications(gomock.Any(), gomock.Any()).Return(nil, expErr).Times(1) }, @@ -727,7 +727,7 @@ func TestBatchDescribeVolumesModifications(t *testing.T) { }, { name: "fail: invalid request", - volumeIds: []string{""}, + volumeIDs: []string{""}, mockFunc: func(mockEC2 *MockEC2API, expErr error, volumeModifications []types.VolumeModification) { mockEC2.EXPECT().DescribeVolumesModifications(gomock.Any(), gomock.Any()).Return(nil, expErr).Times(0) }, @@ -751,26 +751,26 @@ func TestBatchDescribeVolumesModifications(t *testing.T) { // Setup mocks var volumeModifications []types.VolumeModification - for _, volumeId := range tc.volumeIds { - volumeModifications = append(volumeModifications, types.VolumeModification{VolumeId: aws.String(volumeId)}) + for _, volumeID := range tc.volumeIDs { + volumeModifications = append(volumeModifications, types.VolumeModification{VolumeId: aws.String(volumeID)}) } tc.mockFunc(mockEC2, tc.expErr, volumeModifications) - executeDescribeVolumesModificationsTest(t, cloudInstance, tc.volumeIds, tc.expErr) + executeDescribeVolumesModificationsTest(t, cloudInstance, tc.volumeIDs, tc.expErr) }) } } -func executeDescribeVolumesModificationsTest(t *testing.T, c *cloud, volumeIds []string, expErr error) { +func executeDescribeVolumesModificationsTest(t *testing.T, c *cloud, volumeIDs []string, expErr error) { var wg sync.WaitGroup getRequestForID := func(id string) *ec2.DescribeVolumesModificationsInput { return &ec2.DescribeVolumesModificationsInput{VolumeIds: []string{id}} } - requests := make([]*ec2.DescribeVolumesModificationsInput, 0, len(volumeIds)) - for _, volumeId := range volumeIds { - requests = append(requests, getRequestForID(volumeId)) + requests := make([]*ec2.DescribeVolumesModificationsInput, 0, len(volumeIDs)) + for _, volumeID := range volumeIDs { + requests = append(requests, getRequestForID(volumeID)) } r := make([]chan types.VolumeModification, len(requests)) @@ -1452,7 +1452,7 @@ func TestCreateDiskClientToken(t *testing.T) { t.Parallel() const volumeName = "test-vol-client-token" - const volumeId = "vol-abcd1234" + const volumeID = "vol-abcd1234" diskOptions := &DiskOptions{ CapacityBytes: util.GiBToBytes(1), Tags: map[string]string{VolumeNameTagKey: volumeName, AwsEbsDriverTagKey: "true"}, @@ -1485,14 +1485,14 @@ func TestCreateDiskClientToken(t *testing.T) { func(_ context.Context, input *ec2.CreateVolumeInput, _ ...func(*ec2.Options)) (*ec2.CreateVolumeOutput, error) { assert.Equal(t, expectedClientToken3, *input.ClientToken) return &ec2.CreateVolumeOutput{ - VolumeId: aws.String(volumeId), + VolumeId: aws.String(volumeID), Size: aws.Int32(util.BytesToGiB(diskOptions.CapacityBytes)), }, nil }), mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(&ec2.DescribeVolumesOutput{ Volumes: []types.Volume{ { - VolumeId: aws.String(volumeId), + VolumeId: aws.String(volumeID), Size: aws.Int32(util.BytesToGiB(diskOptions.CapacityBytes)), State: types.VolumeState("available"), AvailabilityZone: aws.String(diskOptions.AvailabilityZone), diff --git a/pkg/coalescer/coalescer_test.go b/pkg/coalescer/coalescer_test.go index 2e6fdafaa8..ffead66f84 100644 --- a/pkg/coalescer/coalescer_test.go +++ b/pkg/coalescer/coalescer_test.go @@ -24,8 +24,8 @@ import ( ) var ( - errFailedToMerge = fmt.Errorf("Failed to merge") - errFailedToExecute = fmt.Errorf("Failed to execute") + errFailedToMerge = fmt.Errorf("failed to merge") + errFailedToExecute = fmt.Errorf("failed to execute") ) // Merge function used to test the coalescer diff --git a/pkg/driver/controller_modify_volume.go b/pkg/driver/controller_modify_volume.go index e7ca742d02..1d9969d709 100644 --- a/pkg/driver/controller_modify_volume.go +++ b/pkg/driver/controller_modify_volume.go @@ -90,31 +90,31 @@ func newModifyVolumeCoalescer(c cloud.Cloud, o *Options) coalescer.Coalescer[mod func mergeModifyVolumeRequest(input modifyVolumeRequest, existing modifyVolumeRequest) (modifyVolumeRequest, error) { if input.newSize != 0 { if existing.newSize != 0 && input.newSize != existing.newSize { - return existing, fmt.Errorf("Different size was requested by a previous request. Current: %d, Requested: %d", existing.newSize, input.newSize) + return existing, fmt.Errorf("different size was requested by a previous request. Current: %d, Requested: %d", existing.newSize, input.newSize) } existing.newSize = input.newSize } if input.modifyDiskOptions.IOPS != 0 { if existing.modifyDiskOptions.IOPS != 0 && input.modifyDiskOptions.IOPS != existing.modifyDiskOptions.IOPS { - return existing, fmt.Errorf("Different IOPS was requested by a previous request. Current: %d, Requested: %d", existing.modifyDiskOptions.IOPS, input.modifyDiskOptions.IOPS) + return existing, fmt.Errorf("different IOPS was requested by a previous request. Current: %d, Requested: %d", existing.modifyDiskOptions.IOPS, input.modifyDiskOptions.IOPS) } existing.modifyDiskOptions.IOPS = input.modifyDiskOptions.IOPS } if input.modifyDiskOptions.Throughput != 0 { if existing.modifyDiskOptions.Throughput != 0 && input.modifyDiskOptions.Throughput != existing.modifyDiskOptions.Throughput { - return existing, fmt.Errorf("Different throughput was requested by a previous request. Current: %d, Requested: %d", existing.modifyDiskOptions.Throughput, input.modifyDiskOptions.Throughput) + return existing, fmt.Errorf("different throughput was requested by a previous request. Current: %d, Requested: %d", existing.modifyDiskOptions.Throughput, input.modifyDiskOptions.Throughput) } existing.modifyDiskOptions.Throughput = input.modifyDiskOptions.Throughput } if input.modifyDiskOptions.VolumeType != "" { if existing.modifyDiskOptions.VolumeType != "" && input.modifyDiskOptions.VolumeType != existing.modifyDiskOptions.VolumeType { - return existing, fmt.Errorf("Different volume type was requested by a previous request. Current: %s, Requested: %s", existing.modifyDiskOptions.VolumeType, input.modifyDiskOptions.VolumeType) + return existing, fmt.Errorf("different volume type was requested by a previous request. Current: %s, Requested: %s", existing.modifyDiskOptions.VolumeType, input.modifyDiskOptions.VolumeType) } existing.modifyDiskOptions.VolumeType = input.modifyDiskOptions.VolumeType } if len(input.modifyTagsOptions.TagsToAdd) > 0 || len(input.modifyTagsOptions.TagsToDelete) > 0 { if (len(existing.modifyTagsOptions.TagsToAdd) > 0 || len(existing.modifyTagsOptions.TagsToDelete) > 0) && !(reflect.DeepEqual(input.modifyTagsOptions, existing.modifyTagsOptions)) { - return existing, fmt.Errorf("Different tags were requested by a previous request. Current: %v, Requested: %v", existing.modifyTagsOptions, input.modifyTagsOptions) + return existing, fmt.Errorf("different tags were requested by a previous request. Current: %v, Requested: %v", existing.modifyTagsOptions, input.modifyTagsOptions) } existing.modifyTagsOptions = cloud.ModifyTagsOptions{ TagsToAdd: input.modifyTagsOptions.TagsToAdd, diff --git a/pkg/driver/controller_test.go b/pkg/driver/controller_test.go index d95e920164..b2dd5fc4b6 100644 --- a/pkg/driver/controller_test.go +++ b/pkg/driver/controller_test.go @@ -2699,7 +2699,7 @@ func TestCreateSnapshot(t *testing.T) { const ( snapshotName = "test-snapshot" nameTagValue = "test-name-tag-value" - clusterId = "test-cluster-id" + clusterID = "test-cluster-id" ) req := &csi.CreateSnapshotRequest{ @@ -2728,7 +2728,7 @@ func TestCreateSnapshot(t *testing.T) { cloud.SnapshotNameTagKey: snapshotName, cloud.AwsEbsDriverTagKey: isManagedByDriver, NameTag: nameTagValue, - ResourceLifecycleTagPrefix + clusterId: ResourceLifecycleOwned, + ResourceLifecycleTagPrefix + clusterID: ResourceLifecycleOwned, }, } @@ -2739,7 +2739,7 @@ func TestCreateSnapshot(t *testing.T) { awsDriver := ControllerService{ cloud: mockCloud, inFlight: internal.NewInFlight(), - options: &Options{KubernetesClusterID: clusterId}, + options: &Options{KubernetesClusterID: clusterID}, } resp, err := awsDriver.CreateSnapshot(context.Background(), req) if err != nil { @@ -3358,21 +3358,21 @@ func TestControllerPublishVolume(t *testing.T) { testCases := []struct { name string - volumeId string - nodeId string + volumeID string + nodeID string volumeCapability *csi.VolumeCapability - mockAttach func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) + mockAttach func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) expResp *csi.ControllerPublishVolumeResponse errorCode codes.Code setupFunc func(ControllerService *ControllerService) }{ { name: "AttachDisk successfully with valid volume ID, node ID, and volume capability", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, volumeCapability: stdVolCap, - mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { - mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), volumeId, gomock.Eq(nodeId)).Return(expDevicePath, nil) + mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) { + mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), volumeID, gomock.Eq(nodeID)).Return(expDevicePath, nil) }, expResp: &csi.ControllerPublishVolumeResponse{ PublishContext: map[string]string{DevicePathKey: expDevicePath}, @@ -3381,11 +3381,11 @@ func TestControllerPublishVolume(t *testing.T) { }, { name: "AttachDisk when volume is already attached to the node", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, volumeCapability: stdVolCap, - mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { - mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), gomock.Eq(volumeId), gomock.Eq(expInstanceID)).Return(expDevicePath, nil) + mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) { + mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), gomock.Eq(volumeID), gomock.Eq(expInstanceID)).Return(expDevicePath, nil) }, expResp: &csi.ControllerPublishVolumeResponse{ PublishContext: map[string]string{DevicePathKey: expDevicePath}, @@ -3395,28 +3395,28 @@ func TestControllerPublishVolume(t *testing.T) { { name: "Invalid argument error when no VolumeId provided", - volumeId: "", - nodeId: expInstanceID, + volumeID: "", + nodeID: expInstanceID, volumeCapability: stdVolCap, errorCode: codes.InvalidArgument, }, { name: "Invalid argument error when no NodeId provided", - volumeId: "vol-test", - nodeId: "", + volumeID: "vol-test", + nodeID: "", volumeCapability: stdVolCap, errorCode: codes.InvalidArgument, }, { name: "Invalid argument error when no VolumeCapability provided", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, errorCode: codes.InvalidArgument, }, { name: "Invalid argument error when invalid VolumeCapability provided", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, volumeCapability: &csi.VolumeCapability{ AccessMode: &csi.VolumeCapability_AccessMode{ Mode: csi.VolumeCapability_AccessMode_UNKNOWN, @@ -3426,40 +3426,40 @@ func TestControllerPublishVolume(t *testing.T) { }, { name: "Internal error when AttachDisk fails", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, volumeCapability: stdVolCap, - mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { - mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), gomock.Eq(volumeId), gomock.Eq(expInstanceID)).Return("", status.Error(codes.Internal, "test error")) + mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) { + mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), gomock.Eq(volumeID), gomock.Eq(expInstanceID)).Return("", status.Error(codes.Internal, "test error")) }, errorCode: codes.Internal, }, { name: "Fail when node does not exist", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, volumeCapability: stdVolCap, - mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { - mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), gomock.Eq(volumeId), gomock.Eq(nodeId)).Return("", status.Error(codes.Internal, "test error")) + mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) { + mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), gomock.Eq(volumeID), gomock.Eq(nodeID)).Return("", status.Error(codes.Internal, "test error")) }, errorCode: codes.Internal, }, { name: "Fail when volume does not exist", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, volumeCapability: stdVolCap, - mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { - mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), gomock.Eq(volumeId), gomock.Eq(expInstanceID)).Return("", status.Error(codes.Internal, "volume not found")) + mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) { + mockCloud.EXPECT().AttachDisk(gomock.Eq(ctx), gomock.Eq(volumeID), gomock.Eq(expInstanceID)).Return("", status.Error(codes.Internal, "volume not found")) }, errorCode: codes.Internal, }, { name: "Aborted error when AttachDisk operation already in-flight", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, volumeCapability: stdVolCap, - mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { + mockAttach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) { }, errorCode: codes.Aborted, setupFunc: func(ControllerService *ControllerService) { @@ -3471,9 +3471,9 @@ func TestControllerPublishVolume(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { req := &csi.ControllerPublishVolumeRequest{ - NodeId: tc.nodeId, + NodeId: tc.nodeID, VolumeCapability: tc.volumeCapability, - VolumeId: tc.volumeId, + VolumeId: tc.volumeID, } ctx := context.Background() @@ -3504,58 +3504,58 @@ func TestControllerPublishVolume(t *testing.T) { func TestControllerUnpublishVolume(t *testing.T) { testCases := []struct { name string - volumeId string - nodeId string + volumeID string + nodeID string errorCode codes.Code - mockDetach func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) + mockDetach func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) expResp *csi.ControllerUnpublishVolumeResponse setupFunc func(driver *ControllerService) }{ { name: "DetachDisk successfully with valid volume ID and node ID", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, errorCode: codes.OK, - mockDetach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { - mockCloud.EXPECT().DetachDisk(gomock.Eq(ctx), volumeId, nodeId).Return(nil) + mockDetach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) { + mockCloud.EXPECT().DetachDisk(gomock.Eq(ctx), volumeID, nodeID).Return(nil) }, expResp: &csi.ControllerUnpublishVolumeResponse{}, }, { name: "Return success when volume not found during DetachDisk operation", - volumeId: "vol-not-found", - nodeId: expInstanceID, + volumeID: "vol-not-found", + nodeID: expInstanceID, errorCode: codes.OK, - mockDetach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { - mockCloud.EXPECT().DetachDisk(gomock.Eq(ctx), volumeId, nodeId).Return(cloud.ErrNotFound) + mockDetach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) { + mockCloud.EXPECT().DetachDisk(gomock.Eq(ctx), volumeID, nodeID).Return(cloud.ErrNotFound) }, expResp: &csi.ControllerUnpublishVolumeResponse{}, }, { name: "Invalid argument error when no VolumeId provided", - volumeId: "", - nodeId: expInstanceID, + volumeID: "", + nodeID: expInstanceID, errorCode: codes.InvalidArgument, }, { name: "Invalid argument error when no NodeId provided", - volumeId: "vol-test", - nodeId: "", + volumeID: "vol-test", + nodeID: "", errorCode: codes.InvalidArgument, }, { name: "Internal error when DetachDisk operation fails", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, errorCode: codes.Internal, - mockDetach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeId string, nodeId string) { - mockCloud.EXPECT().DetachDisk(gomock.Eq(ctx), volumeId, nodeId).Return(errors.New("test error")) + mockDetach: func(mockCloud *cloud.MockCloud, ctx context.Context, volumeID string, nodeID string) { + mockCloud.EXPECT().DetachDisk(gomock.Eq(ctx), volumeID, nodeID).Return(errors.New("test error")) }, }, { name: "Aborted error when operation already in-flight", - volumeId: "vol-test", - nodeId: expInstanceID, + volumeID: "vol-test", + nodeID: expInstanceID, errorCode: codes.Aborted, setupFunc: func(driver *ControllerService) { driver.inFlight.Insert("vol-test" + expInstanceID) @@ -3566,8 +3566,8 @@ func TestControllerUnpublishVolume(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { req := &csi.ControllerUnpublishVolumeRequest{ - NodeId: tc.nodeId, - VolumeId: tc.volumeId, + NodeId: tc.nodeID, + VolumeId: tc.volumeID, } ctx := context.Background() diff --git a/pkg/driver/internal/inflight_test.go b/pkg/driver/internal/inflight_test.go index eaaeb9d6ed..748aea1622 100644 --- a/pkg/driver/internal/inflight_test.go +++ b/pkg/driver/internal/inflight_test.go @@ -21,7 +21,7 @@ import ( ) type testRequest struct { - volumeId string + volumeID string extra string expResp bool delete bool @@ -37,33 +37,33 @@ func TestInFlight(t *testing.T) { requests: []testRequest{ { - volumeId: "random-vol-name", + volumeID: "random-vol-name", expResp: true, }, }, }, { - name: "success adding request with different volumeId", + name: "success adding request with different volumeID", requests: []testRequest{ { - volumeId: "random-vol-foobar", + volumeID: "random-vol-foobar", expResp: true, }, { - volumeId: "random-vol-name-foobar", + volumeID: "random-vol-name-foobar", expResp: true, }, }, }, { - name: "failed adding request with same volumeId", + name: "failed adding request with same volumeID", requests: []testRequest{ { - volumeId: "random-vol-name-foobar", + volumeID: "random-vol-name-foobar", expResp: true, }, { - volumeId: "random-vol-name-foobar", + volumeID: "random-vol-name-foobar", expResp: false, }, }, @@ -72,18 +72,18 @@ func TestInFlight(t *testing.T) { name: "success add, delete, add copy", requests: []testRequest{ { - volumeId: "random-vol-name", + volumeID: "random-vol-name", extra: "random-node-id", expResp: true, }, { - volumeId: "random-vol-name", + volumeID: "random-vol-name", extra: "random-node-id", expResp: false, delete: true, }, { - volumeId: "random-vol-name", + volumeID: "random-vol-name", extra: "random-node-id", expResp: true, }, @@ -97,9 +97,9 @@ func TestInFlight(t *testing.T) { for _, r := range tc.requests { var resp bool if r.delete { - db.Delete(r.volumeId) + db.Delete(r.volumeID) } else { - resp = db.Insert(r.volumeId) + resp = db.Insert(r.volumeID) } if r.expResp != resp { t.Fatalf("expected insert to be %+v, got %+v", r.expResp, resp) diff --git a/pkg/driver/node_test.go b/pkg/driver/node_test.go index 60ee7a0931..45fbeb26fa 100644 --- a/pkg/driver/node_test.go +++ b/pkg/driver/node_test.go @@ -2355,7 +2355,7 @@ func TestNodeExpandVolume(t *testing.T) { func TestNodeGetVolumeStats(t *testing.T) { testCases := []struct { name string - validVolId bool + validVolID bool validPath bool metricsStatErr bool mounterMock func(mockCtl *gomock.Controller, dir string) *mounter.MockMounter @@ -2363,7 +2363,7 @@ func TestNodeGetVolumeStats(t *testing.T) { }{ { name: "success normal", - validVolId: true, + validVolID: true, validPath: true, mounterMock: func(ctrl *gomock.Controller, dir string) *mounter.MockMounter { m := mounter.NewMockMounter(ctrl) @@ -2377,14 +2377,14 @@ func TestNodeGetVolumeStats(t *testing.T) { }, { name: "invalid_volume_id", - validVolId: false, + validVolID: false, expectedErr: func(dir string) error { return status.Error(codes.InvalidArgument, "NodeGetVolumeStats volume ID was empty") }, }, { name: "invalid_volume_path", - validVolId: true, + validVolID: true, validPath: false, expectedErr: func(dir string) error { return status.Error(codes.InvalidArgument, "NodeGetVolumeStats volume path was empty") @@ -2392,7 +2392,7 @@ func TestNodeGetVolumeStats(t *testing.T) { }, { name: "path_exists_error", - validVolId: true, + validVolID: true, validPath: true, mounterMock: func(ctrl *gomock.Controller, dir string) *mounter.MockMounter { m := mounter.NewMockMounter(ctrl) @@ -2405,7 +2405,7 @@ func TestNodeGetVolumeStats(t *testing.T) { }, { name: "path_does_not_exist", - validVolId: true, + validVolID: true, validPath: true, mounterMock: func(ctrl *gomock.Controller, dir string) *mounter.MockMounter { m := mounter.NewMockMounter(ctrl) @@ -2418,7 +2418,7 @@ func TestNodeGetVolumeStats(t *testing.T) { }, { name: "is_block_device_error", - validVolId: true, + validVolID: true, validPath: true, mounterMock: func(ctrl *gomock.Controller, dir string) *mounter.MockMounter { m := mounter.NewMockMounter(ctrl) @@ -2432,7 +2432,7 @@ func TestNodeGetVolumeStats(t *testing.T) { }, { name: "get_block_size_bytes_error", - validVolId: true, + validVolID: true, validPath: true, mounterMock: func(ctrl *gomock.Controller, dir string) *mounter.MockMounter { m := mounter.NewMockMounter(ctrl) @@ -2447,7 +2447,7 @@ func TestNodeGetVolumeStats(t *testing.T) { }, { name: "success block device", - validVolId: true, + validVolID: true, validPath: true, mounterMock: func(ctrl *gomock.Controller, dir string) *mounter.MockMounter { m := mounter.NewMockMounter(ctrl) @@ -2481,7 +2481,7 @@ func TestNodeGetVolumeStats(t *testing.T) { } req := &csi.NodeGetVolumeStatsRequest{} - if tc.validVolId { + if tc.validVolID { req.VolumeId = "vol-test" } if tc.validPath { diff --git a/pkg/driver/options.go b/pkg/driver/options.go index 937ac68b0d..c08b65b29e 100644 --- a/pkg/driver/options.go +++ b/pkg/driver/options.go @@ -36,8 +36,8 @@ type Options struct { // Endpoint is the endpoint for the CSI driver server Endpoint string - // HttpEndpoint is the TCP network address where the HTTP server for metrics will listen - HttpEndpoint string + // HTTPEndpoint is the TCP network address where the HTTP server for metrics will listen + HTTPEndpoint string // MetricsCertFile is the location of the certificate for serving the metrics server over HTTPS MetricsCertFile string // MetricsKeyFile is the location of the key for serving the metrics server over HTTPS @@ -96,7 +96,7 @@ func (o *Options) AddFlags(f *flag.FlagSet) { // Server options f.StringVar(&o.Endpoint, "endpoint", DefaultCSIEndpoint, "Endpoint for the CSI driver server") - f.StringVar(&o.HttpEndpoint, "http-endpoint", "", "The TCP network address where the HTTP server for metrics will listen (example: `:8080`). The default is empty string, which means the server is disabled.") + f.StringVar(&o.HTTPEndpoint, "http-endpoint", "", "The TCP network address where the HTTP server for metrics will listen (example: `:8080`). The default is empty string, which means the server is disabled.") f.StringVar(&o.MetricsCertFile, "metrics-cert-file", "", "The path to a certificate to use for serving the metrics server over HTTPS. If the certificate is signed by a certificate authority, this file should be the concatenation of the server's certificate, any intermediates, and the CA's certificate. If this is non-empty, --http-endpoint and --metrics-key-file MUST also be non-empty.") f.StringVar(&o.MetricsKeyFile, "metrics-key-file", "", "The path to a key to use for serving the metrics server over HTTPS. If this is non-empty, --http-endpoint and --metrics-cert-file MUST also be non-empty.") f.BoolVar(&o.EnableOtelTracing, "enable-otel-tracing", false, "To enable opentelemetry tracing for the driver. The tracing is disabled by default. Configure the exporter endpoint with OTEL_EXPORTER_OTLP_ENDPOINT and other env variables, see https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#general-sdk-configuration.") @@ -130,7 +130,7 @@ func (o *Options) Validate() error { if o.MetricsCertFile != "" || o.MetricsKeyFile != "" { switch { - case o.HttpEndpoint == "": + case o.HTTPEndpoint == "": return fmt.Errorf("--http-endpoint MUST be specififed when using the metrics server with HTTPS") case o.MetricsCertFile == "": return fmt.Errorf("--metrics-cert-file MUST be specififed when using the metrics server with HTTPS") diff --git a/pkg/driver/options_test.go b/pkg/driver/options_test.go index 38944b7c9f..0cb8271c1f 100644 --- a/pkg/driver/options_test.go +++ b/pkg/driver/options_test.go @@ -77,8 +77,8 @@ func TestAddFlags(t *testing.T) { if o.Endpoint != "custom-endpoint" { t.Errorf("unexpected Endpoint: got %s, want custom-endpoint", o.Endpoint) } - if o.HttpEndpoint != ":8080" { - t.Errorf("unexpected HttpEndpoint: got %s, want :8080", o.HttpEndpoint) + if o.HTTPEndpoint != ":8080" { + t.Errorf("unexpected HTTPEndpoint: got %s, want :8080", o.HTTPEndpoint) } if !o.EnableOtelTracing { t.Error("unexpected EnableOtelTracing: got false, want true") @@ -215,7 +215,7 @@ func TestValidateMetricsHTTPS(t *testing.T) { t.Run(tt.name, func(t *testing.T) { o := &Options{ Mode: ControllerMode, - HttpEndpoint: tt.httpEndpoint, + HTTPEndpoint: tt.httpEndpoint, MetricsCertFile: tt.metricsCertFile, MetricsKeyFile: tt.metricsKeyFile, } diff --git a/pkg/driver/validation.go b/pkg/driver/validation.go index 456cbc2dd5..e28d6c2db7 100644 --- a/pkg/driver/validation.go +++ b/pkg/driver/validation.go @@ -27,15 +27,15 @@ import ( func ValidateDriverOptions(options *Options) error { if err := validateExtraTags(options.ExtraTags, false); err != nil { - return fmt.Errorf("Invalid extra tags: %w", err) + return fmt.Errorf("invalid extra tags: %w", err) } if err := validateMode(options.Mode); err != nil { - return fmt.Errorf("Invalid mode: %w", err) + return fmt.Errorf("invalid mode: %w", err) } if options.ModifyVolumeRequestHandlerTimeout == 0 && (options.Mode == ControllerMode || options.Mode == AllMode) { - return errors.New("Invalid modifyVolumeRequestHandlerTimeout: Timeout cannot be zero") + return errors.New("invalid modifyVolumeRequestHandlerTimeout: timeout cannot be zero") } return nil @@ -43,32 +43,32 @@ func ValidateDriverOptions(options *Options) error { func validateExtraTags(tags map[string]string, warnOnly bool) error { if len(tags) > cloud.MaxNumTagsPerResource { - return fmt.Errorf("Too many tags (actual: %d, limit: %d)", len(tags), cloud.MaxNumTagsPerResource) + return fmt.Errorf("too many tags (actual: %d, limit: %d)", len(tags), cloud.MaxNumTagsPerResource) } validate := func(k, v string) error { if len(k) > cloud.MaxTagKeyLength { - return fmt.Errorf("Tag key too long (actual: %d, limit: %d)", len(k), cloud.MaxTagKeyLength) + return fmt.Errorf("tag key too long (actual: %d, limit: %d)", len(k), cloud.MaxTagKeyLength) } else if len(k) < cloud.MinTagKeyLength { - return fmt.Errorf("Tag key cannot be empty (min: 1)") + return fmt.Errorf("tag key cannot be empty (min: 1)") } if len(v) > cloud.MaxTagValueLength { - return fmt.Errorf("Tag value too long (actual: %d, limit: %d)", len(v), cloud.MaxTagValueLength) + return fmt.Errorf("tag value too long (actual: %d, limit: %d)", len(v), cloud.MaxTagValueLength) } if k == cloud.VolumeNameTagKey { - return fmt.Errorf("Tag key '%s' is reserved", cloud.VolumeNameTagKey) + return fmt.Errorf("tag key '%s' is reserved", cloud.VolumeNameTagKey) } if k == cloud.AwsEbsDriverTagKey { - return fmt.Errorf("Tag key '%s' is reserved", cloud.AwsEbsDriverTagKey) + return fmt.Errorf("tag key '%s' is reserved", cloud.AwsEbsDriverTagKey) } if k == cloud.SnapshotNameTagKey { - return fmt.Errorf("Tag key '%s' is reserved", cloud.SnapshotNameTagKey) + return fmt.Errorf("tag key '%s' is reserved", cloud.SnapshotNameTagKey) } if strings.HasPrefix(k, cloud.KubernetesTagKeyPrefix) { - return fmt.Errorf("Tag key prefix '%s' is reserved", cloud.KubernetesTagKeyPrefix) + return fmt.Errorf("tag key prefix '%s' is reserved", cloud.KubernetesTagKeyPrefix) } if strings.HasPrefix(k, cloud.AWSTagKeyPrefix) { - return fmt.Errorf("Tag key prefix '%s' is reserved", cloud.AWSTagKeyPrefix) + return fmt.Errorf("tag key prefix '%s' is reserved", cloud.AWSTagKeyPrefix) } return nil } @@ -88,7 +88,7 @@ func validateExtraTags(tags map[string]string, warnOnly bool) error { func validateMode(mode Mode) error { if mode != AllMode && mode != ControllerMode && mode != NodeMode { - return fmt.Errorf("Mode is not supported (actual: %s, supported: %v)", mode, []Mode{AllMode, ControllerMode, NodeMode}) + return fmt.Errorf("mode is not supported (actual: %s, supported: %v)", mode, []Mode{AllMode, ControllerMode, NodeMode}) } return nil diff --git a/pkg/driver/validation_test.go b/pkg/driver/validation_test.go index 056266ee41..55d87ad4cc 100644 --- a/pkg/driver/validation_test.go +++ b/pkg/driver/validation_test.go @@ -64,61 +64,61 @@ func TestValidateExtraTags(t *testing.T) { tags: map[string]string{ randomString(cloud.MaxTagKeyLength + 1): "extra-tag-value", }, - expErr: fmt.Errorf("Tag key too long (actual: %d, limit: %d)", cloud.MaxTagKeyLength+1, cloud.MaxTagKeyLength), + expErr: fmt.Errorf("tag key too long (actual: %d, limit: %d)", cloud.MaxTagKeyLength+1, cloud.MaxTagKeyLength), }, { name: "invaid tag: key is empty", tags: map[string]string{ "": "extra-tag-value", }, - expErr: fmt.Errorf("Tag key cannot be empty (min: 1)"), + expErr: fmt.Errorf("tag key cannot be empty (min: 1)"), }, { name: "invalid tag: value too long", tags: map[string]string{ "extra-tag-key": randomString(cloud.MaxTagValueLength + 1), }, - expErr: fmt.Errorf("Tag value too long (actual: %d, limit: %d)", cloud.MaxTagValueLength+1, cloud.MaxTagValueLength), + expErr: fmt.Errorf("tag value too long (actual: %d, limit: %d)", cloud.MaxTagValueLength+1, cloud.MaxTagValueLength), }, { name: "invalid tag: reserved CSI key", tags: map[string]string{ cloud.VolumeNameTagKey: "extra-tag-value", }, - expErr: fmt.Errorf("Tag key '%s' is reserved", cloud.VolumeNameTagKey), + expErr: fmt.Errorf("tag key '%s' is reserved", cloud.VolumeNameTagKey), }, { name: "invalid tag: reserved driver key", tags: map[string]string{ cloud.AwsEbsDriverTagKey: "false", }, - expErr: fmt.Errorf("Tag key '%s' is reserved", cloud.AwsEbsDriverTagKey), + expErr: fmt.Errorf("tag key '%s' is reserved", cloud.AwsEbsDriverTagKey), }, { name: "invaid tag: reserved snapshot key", tags: map[string]string{ cloud.SnapshotNameTagKey: "false", }, - expErr: fmt.Errorf("Tag key '%s' is reserved", cloud.SnapshotNameTagKey), + expErr: fmt.Errorf("tag key '%s' is reserved", cloud.SnapshotNameTagKey), }, { name: "invalid tag: reserved Kubernetes key prefix", tags: map[string]string{ cloud.KubernetesTagKeyPrefix + "/cluster": "extra-tag-value", }, - expErr: fmt.Errorf("Tag key prefix '%s' is reserved", cloud.KubernetesTagKeyPrefix), + expErr: fmt.Errorf("tag key prefix '%s' is reserved", cloud.KubernetesTagKeyPrefix), }, { name: "invalid tag: reserved AWS key prefix", tags: map[string]string{ cloud.AWSTagKeyPrefix + "foo": "extra-tag-value", }, - expErr: fmt.Errorf("Tag key prefix '%s' is reserved", cloud.AWSTagKeyPrefix), + expErr: fmt.Errorf("tag key prefix '%s' is reserved", cloud.AWSTagKeyPrefix), }, { name: "invalid tag: too many tags", tags: randomStringMap(cloud.MaxNumTagsPerResource + 1), - expErr: fmt.Errorf("Too many tags (actual: %d, limit: %d)", cloud.MaxNumTagsPerResource+1, cloud.MaxNumTagsPerResource), + expErr: fmt.Errorf("too many tags (actual: %d, limit: %d)", cloud.MaxNumTagsPerResource+1, cloud.MaxNumTagsPerResource), }, } @@ -156,7 +156,7 @@ func TestValidateMode(t *testing.T) { { name: "invalid mode: unknown", mode: Mode("unknown"), - expErr: fmt.Errorf("Mode is not supported (actual: unknown, supported: %v)", []Mode{AllMode, ControllerMode, NodeMode}), + expErr: fmt.Errorf("mode is not supported (actual: unknown, supported: %v)", []Mode{AllMode, ControllerMode, NodeMode}), }, } @@ -188,7 +188,7 @@ func TestValidateDriverOptions(t *testing.T) { name: "fail because validateMode fails", mode: Mode("unknown"), modifyVolumeTimeout: 5 * time.Second, - expErr: fmt.Errorf("Invalid mode: %w", fmt.Errorf("Mode is not supported (actual: unknown, supported: %v)", []Mode{AllMode, ControllerMode, NodeMode})), + expErr: fmt.Errorf("invalid mode: %w", fmt.Errorf("mode is not supported (actual: unknown, supported: %v)", []Mode{AllMode, ControllerMode, NodeMode})), }, { name: "fail because validateExtraVolumeTags fails", @@ -197,13 +197,13 @@ func TestValidateDriverOptions(t *testing.T) { randomString(cloud.MaxTagKeyLength + 1): "extra-tag-value", }, modifyVolumeTimeout: 5 * time.Second, - expErr: fmt.Errorf("Invalid extra tags: %w", fmt.Errorf("Tag key too long (actual: %d, limit: %d)", cloud.MaxTagKeyLength+1, cloud.MaxTagKeyLength)), + expErr: fmt.Errorf("invalid extra tags: %w", fmt.Errorf("tag key too long (actual: %d, limit: %d)", cloud.MaxTagKeyLength+1, cloud.MaxTagKeyLength)), }, { name: "fail because modifyVolumeRequestHandlerTimeout is zero", mode: AllMode, modifyVolumeTimeout: 0, - expErr: errors.New("Invalid modifyVolumeRequestHandlerTimeout: Timeout cannot be zero"), + expErr: errors.New("invalid modifyVolumeRequestHandlerTimeout: timeout cannot be zero"), }, } diff --git a/tests/e2e/testsuites/e2e_utils.go b/tests/e2e/testsuites/e2e_utils.go index 31129eb624..bb733acc6f 100644 --- a/tests/e2e/testsuites/e2e_utils.go +++ b/tests/e2e/testsuites/e2e_utils.go @@ -37,7 +37,7 @@ const ( DefaultModificationTimeout = 3 * time.Minute DefaultResizeTimout = 1 * time.Minute - DefaultK8sApiPollingInterval = 5 * time.Second + DefaultK8sAPIPollingInterval = 5 * time.Second Iops = "iops" Throughput = "throughput" @@ -105,7 +105,7 @@ func ResizeTestPvc(client clientset.Interface, namespace *v1.Namespace, testPvc updatedSize = updatedPvc.Spec.Resources.Requests["storage"] framework.Logf("checking the resizing PV result") - err = WaitForPvToResize(client, namespace, updatedPvc.Spec.VolumeName, updatedSize, DefaultResizeTimout, DefaultK8sApiPollingInterval) + err = WaitForPvToResize(client, namespace, updatedPvc.Spec.VolumeName, updatedSize, DefaultResizeTimout, DefaultK8sAPIPollingInterval) framework.ExpectNoError(err) return updatedSize } diff --git a/tests/e2e/testsuites/modify_volume_tester.go b/tests/e2e/testsuites/modify_volume_tester.go index 63454d0aaf..38ee37461b 100644 --- a/tests/e2e/testsuites/modify_volume_tester.go +++ b/tests/e2e/testsuites/modify_volume_tester.go @@ -102,13 +102,13 @@ func (modifyVolumeTest *ModifyVolumeTest) Run(c clientset.Interface, ns *v1.Name By("wait for and confirm pv modification") if testType == VolumeModifierForK8s { - err = WaitForPvToModify(c, ns, testVolume.persistentVolume.Name, parametersWithPrefix, DefaultModificationTimeout, DefaultK8sApiPollingInterval) + err = WaitForPvToModify(c, ns, testVolume.persistentVolume.Name, parametersWithPrefix, DefaultModificationTimeout, DefaultK8sAPIPollingInterval) } else if testType == ExternalResizer { - err = WaitForVacToApplyToPv(c, ns, testVolume.persistentVolume.Name, *modifyingPvc.Spec.VolumeAttributesClassName, DefaultModificationTimeout, DefaultK8sApiPollingInterval) + err = WaitForVacToApplyToPv(c, ns, testVolume.persistentVolume.Name, *modifyingPvc.Spec.VolumeAttributesClassName, DefaultModificationTimeout, DefaultK8sAPIPollingInterval) } framework.ExpectNoError(err, fmt.Sprintf("fail to modify pv(%s): %v", modifyingPvc.Name, err)) if modifyVolumeTest.ShouldResizeVolume { - err = WaitForPvToResize(c, ns, testVolume.persistentVolume.Name, updatedPvcSize, DefaultResizeTimout, DefaultK8sApiPollingInterval) + err = WaitForPvToResize(c, ns, testVolume.persistentVolume.Name, updatedPvcSize, DefaultResizeTimout, DefaultK8sAPIPollingInterval) framework.ExpectNoError(err, fmt.Sprintf("fail to resize pv(%s): %v", modifyingPvc.Name, err)) } } diff --git a/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go b/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go index c88d7f823a..0bee115688 100644 --- a/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go @@ -31,12 +31,12 @@ type PreProvisionedVolumeSnapshotTest struct { Pod PodDetails } -func (t *PreProvisionedVolumeSnapshotTest) Run(client clientset.Interface, restclient k8srestclient.Interface, namespace *v1.Namespace, snapshotId string) { +func (t *PreProvisionedVolumeSnapshotTest) Run(client clientset.Interface, restclient k8srestclient.Interface, namespace *v1.Namespace, snapshotID string) { By("taking snapshots") tvsc, cleanup := CreateVolumeSnapshotClass(restclient, namespace, t.CSIDriver, nil) defer cleanup() - tvolumeSnapshotContent := tvsc.CreateStaticVolumeSnapshotContent(snapshotId) + tvolumeSnapshotContent := tvsc.CreateStaticVolumeSnapshotContent(snapshotID) tvs := tvsc.CreateStaticVolumeSnapshot(tvolumeSnapshotContent) defer tvsc.DeleteVolumeSnapshotContent(tvolumeSnapshotContent) diff --git a/tests/e2e/testsuites/testsuites.go b/tests/e2e/testsuites/testsuites.go index 2176f824f1..f67c7e61e1 100644 --- a/tests/e2e/testsuites/testsuites.go +++ b/tests/e2e/testsuites/testsuites.go @@ -150,8 +150,8 @@ func (t *TestVolumeSnapshotClass) CreateStaticVolumeSnapshot(vsc *volumesnapshot return snapshotObj } -func (t *TestVolumeSnapshotClass) CreateStaticVolumeSnapshotContent(snapshotId string) *volumesnapshotv1.VolumeSnapshotContent { - By("creating a VolumeSnapshotContent from snapshotId: " + snapshotId) +func (t *TestVolumeSnapshotClass) CreateStaticVolumeSnapshotContent(snapshotID string) *volumesnapshotv1.VolumeSnapshotContent { + By("creating a VolumeSnapshotContent from snapshotID: " + snapshotID) snapshotContent := &volumesnapshotv1.VolumeSnapshotContent{ TypeMeta: metav1.TypeMeta{ Kind: VolumeSnapshotContentKind, @@ -171,7 +171,7 @@ func (t *TestVolumeSnapshotClass) CreateStaticVolumeSnapshotContent(snapshotId s }, Driver: "ebs.csi.aws.com", Source: volumesnapshotv1.VolumeSnapshotContentSource{ - SnapshotHandle: aws.String(snapshotId), + SnapshotHandle: aws.String(snapshotID), }, }, } From 4b723c63c4b388bb6f362744f6e738f0630af2eb Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:56:39 -0400 Subject: [PATCH 18/24] Ensure test helper functions start from t.Helper() --- pkg/cloud/cloud_test.go | 9 ++++ pkg/cloud/devicemanager/manager_test.go | 1 + pkg/driver/controller_test.go | 62 +++++++++++++++++++++++++ pkg/driver/node_test.go | 7 +++ pkg/driver/request_coalescing_test.go | 7 +++ 5 files changed, 86 insertions(+) diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index 1141ce9bc1..993453e786 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -238,6 +238,7 @@ func TestBatchDescribeVolumes(t *testing.T) { } } func executeDescribeVolumesTest(t *testing.T, c *cloud, volumeIDs, volumeNames []string, expErr error) { + t.Helper() var wg sync.WaitGroup getRequestForID := func(id string) *ec2.DescribeVolumesInput { @@ -366,6 +367,7 @@ func TestBatchDescribeInstances(t *testing.T) { } func executeDescribeInstancesTest(t *testing.T, c *cloud, instanceIDs []string, expErr error) { + t.Helper() var wg sync.WaitGroup getRequestForID := func(id string) *ec2.DescribeInstancesInput { @@ -537,6 +539,7 @@ func TestBatchDescribeSnapshots(t *testing.T) { } func executeDescribeSnapshotsTest(t *testing.T, c *cloud, snapshotIDs, snapshotNames []string, expErr error) { + t.Helper() var wg sync.WaitGroup getRequestForID := func(id string) *ec2.DescribeSnapshotsInput { @@ -762,6 +765,7 @@ func TestBatchDescribeVolumesModifications(t *testing.T) { } func executeDescribeVolumesModificationsTest(t *testing.T, c *cloud, volumeIDs []string, expErr error) { + t.Helper() var wg sync.WaitGroup getRequestForID := func(id string) *ec2.DescribeVolumesModificationsInput { @@ -2970,6 +2974,7 @@ func TestListSnapshots(t *testing.T) { { name: "success: normal", testFunc: func(t *testing.T) { + t.Helper() expSnapshots := []*Snapshot{ { SourceVolumeID: "snap-test-volume1", @@ -3043,6 +3048,7 @@ func TestListSnapshots(t *testing.T) { { name: "success: with volume ID", testFunc: func(t *testing.T) { + t.Helper() sourceVolumeID := "snap-test-volume" expSnapshots := []*Snapshot{ { @@ -3117,6 +3123,7 @@ func TestListSnapshots(t *testing.T) { { name: "success: max results, next token", testFunc: func(t *testing.T) { + t.Helper() maxResults := 5 nextTokenValue := "nextTokenValue" var expSnapshots []*Snapshot @@ -3189,6 +3196,7 @@ func TestListSnapshots(t *testing.T) { { name: "fail: AWS DescribeSnapshots error", testFunc: func(t *testing.T) { + t.Helper() mockCtl := gomock.NewController(t) defer mockCtl.Finish() mockEC2 := NewMockEC2API(mockCtl) @@ -3206,6 +3214,7 @@ func TestListSnapshots(t *testing.T) { { name: "fail: no snapshots ErrNotFound", testFunc: func(t *testing.T) { + t.Helper() mockCtl := gomock.NewController(t) defer mockCtl.Finish() mockEC2 := NewMockEC2API(mockCtl) diff --git a/pkg/cloud/devicemanager/manager_test.go b/pkg/cloud/devicemanager/manager_test.go index 1504d319a5..859da2f68f 100644 --- a/pkg/cloud/devicemanager/manager_test.go +++ b/pkg/cloud/devicemanager/manager_test.go @@ -239,6 +239,7 @@ func newFakeInstance(instanceID, volumeID, devicePath string) *types.Instance { } func assertDevice(t *testing.T, d *Device, assigned bool, err error) { + t.Helper() if err != nil { t.Fatalf("Expected no error, got %v", err) } diff --git a/pkg/driver/controller_test.go b/pkg/driver/controller_test.go index b2dd5fc4b6..e390cb25c3 100644 --- a/pkg/driver/controller_test.go +++ b/pkg/driver/controller_test.go @@ -98,6 +98,7 @@ func TestCreateVolume(t *testing.T) { { name: "success normal", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "random-vol-name", CapacityRange: stdCapRange, @@ -137,6 +138,7 @@ func TestCreateVolume(t *testing.T) { { name: "success outposts", testFunc: func(t *testing.T) { + t.Helper() outpostArn := strippedOutpostArn req := &csi.CreateVolumeRequest{ Name: "test-vol", @@ -227,6 +229,7 @@ func TestCreateVolume(t *testing.T) { { name: "restore snapshot", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "random-vol-name", CapacityRange: stdCapRange, @@ -283,6 +286,7 @@ func TestCreateVolume(t *testing.T) { { name: "restore snapshot, volume already exists", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "random-vol-name", CapacityRange: stdCapRange, @@ -339,6 +343,7 @@ func TestCreateVolume(t *testing.T) { { name: "restore snapshot, volume already exists with different snapshot ID", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "random-vol-name", CapacityRange: stdCapRange, @@ -374,6 +379,7 @@ func TestCreateVolume(t *testing.T) { { name: "fail no name", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "", CapacityRange: stdCapRange, @@ -410,6 +416,7 @@ func TestCreateVolume(t *testing.T) { { name: "success same name and same capacity", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "test-vol", CapacityRange: stdCapRange, @@ -497,6 +504,7 @@ func TestCreateVolume(t *testing.T) { { name: "fail same name and different capacity", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "test-vol", CapacityRange: stdCapRange, @@ -561,6 +569,7 @@ func TestCreateVolume(t *testing.T) { { name: "success no capacity range", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "test-vol", VolumeCapabilities: stdVolCap, @@ -621,6 +630,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with correct round up", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: &csi.CapacityRange{RequiredBytes: 1073741825}, @@ -675,6 +685,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with volume type gp3", testFunc: func(t *testing.T) { + t.Helper() // iops 5000 requires at least 10GB volSize := int64(20 * 1024 * 1024 * 1024) capRange := &csi.CapacityRange{RequiredBytes: volSize} @@ -721,6 +732,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with volume type io1 using iopsPerGB", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -763,6 +775,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with volume type io1 using iops", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -805,6 +818,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with volume type io2 using iopsPerGB", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -847,6 +861,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with volume type io2 using iops", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -889,6 +904,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with volume type sc1", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -930,6 +946,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with volume type standard", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -971,6 +988,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with volume encryption", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -1012,6 +1030,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with volume encryption with KMS key", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -1054,6 +1073,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with mutable parameters", testFunc: func(t *testing.T) { + t.Helper() volSize := int64(20 * 1024 * 1024 * 1024) capRange := &csi.CapacityRange{RequiredBytes: volSize} req := &csi.CreateVolumeRequest{ @@ -1103,6 +1123,7 @@ func TestCreateVolume(t *testing.T) { { name: "fail with invalid volume parameter", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -1144,6 +1165,7 @@ func TestCreateVolume(t *testing.T) { { name: "fail with invalid iops parameter", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -1184,6 +1206,7 @@ func TestCreateVolume(t *testing.T) { { name: "fail with invalid throughput parameter", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -1224,6 +1247,7 @@ func TestCreateVolume(t *testing.T) { { name: "success when volume exists and contains VolumeContext and AccessibleTopology", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "test-vol", CapacityRange: stdCapRange, @@ -1321,6 +1345,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with extra tags", testFunc: func(t *testing.T) { + t.Helper() const ( volumeName = "random-vol-name" extraVolumeTagKey = "extra-tag-key" @@ -1379,6 +1404,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with cluster-id", testFunc: func(t *testing.T) { + t.Helper() const ( volumeName = "random-vol-name" clusterID = "test-cluster-id" @@ -1442,6 +1468,7 @@ func TestCreateVolume(t *testing.T) { { name: "success with legacy tags", testFunc: func(t *testing.T) { + t.Helper() const ( volumeName = "random-vol-name" expectedPVCNameTag = "kubernetes.io/created-for/pvc/name" @@ -1506,6 +1533,7 @@ func TestCreateVolume(t *testing.T) { { name: "fail with invalid volume access modes", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -1547,6 +1575,7 @@ func TestCreateVolume(t *testing.T) { { name: "fail with in-flight request", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "random-vol-name", CapacityRange: stdCapRange, @@ -1579,6 +1608,7 @@ func TestCreateVolume(t *testing.T) { { name: "Fail with IdempotentParameterMismatch error", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "vol-test", CapacityRange: stdCapRange, @@ -1606,6 +1636,7 @@ func TestCreateVolume(t *testing.T) { { name: "success multi-attach", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "random-vol-name", CapacityRange: stdCapRange, @@ -1645,6 +1676,7 @@ func TestCreateVolume(t *testing.T) { { name: "fail multi-attach - invalid mount capability", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateVolumeRequest{ Name: "random-vol-name", CapacityRange: stdCapRange, @@ -1870,6 +1902,7 @@ func TestDeleteVolume(t *testing.T) { { name: "success normal", testFunc: func(t *testing.T) { + t.Helper() req := &csi.DeleteVolumeRequest{ VolumeId: "vol-test", } @@ -1902,6 +1935,7 @@ func TestDeleteVolume(t *testing.T) { { name: "success invalid volume id", testFunc: func(t *testing.T) { + t.Helper() req := &csi.DeleteVolumeRequest{ VolumeId: "invalid-volume-name", } @@ -1934,6 +1968,7 @@ func TestDeleteVolume(t *testing.T) { { name: "fail delete disk", testFunc: func(t *testing.T) { + t.Helper() req := &csi.DeleteVolumeRequest{ VolumeId: "test-vol", } @@ -1970,6 +2005,7 @@ func TestDeleteVolume(t *testing.T) { { name: "fail another request already in-flight", testFunc: func(t *testing.T) { + t.Helper() req := &csi.DeleteVolumeRequest{ VolumeId: "vol-test", } @@ -2233,6 +2269,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "success normal", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateSnapshotRequest{ Name: "test-snapshot", Parameters: nil, @@ -2274,6 +2311,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "success outpost", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateSnapshotRequest{ Name: "test-snapshot", Parameters: map[string]string{ @@ -2317,6 +2355,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "success with cluster-id", testFunc: func(t *testing.T) { + t.Helper() const ( snapshotName = "test-snapshot" clusterID = "test-cluster-id" @@ -2376,6 +2415,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "success with extra tags", testFunc: func(t *testing.T) { + t.Helper() const ( snapshotName = "test-snapshot" extraVolumeTagKey = "extra-tag-key" @@ -2433,6 +2473,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "fail no name", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateSnapshotRequest{ Parameters: nil, SourceVolumeId: "vol-test", @@ -2464,6 +2505,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "fail outpost arn not valid", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateSnapshotRequest{ Name: "test-snapshot", Parameters: map[string]string{ @@ -2492,6 +2534,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "fail same name different volume ID", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateSnapshotRequest{ Name: "test-snapshot", Parameters: nil, @@ -2559,6 +2602,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "success same name same volume ID", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateSnapshotRequest{ Name: "test-snapshot", Parameters: nil, @@ -2611,6 +2655,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "fail with another request in-flight", testFunc: func(t *testing.T) { + t.Helper() req := &csi.CreateSnapshotRequest{ Name: "test-snapshot", Parameters: nil, @@ -2639,6 +2684,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "success with VolumeSnapshotClass tags", testFunc: func(t *testing.T) { + t.Helper() const ( snapshotName = "test-snapshot" extraTagKey = "test-key" @@ -2696,6 +2742,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "success with VolumeSnapshotClass with Name tag and cluster id", testFunc: func(t *testing.T) { + t.Helper() const ( snapshotName = "test-snapshot" nameTagValue = "test-name-tag-value" @@ -2754,6 +2801,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "success with EnableFastSnapshotRestore - normal", testFunc: func(t *testing.T) { + t.Helper() const ( snapshotName = "test-snapshot" ) @@ -2819,6 +2867,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "success with EnableFastSnapshotRestore - failed to get availability zones", testFunc: func(t *testing.T) { + t.Helper() const ( snapshotName = "test-snapshot" ) @@ -2883,6 +2932,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "fail with EnableFastSnapshotRestore - call to enable FSR failed", testFunc: func(t *testing.T) { + t.Helper() const ( snapshotName = "test-snapshot" ) @@ -2948,6 +2998,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "fail with EnableFastSnapshotRestore - invalid availability zones", testFunc: func(t *testing.T) { + t.Helper() const ( snapshotName = "test-snapshot" ) @@ -2984,6 +3035,7 @@ func TestCreateSnapshot(t *testing.T) { { name: "fail with EnableFastSnapshotRestore", testFunc: func(t *testing.T) { + t.Helper() const ( snapshotName = "test-snapshot" ) @@ -3049,6 +3101,7 @@ func TestDeleteSnapshot(t *testing.T) { { name: "success normal", testFunc: func(t *testing.T) { + t.Helper() ctx := context.Background() mockCtl := gomock.NewController(t) @@ -3074,6 +3127,7 @@ func TestDeleteSnapshot(t *testing.T) { { name: "success not found", testFunc: func(t *testing.T) { + t.Helper() ctx := context.Background() mockCtl := gomock.NewController(t) @@ -3099,6 +3153,7 @@ func TestDeleteSnapshot(t *testing.T) { { name: "fail with another request in-flight", testFunc: func(t *testing.T) { + t.Helper() ctx := context.Background() mockCtl := gomock.NewController(t) @@ -3139,6 +3194,7 @@ func TestListSnapshots(t *testing.T) { { name: "success normal", testFunc: func(t *testing.T) { + t.Helper() req := &csi.ListSnapshotsRequest{} mockCloudSnapshotsResponse := &cloud.ListSnapshotsResponse{ Snapshots: []*cloud.Snapshot{ @@ -3184,6 +3240,7 @@ func TestListSnapshots(t *testing.T) { { name: "success no snapshots", testFunc: func(t *testing.T) { + t.Helper() req := &csi.ListSnapshotsRequest{} ctx := context.Background() mockCtl := gomock.NewController(t) @@ -3211,6 +3268,7 @@ func TestListSnapshots(t *testing.T) { { name: "success snapshot ID", testFunc: func(t *testing.T) { + t.Helper() req := &csi.ListSnapshotsRequest{ SnapshotId: "snapshot-1", } @@ -3247,6 +3305,7 @@ func TestListSnapshots(t *testing.T) { { name: "success snapshot ID not found", testFunc: func(t *testing.T) { + t.Helper() req := &csi.ListSnapshotsRequest{ SnapshotId: "snapshot-1", } @@ -3277,6 +3336,7 @@ func TestListSnapshots(t *testing.T) { { name: "fail snapshot ID multiple found", testFunc: func(t *testing.T) { + t.Helper() req := &csi.ListSnapshotsRequest{ SnapshotId: "snapshot-1", } @@ -3310,6 +3370,7 @@ func TestListSnapshots(t *testing.T) { { name: "fail 0 < MaxEntries < 5", testFunc: func(t *testing.T) { + t.Helper() req := &csi.ListSnapshotsRequest{ MaxEntries: 4, } @@ -3680,6 +3741,7 @@ func TestControllerExpandVolume(t *testing.T) { } func checkExpectedErrorCode(t *testing.T, err error, expectedCode codes.Code) { + t.Helper() if err == nil { t.Fatalf("Expected operation to fail but got no error") } diff --git a/pkg/driver/node_test.go b/pkg/driver/node_test.go index 45fbeb26fa..7074737738 100644 --- a/pkg/driver/node_test.go +++ b/pkg/driver/node_test.go @@ -2510,6 +2510,7 @@ func TestRemoveNotReadyTaint(t *testing.T) { { name: "failed to get node", setup: func(t *testing.T, mockCtl *gomock.Controller) func() (kubernetes.Interface, error) { + t.Helper() t.Setenv("CSI_NODE_NAME", nodeName) getNodeMock, _ := getNodeMock(mockCtl, nodeName, nil, fmt.Errorf("Failed to get node!")) @@ -2522,6 +2523,7 @@ func TestRemoveNotReadyTaint(t *testing.T) { { name: "no taints to remove", setup: func(t *testing.T, mockCtl *gomock.Controller) func() (kubernetes.Interface, error) { + t.Helper() t.Setenv("CSI_NODE_NAME", nodeName) getNodeMock, _ := getNodeMock(mockCtl, nodeName, &corev1.Node{}, nil) @@ -2563,6 +2565,7 @@ func TestRemoveNotReadyTaint(t *testing.T) { { name: "failed to patch node", setup: func(t *testing.T, mockCtl *gomock.Controller) func() (kubernetes.Interface, error) { + t.Helper() t.Setenv("CSI_NODE_NAME", nodeName) getNodeMock, mockNode := getNodeMock(mockCtl, nodeName, &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ @@ -2621,6 +2624,7 @@ func TestRemoveNotReadyTaint(t *testing.T) { { name: "success", setup: func(t *testing.T, mockCtl *gomock.Controller) func() (kubernetes.Interface, error) { + t.Helper() t.Setenv("CSI_NODE_NAME", nodeName) getNodeMock, mockNode := getNodeMock(mockCtl, nodeName, &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ @@ -2679,6 +2683,7 @@ func TestRemoveNotReadyTaint(t *testing.T) { { name: "failed to get CSINode", setup: func(t *testing.T, mockCtl *gomock.Controller) func() (kubernetes.Interface, error) { + t.Helper() t.Setenv("CSI_NODE_NAME", nodeName) getNodeMock, _ := getNodeMock(mockCtl, nodeName, &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ @@ -2714,6 +2719,7 @@ func TestRemoveNotReadyTaint(t *testing.T) { { name: "allocatable value not set for driver on node", setup: func(t *testing.T, mockCtl *gomock.Controller) func() (kubernetes.Interface, error) { + t.Helper() t.Setenv("CSI_NODE_NAME", nodeName) getNodeMock, _ := getNodeMock(mockCtl, nodeName, &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ @@ -2763,6 +2769,7 @@ func TestRemoveNotReadyTaint(t *testing.T) { { name: "driver not found on node", setup: func(t *testing.T, mockCtl *gomock.Controller) func() (kubernetes.Interface, error) { + t.Helper() t.Setenv("CSI_NODE_NAME", nodeName) getNodeMock, _ := getNodeMock(mockCtl, nodeName, &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/driver/request_coalescing_test.go b/pkg/driver/request_coalescing_test.go index 1930d4a658..043cb7f21c 100644 --- a/pkg/driver/request_coalescing_test.go +++ b/pkg/driver/request_coalescing_test.go @@ -96,6 +96,7 @@ func TestVolumeModificationWithCoalescing(t *testing.T) { // TestBasicRequestCoalescingSuccess tests the success case of coalescing 2 requests from ControllerExpandVolume and a modify function respectively. func testBasicRequestCoalescingSuccess(t *testing.T, executor modifyVolumeExecutor) { + t.Helper() const NewVolumeType = "gp3" const NewSize = 5 * util.GiB volumeID := t.Name() @@ -157,6 +158,7 @@ func testBasicRequestCoalescingSuccess(t *testing.T, executor modifyVolumeExecut // TestRequestFail tests failing requests from ResizeOrModifyDisk failure. func testRequestFail(t *testing.T, executor modifyVolumeExecutor) { + t.Helper() const NewVolumeType = "gp3" const NewSize = 5 * util.GiB volumeID := t.Name() @@ -216,6 +218,7 @@ func testRequestFail(t *testing.T, executor modifyVolumeExecutor) { // 3) Change volume type to NewVolumeType2 // The expected result is the resizing request succeeds and one of the volume-type requests fails. func testPartialFail(t *testing.T, executor modifyVolumeExecutor) { + t.Helper() const NewVolumeType1 = "gp3" const NewVolumeType2 = "io2" const NewSize = 5 * util.GiB @@ -310,6 +313,7 @@ func testPartialFail(t *testing.T, executor modifyVolumeExecutor) { // TestSequential tests sending 2 requests sequentially. func testSequentialRequests(t *testing.T, executor modifyVolumeExecutor) { + t.Helper() const NewVolumeType = "gp3" const NewSize = 5 * util.GiB volumeID := t.Name() @@ -368,6 +372,7 @@ func testSequentialRequests(t *testing.T, executor modifyVolumeExecutor) { // TestDuplicateRequest tests sending multiple same requests roughly in parallel. func testDuplicateRequest(t *testing.T, executor modifyVolumeExecutor) { + t.Helper() const NewSize = 5 * util.GiB volumeID := t.Name() @@ -423,6 +428,7 @@ func testDuplicateRequest(t *testing.T, executor modifyVolumeExecutor) { // TestResponseReturnTiming tests the caller of request coalescing blocking until receiving response from cloud.ResizeOrModifyDisk func testResponseReturnTiming(t *testing.T, executor modifyVolumeExecutor) { + t.Helper() const NewVolumeType = "gp3" const NewSize = 5 * util.GiB var ec2ModifyVolumeFinished = false @@ -490,6 +496,7 @@ func testResponseReturnTiming(t *testing.T, executor modifyVolumeExecutor) { } func wrapTimeout(t *testing.T, failMessage string, execFunc func()) { + t.Helper() timeout := time.After(15 * time.Second) done := make(chan bool) go func() { From 1267d0af44cd4d28061659882c6a6312990729d8 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:32:34 -0400 Subject: [PATCH 19/24] Enable goconst linter --- pkg/driver/controller.go | 23 +++++++++++------------ pkg/driver/controller_test.go | 8 +++++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index 03965f9b36..b0683a3d2b 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -55,7 +55,8 @@ var ( } ) -const isManagedByDriver = "true" +const trueStr = "true" +const isManagedByDriver = trueStr // ControllerService represents the controller service of CSI driver type ControllerService struct { @@ -141,7 +142,7 @@ func (d *ControllerService) CreateVolume(ctx context.Context, req *csi.CreateVol } iopsPerGB = int32(parseIopsPerGBKey) case AllowAutoIOPSPerGBIncreaseKey: - allowIOPSPerGBIncrease = value == "true" + allowIOPSPerGBIncrease = isTrue(value) case IopsKey: parseIopsKey, parseIopsKeyErr := strconv.ParseInt(value, 10, 32) if parseIopsKeyErr != nil { @@ -155,9 +156,7 @@ func (d *ControllerService) CreateVolume(ctx context.Context, req *csi.CreateVol } throughput = int32(parseThroughput) case EncryptedKey: - if value == "true" { - isEncrypted = true - } + isEncrypted = isTrue(value) case KmsKeyIDKey: kmsKeyID = value case PVCNameKey: @@ -170,9 +169,7 @@ func (d *ControllerService) CreateVolume(ctx context.Context, req *csi.CreateVol volumeTags[PVNameTag] = value tProps.PVName = value case BlockExpressKey: - if value == "true" { - blockExpress = true - } + blockExpress = isTrue(value) case BlockSizeKey: if isAlphanumeric := util.StringIsAlphanumeric(value); !isAlphanumeric { return nil, status.Errorf(codes.InvalidArgument, "Could not parse blockSize (%s): %v", value, err) @@ -194,9 +191,7 @@ func (d *ControllerService) CreateVolume(ctx context.Context, req *csi.CreateVol } numberOfInodes = value case Ext4BigAllocKey: - if value == "true" { - ext4BigAlloc = true - } + ext4BigAlloc = isTrue(value) case Ext4ClusterSizeKey: if isAlphanumeric := util.StringIsAlphanumeric(value); !isAlphanumeric { return nil, status.Errorf(codes.InvalidArgument, "Could not parse ext4ClusterSize (%s): %v", value, err) @@ -255,7 +250,7 @@ func (d *ControllerService) CreateVolume(ctx context.Context, req *csi.CreateVol } } if ext4BigAlloc { - responseCtx[Ext4BigAllocKey] = "true" + responseCtx[Ext4BigAllocKey] = trueStr if err = validateFormattingOption(volCap, Ext4BigAllocKey, FileSystemConfigs); err != nil { return nil, err } @@ -1059,3 +1054,7 @@ func validateFormattingOption(volumeCapabilities []*csi.VolumeCapability, paramN return nil } + +func isTrue(value string) bool { + return value == trueStr +} diff --git a/pkg/driver/controller_test.go b/pkg/driver/controller_test.go index e390cb25c3..c9827fbcf8 100644 --- a/pkg/driver/controller_test.go +++ b/pkg/driver/controller_test.go @@ -45,6 +45,8 @@ const ( expZone = "us-west-2b" expInstanceID = "i-123456789abcdef01" expDevicePath = "/dev/xvda" + + testOutpostARN = "arn:aws:outposts:us-west-2:111111111111:outpost/op-0aaa000a0aaaa00a0" ) func TestCreateVolume(t *testing.T) { @@ -88,7 +90,7 @@ func TestCreateVolume(t *testing.T) { stdVolSize := int64(5 * 1024 * 1024 * 1024) stdCapRange := &csi.CapacityRange{RequiredBytes: stdVolSize} stdParams := map[string]string{} - rawOutpostArn := "arn:aws:outposts:us-west-2:111111111111:outpost/op-0aaa000a0aaaa00a0" + rawOutpostArn := testOutpostARN strippedOutpostArn, _ := arn.Parse(strings.ReplaceAll(rawOutpostArn, "outpost/", "")) testCases := []struct { @@ -2121,7 +2123,7 @@ func TestPickAvailabilityZone(t *testing.T) { } func TestGetOutpostArn(t *testing.T) { - expRawOutpostArn := "arn:aws:outposts:us-west-2:111111111111:outpost/op-0aaa000a0aaaa00a0" + expRawOutpostArn := testOutpostARN outpostArn, _ := arn.Parse(strings.ReplaceAll(expRawOutpostArn, "outpost/", "")) testCases := []struct { name string @@ -2198,7 +2200,7 @@ func TestGetOutpostArn(t *testing.T) { } func TestBuildOutpostArn(t *testing.T) { - expRawOutpostArn := "arn:aws:outposts:us-west-2:111111111111:outpost/op-0aaa000a0aaaa00a0" + expRawOutpostArn := testOutpostARN testCases := []struct { name string awsPartition string From 46c49c425811838762cd11b4493a57e1ace87548 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:40:19 -0400 Subject: [PATCH 20/24] Enable gochecknoinits linter --- pkg/cloud/volume_limits.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cloud/volume_limits.go b/pkg/cloud/volume_limits.go index 894ea840e0..b1ee0174af 100644 --- a/pkg/cloud/volume_limits.go +++ b/pkg/cloud/volume_limits.go @@ -28,6 +28,7 @@ const ( nitroMaxAttachments = 28 ) +//nolint:gochecknoinits // TODO Refactor to avoid using init function to prevent side-effects func init() { // This list of Nitro instance types have a dedicated Amazon EBS volume limit of up to 128 attachments, depending on instance size. // The limit is not shared with other device attachments: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/volume_limits.html#nitro-system-limits From af016debbcd57c802a64f63fccc02a6a5d645999 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:50:07 -0400 Subject: [PATCH 21/24] Enable perfsprint linter --- .golangci.yml | 25 +++--- cmd/hooks/prestop.go | 3 +- cmd/hooks/prestop_test.go | 8 +- pkg/batcher/batcher_test.go | 3 +- pkg/cloud/cloud.go | 16 ++-- pkg/cloud/cloud_test.go | 78 +++++++++---------- pkg/cloud/devicemanager/allocator.go | 4 +- pkg/cloud/devicemanager/manager.go | 5 +- pkg/cloud/metadata/ec2.go | 9 ++- pkg/cloud/metadata/k8s.go | 14 ++-- pkg/cloud/metadata/metadata.go | 4 +- pkg/coalescer/coalescer_test.go | 11 +-- pkg/driver/controller_test.go | 10 +-- pkg/driver/node_test.go | 14 ++-- pkg/driver/options.go | 10 +-- pkg/driver/request_coalescing_test.go | 4 +- pkg/driver/validation.go | 2 +- pkg/driver/validation_test.go | 2 +- pkg/mounter/mount_linux.go | 3 +- pkg/mounter/mount_unsupported.go | 5 +- pkg/mounter/mount_windows.go | 3 +- pkg/mounter/mount_windows_proxyV1.go | 26 +++---- pkg/mounter/mount_windows_proxyV2.go | 26 +++---- pkg/util/template/funcs.go | 17 ++-- pkg/util/util_test.go | 4 +- tests/e2e/suite_test.go | 1 + .../pre_provisioned_snapshot_volume_tester.go | 5 +- 27 files changed, 158 insertions(+), 154 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 4f2c1ed449..21c168376e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,7 +21,7 @@ linters-settings: - name: increment-decrement disabled: true stylecheck: - # dot importing ginkgo and gomega is standard + # Dot importing ginkgo and gomega is standard practice dot-import-whitelist: - "github.com/onsi/gomega" - "github.com/onsi/ginkgo/v2" @@ -103,12 +103,15 @@ linters: - goconst - gochecknoinits - perfsprint - - revive # TODO + - revive + - gosec disable: - govet # We already run with `make verify/govet` # We do not use today - gomoddirectives # We need `replace` in `go.mod` - depguard # We don't use this today + - testpackage # We don't do this + # TODO Investigate FIXME - cyclop # TODO FIXME (at least for non-tests) - dupl # TODO FIXME (tests + non-tests) - err113 # TODO FIXME @@ -117,21 +120,19 @@ linters: - varnamelen # Many false positives - mnd # Many false positives - gomnd # Many false positives - - testpackage # We don't do this - paralleltest # TODO FIXME (Too many tests that aren't parallelized right now) - funlen # Many false positives - gochecknoglobals # TODO FIXME - interfacebloat # False positives on AWS SDK interfaces - - exhaustruct # TODO FIXME - nestif # Many false positives - gocognit # Many false positives + - godox # TODO FIXME + - maintidx # Maintainability index... Oof # Ask team - lll # Limit line length - - godot # TODO Ask team - - godox # TODO FIXME - - gofumpt # TODO Ask team - - gosec # TODO Urgent not too bad - - ireturn # ask team - - maintidx # oof - - nlreturn # Meh, ask team - - nonamedreturns # meh ask team + - godot # TODO Ask team if we want to end all godoc comments with period + - gofumpt # TODO Ask team if we want to rely on gofumpt + - ireturn # ask team if we want to accept interfaces return concrete types + - nlreturn # Meh, ask team newline return + - nonamedreturns # Ask team, maybe nolint a few of the places it actually leads to cleaner code + - exhaustruct # Seems REALLY painful for kubernetes structs... diff --git a/cmd/hooks/prestop.go b/cmd/hooks/prestop.go index 786f4cd0a0..b1e07396a6 100644 --- a/cmd/hooks/prestop.go +++ b/cmd/hooks/prestop.go @@ -16,6 +16,7 @@ package hooks import ( "context" + "errors" "fmt" "os" @@ -59,7 +60,7 @@ func PreStop(clientset kubernetes.Interface) error { nodeName := os.Getenv("CSI_NODE_NAME") if nodeName == "" { - return fmt.Errorf("PreStop: CSI_NODE_NAME missing") + return errors.New("PreStop: CSI_NODE_NAME missing") } node, err := fetchNode(clientset, nodeName) diff --git a/cmd/hooks/prestop_test.go b/cmd/hooks/prestop_test.go index cb8aa17962..49df9cd3e6 100644 --- a/cmd/hooks/prestop_test.go +++ b/cmd/hooks/prestop_test.go @@ -15,7 +15,7 @@ package hooks import ( - "fmt" + "errors" "testing" "github.com/golang/mock/gomock" @@ -37,7 +37,7 @@ func TestPreStopHook(t *testing.T) { { name: "TestPreStopHook: CSI_NODE_NAME not set", nodeName: "", - expErr: fmt.Errorf("PreStop: CSI_NODE_NAME missing"), + expErr: errors.New("PreStop: CSI_NODE_NAME missing"), mockFunc: func(nodeName string, mockClient *driver.MockKubernetesClient, mockCoreV1 *driver.MockCoreV1Interface, mockNode *driver.MockNodeInterface, mockStorageV1 *driver.MockVolumeAttachmentInterface, mockStorageV1Interface *driver.MockStorageV1Interface) error { return nil }, @@ -45,11 +45,11 @@ func TestPreStopHook(t *testing.T) { { name: "TestPreStopHook: failed to retrieve node information", nodeName: "test-node", - expErr: fmt.Errorf("fetchNode: failed to retrieve node information: non-existent node"), + expErr: errors.New("fetchNode: failed to retrieve node information: non-existent node"), mockFunc: func(nodeName string, mockClient *driver.MockKubernetesClient, mockCoreV1 *driver.MockCoreV1Interface, mockNode *driver.MockNodeInterface, mockStorageV1 *driver.MockVolumeAttachmentInterface, mockStorageV1Interface *driver.MockStorageV1Interface) error { mockClient.EXPECT().CoreV1().Return(mockCoreV1).Times(1) mockCoreV1.EXPECT().Nodes().Return(mockNode).Times(1) - mockNode.EXPECT().Get(gomock.Any(), gomock.Eq(nodeName), gomock.Any()).Return(nil, fmt.Errorf("non-existent node")).Times(1) + mockNode.EXPECT().Get(gomock.Any(), gomock.Eq(nodeName), gomock.Any()).Return(nil, errors.New("non-existent node")).Times(1) return nil }, diff --git a/pkg/batcher/batcher_test.go b/pkg/batcher/batcher_test.go index 05ee8d6e7f..01e1688bb4 100644 --- a/pkg/batcher/batcher_test.go +++ b/pkg/batcher/batcher_test.go @@ -15,6 +15,7 @@ package batcher import ( + "errors" "fmt" "sync" "testing" @@ -39,7 +40,7 @@ func mockExecutionWithError(inputs []string) (map[string]string, error) { for _, input := range inputs { results[input] = input } - return results, fmt.Errorf("mock execution error") + return results, errors.New("mock execution error") } func TestBatcher(t *testing.T) { diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 965aaa7736..8ccf1d5f6e 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -169,7 +169,7 @@ var ( ErrInvalidMaxResults = errors.New("maxResults parameter must be 0 or greater than or equal to 5") // ErrVolumeNotBeingModified is returned if volume being described is not being modified - ErrVolumeNotBeingModified = fmt.Errorf("volume is not being modified") + ErrVolumeNotBeingModified = errors.New("volume is not being modified") // ErrInvalidArgument is returned if parameters were rejected by cloud provider ErrInvalidArgument = errors.New("invalid argument") @@ -438,7 +438,7 @@ func execBatchDescribeVolumes(svc EC2API, input []string, batcher volumeBatcherT } default: - return nil, fmt.Errorf("execBatchDescribeVolumes: unsupported request type") + return nil, errors.New("execBatchDescribeVolumes: unsupported request type") } ctx, cancel := context.WithTimeout(context.Background(), batchDescribeTimeout) @@ -538,7 +538,7 @@ func (c *cloud) CreateDisk(ctx context.Context, volumeName string, diskOptions * capacityGiB := util.BytesToGiB(diskOptions.CapacityBytes) if diskOptions.IOPS > 0 && diskOptions.IOPSPerGB > 0 { - return nil, fmt.Errorf("invalid StorageClass parameters; specify either IOPS or IOPSPerGb, not both") + return nil, errors.New("invalid StorageClass parameters; specify either IOPS or IOPSPerGb, not both") } createType = diskOptions.VolumeType @@ -571,7 +571,7 @@ func (c *cloud) CreateDisk(ctx context.Context, volumeName string, diskOptions * } if diskOptions.MultiAttachEnabled && createType != VolumeTypeIO2 { - return nil, fmt.Errorf("CreateDisk: multi-attach is only supported for io2 volumes") + return nil, errors.New("CreateDisk: multi-attach is only supported for io2 volumes") } if maxIops > 0 { @@ -671,12 +671,12 @@ func (c *cloud) CreateDisk(ctx context.Context, volumeName string, diskOptions * volumeID := aws.ToString(response.VolumeId) if len(volumeID) == 0 { - return nil, fmt.Errorf("volume ID was not returned by CreateVolume") + return nil, errors.New("volume ID was not returned by CreateVolume") } size := *response.Size if size == 0 { - return nil, fmt.Errorf("disk size was not returned by CreateVolume") + return nil, errors.New("disk size was not returned by CreateVolume") } if err := c.waitForVolume(ctx, volumeID); err != nil { @@ -1187,7 +1187,7 @@ func execBatchDescribeSnapshots(svc EC2API, input []string, batcher snapshotBatc } default: - return nil, fmt.Errorf("execBatchDescribeSnapshots: unsupported request type") + return nil, errors.New("execBatchDescribeSnapshots: unsupported request type") } ctx, cancel := context.WithTimeout(context.Background(), batchDescribeTimeout) @@ -1299,7 +1299,7 @@ func (c *cloud) CreateSnapshot(ctx context.Context, volumeID string, snapshotOpt return nil, fmt.Errorf("error creating snapshot of volume %s: %w", volumeID, err) } if res == nil { - return nil, fmt.Errorf("nil CreateSnapshotResponse") + return nil, errors.New("nil CreateSnapshotResponse") } return &Snapshot{ diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index 993453e786..bf31712438 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -182,7 +182,7 @@ func TestBatchDescribeVolumes(t *testing.T) { mockFunc: func(mockEC2 *MockEC2API, expErr error, volumes []types.Volume) { mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(nil, expErr).Times(1) }, - expErr: fmt.Errorf("Generic EC2 API error"), + expErr: errors.New("Generic EC2 API error"), }, { name: "fail: volume not found", @@ -190,7 +190,7 @@ func TestBatchDescribeVolumes(t *testing.T) { mockFunc: func(mockEC2 *MockEC2API, expErr error, volumes []types.Volume) { mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(nil, expErr).Times(1) }, - expErr: fmt.Errorf("volume not found"), + expErr: errors.New("volume not found"), }, { name: "fail: invalid tag", @@ -205,7 +205,7 @@ func TestBatchDescribeVolumes(t *testing.T) { volumeOutput := &ec2.DescribeVolumesOutput{Volumes: volumes} mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(volumeOutput, expErr).Times(0) }, - expErr: fmt.Errorf("invalid tag"), + expErr: errors.New("invalid tag"), }, { name: "fail: invalid request", @@ -326,7 +326,7 @@ func TestBatchDescribeInstances(t *testing.T) { mockFunc: func(mockEC2 *MockEC2API, expErr error, reservations []types.Reservation) { mockEC2.EXPECT().DescribeInstances(gomock.Any(), gomock.Any()).Return(nil, expErr).Times(1) }, - expErr: fmt.Errorf("generic EC2 API error"), + expErr: errors.New("generic EC2 API error"), }, { name: "fail: invalid request", @@ -487,7 +487,7 @@ func TestBatchDescribeSnapshots(t *testing.T) { mockFunc: func(mockEC2 *MockEC2API, expErr error, snapshots []types.Snapshot) { mockEC2.EXPECT().DescribeSnapshots(gomock.Any(), gomock.Any()).Return(nil, expErr).Times(2) }, - expErr: fmt.Errorf("generic EC2 API error"), + expErr: errors.New("generic EC2 API error"), }, { name: "fail: Snapshot not found by ID", @@ -632,7 +632,7 @@ func TestCheckDesiredState(t *testing.T) { IOPS: 3000, Throughput: 1000, }, - expErr: fmt.Errorf("volume \"vol-001\" is still being expanded to 500 size"), + expErr: errors.New("volume \"vol-001\" is still being expanded to 500 size"), }, { name: "failure: volume is still being modified to iops", @@ -643,7 +643,7 @@ func TestCheckDesiredState(t *testing.T) { IOPS: 4000, Throughput: 1000, }, - expErr: fmt.Errorf("volume \"vol-001\" is still being modified to iops 4000"), + expErr: errors.New("volume \"vol-001\" is still being modified to iops 4000"), }, { name: "failure: volume is still being modifed to type", @@ -665,7 +665,7 @@ func TestCheckDesiredState(t *testing.T) { IOPS: 3000, Throughput: 2000, }, - expErr: fmt.Errorf("volume \"vol-001\" is still being modified to throughput 2000"), + expErr: errors.New("volume \"vol-001\" is still being modified to throughput 2000"), }, } for _, tc := range testCases { @@ -726,7 +726,7 @@ func TestBatchDescribeVolumesModifications(t *testing.T) { mockFunc: func(mockEC2 *MockEC2API, expErr error, volumeModifications []types.VolumeModification) { mockEC2.EXPECT().DescribeVolumesModifications(gomock.Any(), gomock.Any()).Return(nil, expErr).Times(1) }, - expErr: fmt.Errorf("generic EC2 API error"), + expErr: errors.New("generic EC2 API error"), }, { name: "fail: invalid request", @@ -1036,8 +1036,8 @@ func TestCreateDisk(t *testing.T) { AvailabilityZone: expZone, }, expCreateVolumeInput: &ec2.CreateVolumeInput{}, - expErr: fmt.Errorf("could not create volume in EC2: CreateVolume generic error"), - expCreateVolumeErr: fmt.Errorf("CreateVolume generic error"), + expErr: errors.New("could not create volume in EC2: CreateVolume generic error"), + expCreateVolumeErr: errors.New("CreateVolume generic error"), }, { name: "fail: ec2.CreateVolume returned snapshot not found error", @@ -1076,8 +1076,8 @@ func TestCreateDisk(t *testing.T) { AvailabilityZone: "", }, expCreateVolumeInput: &ec2.CreateVolumeInput{}, - expErr: fmt.Errorf("timed out waiting for volume to create: DescribeVolumes generic error"), - expDescVolumeErr: fmt.Errorf("DescribeVolumes generic error"), + expErr: errors.New("timed out waiting for volume to create: DescribeVolumes generic error"), + expDescVolumeErr: errors.New("DescribeVolumes generic error"), }, { name: "fail: Volume is not ready to use, volume stuck in creating status and controller context deadline exceeded", @@ -1089,7 +1089,7 @@ func TestCreateDisk(t *testing.T) { AvailabilityZone: "", }, expCreateVolumeInput: &ec2.CreateVolumeInput{}, - expErr: fmt.Errorf("timed out waiting for volume to create: timed out waiting for the condition"), + expErr: errors.New("timed out waiting for volume to create: timed out waiting for the condition"), }, { name: "success: normal from snapshot", @@ -1144,7 +1144,7 @@ func TestCreateDisk(t *testing.T) { AvailabilityZone: defaultZone, }, expCreateVolumeInput: nil, - expErr: fmt.Errorf("invalid StorageClass parameters; specify either IOPS or IOPSPerGb, not both"), + expErr: errors.New("invalid StorageClass parameters; specify either IOPS or IOPSPerGb, not both"), }, { name: "success: small io1 with too high iopsPerGB", @@ -1289,13 +1289,13 @@ func TestCreateDisk(t *testing.T) { VolumeType: "sbg1", }, expCreateVolumeInput: &ec2.CreateVolumeInput{}, - expCreateTagsErr: fmt.Errorf("CreateTags generic error"), + expCreateTagsErr: errors.New("CreateTags generic error"), expDisk: &Disk{ VolumeID: "vol-test", CapacityGiB: 1, AvailabilityZone: snowZone, }, - expErr: fmt.Errorf("could not attach tags to volume: vol-test. CreateTags generic error"), + expErr: errors.New("could not attach tags to volume: vol-test. CreateTags generic error"), }, { name: "success: create default volume with throughput", @@ -1350,7 +1350,7 @@ func TestCreateDisk(t *testing.T) { CapacityGiB: 4, AvailabilityZone: defaultZone, }, - expErr: fmt.Errorf("CreateDisk: multi-attach is only supported for io2 volumes"), + expErr: errors.New("CreateDisk: multi-attach is only supported for io2 volumes"), }, { name: "failure: invalid VolumeType", @@ -1534,13 +1534,13 @@ func TestDeleteDisk(t *testing.T) { name: "fail: DeleteVolume returned generic error", volumeID: "vol-test-1234", expResp: false, - expErr: fmt.Errorf("DeleteVolume generic error"), + expErr: errors.New("DeleteVolume generic error"), }, { name: "fail: DeleteVolume returned not found error", volumeID: "vol-test-1234", expResp: false, - expErr: fmt.Errorf("InvalidVolume.NotFound"), + expErr: errors.New("InvalidVolume.NotFound"), }, } @@ -1892,7 +1892,7 @@ func TestGetDiskByName(t *testing.T) { name: "fail: DescribeVolumes returned generic error", volumeName: "vol-test-1234", volumeCapacity: util.GiBToBytes(1), - expErr: fmt.Errorf("DescribeVolumes generic error"), + expErr: errors.New("DescribeVolumes generic error"), }, } @@ -1999,7 +1999,7 @@ func TestGetDiskByID(t *testing.T) { { name: "fail: DescribeVolumes returned generic error", volumeID: "vol-test-1234", - expErr: fmt.Errorf("DescribeVolumes generic error"), + expErr: errors.New("DescribeVolumes generic error"), }, } @@ -2209,14 +2209,14 @@ func TestEnableFastSnapshotRestores(t *testing.T) { }, }}, }, - expErr: fmt.Errorf("failed to create fast snapshot restores for snapshot"), + expErr: errors.New("failed to create fast snapshot restores for snapshot"), }, { name: "fail: error", snapshotID: "", availabilityZones: nil, expOutput: nil, - expErr: fmt.Errorf("EnableFastSnapshotRestores error"), + expErr: errors.New("EnableFastSnapshotRestores error"), }, } @@ -2279,7 +2279,7 @@ func TestAvailabilityZones(t *testing.T) { name: "fail: error", availabilityZone: "", expOutput: nil, - expErr: fmt.Errorf("TestAvailabilityZones error"), + expErr: errors.New("TestAvailabilityZones error"), }, } @@ -2325,7 +2325,7 @@ func TestDeleteSnapshot(t *testing.T) { { name: "fail: delete snapshot return generic error", snapshotName: "snap-test-name", - expErr: fmt.Errorf("DeleteSnapshot generic error"), + expErr: errors.New("DeleteSnapshot generic error"), }, { name: "fail: delete snapshot return not found error", @@ -2506,9 +2506,9 @@ func TestResizeOrModifyDisk(t *testing.T) { name: "fail: volume doesn't exist", volumeID: "vol-test", existingVolume: &types.Volume{}, - existingVolumeError: fmt.Errorf("DescribeVolumes generic error"), + existingVolumeError: errors.New("DescribeVolumes generic error"), reqSizeGiB: 2, - expErr: fmt.Errorf("ResizeDisk generic error"), + expErr: errors.New("ResizeDisk generic error"), }, { name: "failure: volume in modifying state", @@ -2528,7 +2528,7 @@ func TestResizeOrModifyDisk(t *testing.T) { }, }, reqSizeGiB: 2, - expErr: fmt.Errorf("ResizeDisk generic error"), + expErr: errors.New("ResizeDisk generic error"), }, { name: "failure: ModifyVolume returned generic error", @@ -2542,8 +2542,8 @@ func TestResizeOrModifyDisk(t *testing.T) { AvailabilityZone: aws.String(defaultZone), VolumeType: types.VolumeTypeGp2, }, - modifiedVolumeError: fmt.Errorf("GenericErr"), - expErr: fmt.Errorf("GenericErr"), + modifiedVolumeError: errors.New("GenericErr"), + expErr: errors.New("GenericErr"), }, { name: "failure: returned ErrInvalidArgument when ModifyVolume returned InvalidParameterCombination", @@ -2558,8 +2558,8 @@ func TestResizeOrModifyDisk(t *testing.T) { VolumeType: types.VolumeTypeGp2, Size: aws.Int32(1), }, - modifiedVolumeError: fmt.Errorf("InvalidParameterCombination: The parameter iops is not supported for gp2 volumes"), - expErr: fmt.Errorf("InvalidParameterCombination: The parameter iops is not supported for gp2 volumes"), + modifiedVolumeError: errors.New("InvalidParameterCombination: The parameter iops is not supported for gp2 volumes"), + expErr: errors.New("InvalidParameterCombination: The parameter iops is not supported for gp2 volumes"), }, { name: "failure: returned ErrInvalidArgument when ModifyVolume returned UnknownVolumeType", @@ -2573,8 +2573,8 @@ func TestResizeOrModifyDisk(t *testing.T) { VolumeType: types.VolumeTypeGp2, Size: aws.Int32(1), }, - modifiedVolumeError: fmt.Errorf("UnknownVolumeType: Unknown volume type: GPFake"), - expErr: fmt.Errorf("UnknownVolumeType: Unknown volume type: GPFake"), + modifiedVolumeError: errors.New("UnknownVolumeType: Unknown volume type: GPFake"), + expErr: errors.New("UnknownVolumeType: Unknown volume type: GPFake"), }, { name: "failure: returned ErrInvalidArgument when ModifyVolume returned InvalidParameterValue", @@ -2589,8 +2589,8 @@ func TestResizeOrModifyDisk(t *testing.T) { VolumeType: types.VolumeTypeGp2, Size: aws.Int32(1), }, - modifiedVolumeError: fmt.Errorf("InvalidParameterValue: iops value 9999999 is not valid"), - expErr: fmt.Errorf("InvalidParameterValue: iops value 9999999 is not valid"), + modifiedVolumeError: errors.New("InvalidParameterValue: iops value 9999999 is not valid"), + expErr: errors.New("InvalidParameterValue: iops value 9999999 is not valid"), }, { name: "success: does not call ModifyVolume when no modification required", @@ -2742,7 +2742,7 @@ func TestModifyTags(t *testing.T) { name: "fail: EC2 API generic error TagsToAdd", volumeID: "mod-tag-test-name", negativeCase: true, - expErr: fmt.Errorf("Generic EC2 API error"), + expErr: errors.New("Generic EC2 API error"), modifyTagsOptions: ModifyTagsOptions{ TagsToAdd: validTagsToAddInput, TagsToDelete: emptyTagsToDeleteInput, @@ -2752,7 +2752,7 @@ func TestModifyTags(t *testing.T) { name: "fail: EC2 API generic error TagsToDelete", volumeID: "mod-tag-test-name", negativeCase: true, - expErr: fmt.Errorf("Generic EC2 API error"), + expErr: errors.New("Generic EC2 API error"), modifyTagsOptions: ModifyTagsOptions{ TagsToAdd: emptyTagsToAddInput, TagsToDelete: validTagsToDeleteInput, diff --git a/pkg/cloud/devicemanager/allocator.go b/pkg/cloud/devicemanager/allocator.go index 683eee5b03..59d23ed4cb 100644 --- a/pkg/cloud/devicemanager/allocator.go +++ b/pkg/cloud/devicemanager/allocator.go @@ -17,7 +17,7 @@ limitations under the License. package devicemanager import ( - "fmt" + "errors" "sync" ) @@ -70,5 +70,5 @@ func (d *nameAllocator) GetNext(existingNames ExistingNames, likelyBadNames *syn return finalResortName, nil } - return "", fmt.Errorf("there are no names available") + return "", errors.New("there are no names available") } diff --git a/pkg/cloud/devicemanager/manager.go b/pkg/cloud/devicemanager/manager.go index 76fa703677..08e42e77ff 100644 --- a/pkg/cloud/devicemanager/manager.go +++ b/pkg/cloud/devicemanager/manager.go @@ -17,6 +17,7 @@ limitations under the License. package devicemanager import ( + "errors" "fmt" "sync" @@ -108,7 +109,7 @@ func (d *deviceManager) NewDevice(instance *types.Instance, volumeID string, lik defer d.mux.Unlock() if instance == nil { - return nil, fmt.Errorf("instance is nil") + return nil, errors.New("instance is nil") } // Get device names being attached and already attached to this instance @@ -220,7 +221,7 @@ func (d *deviceManager) getPath(inUse map[string]string, volumeID string) string func getInstanceID(instance *types.Instance) (string, error) { if instance == nil { - return "", fmt.Errorf("can't get ID from a nil instance") + return "", errors.New("can't get ID from a nil instance") } return aws.ToString(instance.InstanceId), nil } diff --git a/pkg/cloud/metadata/ec2.go b/pkg/cloud/metadata/ec2.go index a20afd6342..c4e2ea60be 100644 --- a/pkg/cloud/metadata/ec2.go +++ b/pkg/cloud/metadata/ec2.go @@ -16,6 +16,7 @@ package metadata import ( "context" + "errors" "fmt" "io" "strings" @@ -57,18 +58,18 @@ func EC2MetadataInstanceInfo(svc EC2Metadata, regionFromSession string) (*Metada doc := docOutput.InstanceIdentityDocument if len(doc.InstanceID) == 0 { - return nil, fmt.Errorf("could not get valid EC2 instance ID") + return nil, errors.New("could not get valid EC2 instance ID") } if len(doc.InstanceType) == 0 { - return nil, fmt.Errorf("could not get valid EC2 instance type") + return nil, errors.New("could not get valid EC2 instance type") } if len(doc.Region) == 0 { if len(regionFromSession) != 0 && util.IsSBE(regionFromSession) { doc.Region = regionFromSession } else { - return nil, fmt.Errorf("could not get valid EC2 region") + return nil, errors.New("could not get valid EC2 region") } } @@ -76,7 +77,7 @@ func EC2MetadataInstanceInfo(svc EC2Metadata, regionFromSession string) (*Metada if len(regionFromSession) != 0 && util.IsSBE(regionFromSession) { doc.AvailabilityZone = regionFromSession } else { - return nil, fmt.Errorf("could not get valid EC2 availability zone") + return nil, errors.New("could not get valid EC2 availability zone") } } diff --git a/pkg/cloud/metadata/k8s.go b/pkg/cloud/metadata/k8s.go index c57a220417..22c96f737c 100644 --- a/pkg/cloud/metadata/k8s.go +++ b/pkg/cloud/metadata/k8s.go @@ -55,7 +55,7 @@ func DefaultKubernetesAPIClient(kubeconfig string) KubernetesAPIClient { // it provides the absolute host path to the container volume. sandboxMountPoint := os.Getenv("CONTAINER_SANDBOX_MOUNT_POINT") if sandboxMountPoint == "" { - return nil, fmt.Errorf("CONTAINER_SANDBOX_MOUNT_POINT environment variable is not set") + return nil, errors.New("CONTAINER_SANDBOX_MOUNT_POINT environment variable is not set") } tokenFile := filepath.Join(sandboxMountPoint, "var", "run", "secrets", "kubernetes.io", "serviceaccount", "token") @@ -98,7 +98,7 @@ func DefaultKubernetesAPIClient(kubeconfig string) KubernetesAPIClient { func KubernetesAPIInstanceInfo(clientset kubernetes.Interface) (*Metadata, error) { nodeName := os.Getenv("CSI_NODE_NAME") if nodeName == "" { - return nil, fmt.Errorf("CSI_NODE_NAME env var not set") + return nil, errors.New("CSI_NODE_NAME env var not set") } // get node with k8s API @@ -109,7 +109,7 @@ func KubernetesAPIInstanceInfo(clientset kubernetes.Interface) (*Metadata, error providerID := node.Spec.ProviderID if providerID == "" { - return nil, fmt.Errorf("node providerID empty, cannot parse") + return nil, errors.New("node providerID empty, cannot parse") } awsInstanceIDRegex := "s\\.i-[a-z0-9]+|i-[a-z0-9]+$" @@ -117,28 +117,28 @@ func KubernetesAPIInstanceInfo(clientset kubernetes.Interface) (*Metadata, error re := regexp.MustCompile(awsInstanceIDRegex) instanceID := re.FindString(providerID) if instanceID == "" { - return nil, fmt.Errorf("did not find aws instance ID in node providerID string") + return nil, errors.New("did not find aws instance ID in node providerID string") } var instanceType string if val, ok := node.GetLabels()[corev1.LabelInstanceTypeStable]; ok { instanceType = val } else { - return nil, fmt.Errorf("could not retrieve instance type from topology label") + return nil, errors.New("could not retrieve instance type from topology label") } var region string if val, ok := node.GetLabels()[corev1.LabelTopologyRegion]; ok { region = val } else { - return nil, fmt.Errorf("could not retrieve region from topology label") + return nil, errors.New("could not retrieve region from topology label") } var availabilityZone string if val, ok := node.GetLabels()[corev1.LabelTopologyZone]; ok { availabilityZone = val } else { - return nil, fmt.Errorf("could not retrieve AZ from topology label") + return nil, errors.New("could not retrieve AZ from topology label") } instanceInfo := Metadata{ diff --git a/pkg/cloud/metadata/metadata.go b/pkg/cloud/metadata/metadata.go index 1497a9944b..af68db2896 100644 --- a/pkg/cloud/metadata/metadata.go +++ b/pkg/cloud/metadata/metadata.go @@ -17,7 +17,7 @@ limitations under the License. package metadata import ( - "fmt" + "errors" "os" "github.com/aws/aws-sdk-go-v2/aws/arn" @@ -57,7 +57,7 @@ func NewMetadataService(cfg MetadataServiceConfig, region string) (MetadataServi } klog.ErrorS(err, "Retrieving Kubernetes metadata failed") - return nil, fmt.Errorf("IMDS metadata and Kubernetes metadata are both unavailable") + return nil, errors.New("IMDS metadata and Kubernetes metadata are both unavailable") } func retrieveEC2Metadata(ec2MetadataClient EC2MetadataClient, region string) (*Metadata, error) { diff --git a/pkg/coalescer/coalescer_test.go b/pkg/coalescer/coalescer_test.go index ffead66f84..c39cdc0640 100644 --- a/pkg/coalescer/coalescer_test.go +++ b/pkg/coalescer/coalescer_test.go @@ -18,14 +18,13 @@ package coalescer import ( "errors" - "fmt" "testing" "time" ) var ( - errFailedToMerge = fmt.Errorf("failed to merge") - errFailedToExecute = fmt.Errorf("failed to execute") + errFailedToMerge = errors.New("failed to merge") + errFailedToExecute = errors.New("failed to execute") ) // Merge function used to test the coalescer @@ -34,9 +33,8 @@ var ( func mockMerge(input int, existing int) (int, error) { if input < 0 { return 0, errFailedToMerge - } else { - return input + existing, nil } + return input + existing, nil } // Execute function used to test the coalescer @@ -45,9 +43,8 @@ func mockMerge(input int, existing int) (int, error) { func mockExecute(_ string, input int) (string, error) { if input < 100 { return "success", nil - } else { - return "failure", errFailedToExecute } + return "failure", errFailedToExecute } func TestCoalescer(t *testing.T) { diff --git a/pkg/driver/controller_test.go b/pkg/driver/controller_test.go index c9827fbcf8..b027d0a235 100644 --- a/pkg/driver/controller_test.go +++ b/pkg/driver/controller_test.go @@ -1980,7 +1980,7 @@ func TestDeleteVolume(t *testing.T) { defer mockCtl.Finish() mockCloud := cloud.NewMockCloud(mockCtl) - mockCloud.EXPECT().DeleteDisk(gomock.Eq(ctx), gomock.Eq(req.GetVolumeId())).Return(false, fmt.Errorf("DeleteDisk could not delete volume")) + mockCloud.EXPECT().DeleteDisk(gomock.Eq(ctx), gomock.Eq(req.GetVolumeId())).Return(false, errors.New("DeleteDisk could not delete volume")) awsDriver := ControllerService{ cloud: mockCloud, inFlight: internal.NewInFlight(), @@ -2911,7 +2911,7 @@ func TestCreateSnapshot(t *testing.T) { mockCloud := cloud.NewMockCloud(mockCtl) mockCloud.EXPECT().GetSnapshotByName(gomock.Eq(ctx), gomock.Eq(req.GetName())).Return(nil, cloud.ErrNotFound).AnyTimes() - mockCloud.EXPECT().AvailabilityZones(gomock.Eq(ctx)).Return(nil, fmt.Errorf("error describing availability zones")).AnyTimes() + mockCloud.EXPECT().AvailabilityZones(gomock.Eq(ctx)).Return(nil, errors.New("error describing availability zones")).AnyTimes() mockCloud.EXPECT().CreateSnapshot(gomock.Eq(ctx), gomock.Eq(req.GetSourceVolumeId()), gomock.Eq(snapshotOptions)).Return(mockSnapshot, nil).AnyTimes() mockCloud.EXPECT().EnableFastSnapshotRestores(gomock.Eq(ctx), gomock.Eq([]string{"us-east-1a", "us-east-1f"}), gomock.Eq(mockSnapshot.SnapshotID)).Return(expOutput, nil).AnyTimes() @@ -2979,10 +2979,10 @@ func TestCreateSnapshot(t *testing.T) { mockCloud := cloud.NewMockCloud(mockCtl) mockCloud.EXPECT().GetSnapshotByName(gomock.Eq(ctx), gomock.Eq(req.GetName())).Return(nil, cloud.ErrNotFound).AnyTimes() - mockCloud.EXPECT().AvailabilityZones(gomock.Eq(ctx)).Return(nil, fmt.Errorf("error describing availability zones")).AnyTimes() + mockCloud.EXPECT().AvailabilityZones(gomock.Eq(ctx)).Return(nil, errors.New("error describing availability zones")).AnyTimes() mockCloud.EXPECT().CreateSnapshot(gomock.Eq(ctx), gomock.Eq(req.GetSourceVolumeId()), gomock.Eq(snapshotOptions)).Return(mockSnapshot, nil).AnyTimes() mockCloud.EXPECT().EnableFastSnapshotRestores(gomock.Eq(ctx), gomock.Eq([]string{"us-west-1a", "us-east-1f"}), gomock.Eq(mockSnapshot.SnapshotID)). - Return(expOutput, fmt.Errorf("Failed to create Fast Snapshot Restores")).AnyTimes() + Return(expOutput, errors.New("Failed to create Fast Snapshot Restores")).AnyTimes() mockCloud.EXPECT().DeleteSnapshot(gomock.Eq(ctx), gomock.Eq(mockSnapshot.SnapshotID)).Return(true, nil).AnyTimes() awsDriver := ControllerService{ @@ -3073,7 +3073,7 @@ func TestCreateSnapshot(t *testing.T) { "us-east-1a": {}, "us-east-1f": {}}, nil).AnyTimes() mockCloud.EXPECT().CreateSnapshot(gomock.Eq(ctx), gomock.Eq(req.GetSourceVolumeId()), gomock.Eq(snapshotOptions)).Return(mockSnapshot, nil).AnyTimes() mockCloud.EXPECT().EnableFastSnapshotRestores(gomock.Eq(ctx), gomock.Eq([]string{"us-east-1a", "us-east-1f"}), - gomock.Eq(mockSnapshot.SnapshotID)).Return(nil, fmt.Errorf("error")).AnyTimes() + gomock.Eq(mockSnapshot.SnapshotID)).Return(nil, errors.New("error")).AnyTimes() mockCloud.EXPECT().DeleteSnapshot(gomock.Eq(ctx), gomock.Eq(mockSnapshot.SnapshotID)).Return(true, nil).AnyTimes() awsDriver := ControllerService{ diff --git a/pkg/driver/node_test.go b/pkg/driver/node_test.go index 7074737738..59f6ebab27 100644 --- a/pkg/driver/node_test.go +++ b/pkg/driver/node_test.go @@ -2512,13 +2512,13 @@ func TestRemoveNotReadyTaint(t *testing.T) { setup: func(t *testing.T, mockCtl *gomock.Controller) func() (kubernetes.Interface, error) { t.Helper() t.Setenv("CSI_NODE_NAME", nodeName) - getNodeMock, _ := getNodeMock(mockCtl, nodeName, nil, fmt.Errorf("Failed to get node!")) + getNodeMock, _ := getNodeMock(mockCtl, nodeName, nil, errors.New("Failed to get node!")) return func() (kubernetes.Interface, error) { return getNodeMock, nil } }, - expResult: fmt.Errorf("Failed to get node!"), + expResult: errors.New("Failed to get node!"), }, { name: "no taints to remove", @@ -2612,14 +2612,14 @@ func TestRemoveNotReadyTaint(t *testing.T) { mockNode.EXPECT(). Patch(gomock.Any(), gomock.Eq(nodeName), gomock.Any(), gomock.Any(), gomock.Any()). - Return(nil, fmt.Errorf("Failed to patch node!")). + Return(nil, errors.New("Failed to patch node!")). Times(1) return func() (kubernetes.Interface, error) { return getNodeMock, nil } }, - expResult: fmt.Errorf("Failed to patch node!"), + expResult: errors.New("Failed to patch node!"), }, { name: "success", @@ -2707,7 +2707,7 @@ func TestRemoveNotReadyTaint(t *testing.T) { csiNodesMock.EXPECT(). Get(gomock.Any(), gomock.Eq(nodeName), gomock.Any()). - Return(nil, fmt.Errorf("Failed to get CSINode")). + Return(nil, errors.New("Failed to get CSINode")). Times(1) return func() (kubernetes.Interface, error) { @@ -2842,7 +2842,7 @@ func TestRemoveTaintInBackground(t *testing.T) { if mockRemovalCount == 3 { return nil } else { - return fmt.Errorf("Taint removal failed!") + return errors.New("Taint removal failed!") } } removeTaintInBackground(nil, taintRemovalBackoff, mockRemovalFunc) @@ -2853,7 +2853,7 @@ func TestRemoveTaintInBackground(t *testing.T) { mockRemovalCount := 0 mockRemovalFunc := func(_ kubernetes.Interface) error { mockRemovalCount += 1 - return fmt.Errorf("Taint removal failed!") + return errors.New("Taint removal failed!") } removeTaintInBackground(nil, wait.Backoff{ Steps: 5, diff --git a/pkg/driver/options.go b/pkg/driver/options.go index c08b65b29e..adf291ad5a 100644 --- a/pkg/driver/options.go +++ b/pkg/driver/options.go @@ -17,7 +17,7 @@ limitations under the License. package driver import ( - "fmt" + "errors" "time" flag "github.com/spf13/pflag" @@ -124,18 +124,18 @@ func (o *Options) AddFlags(f *flag.FlagSet) { func (o *Options) Validate() error { if o.Mode == AllMode || o.Mode == NodeMode { if o.VolumeAttachLimit != -1 && o.ReservedVolumeAttachments != -1 { - return fmt.Errorf("only one of --volume-attach-limit and --reserved-volume-attachments may be specified") + return errors.New("only one of --volume-attach-limit and --reserved-volume-attachments may be specified") } } if o.MetricsCertFile != "" || o.MetricsKeyFile != "" { switch { case o.HTTPEndpoint == "": - return fmt.Errorf("--http-endpoint MUST be specififed when using the metrics server with HTTPS") + return errors.New("--http-endpoint MUST be specififed when using the metrics server with HTTPS") case o.MetricsCertFile == "": - return fmt.Errorf("--metrics-cert-file MUST be specififed when using the metrics server with HTTPS") + return errors.New("--metrics-cert-file MUST be specififed when using the metrics server with HTTPS") case o.MetricsKeyFile == "": - return fmt.Errorf("--metrics-key-file MUST be specififed when using the metrics server with HTTPS") + return errors.New("--metrics-key-file MUST be specififed when using the metrics server with HTTPS") } } diff --git a/pkg/driver/request_coalescing_test.go b/pkg/driver/request_coalescing_test.go index 043cb7f21c..dabb2d4706 100644 --- a/pkg/driver/request_coalescing_test.go +++ b/pkg/driver/request_coalescing_test.go @@ -18,7 +18,7 @@ package driver import ( "context" - "fmt" + "errors" "sync" "testing" "time" @@ -169,7 +169,7 @@ func testRequestFail(t *testing.T, executor modifyVolumeExecutor) { mockCloud := cloud.NewMockCloud(mockCtl) mockCloud.EXPECT().ResizeOrModifyDisk(gomock.Any(), gomock.Eq(volumeID), gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, volumeID string, newSize int64, options *cloud.ModifyDiskOptions) (int64, error) { klog.InfoS("ResizeOrModifyDisk called", "volumeID", volumeID, "newSize", newSize, "options", options) - return 0, fmt.Errorf("ResizeOrModifyDisk failed") + return 0, errors.New("ResizeOrModifyDisk failed") }) options := &Options{ diff --git a/pkg/driver/validation.go b/pkg/driver/validation.go index e28d6c2db7..dd7667c40b 100644 --- a/pkg/driver/validation.go +++ b/pkg/driver/validation.go @@ -50,7 +50,7 @@ func validateExtraTags(tags map[string]string, warnOnly bool) error { if len(k) > cloud.MaxTagKeyLength { return fmt.Errorf("tag key too long (actual: %d, limit: %d)", len(k), cloud.MaxTagKeyLength) } else if len(k) < cloud.MinTagKeyLength { - return fmt.Errorf("tag key cannot be empty (min: 1)") + return errors.New("tag key cannot be empty (min: 1)") } if len(v) > cloud.MaxTagValueLength { return fmt.Errorf("tag value too long (actual: %d, limit: %d)", len(v), cloud.MaxTagValueLength) diff --git a/pkg/driver/validation_test.go b/pkg/driver/validation_test.go index 55d87ad4cc..c476c231cd 100644 --- a/pkg/driver/validation_test.go +++ b/pkg/driver/validation_test.go @@ -71,7 +71,7 @@ func TestValidateExtraTags(t *testing.T) { tags: map[string]string{ "": "extra-tag-value", }, - expErr: fmt.Errorf("tag key cannot be empty (min: 1)"), + expErr: errors.New("tag key cannot be empty (min: 1)"), }, { name: "invalid tag: value too long", diff --git a/pkg/mounter/mount_linux.go b/pkg/mounter/mount_linux.go index 7df2219b84..ccb9d0863b 100644 --- a/pkg/mounter/mount_linux.go +++ b/pkg/mounter/mount_linux.go @@ -20,6 +20,7 @@ limitations under the License. package mounter import ( + "errors" "fmt" "os" "os/exec" @@ -48,7 +49,7 @@ func NewSafeMounter() (*mountutils.SafeFormatAndMount, error) { } func NewSafeMounterV2() (*mountutils.SafeFormatAndMount, error) { - return nil, fmt.Errorf("NewSafeMounterV2 is not supported on this platform") + return nil, errors.New("NewSafeMounterV2 is not supported on this platform") } // FindDevicePath finds path of device and verifies its existence diff --git a/pkg/mounter/mount_unsupported.go b/pkg/mounter/mount_unsupported.go index f04106961c..2637a01079 100644 --- a/pkg/mounter/mount_unsupported.go +++ b/pkg/mounter/mount_unsupported.go @@ -21,7 +21,6 @@ package mounter import ( "errors" - "fmt" mountutils "k8s.io/mount-utils" ) @@ -36,11 +35,11 @@ run driver e2e tests. */ func NewSafeMounter() (*mountutils.SafeFormatAndMount, error) { - return nil, fmt.Errorf("NewSafeMounter is not supported on this platform") + return nil, errors.New("NewSafeMounter is not supported on this platform") } func NewSafeMounterV2() (*mountutils.SafeFormatAndMount, error) { - return nil, fmt.Errorf("NewSafeMounterV2 is not supported on this platform") + return nil, errors.New("NewSafeMounterV2 is not supported on this platform") } func (m *NodeMounter) FindDevicePath(devicePath, volumeID, partition, region string) (string, error) { diff --git a/pkg/mounter/mount_windows.go b/pkg/mounter/mount_windows.go index afb4f41151..6883cf4edf 100644 --- a/pkg/mounter/mount_windows.go +++ b/pkg/mounter/mount_windows.go @@ -20,6 +20,7 @@ limitations under the License. package mounter import ( + "errors" "fmt" "regexp" @@ -31,7 +32,7 @@ import ( ) var ( - ErrUnsupportedMounter = fmt.Errorf("unsupported mounter type") + ErrUnsupportedMounter = errors.New("unsupported mounter type") ) func (m NodeMounter) FindDevicePath(devicePath, volumeID, _, _ string) (string, error) { diff --git a/pkg/mounter/mount_windows_proxyV1.go b/pkg/mounter/mount_windows_proxyV1.go index 71cdf93cad..af6cbc225f 100644 --- a/pkg/mounter/mount_windows_proxyV1.go +++ b/pkg/mounter/mount_windows_proxyV1.go @@ -162,7 +162,7 @@ func (mounter *CSIProxyMounter) WriteVolumeCache(target string) { } func (mounter *CSIProxyMounter) List() ([]mountutils.MountPoint, error) { - return []mountutils.MountPoint{}, fmt.Errorf("List not implemented for CSIProxyMounter") + return []mountutils.MountPoint{}, errors.New("List not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) IsMountPointMatch(mp mountutils.MountPoint, dir string) bool { @@ -203,11 +203,11 @@ func (mounter *CSIProxyMounter) IsLikelyNotMountPoint(path string) (bool, error) } func (mounter *CSIProxyMounter) PathIsDevice(pathname string) (bool, error) { - return false, fmt.Errorf("PathIsDevice not implemented for CSIProxyMounter") + return false, errors.New("PathIsDevice not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) DeviceOpened(pathname string) (bool, error) { - return false, fmt.Errorf("DeviceOpened not implemented for CSIProxyMounter") + return false, errors.New("DeviceOpened not implemented for CSIProxyMounter") } // GetDeviceNameFromMount returns the disk number for a mount path. @@ -230,11 +230,11 @@ func (mounter *CSIProxyMounter) GetDeviceNameFromMount(mountPath, _ string) (str } func (mounter *CSIProxyMounter) MakeRShared(path string) error { - return fmt.Errorf("MakeRShared not implemented for CSIProxyMounter") + return errors.New("MakeRShared not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) MakeFile(pathname string) error { - return fmt.Errorf("MakeFile not implemented for CSIProxyMounter") + return errors.New("MakeFile not implemented for CSIProxyMounter") } // MakeDir - Creates a directory. The CSI proxy takes in context information. @@ -266,35 +266,35 @@ func (mounter *CSIProxyMounter) ExistsPath(path string) (bool, error) { } func (mounter *CSIProxyMounter) EvalHostSymlinks(pathname string) (string, error) { - return "", fmt.Errorf("EvalHostSymlinks is not implemented for CSIProxyMounter") + return "", errors.New("EvalHostSymlinks is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) GetMountRefs(pathname string) ([]string, error) { - return []string{}, fmt.Errorf("GetMountRefs is not implemented for CSIProxyMounter") + return []string{}, errors.New("GetMountRefs is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) GetFSGroup(pathname string) (int64, error) { - return -1, fmt.Errorf("GetFSGroup is not implemented for CSIProxyMounter") + return -1, errors.New("GetFSGroup is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) GetSELinuxSupport(pathname string) (bool, error) { - return false, fmt.Errorf("GetSELinuxSupport is not implemented for CSIProxyMounter") + return false, errors.New("GetSELinuxSupport is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) GetMode(pathname string) (os.FileMode, error) { - return 0, fmt.Errorf("GetMode is not implemented for CSIProxyMounter") + return 0, errors.New("GetMode is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { - return fmt.Errorf("MountSensitive is not implemented for CSIProxyMounter") + return errors.New("MountSensitive is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) MountSensitiveWithoutSystemd(source string, target string, fstype string, options []string, sensitiveOptions []string) error { - return fmt.Errorf("MountSensitiveWithoutSystemd is not implemented for CSIProxyMounter") + return errors.New("MountSensitiveWithoutSystemd is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounter) MountSensitiveWithoutSystemdWithMountFlags(source string, target string, fstype string, options []string, sensitiveOptions []string, mountFlags []string) error { - return fmt.Errorf("MountSensitiveWithoutSystemdWithMountFlags is not implemented for CSIProxyMounter") + return errors.New("MountSensitiveWithoutSystemdWithMountFlags is not implemented for CSIProxyMounter") } // Rescan would trigger an update storage cache via the CSI proxy. diff --git a/pkg/mounter/mount_windows_proxyV2.go b/pkg/mounter/mount_windows_proxyV2.go index 468ee219f0..bc340650b9 100644 --- a/pkg/mounter/mount_windows_proxyV2.go +++ b/pkg/mounter/mount_windows_proxyV2.go @@ -161,7 +161,7 @@ func (mounter *CSIProxyMounterV2) WriteVolumeCache(target string) { } func (mounter *CSIProxyMounterV2) List() ([]mountutils.MountPoint, error) { - return []mountutils.MountPoint{}, fmt.Errorf("List not implemented for CSIProxyMounter") + return []mountutils.MountPoint{}, errors.New("List not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) IsMountPointMatch(mp mountutils.MountPoint, dir string) bool { @@ -202,11 +202,11 @@ func (mounter *CSIProxyMounterV2) IsLikelyNotMountPoint(path string) (bool, erro } func (mounter *CSIProxyMounterV2) PathIsDevice(pathname string) (bool, error) { - return false, fmt.Errorf("PathIsDevice not implemented for CSIProxyMounter") + return false, errors.New("PathIsDevice not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) DeviceOpened(pathname string) (bool, error) { - return false, fmt.Errorf("DeviceOpened not implemented for CSIProxyMounter") + return false, errors.New("DeviceOpened not implemented for CSIProxyMounter") } // GetDeviceNameFromMount returns the disk number for a mount path. @@ -229,11 +229,11 @@ func (mounter *CSIProxyMounterV2) GetDeviceNameFromMount(mountPath, _ string) (s } func (mounter *CSIProxyMounterV2) MakeRShared(path string) error { - return fmt.Errorf("MakeRShared not implemented for CSIProxyMounter") + return errors.New("MakeRShared not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) MakeFile(pathname string) error { - return fmt.Errorf("MakeFile not implemented for CSIProxyMounter") + return errors.New("MakeFile not implemented for CSIProxyMounter") } // MakeDir - Creates a directory. The CSI proxy takes in context information. @@ -265,35 +265,35 @@ func (mounter *CSIProxyMounterV2) ExistsPath(path string) (bool, error) { } func (mounter *CSIProxyMounterV2) EvalHostSymlinks(pathname string) (string, error) { - return "", fmt.Errorf("EvalHostSymlinks is not implemented for CSIProxyMounter") + return "", errors.New("EvalHostSymlinks is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) GetMountRefs(pathname string) ([]string, error) { - return []string{}, fmt.Errorf("GetMountRefs is not implemented for CSIProxyMounter") + return []string{}, errors.New("GetMountRefs is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) GetFSGroup(pathname string) (int64, error) { - return -1, fmt.Errorf("GetFSGroup is not implemented for CSIProxyMounter") + return -1, errors.New("GetFSGroup is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) GetSELinuxSupport(pathname string) (bool, error) { - return false, fmt.Errorf("GetSELinuxSupport is not implemented for CSIProxyMounter") + return false, errors.New("GetSELinuxSupport is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) GetMode(pathname string) (os.FileMode, error) { - return 0, fmt.Errorf("GetMode is not implemented for CSIProxyMounter") + return 0, errors.New("GetMode is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { - return fmt.Errorf("MountSensitive is not implemented for CSIProxyMounter") + return errors.New("MountSensitive is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) MountSensitiveWithoutSystemd(source string, target string, fstype string, options []string, sensitiveOptions []string) error { - return fmt.Errorf("MountSensitiveWithoutSystemd is not implemented for CSIProxyMounter") + return errors.New("MountSensitiveWithoutSystemd is not implemented for CSIProxyMounter") } func (mounter *CSIProxyMounterV2) MountSensitiveWithoutSystemdWithMountFlags(source string, target string, fstype string, options []string, sensitiveOptions []string, mountFlags []string) error { - return fmt.Errorf("MountSensitiveWithoutSystemdWithMountFlags is not implemented for CSIProxyMounter") + return errors.New("MountSensitiveWithoutSystemdWithMountFlags is not implemented for CSIProxyMounter") } // Rescan would trigger an update storage cache via the CSI proxy. diff --git a/pkg/util/template/funcs.go b/pkg/util/template/funcs.go index 9b1b868cc2..7880b80912 100644 --- a/pkg/util/template/funcs.go +++ b/pkg/util/template/funcs.go @@ -15,26 +15,27 @@ package template import ( + "errors" "fmt" "html/template" "strings" ) // Disable functions -func html(args ...interface{}) (string, error) { - return "", fmt.Errorf("cannot call 'html' function") +func html(...interface{}) (string, error) { + return "", errors.New("cannot call 'html' function") } -func js(args ...interface{}) (string, error) { - return "", fmt.Errorf("cannot call 'js' function") +func js(...interface{}) (string, error) { + return "", errors.New("cannot call 'js' function") } -func call(args ...interface{}) (string, error) { - return "", fmt.Errorf("cannot call 'call' function") +func call(...interface{}) (string, error) { + return "", errors.New("cannot call 'call' function") } -func urlquery(args ...interface{}) (string, error) { - return "", fmt.Errorf("cannot call 'urlquery' function") +func urlquery(...interface{}) (string, error) { + return "", errors.New("cannot call 'urlquery' function") } func contains(arg1, arg2 string) bool { diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index c5a71f3ca6..56aac97efe 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -20,7 +20,7 @@ limitations under the License. package util import ( - "fmt" + "errors" "reflect" "testing" @@ -107,7 +107,7 @@ func TestParseEndpoint(t *testing.T) { { name: "invalid endpoint", endpoint: "http://127.0.0.1", - expErr: fmt.Errorf("unsupported protocol: http"), + expErr: errors.New("unsupported protocol: http"), }, } diff --git a/tests/e2e/suite_test.go b/tests/e2e/suite_test.go index 616c9ca2dd..6668c451ea 100644 --- a/tests/e2e/suite_test.go +++ b/tests/e2e/suite_test.go @@ -31,6 +31,7 @@ import ( const kubeconfigEnvVar = "KUBECONFIG" +//nolint:gochecknoinits func init() { rand.New(rand.NewSource(time.Now().UnixNano())) testing.Init() diff --git a/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go b/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go index 0bee115688..fcc72f5a22 100644 --- a/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_snapshot_volume_tester.go @@ -15,7 +15,7 @@ limitations under the License. package testsuites import ( - "fmt" + "errors" "github.com/kubernetes-sigs/aws-ebs-csi-driver/tests/e2e/driver" . "github.com/onsi/ginkgo/v2" @@ -42,8 +42,7 @@ func (t *PreProvisionedVolumeSnapshotTest) Run(client clientset.Interface, restc defer tvsc.DeleteVolumeSnapshotContent(tvolumeSnapshotContent) defer tvsc.DeleteSnapshot(tvs) if len(t.Pod.Volumes) < 1 { - err := fmt.Errorf("Volume is not setup for testing pod, exit. ") - framework.ExpectNoError(err) + framework.ExpectNoError(errors.New("volume is not setup for testing pod, exit")) } volume := t.Pod.Volumes[0] From a2fc4a44d56c5689f872e24ce6af3b170778a894 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Wed, 30 Oct 2024 14:59:00 -0400 Subject: [PATCH 22/24] Enable gosec; Fix potential integer overflows --- .golangci.yml | 27 +++++++++++++++++--------- pkg/driver/controller_modify_volume.go | 4 ++-- pkg/util/util.go | 2 ++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 21c168376e..8c4314529d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -14,6 +14,15 @@ run: modules-download-mode: readonly +issues: + exclude-rules: + # gosec recommends ignoring test files + - path: (.+)_test.go + linters: + - gosec + - path: tests/e2e + linters: + - gosec linters-settings: revive: rules: @@ -105,29 +114,28 @@ linters: - perfsprint - revive - gosec + - gochecknoglobals # TODO FIXME disable: - govet # We already run with `make verify/govet` - # We do not use today + # We do not use - gomoddirectives # We need `replace` in `go.mod` - - depguard # We don't use this today - - testpackage # We don't do this + - depguard # We don't guard against dependencies + - testpackage # Requires separate test package + - funlen # Long names happen + - maintidx # Maintainability Index + - cyclop # Cyclomatic complexity + - wsl # Too strict of a whitespace linter # TODO Investigate FIXME - - cyclop # TODO FIXME (at least for non-tests) - dupl # TODO FIXME (tests + non-tests) - err113 # TODO FIXME - - wsl # Doesn't fit with existing code style - wrapcheck # Many false positives - varnamelen # Many false positives - mnd # Many false positives - gomnd # Many false positives - paralleltest # TODO FIXME (Too many tests that aren't parallelized right now) - - funlen # Many false positives - - gochecknoglobals # TODO FIXME - - interfacebloat # False positives on AWS SDK interfaces - nestif # Many false positives - gocognit # Many false positives - godox # TODO FIXME - - maintidx # Maintainability index... Oof # Ask team - lll # Limit line length - godot # TODO Ask team if we want to end all godoc comments with period @@ -136,3 +144,4 @@ linters: - nlreturn # Meh, ask team newline return - nonamedreturns # Ask team, maybe nolint a few of the places it actually leads to cleaner code - exhaustruct # Seems REALLY painful for kubernetes structs... + - interfacebloat # Cloud and Mounter have 15 instead of 10... Smell or necessary? We can nolint diff --git a/pkg/driver/controller_modify_volume.go b/pkg/driver/controller_modify_volume.go index 1d9969d709..8e1f797251 100644 --- a/pkg/driver/controller_modify_volume.go +++ b/pkg/driver/controller_modify_volume.go @@ -171,13 +171,13 @@ func parseModifyVolumeParameters(params map[string]string) (*modifyVolumeRequest for key, value := range params { switch key { case ModificationKeyIOPS: - iops, err := strconv.Atoi(value) + iops, err := strconv.ParseInt(value, 10, 32) if err != nil { return nil, status.Errorf(codes.InvalidArgument, "Could not parse IOPS: %q", value) } options.modifyDiskOptions.IOPS = int32(iops) case ModificationKeyThroughput: - throughput, err := strconv.Atoi(value) + throughput, err := strconv.ParseInt(value, 10, 32) if err != nil { return nil, status.Errorf(codes.InvalidArgument, "Could not parse throughput: %q", value) } diff --git a/pkg/util/util.go b/pkg/util/util.go index afb82fab44..1d34932cf7 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -53,6 +53,7 @@ func RoundUpGiB(volumeSizeBytes int64) (int32, error) { if result > int64(math.MaxInt32) { return 0, fmt.Errorf("rounded up size exceeds maximum value of int32: %d", result) } + //nolint:gosec // Integer overflow handled return int32(result), nil } @@ -63,6 +64,7 @@ func BytesToGiB(volumeSizeBytes int64) int32 { // Handle overflow return math.MaxInt32 } + //nolint:gosec // Integer overflow handled return int32(result) } From aa668bd60de9a33d2d6661012b45c948f0970938 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Wed, 30 Oct 2024 16:56:16 -0400 Subject: [PATCH 23/24] Ensure comments end with period as godoc suggests --- .golangci.yml | 44 +++++------ cmd/hooks/prestop.go | 2 +- pkg/cloud/cloud.go | 56 +++++++------- pkg/cloud/cloud_test.go | 2 +- pkg/cloud/devicemanager/allocator.go | 2 +- pkg/cloud/devicemanager/manager.go | 4 +- pkg/cloud/handlers.go | 4 +- pkg/cloud/metadata/ec2.go | 6 +- pkg/cloud/metadata/metadata.go | 4 +- pkg/cloud/volume_limits.go | 4 +- pkg/coalescer/coalescer.go | 10 +-- pkg/coalescer/coalescer_test.go | 4 +- pkg/driver/constants.go | 74 +++++++++---------- pkg/driver/controller.go | 10 +-- pkg/driver/controller_modify_volume.go | 2 +- pkg/driver/driver.go | 2 +- pkg/driver/node.go | 18 ++--- pkg/driver/request_coalescing_test.go | 2 +- pkg/driver/version.go | 2 +- pkg/expiringcache/expiring_cache.go | 4 +- pkg/mounter/mount.go | 2 +- pkg/util/template/funcs.go | 2 +- pkg/util/util.go | 14 ++-- tests/e2e/driver/driver.go | 4 +- tests/e2e/driver/ebs_csi_driver.go | 6 +- tests/e2e/pre_provsioning.go | 2 +- tests/e2e/requires_aws_api.go | 2 +- ...namically_provisioned_cmd_volume_tester.go | 2 +- ...cally_provisioned_collocated_pod_tester.go | 2 +- ...namically_provisioned_delete_pod_tester.go | 2 +- ...lly_provisioned_read_only_volume_tester.go | 2 +- ...cally_provisioned_reclaim_policy_tester.go | 2 +- ...rovisioned_topology_aware_volume_tester.go | 2 +- ...ally_provisioned_volume_snapshot_tester.go | 2 +- tests/e2e/testsuites/e2e_utils.go | 20 ++--- ...pre_provisioned_read_only_volume_tester.go | 2 +- .../pre_provisioned_reclaim_policy_tester.go | 2 +- .../pre_provisioned_volume_tester.go | 2 +- tests/e2e/testsuites/testsuites.go | 4 +- 39 files changed, 166 insertions(+), 166 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 8c4314529d..a99c01395d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -114,34 +114,34 @@ linters: - perfsprint - revive - gosec - - gochecknoglobals # TODO FIXME + - godot # End all godoc comments with period disable: - govet # We already run with `make verify/govet` # We do not use - gomoddirectives # We need `replace` in `go.mod` - depguard # We don't guard against dependencies - - testpackage # Requires separate test package - - funlen # Long names happen - - maintidx # Maintainability Index + - testpackage # Require separate test package to catch leaky unexported dependencies + - funlen # Long func names happen + - varnamelen # Long var names happen + - maintidx # Maintainability index - cyclop # Cyclomatic complexity + - gocognit # Cognitive complexity - wsl # Too strict of a whitespace linter # TODO Investigate FIXME - - dupl # TODO FIXME (tests + non-tests) - - err113 # TODO FIXME - - wrapcheck # Many false positives - - varnamelen # Many false positives - - mnd # Many false positives - - gomnd # Many false positives - - paralleltest # TODO FIXME (Too many tests that aren't parallelized right now) - - nestif # Many false positives - - gocognit # Many false positives - - godox # TODO FIXME - # Ask team + - err113 # TODO FIXME do not create errors dynamically from scratch. Instead wrap static (package-level) error. 2 points + - wrapcheck # TODO ^^ + - gochecknoglobals # TODO FIXME A whole 2 point task on its own + - paralleltest # TODO FIXME (Too many tests that aren't parallelized right now, probably half a day to attempt to mark the ones we can) + - nestif # TODO whole refactoring/readability task, should split up + - godox # TODO FIXME audit our project TODOs + # TODO Q: Consult with team - lll # Limit line length - - godot # TODO Ask team if we want to end all godoc comments with period - - gofumpt # TODO Ask team if we want to rely on gofumpt - - ireturn # ask team if we want to accept interfaces return concrete types - - nlreturn # Meh, ask team newline return - - nonamedreturns # Ask team, maybe nolint a few of the places it actually leads to cleaner code - - exhaustruct # Seems REALLY painful for kubernetes structs... - - interfacebloat # Cloud and Mounter have 15 instead of 10... Smell or necessary? We can nolint + - gofumpt # Rely on gofumpt's stricter formatting opinions + - ireturn # Always accept interfaces return concrete types (gopherism) + - nlreturn # Always have emptyline before return + - nonamedreturns # No using named returns in functions. Maybe nolint a few of the places it actually leads to cleaner code + - exhaustruct # Forces you to explicitly instantiate all structs. REALLY painful for kubernetes structs... + - interfacebloat # Cloud and Mounter have 15 methods instead of linter's recommended 10... Smell or necessary? We can nolint specific ones + - mnd # Magic Number Detection. Many false positives, still worth with nolint? + - gomnd # Many false positives, still worth with nolint? + - dupl # Tracks code duplication. Brutal amount of duplication in tests. False positives in non-tests. diff --git a/cmd/hooks/prestop.go b/cmd/hooks/prestop.go index b1e07396a6..d948673fd3 100644 --- a/cmd/hooks/prestop.go +++ b/cmd/hooks/prestop.go @@ -47,7 +47,7 @@ const clusterAutoscalerTaint = "ToBeDeletedByClusterAutoscaler" const v1KarpenterTaint = "karpenter.sh/disrupted" const v1beta1KarpenterTaint = "karpenter.sh/disruption" -// drainTaints includes taints used by K8s or autoscalers that signify node draining or pod eviction +// drainTaints includes taints used by K8s or autoscalers that signify node draining or pod eviction. var drainTaints = map[string]struct{}{ v1.TaintNodeUnschedulable: {}, // Kubernetes common eviction taint (kubectl drain) clusterAutoscalerTaint: {}, diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 8ccf1d5f6e..f4c8194e1e 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -41,7 +41,7 @@ import ( "k8s.io/klog/v2" ) -// AWS volume types +// AWS volume types. const ( // VolumeTypeIO1 represents a provisioned IOPS SSD type of volume. VolumeTypeIO1 = "io1" @@ -111,13 +111,13 @@ const ( MaxTagValueLength = 256 ) -// Defaults +// Defaults. const ( // DefaultVolumeSize represents the default volume size. DefaultVolumeSize int64 = 100 * util.GiB ) -// Tags +// Tags. const ( // VolumeNameTagKey is the key value that refers to the volume's name. VolumeNameTagKey = "CSIVolumeName" @@ -127,11 +127,11 @@ const ( KubernetesTagKeyPrefix = "kubernetes.io" // AWSTagKeyPrefix is the prefix of the key value that is reserved for AWS. AWSTagKeyPrefix = "aws:" - // AwsEbsDriverTagKey is the tag to identify if a volume/snapshot is managed by ebs csi driver + // AwsEbsDriverTagKey is the tag to identify if a volume/snapshot is managed by ebs csi driver. AwsEbsDriverTagKey = "ebs.csi.aws.com/cluster" ) -// Batcher +// Batcher. const ( volumeIDBatcher volumeBatcherType = iota volumeTagBatcher @@ -162,23 +162,23 @@ var ( ErrAlreadyExists = errors.New("resource already exists") // ErrMultiSnapshots is returned when multiple snapshots are found - // with the same ID + // with the same ID. ErrMultiSnapshots = errors.New("multiple snapshots with the same name found") - // ErrInvalidMaxResults is returned when a MaxResults pagination parameter is between 1 and 4 + // ErrInvalidMaxResults is returned when a MaxResults pagination parameter is between 1 and 4. ErrInvalidMaxResults = errors.New("maxResults parameter must be 0 or greater than or equal to 5") - // ErrVolumeNotBeingModified is returned if volume being described is not being modified + // ErrVolumeNotBeingModified is returned if volume being described is not being modified. ErrVolumeNotBeingModified = errors.New("volume is not being modified") - // ErrInvalidArgument is returned if parameters were rejected by cloud provider + // ErrInvalidArgument is returned if parameters were rejected by cloud provider. ErrInvalidArgument = errors.New("invalid argument") - // ErrInvalidRequest is returned if parameters were rejected by driver + // ErrInvalidRequest is returned if parameters were rejected by driver. ErrInvalidRequest = errors.New("invalid request") ) -// Set during build time via -ldflags +// Set during build time via -ldflags. var driverVersion string var invalidParameterErrorCodes = map[string]struct{}{ @@ -192,7 +192,7 @@ var invalidParameterErrorCodes = map[string]struct{}{ "ValidationError": {}, } -// Disk represents a EBS volume +// Disk represents a EBS volume. type Disk struct { VolumeID string CapacityGiB int32 @@ -202,7 +202,7 @@ type Disk struct { Attachments []string } -// DiskOptions represents parameters to create an EBS volume +// DiskOptions represents parameters to create an EBS volume. type DiskOptions struct { CapacityBytes int64 Tags map[string]string @@ -222,20 +222,20 @@ type DiskOptions struct { SnapshotID string } -// ModifyDiskOptions represents parameters to modify an EBS volume +// ModifyDiskOptions represents parameters to modify an EBS volume. type ModifyDiskOptions struct { VolumeType string IOPS int32 Throughput int32 } -// ModifyTagsOptions represents parameter to modify the tags of an existing EBS volume +// ModifyTagsOptions represents parameter to modify the tags of an existing EBS volume. type ModifyTagsOptions struct { TagsToAdd map[string]string TagsToDelete []string } -// Snapshot represents an EBS volume snapshot +// Snapshot represents an EBS volume snapshot. type Snapshot struct { SnapshotID string SourceVolumeID string @@ -244,19 +244,19 @@ type Snapshot struct { ReadyToUse bool } -// ListSnapshotsResponse is the container for our snapshots along with a pagination token to pass back to the caller +// ListSnapshotsResponse is the container for our snapshots along with a pagination token to pass back to the caller. type ListSnapshotsResponse struct { Snapshots []*Snapshot NextToken string } -// SnapshotOptions represents parameters to create an EBS volume +// SnapshotOptions represents parameters to create an EBS volume. type SnapshotOptions struct { Tags map[string]string OutpostArn string } -// ec2ListSnapshotsResponse is a helper struct returned from the AWS API calling function to the main ListSnapshots function +// ec2ListSnapshotsResponse is a helper struct returned from the AWS API calling function to the main ListSnapshots function. type ec2ListSnapshotsResponse struct { Snapshots []types.Snapshot NextToken *string @@ -333,7 +333,7 @@ type cloud struct { var _ Cloud = &cloud{} // NewCloud returns a new instance of AWS cloud -// It panics if session is invalid +// It panics if session is invalid. func NewCloud(region string, awsSdkDebugLog bool, userAgentExtra string, batching bool) (Cloud, error) { c := newEC2Cloud(region, awsSdkDebugLog, userAgentExtra, batching) return c, nil @@ -705,7 +705,7 @@ func (c *cloud) CreateDisk(ctx context.Context, volumeName string, diskOptions * return &Disk{CapacityGiB: size, VolumeID: volumeID, AvailabilityZone: zone, SnapshotID: snapshotID, OutpostArn: outpostArn}, nil } -// execBatchDescribeVolumesModifications executes a batched DescribeVolumesModifications API call +// execBatchDescribeVolumesModifications executes a batched DescribeVolumesModifications API call. func execBatchDescribeVolumesModifications(svc EC2API, input []string) (map[string]*types.VolumeModification, error) { klog.V(7).InfoS("execBatchDescribeVolumeModifications", "volumeIds", input) request := &ec2.DescribeVolumesModificationsInput{ @@ -861,7 +861,7 @@ func (c *cloud) DeleteDisk(ctx context.Context, volumeID string) (bool, error) { return true, nil } -// execBatchDescribeInstances executes a batched DescribeInstances API call +// execBatchDescribeInstances executes a batched DescribeInstances API call. func execBatchDescribeInstances(svc EC2API, input []string) (map[string]*types.Instance, error) { klog.V(7).InfoS("execBatchDescribeInstances", "instanceIds", input) request := &ec2.DescribeInstancesInput{ @@ -1401,7 +1401,7 @@ func (c *cloud) ListSnapshots(ctx context.Context, volumeID string, maxResults i }, nil } -// Helper method converting EC2 snapshot type to the internal struct +// Helper method converting EC2 snapshot type to the internal struct. func (c *cloud) ec2SnapshotResponseToStruct(ec2Snapshot types.Snapshot) *Snapshot { snapshotSize := *ec2Snapshot.VolumeSize snapshot := &Snapshot{ @@ -1557,7 +1557,7 @@ func (c *cloud) getSnapshot(ctx context.Context, request *ec2.DescribeSnapshotsI } } -// listSnapshots returns all snapshots based from a request +// listSnapshots returns all snapshots based from a request. func (c *cloud) listSnapshots(ctx context.Context, request *ec2.DescribeSnapshotsInput) (*ec2ListSnapshotsResponse, error) { var snapshots []types.Snapshot var nextToken *string @@ -1643,7 +1643,7 @@ func isAWSErrorInvalidAttachmentNotFound(err error) bool { } // isAWSErrorModificationNotFound returns a boolean indicating whether the given -// error is an AWS InvalidVolumeModification.NotFound error +// error is an AWS InvalidVolumeModification.NotFound error. func isAWSErrorModificationNotFound(err error) bool { return isAWSError(err, "InvalidVolumeModification.NotFound") } @@ -1657,7 +1657,7 @@ func isAWSErrorSnapshotNotFound(err error) bool { // isAWSErrorIdempotentParameterMismatch returns a boolean indicating whether the // given error is an AWS IdempotentParameterMismatch error. -// This error is reported when the two request contains same client-token but different parameters +// This error is reported when the two request contains same client-token but different parameters. func isAWSErrorIdempotentParameterMismatch(err error) bool { return isAWSError(err, "IdempotentParameterMismatch") } @@ -1794,7 +1794,7 @@ func (c *cloud) getLatestVolumeModification(ctx context.Context, volumeID string } // randomAvailabilityZone returns a random zone from the given region -// the randomness relies on the response of DescribeAvailabilityZones +// the randomness relies on the response of DescribeAvailabilityZones. func (c *cloud) randomAvailabilityZone(ctx context.Context) (string, error) { request := &ec2.DescribeAvailabilityZonesInput{} response, err := c.ec2.DescribeAvailabilityZones(ctx, request) @@ -1810,7 +1810,7 @@ func (c *cloud) randomAvailabilityZone(ctx context.Context) (string, error) { return zones[0], nil } -// AvailabilityZones returns availability zones from the given region +// AvailabilityZones returns availability zones from the given region. func (c *cloud) AvailabilityZones(ctx context.Context) (map[string]struct{}, error) { response, err := c.ec2.DescribeAvailabilityZones(ctx, &ec2.DescribeAvailabilityZonesInput{}) if err != nil { diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index bf31712438..3b09e107ff 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -1451,7 +1451,7 @@ func TestCreateDisk(t *testing.T) { } } -// Test client error IdempotentParameterMismatch by forcing it to progress twice +// Test client error IdempotentParameterMismatch by forcing it to progress twice. func TestCreateDiskClientToken(t *testing.T) { t.Parallel() diff --git a/pkg/cloud/devicemanager/allocator.go b/pkg/cloud/devicemanager/allocator.go index 59d23ed4cb..95567396fb 100644 --- a/pkg/cloud/devicemanager/allocator.go +++ b/pkg/cloud/devicemanager/allocator.go @@ -46,7 +46,7 @@ var _ NameAllocator = &nameAllocator{} // It does this by using a list of legal EBS device names from device_names.go // // likelyBadNames is a map of names that have previously returned an "in use" error when attempting to mount to them -// These names are unlikely to result in a successful mount, and may be permanently unavailable, so use them last +// These names are unlikely to result in a successful mount, and may be permanently unavailable, so use them last. func (d *nameAllocator) GetNext(existingNames ExistingNames, likelyBadNames *sync.Map) (string, error) { for _, name := range deviceNames { _, existing := existingNames[name] diff --git a/pkg/cloud/devicemanager/manager.go b/pkg/cloud/devicemanager/manager.go index 08e42e77ff..85d4d18ec6 100644 --- a/pkg/cloud/devicemanager/manager.go +++ b/pkg/cloud/devicemanager/manager.go @@ -44,7 +44,7 @@ func (d *Device) Release(force bool) { } } -// Taint marks the device as no longer reusable +// Taint marks the device as no longer reusable. func (d *Device) Taint() { d.isTainted = true } @@ -194,7 +194,7 @@ func (d *deviceManager) release(device *Device) error { } // getDeviceNamesInUse returns the device to volume ID mapping -// the mapping includes both already attached and being attached volumes +// the mapping includes both already attached and being attached volumes. func (d *deviceManager) getDeviceNamesInUse(instance *types.Instance) map[string]string { nodeID := aws.ToString(instance.InstanceId) inUse := map[string]string{} diff --git a/pkg/cloud/handlers.go b/pkg/cloud/handlers.go index 2b82c8a8af..2f725943d7 100644 --- a/pkg/cloud/handlers.go +++ b/pkg/cloud/handlers.go @@ -29,7 +29,7 @@ import ( "k8s.io/klog/v2" ) -// RecordRequestsHandler is added to the Complete chain; called after any request +// RecordRequestsHandler is added to the Complete chain; called after any request. func RecordRequestsMiddleware() func(*middleware.Stack) error { return func(stack *middleware.Stack) error { return stack.Finalize.Add(middleware.FinalizeMiddlewareFunc("RecordRequestsMiddleware", func(ctx context.Context, input middleware.FinalizeInput, next middleware.FinalizeHandler) (output middleware.FinalizeOutput, metadata middleware.Metadata, err error) { @@ -60,7 +60,7 @@ func RecordRequestsMiddleware() func(*middleware.Stack) error { // LogServerErrorsMiddleware is a middleware that logs server errors received when attempting to contact the AWS API // A specialized middleware is used instead of the SDK's built-in retry logging to allow for customizing the verbosity -// of throttle errors vs server/unknown errors, to prevent flooding the logs with throttle error +// of throttle errors vs server/unknown errors, to prevent flooding the logs with throttle error. func LogServerErrorsMiddleware() func(*middleware.Stack) error { return func(stack *middleware.Stack) error { return stack.Finalize.Add(middleware.FinalizeMiddlewareFunc("LogServerErrorsMiddleware", func(ctx context.Context, input middleware.FinalizeInput, next middleware.FinalizeHandler) (output middleware.FinalizeOutput, metadata middleware.Metadata, err error) { diff --git a/pkg/cloud/metadata/ec2.go b/pkg/cloud/metadata/ec2.go index c4e2ea60be..b0c39a6b48 100644 --- a/pkg/cloud/metadata/ec2.go +++ b/pkg/cloud/metadata/ec2.go @@ -29,13 +29,13 @@ import ( ) const ( - // OutpostArnEndpoint is the ec2 instance metadata endpoint to query to get the outpost arn + // OutpostArnEndpoint is the ec2 instance metadata endpoint to query to get the outpost arn. OutpostArnEndpoint string = "outpost-arn" - // enisEndpoint is the ec2 instance metadata endpoint to query the number of attached ENIs + // enisEndpoint is the ec2 instance metadata endpoint to query the number of attached ENIs. EnisEndpoint string = "network/interfaces/macs" - // blockDevicesEndpoint is the ec2 instance metadata endpoint to query the number of attached block devices + // blockDevicesEndpoint is the ec2 instance metadata endpoint to query the number of attached block devices. BlockDevicesEndpoint string = "block-device-mapping" ) diff --git a/pkg/cloud/metadata/metadata.go b/pkg/cloud/metadata/metadata.go index af68db2896..ee644d67c0 100644 --- a/pkg/cloud/metadata/metadata.go +++ b/pkg/cloud/metadata/metadata.go @@ -24,7 +24,7 @@ import ( "k8s.io/klog/v2" ) -// Metadata is info about the ec2 instance on which the driver is running +// Metadata is info about the ec2 instance on which the driver is running. type Metadata struct { InstanceID string InstanceType string @@ -83,7 +83,7 @@ func retrieveK8sMetadata(k8sAPIClient KubernetesAPIClient) (*Metadata, error) { return KubernetesAPIInstanceInfo(clientset) } -// Override the region on a Metadata object if it is non-empty +// Override the region on a Metadata object if it is non-empty. func (m *Metadata) overrideRegion(region string) *Metadata { if region != "" { m.Region = region diff --git a/pkg/cloud/volume_limits.go b/pkg/cloud/volume_limits.go index b1ee0174af..7b91db018d 100644 --- a/pkg/cloud/volume_limits.go +++ b/pkg/cloud/volume_limits.go @@ -160,7 +160,7 @@ func GetReservedSlotsForInstanceType(it string) int { // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-store-volumes.html // IMDS does not provide NVMe instance store data; we'll just list all instances here -// g5.48xlarge is not added to this table as it is in the maxVolumeLimits +// g5.48xlarge is not added to this table as it is in the maxVolumeLimits. var nvmeInstanceStoreVolumes = map[string]int{ "c1.medium": 1, "c1.xlarge": 4, @@ -513,7 +513,7 @@ var nvmeInstanceStoreVolumes = map[string]int{ // https://aws.amazon.com/ec2/instance-types // Despite the dl1.24xlarge having Gaudi Accelerators describe instance types considers them GPUs as such that instance type is in this table -// g5.48xlarge is not added to this table as it is in the maxVolumeLimits +// g5.48xlarge is not added to this table as it is in the maxVolumeLimits. var gpuInstanceGpus = map[string]int{ "dl1.24xlarge": 8, "g3.16xlarge": 4, diff --git a/pkg/coalescer/coalescer.go b/pkg/coalescer/coalescer.go index db4e23f5c3..9fbc85afca 100644 --- a/pkg/coalescer/coalescer.go +++ b/pkg/coalescer/coalescer.go @@ -30,7 +30,7 @@ import ( // // When the delay on the request expires (determined by the time the first request comes in), the merged // input is passed to the execution function, and the result to all waiting callers (those that were -// not rejected during the merge step) +// not rejected during the merge step). type Coalescer[InputType any, ResultType any] interface { // Coalesce is a function to coalesce a given input // key = only requests with this same key will be coalesced (such as volume ID) @@ -45,7 +45,7 @@ type Coalescer[InputType any, ResultType any] interface { // mergeFunction = a function to merge a new input with the existing inputs // (should return an error if the new input cannot be combined with the existing inputs, // otherwise return the new merged input) -// executeFunction = the function to call when the delay expires +// executeFunction = the function to call when the delay expires. func New[InputType any, ResultType any](delay time.Duration, mergeFunction func(input InputType, existing InputType) (InputType, error), executeFunction func(key string, input InputType) (ResultType, error), @@ -63,21 +63,21 @@ func New[InputType any, ResultType any](delay time.Duration, return &c } -// Type to store a result or error in channels +// Type to store a result or error in channels. type result[ResultType any] struct { result ResultType err error } // Type to send inputs from Coalesce() to coalescerThread() via channel -// Includes a return channel for the result +// Includes a return channel for the result. type newInput[InputType any, ResultType any] struct { key string input InputType resultChannel chan result[ResultType] } -// Type to store pending inputs in the input map +// Type to store pending inputs in the input map. type pendingInput[InputType any, ResultType any] struct { input InputType resultChannels []chan result[ResultType] diff --git a/pkg/coalescer/coalescer_test.go b/pkg/coalescer/coalescer_test.go index c39cdc0640..ca705fe906 100644 --- a/pkg/coalescer/coalescer_test.go +++ b/pkg/coalescer/coalescer_test.go @@ -29,7 +29,7 @@ var ( // Merge function used to test the coalescer // For testing purposes, positive numbers are added to the existing input, -// and negative numbers return an error ("fail to merge") +// and negative numbers return an error ("fail to merge"). func mockMerge(input int, existing int) (int, error) { if input < 0 { return 0, errFailedToMerge @@ -39,7 +39,7 @@ func mockMerge(input int, existing int) (int, error) { // Execute function used to test the coalescer // For testing purposes, small numbers (numbers less than 100) successfully execute, -// and large numbers (numbers 100 or greater) fail to execute +// and large numbers (numbers 100 or greater) fail to execute. func mockExecute(_ string, input int) (string, error) { if input < 100 { return "success", nil diff --git a/pkg/driver/constants.go b/pkg/driver/constants.go index 17d6ab69c8..4f5aa7d6f5 100644 --- a/pkg/driver/constants.go +++ b/pkg/driver/constants.go @@ -18,41 +18,41 @@ package driver import "time" -// constants of keys in PublishContext +// constants of keys in PublishContext. const ( // devicePathKey represents key for device path in PublishContext - // devicePath is the device path where the volume is attached to + // devicePath is the device path where the volume is attached to. DevicePathKey = "devicePath" ) -// constants of keys in VolumeContext +// constants of keys in VolumeContext. const ( // VolumeAttributePartition represents key for partition config in VolumeContext - // this represents the partition number on a device used to mount + // this represents the partition number on a device used to mount. VolumeAttributePartition = "partition" ) -// constants of keys in volume parameters +// constants of keys in volume parameters. const ( - // VolumeTypeKey represents key for volume type + // VolumeTypeKey represents key for volume type. VolumeTypeKey = "type" - // IopsPerGBKey represents key for IOPS per GB + // IopsPerGBKey represents key for IOPS per GB. IopsPerGBKey = "iopspergb" - // AllowAutoIOPSPerGBIncreaseKey represents key for allowing automatic increase of IOPS + // AllowAutoIOPSPerGBIncreaseKey represents key for allowing automatic increase of IOPS. AllowAutoIOPSPerGBIncreaseKey = "allowautoiopspergbincrease" - // Iops represents key for IOPS for volume + // Iops represents key for IOPS for volume. IopsKey = "iops" - // ThroughputKey represents key for throughput + // ThroughputKey represents key for throughput. ThroughputKey = "throughput" - // EncryptedKey represents key for whether filesystem is encrypted + // EncryptedKey represents key for whether filesystem is encrypted. EncryptedKey = "encrypted" - // KmsKeyId represents key for KMS encryption key + // KmsKeyId represents key for KMS encryption key. KmsKeyIDKey = "kmskeyid" // PVCNameKey contains name of the PVC for which is a volume provisioned. @@ -62,58 +62,58 @@ const ( PVCNamespaceKey = "csi.storage.k8s.io/pvc/namespace" // PVNameKey contains name of the final PV that will be used for the dynamically - // provisioned volume + // provisioned volume. PVNameKey = "csi.storage.k8s.io/pv/name" - // VolumeSnapshotNameKey contains name of the snapshot + // VolumeSnapshotNameKey contains name of the snapshot. VolumeSnapshotNameKey = "csi.storage.k8s.io/volumesnapshot/name" - // VolumeSnapshotNamespaceKey contains namespace of the snapshot + // VolumeSnapshotNamespaceKey contains namespace of the snapshot. VolumeSnapshotNamespaceKey = "csi.storage.k8s.io/volumesnapshot/namespace" // VolumeSnapshotCotentNameKey contains name of the VolumeSnapshotContent that is the source - // for the snapshot + // for the snapshot. VolumeSnapshotContentNameKey = "csi.storage.k8s.io/volumesnapshotcontent/name" - // BlockExpressKey increases the iops limit for io2 volumes to the block express limit + // BlockExpressKey increases the iops limit for io2 volumes to the block express limit. BlockExpressKey = "blockexpress" // FSTypeKey configures the file system type that will be formatted during volume creation. FSTypeKey = "csi.storage.k8s.io/fstype" - // BlockSizeKey configures the block size when formatting a volume + // BlockSizeKey configures the block size when formatting a volume. BlockSizeKey = "blocksize" - // InodeSizeKey configures the inode size when formatting a volume + // InodeSizeKey configures the inode size when formatting a volume. InodeSizeKey = "inodesize" - // BytesPerInodeKey configures the `bytes-per-inode` when formatting a volume + // BytesPerInodeKey configures the `bytes-per-inode` when formatting a volume. BytesPerInodeKey = "bytesperinode" - // NumberOfInodesKey configures the `number-of-inodes` when formatting a volume + // NumberOfInodesKey configures the `number-of-inodes` when formatting a volume. NumberOfInodesKey = "numberofinodes" - // Ext4ClusterSizeKey enables the bigalloc option when formatting an ext4 volume + // Ext4ClusterSizeKey enables the bigalloc option when formatting an ext4 volume. Ext4BigAllocKey = "ext4bigalloc" - // Ext4ClusterSizeKey configures the cluster size when formatting an ext4 volume with the bigalloc option enabled + // Ext4ClusterSizeKey configures the cluster size when formatting an ext4 volume with the bigalloc option enabled. Ext4ClusterSizeKey = "ext4clustersize" // TagKeyPrefix contains the prefix of a volume parameter that designates it as - // a tag to be attached to the resource + // a tag to be attached to the resource. TagKeyPrefix = "tagSpecification" - // OutpostArn represents key for outpost's arn + // OutpostArn represents key for outpost's arn. OutpostArnKey = "outpostarn" ) -// constants of keys in snapshot parameters +// constants of keys in snapshot parameters. const ( - // FastSnapShotRestoreAvailabilityZones represents key for fast snapshot restore availability zones + // FastSnapShotRestoreAvailabilityZones represents key for fast snapshot restore availability zones. FastSnapshotRestoreAvailabilityZones = "fastsnapshotrestoreavailabilityzones" ) -// constants for volume tags and their values +// constants for volume tags and their values. const ( // ResourceLifecycleTagPrefix is prefix of tag for provisioned EBS volume that // marks them as owned by the cluster. Used only when --cluster-id is set. @@ -153,29 +153,29 @@ const ( PVNameTag = "kubernetes.io/created-for/pv/name" ) -// constants for default command line flag values +// constants for default command line flag values. const ( DefaultCSIEndpoint = "unix://tmp/csi.sock" DefaultModifyVolumeRequestHandlerTimeout = 2 * time.Second ) -// constants for fstypes +// constants for fstypes. const ( - // FSTypeExt2 represents the ext2 filesystem type + // FSTypeExt2 represents the ext2 filesystem type. FSTypeExt2 = "ext2" - // FSTypeExt3 represents the ext3 filesystem type + // FSTypeExt3 represents the ext3 filesystem type. FSTypeExt3 = "ext3" - // FSTypeExt4 represents the ext4 filesystem type + // FSTypeExt4 represents the ext4 filesystem type. FSTypeExt4 = "ext4" - // FSTypeXfs represents the xfs filesystem type + // FSTypeXfs represents the xfs filesystem type. FSTypeXfs = "xfs" - // FSTypeNtfs represents the ntfs filesystem type + // FSTypeNtfs represents the ntfs filesystem type. FSTypeNtfs = "ntfs" ) -// constants for node k8s API use +// constants for node k8s API use. const ( - // AgentNotReadyNodeTaintKey contains the key of taints to be removed on driver startup + // AgentNotReadyNodeTaintKey contains the key of taints to be removed on driver startup. AgentNotReadyNodeTaintKey = "ebs.csi.aws.com/agent-not-ready" ) diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index b0683a3d2b..dc4e0ef081 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -37,14 +37,14 @@ import ( "k8s.io/klog/v2" ) -// Supported access modes +// Supported access modes. const ( SingleNodeWriter = csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER MultiNodeMultiWriter = csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER ) var ( - // controllerCaps represents the capability of controller service + // controllerCaps represents the capability of controller service. controllerCaps = []csi.ControllerServiceCapability_RPC_Type{ csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME, @@ -58,7 +58,7 @@ var ( const trueStr = "true" const isManagedByDriver = trueStr -// ControllerService represents the controller service of CSI driver +// ControllerService represents the controller service of CSI driver. type ControllerService struct { cloud cloud.Cloud inFlight *internal.InFlight @@ -68,7 +68,7 @@ type ControllerService struct { csi.UnimplementedControllerServer } -// NewControllerService creates a new controller service +// NewControllerService creates a new controller service. func NewControllerService(c cloud.Cloud, o *Options) *ControllerService { return &ControllerService{ cloud: c, @@ -1011,7 +1011,7 @@ func getVolSizeBytes(req *csi.CreateVolumeRequest) (int64, error) { return volSizeBytes, nil } -// BuildOutpostArn returns the string representation of the outpost ARN from the given csi.TopologyRequirement.segments +// BuildOutpostArn returns the string representation of the outpost ARN from the given csi.TopologyRequirement.segments. func BuildOutpostArn(segments map[string]string) string { if len(segments[AwsPartitionKey]) == 0 { return "" diff --git a/pkg/driver/controller_modify_volume.go b/pkg/driver/controller_modify_volume.go index 8e1f797251..10f40b8c7b 100644 --- a/pkg/driver/controller_modify_volume.go +++ b/pkg/driver/controller_modify_volume.go @@ -35,7 +35,7 @@ import ( const ( ModificationKeyVolumeType = "type" - // Retained for backwards compatibility, but not recommended + // Retained for backwards compatibility, but not recommended. DeprecatedModificationKeyVolumeType = "volumeType" ModificationKeyIOPS = "iops" diff --git a/pkg/driver/driver.go b/pkg/driver/driver.go index c8eeb17cd6..031da14d10 100644 --- a/pkg/driver/driver.go +++ b/pkg/driver/driver.go @@ -52,7 +52,7 @@ const ( AwsRegionKey = "topology." + DriverName + "/region" AwsOutpostIDKey = "topology." + DriverName + "/outpost-id" WellKnownZoneTopologyKey = "topology.kubernetes.io/zone" - // Deprecated: Use the WellKnownZoneTopologyKey instead + // Deprecated: Use the WellKnownZoneTopologyKey instead. ZoneTopologyKey = "topology." + DriverName + "/zone" OSTopologyKey = "kubernetes.io/os" ) diff --git a/pkg/driver/node.go b/pkg/driver/node.go index fd1a9478f2..0c4ab698e0 100644 --- a/pkg/driver/node.go +++ b/pkg/driver/node.go @@ -44,10 +44,10 @@ import ( ) const ( - // default file system type to be used when it is not provided + // default file system type to be used when it is not provided. defaultFsType = FSTypeExt4 - // VolumeOperationAlreadyExists is message fmt returned to CO when there is another in-flight call on the given volumeID + // VolumeOperationAlreadyExists is message fmt returned to CO when there is another in-flight call on the given volumeID. VolumeOperationAlreadyExists = "An operation with the given volume=%q is already in progress" // sbeDeviceVolumeAttachmentLimit refers to the maximum number of volumes that can be attached to an instance on snow. @@ -72,9 +72,9 @@ var ( csi.NodeServiceCapability_RPC_GET_VOLUME_STATS, } - // taintRemovalInitialDelay is the initial delay for node taint removal + // taintRemovalInitialDelay is the initial delay for node taint removal. taintRemovalInitialDelay = 1 * time.Second - // taintRemovalBackoff is the exponential backoff configuration for node taint removal + // taintRemovalBackoff is the exponential backoff configuration for node taint removal. taintRemovalBackoff = wait.Backoff{ Duration: 500 * time.Millisecond, Factor: 2, @@ -82,7 +82,7 @@ var ( } ) -// NodeService represents the node service of CSI driver +// NodeService represents the node service of CSI driver. type NodeService struct { metadata metadata.MetadataService mounter mounter.Mounter @@ -91,7 +91,7 @@ type NodeService struct { csi.UnimplementedNodeServer } -// NewNodeService creates a new node service +// NewNodeService creates a new node service. func NewNodeService(o *Options, md metadata.MetadataService, m mounter.Mounter, k kubernetes.Interface) *NodeService { if k != nil { // Remove taint from node to indicate driver startup success @@ -761,7 +761,7 @@ func (d *NodeService) nodePublishVolumeForFileSystem(req *csi.NodePublishVolumeR return nil } -// getVolumesLimit returns the limit of volumes that the node supports +// getVolumesLimit returns the limit of volumes that the node supports. func (d *NodeService) getVolumesLimit() int64 { if d.options.VolumeAttachLimit >= 0 { return d.options.VolumeAttachLimit @@ -839,14 +839,14 @@ func collectMountOptions(fsType string, mntFlags []string) []string { return options } -// Struct for JSON patch operations +// Struct for JSON patch operations. type JSONPatch struct { OP string `json:"op,omitempty"` Path string `json:"path,omitempty"` Value interface{} `json:"value"` } -// removeTaintInBackground is a goroutine that retries removeNotReadyTaint with exponential backoff +// removeTaintInBackground is a goroutine that retries removeNotReadyTaint with exponential backoff. func removeTaintInBackground(k8sClient kubernetes.Interface, backoff wait.Backoff, removalFunc func(kubernetes.Interface) error) { backoffErr := wait.ExponentialBackoff(backoff, func() (bool, error) { err := removalFunc(k8sClient) diff --git a/pkg/driver/request_coalescing_test.go b/pkg/driver/request_coalescing_test.go index dabb2d4706..2b7b2c2c64 100644 --- a/pkg/driver/request_coalescing_test.go +++ b/pkg/driver/request_coalescing_test.go @@ -426,7 +426,7 @@ func testDuplicateRequest(t *testing.T, executor modifyVolumeExecutor) { wg.Wait() } -// TestResponseReturnTiming tests the caller of request coalescing blocking until receiving response from cloud.ResizeOrModifyDisk +// TestResponseReturnTiming tests the caller of request coalescing blocking until receiving response from cloud.ResizeOrModifyDisk. func testResponseReturnTiming(t *testing.T, executor modifyVolumeExecutor) { t.Helper() const NewVolumeType = "gp3" diff --git a/pkg/driver/version.go b/pkg/driver/version.go index f168fa4e2d..7adb7b9bac 100644 --- a/pkg/driver/version.go +++ b/pkg/driver/version.go @@ -22,7 +22,7 @@ import ( "runtime" ) -// These are set during build time via -ldflags +// These are set during build time via -ldflags. var ( driverVersion string gitCommit string diff --git a/pkg/expiringcache/expiring_cache.go b/pkg/expiringcache/expiring_cache.go index 962301d12d..74cb5c7c1d 100644 --- a/pkg/expiringcache/expiring_cache.go +++ b/pkg/expiringcache/expiring_cache.go @@ -30,7 +30,7 @@ import ( // // From the consumer's perspective, it behaves similarly to a map // KeyType is the type of the object that is used as a key -// ValueType is the type of the object that is stored +// ValueType is the type of the object that is stored. type ExpiringCache[KeyType comparable, ValueType any] interface { // Get operates identically to retrieving from a map, returning // the value and/or boolean indicating if the value existed in the map @@ -55,7 +55,7 @@ type expiringCache[KeyType comparable, ValueType any] struct { } // New returns a new ExpiringCache -// for a given KeyType, ValueType, and expiration delay +// for a given KeyType, ValueType, and expiration delay. func New[KeyType comparable, ValueType any](expirationDelay time.Duration) ExpiringCache[KeyType, ValueType] { return &expiringCache[KeyType, ValueType]{ expirationDelay: expirationDelay, diff --git a/pkg/mounter/mount.go b/pkg/mounter/mount.go index c75bfb4c00..344224522e 100644 --- a/pkg/mounter/mount.go +++ b/pkg/mounter/mount.go @@ -27,7 +27,7 @@ import ( // A mix & match of functions defined in upstream libraries. (FormatAndMount // from struct SafeFormatAndMount, PathExists from an old edition of // mount.Interface). Define it explicitly so that it can be mocked and to -// insulate from oft-changing upstream interfaces/structs +// insulate from oft-changing upstream interfaces/structs. type Mounter interface { mountutils.Interface diff --git a/pkg/util/template/funcs.go b/pkg/util/template/funcs.go index 7880b80912..3195138455 100644 --- a/pkg/util/template/funcs.go +++ b/pkg/util/template/funcs.go @@ -21,7 +21,7 @@ import ( "strings" ) -// Disable functions +// Disable functions. func html(...interface{}) (string, error) { return "", errors.New("cannot call 'html' function") } diff --git a/pkg/util/util.go b/pkg/util/util.go index 1d34932cf7..346dde5d5a 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -41,13 +41,13 @@ var ( isMACAddressRegex = regexp.MustCompile(`([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})`) ) -// RoundUpBytes rounds up the volume size in bytes up to multiplications of GiB +// RoundUpBytes rounds up the volume size in bytes up to multiplications of GiB. func RoundUpBytes(volumeSizeBytes int64) int64 { return roundUpSize(volumeSizeBytes, GiB) * GiB } // RoundUpGiB rounds up the volume size in bytes upto multiplications of GiB -// in the unit of GiB +// in the unit of GiB. func RoundUpGiB(volumeSizeBytes int64) (int32, error) { result := roundUpSize(volumeSizeBytes, GiB) if result > int64(math.MaxInt32) { @@ -57,7 +57,7 @@ func RoundUpGiB(volumeSizeBytes int64) (int32, error) { return int32(result), nil } -// BytesToGiB converts Bytes to GiB +// BytesToGiB converts Bytes to GiB. func BytesToGiB(volumeSizeBytes int64) int32 { result := volumeSizeBytes / GiB if result > int64(math.MaxInt32) { @@ -68,7 +68,7 @@ func BytesToGiB(volumeSizeBytes int64) int32 { return int32(result) } -// GiBToBytes converts GiB to Bytes +// GiBToBytes converts GiB to Bytes. func GiBToBytes(volumeSizeGiB int32) int64 { return int64(volumeSizeGiB) * GiB } @@ -136,18 +136,18 @@ func IsSBE(region string) bool { return region == "snow" } -// StringIsAlphanumeric returns true if a given string contains only English letters or numbers +// StringIsAlphanumeric returns true if a given string contains only English letters or numbers. func StringIsAlphanumeric(s string) bool { return isAlphanumericRegex(s) } -// CountMACAddresses returns the amount of MAC addresses within a string +// CountMACAddresses returns the amount of MAC addresses within a string. func CountMACAddresses(s string) int { matches := isMACAddressRegex.FindAllStringIndex(s, -1) return len(matches) } -// NormalizeWindowsPath normalizes a Windows path +// NormalizeWindowsPath normalizes a Windows path. func NormalizeWindowsPath(path string) string { normalizedPath := strings.ReplaceAll(path, "/", "\\") if strings.HasPrefix(normalizedPath, "\\") { diff --git a/tests/e2e/driver/driver.go b/tests/e2e/driver/driver.go index b3faa749c4..0fff49879c 100644 --- a/tests/e2e/driver/driver.go +++ b/tests/e2e/driver/driver.go @@ -32,13 +32,13 @@ type PVTestDriver interface { VolumeSnapshotTestDriver } -// DynamicPVTestDriver represents an interface for a CSI driver that supports DynamicPV +// DynamicPVTestDriver represents an interface for a CSI driver that supports DynamicPV. type DynamicPVTestDriver interface { // GetDynamicProvisionStorageClass returns a StorageClass dynamic provision Persistent Volume GetDynamicProvisionStorageClass(parameters map[string]string, mountOptions []string, reclaimPolicy *v1.PersistentVolumeReclaimPolicy, volumeExpansion *bool, bindingMode *storagev1.VolumeBindingMode, allowedTopologyValues []string, namespace string) *storagev1.StorageClass } -// PreProvisionedVolumeTestDriver represents an interface for a CSI driver that supports pre-provisioned volume +// PreProvisionedVolumeTestDriver represents an interface for a CSI driver that supports pre-provisioned volume. type PreProvisionedVolumeTestDriver interface { // GetPersistentVolume returns a PersistentVolume with pre-provisioned volumeHandle GetPersistentVolume(volumeID string, fsType string, size string, reclaimPolicy *v1.PersistentVolumeReclaimPolicy, namespace string, accessMode v1.PersistentVolumeAccessMode, volumeMode v1.PersistentVolumeMode) *v1.PersistentVolume diff --git a/tests/e2e/driver/ebs_csi_driver.go b/tests/e2e/driver/ebs_csi_driver.go index 52bfb9fabb..efab4571e3 100644 --- a/tests/e2e/driver/ebs_csi_driver.go +++ b/tests/e2e/driver/ebs_csi_driver.go @@ -29,12 +29,12 @@ const ( True = "true" ) -// Implement DynamicPVTestDriver interface +// Implement DynamicPVTestDriver interface. type ebsCSIDriver struct { driverName string } -// InitEbsCSIDriver returns ebsCSIDriver that implements DynamicPVTestDriver interface +// InitEbsCSIDriver returns ebsCSIDriver that implements DynamicPVTestDriver interface. func InitEbsCSIDriver() PVTestDriver { return &ebsCSIDriver{ driverName: ebscsidriver.DriverName, @@ -107,7 +107,7 @@ func (d *ebsCSIDriver) GetPersistentVolume(volumeID string, fsType string, size } } -// MinimumSizeForVolumeType returns the minimum disk size for each volumeType +// MinimumSizeForVolumeType returns the minimum disk size for each volumeType. func MinimumSizeForVolumeType(volumeType string) string { switch volumeType { case "st1", "sc1": diff --git a/tests/e2e/pre_provsioning.go b/tests/e2e/pre_provsioning.go index 6a3c82dfbd..a95e974561 100644 --- a/tests/e2e/pre_provsioning.go +++ b/tests/e2e/pre_provsioning.go @@ -48,7 +48,7 @@ var ( defaultDiskSizeBytes int64 = defaultDiskSize * 1024 * 1024 * 1024 ) -// Requires env AWS_AVAILABILITY_ZONES a comma separated list of AZs to be set +// Requires env AWS_AVAILABILITY_ZONES a comma separated list of AZs to be set. var _ = Describe("[ebs-csi-e2e] [single-az] Pre-Provisioned", func() { f := framework.NewDefaultFramework("ebs") f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged diff --git a/tests/e2e/requires_aws_api.go b/tests/e2e/requires_aws_api.go index 682593e292..c5a5696514 100644 --- a/tests/e2e/requires_aws_api.go +++ b/tests/e2e/requires_aws_api.go @@ -39,7 +39,7 @@ import ( const testTagNamePrefix = "testTag" const testTagValue = "3.1415926" -// generateTagName appends a random uuid to tag name to prevent clashes on parallel e2e test runs on shared cluster +// generateTagName appends a random uuid to tag name to prevent clashes on parallel e2e test runs on shared cluster. func generateTagName() string { return testTagNamePrefix + uuid.NewString()[:8] } diff --git a/tests/e2e/testsuites/dynamically_provisioned_cmd_volume_tester.go b/tests/e2e/testsuites/dynamically_provisioned_cmd_volume_tester.go index d28f2a60a8..f776e2f289 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_cmd_volume_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_cmd_volume_tester.go @@ -23,7 +23,7 @@ import ( // DynamicallyProvisionedCmdVolumeTest will provision required StorageClass(es), PVC(s) and Pod(s) // Waiting for the PV provisioner to create a new PV -// Testing if the Pod(s) Cmd is run with a 0 exit code +// Testing if the Pod(s) Cmd is run with a 0 exit code. type DynamicallyProvisionedCmdVolumeTest struct { CSIDriver driver.DynamicPVTestDriver Pods []PodDetails diff --git a/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go b/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go index 487ef47c3a..d4bd17ea17 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_collocated_pod_tester.go @@ -23,7 +23,7 @@ import ( // DynamicallyProvisionedCollocatedPodTest will provision required StorageClass(es), PVC(s) and Pod(s) // Waiting for the PV provisioner to create a new PV -// Testing if multiple Pod(s) can write simultaneously +// Testing if multiple Pod(s) can write simultaneously. type DynamicallyProvisionedCollocatedPodTest struct { CSIDriver driver.DynamicPVTestDriver Pods []PodDetails diff --git a/tests/e2e/testsuites/dynamically_provisioned_delete_pod_tester.go b/tests/e2e/testsuites/dynamically_provisioned_delete_pod_tester.go index 20502c43b7..88ce4355f8 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_delete_pod_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_delete_pod_tester.go @@ -23,7 +23,7 @@ import ( // DynamicallyProvisionedDeletePodTest will provision required StorageClass and Deployment // Testing if the Pod can write and read to mounted volumes -// Deleting a pod, and again testing if the Pod can write and read to mounted volumes +// Deleting a pod, and again testing if the Pod can write and read to mounted volumes. type DynamicallyProvisionedDeletePodTest struct { CSIDriver driver.DynamicPVTestDriver Pod PodDetails diff --git a/tests/e2e/testsuites/dynamically_provisioned_read_only_volume_tester.go b/tests/e2e/testsuites/dynamically_provisioned_read_only_volume_tester.go index 92de0c68f9..0ada41e7ef 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_read_only_volume_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_read_only_volume_tester.go @@ -29,7 +29,7 @@ const expectedReadOnlyLog = "Read-only file system" // DynamicallyProvisionedReadOnlyVolumeTest will provision required StorageClass(es), PVC(s) and Pod(s) // Waiting for the PV provisioner to create a new PV -// Testing that the Pod(s) cannot write to the volume when mounted +// Testing that the Pod(s) cannot write to the volume when mounted. type DynamicallyProvisionedReadOnlyVolumeTest struct { CSIDriver driver.DynamicPVTestDriver Pods []PodDetails diff --git a/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go b/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go index 3211c18031..fb94bbe7d2 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_reclaim_policy_tester.go @@ -22,7 +22,7 @@ import ( ) // DynamicallyProvisionedReclaimPolicyTest will provision required PV(s) and PVC(s) -// Testing the correct behavior for different reclaimPolicies +// Testing the correct behavior for different reclaimPolicies. type DynamicallyProvisionedReclaimPolicyTest struct { CSIDriver driver.DynamicPVTestDriver Volumes []VolumeDetails diff --git a/tests/e2e/testsuites/dynamically_provisioned_topology_aware_volume_tester.go b/tests/e2e/testsuites/dynamically_provisioned_topology_aware_volume_tester.go index 1d64f77c2b..e4d24ee8e6 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_topology_aware_volume_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_topology_aware_volume_tester.go @@ -26,7 +26,7 @@ import ( // DynamicallyProvisionedTopologyAwareVolumeTest will provision required StorageClass(es), PVC(s) and Pod(s) // Waiting for the PV provisioner to create a new PV // Testing if the Pod(s) can write and read to mounted volumes -// Validate PVs have expected PV nodeAffinity +// Validate PVs have expected PV nodeAffinity. type DynamicallyProvisionedTopologyAwareVolumeTest struct { CSIDriver driver.DynamicPVTestDriver Pods []PodDetails diff --git a/tests/e2e/testsuites/dynamically_provisioned_volume_snapshot_tester.go b/tests/e2e/testsuites/dynamically_provisioned_volume_snapshot_tester.go index 23880778bf..e28405f875 100644 --- a/tests/e2e/testsuites/dynamically_provisioned_volume_snapshot_tester.go +++ b/tests/e2e/testsuites/dynamically_provisioned_volume_snapshot_tester.go @@ -28,7 +28,7 @@ import ( // Testing if the Pod(s) can write and read to mounted volumes // Create a snapshot, validate the data is still on the disk, and then write and read to it again // And finally delete the snapshot -// This test only supports a single volume +// This test only supports a single volume. type DynamicallyProvisionedVolumeSnapshotTest struct { CSIDriver driver.PVTestDriver Pod PodDetails diff --git a/tests/e2e/testsuites/e2e_utils.go b/tests/e2e/testsuites/e2e_utils.go index bb733acc6f..0c5a03d8c4 100644 --- a/tests/e2e/testsuites/e2e_utils.go +++ b/tests/e2e/testsuites/e2e_utils.go @@ -51,22 +51,22 @@ var DefaultGeneratedVolumeMount = VolumeMountDetails{ MountPathGenerate: "/mnt/test-", } -// PodCmdWriteToVolume returns pod command that would write to mounted volume +// PodCmdWriteToVolume returns pod command that would write to mounted volume. func PodCmdWriteToVolume(volumeMountPath string) string { return fmt.Sprintf("echo 'hello world' >> %s/data && grep 'hello world' %s/data && sync", volumeMountPath, volumeMountPath) } -// PodCmdContinuousWrite returns pod command that would continuously write to mounted volume +// PodCmdContinuousWrite returns pod command that would continuously write to mounted volume. func PodCmdContinuousWrite(volumeMountPath string) string { return fmt.Sprintf("while true; do echo \"$(date -u)\" >> /%s/out.txt; sleep 5; done", volumeMountPath) } -// PodCmdGrepVolumeData returns pod command that would check that a volume was written to by PodCmdWriteToVolume +// PodCmdGrepVolumeData returns pod command that would check that a volume was written to by PodCmdWriteToVolume. func PodCmdGrepVolumeData(volumeMountPath string) string { return fmt.Sprintf("grep 'hello world' %s/data", volumeMountPath) } -// IncreasePvcObjectStorage increases `storage` of a K8s PVC object by specified Gigabytes +// IncreasePvcObjectStorage increases `storage` of a K8s PVC object by specified Gigabytes. func IncreasePvcObjectStorage(pvc *v1.PersistentVolumeClaim, sizeIncreaseGi int32) resource.Quantity { pvcSize := pvc.Spec.Resources.Requests["storage"] delta := resource.Quantity{} @@ -76,7 +76,7 @@ func IncreasePvcObjectStorage(pvc *v1.PersistentVolumeClaim, sizeIncreaseGi int3 return pvcSize } -// WaitForPvToResize waiting for pvc size to be resized to desired size +// WaitForPvToResize waiting for pvc size to be resized to desired size. func WaitForPvToResize(c clientset.Interface, ns *v1.Namespace, pvName string, desiredSize resource.Quantity, timeout time.Duration, interval time.Duration) error { framework.Logf("waiting up to %v for pv resize in namespace %q to be complete", timeout, ns.Name) for start := time.Now(); time.Since(start) < timeout; time.Sleep(interval) { @@ -90,7 +90,7 @@ func WaitForPvToResize(c clientset.Interface, ns *v1.Namespace, pvName string, d return fmt.Errorf("gave up after waiting %v for pv %q to complete resizing", timeout, pvName) } -// ResizeTestPvc increases size of given `TestPersistentVolumeClaim` by specified Gigabytes +// ResizeTestPvc increases size of given `TestPersistentVolumeClaim` by specified Gigabytes. func ResizeTestPvc(client clientset.Interface, namespace *v1.Namespace, testPvc *TestPersistentVolumeClaim, sizeIncreaseGi int32) (updatedSize resource.Quantity) { framework.Logf("getting pvc name: %v", testPvc.persistentVolumeClaim.Name) pvc, _ := client.CoreV1().PersistentVolumeClaims(namespace.Name).Get(context.TODO(), testPvc.persistentVolumeClaim.Name, metav1.GetOptions{}) @@ -110,14 +110,14 @@ func ResizeTestPvc(client clientset.Interface, namespace *v1.Namespace, testPvc return updatedSize } -// AnnotatePvc annotates supplied k8s pvc object with supplied annotations +// AnnotatePvc annotates supplied k8s pvc object with supplied annotations. func AnnotatePvc(pvc *v1.PersistentVolumeClaim, annotations map[string]string) { for annotation, value := range annotations { pvc.Annotations[annotation] = value } } -// CheckPvAnnotations checks whether supplied k8s pv object contains supplied annotations +// CheckPvAnnotations checks whether supplied k8s pv object contains supplied annotations. func CheckPvAnnotations(pv *v1.PersistentVolume, annotations map[string]string) bool { for annotation, value := range annotations { if pv.Annotations[annotation] != value { @@ -127,7 +127,7 @@ func CheckPvAnnotations(pv *v1.PersistentVolume, annotations map[string]string) return true } -// WaitForPvToModify waiting for PV to be modified +// WaitForPvToModify waiting for PV to be modified. func WaitForPvToModify(c clientset.Interface, ns *v1.Namespace, pvName string, expectedAnnotations map[string]string, timeout time.Duration, interval time.Duration) error { framework.Logf("waiting up to %v for pv in namespace %q to be modified", timeout, ns.Name) @@ -142,7 +142,7 @@ func WaitForPvToModify(c clientset.Interface, ns *v1.Namespace, pvName string, e return fmt.Errorf("gave up after waiting %v for pv %q to complete modifying", timeout, pvName) } -// WaitForVacToApplyToPv waits for a PV's VAC to match the PVC's VAC +// WaitForVacToApplyToPv waits for a PV's VAC to match the PVC's VAC. func WaitForVacToApplyToPv(c clientset.Interface, ns *v1.Namespace, pvName string, expectedVac string, timeout time.Duration, interval time.Duration) error { framework.Logf("waiting up to %v for pv in namespace %q to be modified via VAC", timeout, ns.Name) diff --git a/tests/e2e/testsuites/pre_provisioned_read_only_volume_tester.go b/tests/e2e/testsuites/pre_provisioned_read_only_volume_tester.go index 04156914dd..2db452a027 100644 --- a/tests/e2e/testsuites/pre_provisioned_read_only_volume_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_read_only_volume_tester.go @@ -26,7 +26,7 @@ import ( ) // PreProvisionedReadOnlyVolumeTest will provision required PV(s), PVC(s) and Pod(s) -// Testing that the Pod(s) cannot write to the volume when mounted +// Testing that the Pod(s) cannot write to the volume when mounted. type PreProvisionedReadOnlyVolumeTest struct { CSIDriver driver.PreProvisionedVolumeTestDriver Pods []PodDetails diff --git a/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go b/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go index 9a9538a713..c3e0b7b5ac 100644 --- a/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_reclaim_policy_tester.go @@ -21,7 +21,7 @@ import ( ) // PreProvisionedReclaimPolicyTest will provision required PV(s) and PVC(s) -// Testing the correct behavior for different reclaimPolicies +// Testing the correct behavior for different reclaimPolicies. type PreProvisionedReclaimPolicyTest struct { CSIDriver driver.PreProvisionedVolumeTestDriver Volumes []VolumeDetails diff --git a/tests/e2e/testsuites/pre_provisioned_volume_tester.go b/tests/e2e/testsuites/pre_provisioned_volume_tester.go index 30143fd38f..56ec2cd119 100644 --- a/tests/e2e/testsuites/pre_provisioned_volume_tester.go +++ b/tests/e2e/testsuites/pre_provisioned_volume_tester.go @@ -22,7 +22,7 @@ import ( ) // PreProvisionedVolumeTest will provision required PV(s), PVC(s) and Pod(s) -// Testing if the Pod(s) can write and read to mounted volumes +// Testing if the Pod(s) can write and read to mounted volumes. type PreProvisionedVolumeTest struct { CSIDriver driver.PreProvisionedVolumeTestDriver Pods []PodDetails diff --git a/tests/e2e/testsuites/testsuites.go b/tests/e2e/testsuites/testsuites.go index f67c7e61e1..32ffe0f43b 100644 --- a/tests/e2e/testsuites/testsuites.go +++ b/tests/e2e/testsuites/testsuites.go @@ -47,7 +47,7 @@ const ( execTimeout = 10 * time.Second // Some pods can take much longer to get ready due to volume attach/detach latency. slowPodStartTimeout = 15 * time.Minute - // Description that will printed during tests + // Description that will printed during tests. failedConditionDescription = "Error status code" volumeSnapshotNameStatic = "volume-snapshot-tester" @@ -665,7 +665,7 @@ func (t *TestPod) WaitForRunning() { } // Ideally this would be in "k8s.io/kubernetes/test/e2e/framework" -// Similar to framework.WaitForPodSuccessInNamespace +// Similar to framework.WaitForPodSuccessInNamespace. var podFailedCondition = func(pod *v1.Pod) (bool, error) { switch pod.Status.Phase { case v1.PodFailed: From c0949b3d8e91bef625a93a2b4d5143d69dd07977 Mon Sep 17 00:00:00 2001 From: Drew Sirenko <68304519+AndrewSirenko@users.noreply.github.com> Date: Fri, 1 Nov 2024 09:50:59 -0400 Subject: [PATCH 24/24] Enable golang-ci linters --- .golangci.yml | 129 +++++++------------------------- cmd/main.go | 4 +- pkg/driver/controller.go | 4 +- pkg/mounter/mount_linux.go | 34 ++++----- pkg/mounter/mount_linux_test.go | 4 - pkg/util/util_test.go | 5 +- 6 files changed, 49 insertions(+), 131 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a99c01395d..f77b17e7c5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -35,113 +35,36 @@ linters-settings: - "github.com/onsi/gomega" - "github.com/onsi/ginkgo/v2" linters: - enable: - - asasalint - - asciicheck - - bidichk - - bodyclose - - canonicalheader - - containedctx - - contextcheck - - copyloopvar - - decorder - - dogsled - - dupword - - durationcheck - - errcheck - - errchkjson - - errname - - errorlint - - exhaustive - - fatcontext - - forbidigo - - forcetypeassert - - gci - - ginkgolinter - - gocheckcompilerdirectives - - gochecksumtype - - gocritic - - gofmt - - goheader - - goimports - - gomodguard - - goprintffuncname - - gosimple - - gosmopolitan - - grouper - - importas - - inamedparam - - ineffassign - - intrange - - loggercheck - - makezero - - mirror - - misspell - - musttag - - nakedret - - nilerr - - nilnil - - noctx - - nolintlint - - nosprintfhostport - - prealloc - - predeclared - - promlinter - - protogetter - - reassign - - rowserrcheck - - sloglint - - spancheck - - sqlclosecheck - - staticcheck - - stylecheck - - tagalign - - tagliatelle - - tenv - - testableexamples - - testifylint - - thelper - - tparallel - - unconvert - - unparam - - unused - - usestdlibvars - - wastedassign - - whitespace - - zerologlint - - goconst - - gochecknoinits - - perfsprint - - revive - - gosec - - godot # End all godoc comments with period + enable-all: true disable: - govet # We already run with `make verify/govet` # We do not use - - gomoddirectives # We need `replace` in `go.mod` + - cyclop # Cyclomatic complexity - depguard # We don't guard against dependencies - - testpackage # Require separate test package to catch leaky unexported dependencies + - dupl # Tracks code duplication. Too much duplication in tests. False positives in non-tests + - execinquery # Deprecated but enabled-by-default + - exhaustruct # Explicitly instantiating all structs is painful for K8s structs + - exportloopref # Deprecated but enabled-by-default - funlen # Long func names happen - - varnamelen # Long var names happen - - maintidx # Maintainability index - - cyclop # Cyclomatic complexity - gocognit # Cognitive complexity - - wsl # Too strict of a whitespace linter - # TODO Investigate FIXME - - err113 # TODO FIXME do not create errors dynamically from scratch. Instead wrap static (package-level) error. 2 points - - wrapcheck # TODO ^^ - - gochecknoglobals # TODO FIXME A whole 2 point task on its own - - paralleltest # TODO FIXME (Too many tests that aren't parallelized right now, probably half a day to attempt to mark the ones we can) - - nestif # TODO whole refactoring/readability task, should split up - - godox # TODO FIXME audit our project TODOs - # TODO Q: Consult with team + - gocyclo # Cyclomatic complexity + - gofumpt # We don't rely on gofumpt + - gomnd # Magic Number Detection. Many false positives. + - gomoddirectives # We need `replace` in `go.mod` + - interfacebloat # No more than 10 interface methods + - ireturn # Accept interfaces return concrete types - lll # Limit line length - - gofumpt # Rely on gofumpt's stricter formatting opinions - - ireturn # Always accept interfaces return concrete types (gopherism) - - nlreturn # Always have emptyline before return - - nonamedreturns # No using named returns in functions. Maybe nolint a few of the places it actually leads to cleaner code - - exhaustruct # Forces you to explicitly instantiate all structs. REALLY painful for kubernetes structs... - - interfacebloat # Cloud and Mounter have 15 methods instead of linter's recommended 10... Smell or necessary? We can nolint specific ones - - mnd # Magic Number Detection. Many false positives, still worth with nolint? - - gomnd # Many false positives, still worth with nolint? - - dupl # Tracks code duplication. Brutal amount of duplication in tests. False positives in non-tests. + - maintidx # Maintainability index + - mnd # Magic Number Detection. Many false positives + - nestif # Don't allow too many nested if statements + - nlreturn # Always have empty line before return + - testpackage # Require separate test package to catch leaky unexported dependencies + - varnamelen # Long var names happen + - wsl # Too strict of a whitespace linter + # Consider adding in future + - err113 # Do not create errors dynamically from scratch. Instead, wrap static (package-level) error. + - wrapcheck # Same as err113 + - gochecknoglobals # Do not allow global variables + - godox # Do not allow TODOs + - nonamedreturns # Need to nolint/refactor a few places our code + - paralleltest # There are many tests that aren't parallelized diff --git a/cmd/main.go b/cmd/main.go index f0fe09abbf..3579f23350 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -18,6 +18,7 @@ package main import ( "context" + "fmt" "os" "strings" "time" @@ -107,7 +108,8 @@ func main() { klog.ErrorS(err, "failed to get version") klog.FlushAndExit(klog.ExitFlushTimeout, 1) } - klog.Info(versionInfo) + //nolint:forbidigo // Print version info without klog/timestamp + fmt.Println(versionInfo) os.Exit(0) } diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index dc4e0ef081..352cc278c2 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -486,11 +486,11 @@ func (d *ControllerService) ControllerGetCapabilities(ctx context.Context, req * klog.V(4).InfoS("ControllerGetCapabilities: called", "args", req) caps := make([]*csi.ControllerServiceCapability, 0, len(controllerCaps)) - for _, cap := range controllerCaps { + for _, capability := range controllerCaps { c := &csi.ControllerServiceCapability{ Type: &csi.ControllerServiceCapability_Rpc{ Rpc: &csi.ControllerServiceCapability_RPC{ - Type: cap, + Type: capability, }, }, } diff --git a/pkg/mounter/mount_linux.go b/pkg/mounter/mount_linux.go index ccb9d0863b..82cce121f1 100644 --- a/pkg/mounter/mount_linux.go +++ b/pkg/mounter/mount_linux.go @@ -54,9 +54,9 @@ func NewSafeMounterV2() (*mountutils.SafeFormatAndMount, error) { // FindDevicePath finds path of device and verifies its existence // if the device is not nvme, return the path directly -// if the device is nvme, finds and returns the nvme device path eg. /dev/nvme1n1 +// if the device is nvme, finds and returns the nvme device path eg. /dev/nvme1n1. func (m *NodeMounter) FindDevicePath(devicePath, volumeID, partition, region string) (string, error) { - strippedVolumeName := strings.Replace(volumeID, "-", "", -1) + strippedVolumeName := strings.ReplaceAll(volumeID, "-", "") canonicalDevicePath := "" // If the given path exists, the device MAY be nvme. Further, it MAY be a @@ -132,7 +132,7 @@ func (m *NodeMounter) FindDevicePath(devicePath, volumeID, partition, region str } // findNvmeVolume looks for the nvme volume with the specified name -// It follows the symlink (if it exists) and returns the absolute path to the device +// It follows the symlink (if it exists) and returns the absolute path to the device. func findNvmeVolume(findName string) (device string, err error) { p := filepath.Join("/dev/disk/by-id/", findName) stat, err := os.Lstat(p) @@ -163,12 +163,12 @@ func findNvmeVolume(findName string) (device string, err error) { } // execRunner is a helper to inject exec.Comamnd().CombinedOutput() for verifyVolumeSerialMatch -// Tests use a mocked version that does not actually execute any binaries +// Tests use a mocked version that does not actually execute any binaries. func execRunner(name string, arg ...string) ([]byte, error) { return exec.Command(name, arg...).CombinedOutput() } -// verifyVolumeSerialMatch checks the volume serial of the device against the expected volume +// verifyVolumeSerialMatch checks the volume serial of the device against the expected volume. func verifyVolumeSerialMatch(canonicalDevicePath string, strippedVolumeName string, execRunner func(string, ...string) ([]byte, error)) error { // In some rare cases, a race condition can lead to the /dev/disk/by-id/ symlink becoming out of date // See https://github.com/kubernetes-sigs/aws-ebs-csi-driver/issues/1224 for more info @@ -194,7 +194,7 @@ func verifyVolumeSerialMatch(canonicalDevicePath string, strippedVolumeName stri return nil } -// PreparePublishTarget creates the target directory for the volume to be mounted +// PreparePublishTarget creates the target directory for the volume to be mounted. func (m *NodeMounter) PreparePublishTarget(target string) error { klog.V(4).InfoS("NodePublishVolume: creating dir", "target", target) if err := m.MakeDir(target); err != nil { @@ -203,7 +203,7 @@ func (m *NodeMounter) PreparePublishTarget(target string) error { return nil } -// IsBlockDevice checks if the given path is a block device +// IsBlockDevice checks if the given path is a block device. func (m *NodeMounter) IsBlockDevice(fullPath string) (bool, error) { var st unix.Stat_t err := unix.Stat(fullPath, &st) @@ -214,7 +214,7 @@ func (m *NodeMounter) IsBlockDevice(fullPath string) (bool, error) { return (st.Mode & unix.S_IFMT) == unix.S_IFBLK, nil } -// GetBlockSizeBytes gets the size of the disk in bytes +// GetBlockSizeBytes gets the size of the disk in bytes. func (m *NodeMounter) GetBlockSizeBytes(devicePath string) (int64, error) { output, err := m.Exec.Command("blockdev", "--getsize64", devicePath).Output() if err != nil { @@ -228,7 +228,7 @@ func (m *NodeMounter) GetBlockSizeBytes(devicePath string) (int64, error) { return gotSizeBytes, nil } -// appendPartition appends the partition to the device path +// appendPartition appends the partition to the device path. func (m *NodeMounter) appendPartition(devicePath, partition string) string { if partition == "" { return devicePath @@ -246,13 +246,13 @@ func (m NodeMounter) GetDeviceNameFromMount(mountPath string) (string, int, erro return mountutils.GetDeviceNameFromMount(m, mountPath) } -// IsCorruptedMnt return true if err is about corrupted mount point +// IsCorruptedMnt return true if err is about corrupted mount point. func (m NodeMounter) IsCorruptedMnt(err error) bool { return mountutils.IsCorruptedMnt(err) } // This function is mirrored in ./sanity_test.go to make sure sanity test covered this block of code -// Please mirror the change to func MakeFile in ./sanity_test.go +// Please mirror the change to func MakeFile in ./sanity_test.go. func (m *NodeMounter) MakeFile(path string) error { f, err := os.OpenFile(path, os.O_CREATE, os.FileMode(0644)) if err != nil { @@ -267,7 +267,7 @@ func (m *NodeMounter) MakeFile(path string) error { } // This function is mirrored in ./sanity_test.go to make sure sanity test covered this block of code -// Please mirror the change to func MakeFile in ./sanity_test.go +// Please mirror the change to func MakeFile in ./sanity_test.go. func (m *NodeMounter) MakeDir(path string) error { err := os.MkdirAll(path, os.FileMode(0755)) if err != nil { @@ -279,28 +279,28 @@ func (m *NodeMounter) MakeDir(path string) error { } // This function is mirrored in ./sanity_test.go to make sure sanity test covered this block of code -// Please mirror the change to func MakeFile in ./sanity_test.go +// Please mirror the change to func MakeFile in ./sanity_test.go. func (m *NodeMounter) PathExists(path string) (bool, error) { return mountutils.PathExists(path) } -// Resize resizes the filesystem of the given devicePath +// Resize resizes the filesystem of the given devicePath. func (m *NodeMounter) Resize(devicePath, deviceMountPath string) (bool, error) { return mountutils.NewResizeFs(m.Exec).Resize(devicePath, deviceMountPath) } -// NeedResize checks if the filesystem of the given devicePath needs to be resized +// NeedResize checks if the filesystem of the given devicePath needs to be resized. func (m *NodeMounter) NeedResize(devicePath string, deviceMountPath string) (bool, error) { return mountutils.NewResizeFs(m.Exec).NeedResize(devicePath, deviceMountPath) } -// Unpublish unmounts the given path +// Unpublish unmounts the given path. func (m *NodeMounter) Unpublish(path string) error { // On linux, unpublish and unstage both perform an unmount return m.Unstage(path) } -// Unstage unmounts the given path +// Unstage unmounts the given path. func (m *NodeMounter) Unstage(path string) error { err := mountutils.CleanupMountPoint(path, m, false) // Ignore the error when it contains "not mounted", because that indicates the diff --git a/pkg/mounter/mount_linux_test.go b/pkg/mounter/mount_linux_test.go index c36826642f..ebb4f70b46 100644 --- a/pkg/mounter/mount_linux_test.go +++ b/pkg/mounter/mount_linux_test.go @@ -27,7 +27,6 @@ import ( "github.com/stretchr/testify/assert" "k8s.io/mount-utils" - utilexec "k8s.io/utils/exec" fakeexec "k8s.io/utils/exec/testing" ) @@ -138,7 +137,6 @@ func TestMakeFile(t *testing.T) { if exists, err := mountObj.PathExists(targetPath); !exists { t.Fatalf("Expect no error but got: %v", err) } - } func TestPathExists(t *testing.T) { @@ -165,7 +163,6 @@ func TestPathExists(t *testing.T) { if exists { t.Fatalf("Expected file %s to not exist", targetPath) } - } func TestGetDeviceName(t *testing.T) { @@ -186,7 +183,6 @@ func TestGetDeviceName(t *testing.T) { if _, _, err := mountObj.GetDeviceNameFromMount(targetPath); err != nil { t.Fatalf("Expect no error but got: %v", err) } - } func TestFindDevicePath(t *testing.T) { diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 56aac97efe..2d9f6cf760 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -24,9 +24,8 @@ import ( "reflect" "testing" - "github.com/stretchr/testify/assert" - csi "github.com/container-storage-interface/spec/lib/go/csi" + "github.com/stretchr/testify/assert" ) func TestRoundUpBytes(t *testing.T) { @@ -119,7 +118,6 @@ func TestParseEndpoint(t *testing.T) { if err.Error() != tc.expErr.Error() { t.Fatalf("Expecting err: expected %v, got %v", tc.expErr, err) } - } else { if err != nil { t.Fatalf("err is not nil. got: %v", err) @@ -134,7 +132,6 @@ func TestParseEndpoint(t *testing.T) { } }) } - } func TestGetAccessModes(t *testing.T) {