diff --git a/.github/workflows/pr-ci-check.yml b/.github/workflows/pr-ci-check.yml index 2b98f731fa..bc1beca3e5 100644 --- a/.github/workflows/pr-ci-check.yml +++ b/.github/workflows/pr-ci-check.yml @@ -4,6 +4,8 @@ jobs: build: name: Run CI runs-on: ubuntu-latest + strategy: + fail-fast: false steps: - name: Set up Go uses: actions/setup-go@v2 @@ -27,3 +29,7 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.out verbose: true + - name: Run staticcheck + uses: dominikh/staticcheck-action@v1.3.0 + with: + version: "2022.1.3" \ No newline at end of file diff --git a/changelogs/unreleased/5788-blackpiglet b/changelogs/unreleased/5788-blackpiglet new file mode 100644 index 0000000000..809de914bb --- /dev/null +++ b/changelogs/unreleased/5788-blackpiglet @@ -0,0 +1 @@ +Enable staticcheck linter. \ No newline at end of file diff --git a/cmd/velero-restore-helper/velero-restore-helper.go b/cmd/velero-restore-helper/velero-restore-helper.go index 6622bee11b..a6039381d1 100644 --- a/cmd/velero-restore-helper/velero-restore-helper.go +++ b/cmd/velero-restore-helper/velero-restore-helper.go @@ -18,7 +18,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" "time" @@ -34,18 +33,16 @@ func main() { defer ticker.Stop() for { - select { - case <-ticker.C: - if done() { - fmt.Println("All restic restores are done") - err := removeFolder() - if err != nil { - fmt.Println(err) - } else { - fmt.Println("Done cleanup .velero folder") - } - return + <-ticker.C + if done() { + fmt.Println("All restic restores are done") + err := removeFolder() + if err != nil { + fmt.Println(err) + } else { + fmt.Println("Done cleanup .velero folder") } + return } } } @@ -54,7 +51,7 @@ func main() { // within the .velero/ subdirectory whose name is equal to os.Args[1], or // false otherwise func done() bool { - children, err := ioutil.ReadDir("/restores") + children, err := os.ReadDir("/restores") if err != nil { fmt.Fprintf(os.Stderr, "ERROR reading /restores directory: %s\n", err) return false @@ -84,7 +81,7 @@ func done() bool { // remove .velero folder func removeFolder() error { - children, err := ioutil.ReadDir("/restores") + children, err := os.ReadDir("/restores") if err != nil { return err } diff --git a/config/crd/v1/crds/crds.go b/config/crd/v1/crds/crds.go index 65a3378067..3d5502e743 100644 --- a/config/crd/v1/crds/crds.go +++ b/config/crd/v1/crds/crds.go @@ -21,7 +21,7 @@ package crds import ( "bytes" "compress/gzip" - "io/ioutil" + "io" apiextinstall "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/install" apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -53,7 +53,7 @@ func crds() []*apiextv1.CustomResourceDefinition { if err != nil { panic(err) } - bytes, err := ioutil.ReadAll(gzr) + bytes, err := io.ReadAll(gzr) if err != nil { panic(err) } diff --git a/hack/crd-gen/v1/main.go b/hack/crd-gen/v1/main.go index 9bb05a4009..e61763531f 100644 --- a/hack/crd-gen/v1/main.go +++ b/hack/crd-gen/v1/main.go @@ -24,7 +24,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "log" "os" "text/template" @@ -41,7 +40,7 @@ package crds import ( "bytes" "compress/gzip" - "io/ioutil" + "io" apiextinstall "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/install" apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -65,7 +64,7 @@ func crds() []*apiextv1.CustomResourceDefinition { if err != nil { panic(err) } - bytes, err := ioutil.ReadAll(gzr) + bytes, err := io.ReadAll(gzr) if err != nil { panic(err) } @@ -87,7 +86,7 @@ type templateData struct { } func main() { - headerBytes, err := ioutil.ReadFile(goHeaderFile) + headerBytes, err := os.ReadFile(goHeaderFile) if err != nil { log.Fatalln(err) } @@ -97,7 +96,7 @@ func main() { } // This is relative to config/crd/crds - manifests, err := ioutil.ReadDir("../bases") + manifests, err := os.ReadDir("../bases") if err != nil { log.Fatalln(err) } diff --git a/hack/release-tools/chk_version.go b/hack/release-tools/chk_version.go index b0c7ecd8d7..0b68f8ed73 100644 --- a/hack/release-tools/chk_version.go +++ b/hack/release-tools/chk_version.go @@ -28,7 +28,7 @@ import ( // minor // patch // prerelease (this will be alpha/beta/rc followed by a ".", followed by 1 or more digits (alpha.5) -var release_regex *regexp.Regexp = regexp.MustCompile("^v(?P[[:digit:]]+)\\.(?P[[:digit:]]+)\\.(?P[[:digit:]]+)(-{1}(?P(alpha|beta|rc)\\.[[:digit:]]+))*") +var release_regex *regexp.Regexp = regexp.MustCompile(`^v(?P[[:digit:]]+)\.(?P[[:digit:]]+)\.(?P[[:digit:]]+)(-{1}(?P(alpha|beta|rc)\.[[:digit:]]+))*`) // This small program exists because checking the VELERO_VERSION rules in bash is difficult, and difficult to test for correctness. // Calling it with --verify will verify whether or not the VELERO_VERSION environment variable is a valid version string, without parsing for its components. diff --git a/internal/hook/item_hook_handler_test.go b/internal/hook/item_hook_handler_test.go index ea2c518897..2fe6a2bbb4 100644 --- a/internal/hook/item_hook_handler_test.go +++ b/internal/hook/item_hook_handler_test.go @@ -22,7 +22,6 @@ import ( "time" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -40,15 +39,6 @@ import ( "github.com/vmware-tanzu/velero/pkg/util/collections" ) -type mockItemHookHandler struct { - mock.Mock -} - -func (h *mockItemHookHandler) HandleHooks(log logrus.FieldLogger, groupResource schema.GroupResource, obj runtime.Unstructured, resourceHooks []ResourceHook, phase hookPhase) error { - args := h.Called(log, groupResource, obj, resourceHooks, phase) - return args.Error(0) -} - func TestHandleHooksSkips(t *testing.T) { tests := []struct { name string diff --git a/internal/hook/wait_exec_hook_handler.go b/internal/hook/wait_exec_hook_handler.go index 35d90215d1..890a8117bd 100644 --- a/internal/hook/wait_exec_hook_handler.go +++ b/internal/hook/wait_exec_hook_handler.go @@ -119,7 +119,7 @@ func (e *DefaultWaitExecHookHandler) HandleHooks( ) if newPod.Status.Phase == v1.PodSucceeded || newPod.Status.Phase == v1.PodFailed { - err := fmt.Errorf("Pod entered phase %s before some post-restore exec hooks ran", newPod.Status.Phase) + err := fmt.Errorf("pod entered phase %s before some post-restore exec hooks ran", newPod.Status.Phase) podLog.Warning(err) cancel() return @@ -155,7 +155,7 @@ func (e *DefaultWaitExecHookHandler) HandleHooks( ) // Check the individual hook's wait timeout is not expired if hook.Hook.WaitTimeout.Duration != 0 && time.Since(waitStart) > hook.Hook.WaitTimeout.Duration { - err := fmt.Errorf("Hook %s in container %s expired before executing", hook.HookName, hook.Hook.Container) + err := fmt.Errorf("hook %s in container %s expired before executing", hook.HookName, hook.Hook.Container) hookLog.Error(err) if hook.Hook.OnError == velerov1api.HookErrorModeFail { errors = append(errors, err) @@ -194,7 +194,7 @@ func (e *DefaultWaitExecHookHandler) HandleHooks( handler(newObj) }, DeleteFunc: func(obj interface{}) { - err := fmt.Errorf("Pod %s deleted before all hooks were executed", kube.NamespaceAndName(pod)) + err := fmt.Errorf("pod %s deleted before all hooks were executed", kube.NamespaceAndName(pod)) log.Error(err) cancel() }, @@ -212,7 +212,7 @@ func (e *DefaultWaitExecHookHandler) HandleHooks( if hook.executed { continue } - err := fmt.Errorf("Hook %s in container %s in pod %s not executed: %v", hook.HookName, hook.Hook.Container, kube.NamespaceAndName(pod), ctx.Err()) + err := fmt.Errorf("hook %s in container %s in pod %s not executed: %v", hook.HookName, hook.Hook.Container, kube.NamespaceAndName(pod), ctx.Err()) hookLog := log.WithFields( logrus.Fields{ "hookSource": hook.HookSource, diff --git a/internal/hook/wait_exec_hook_handler_test.go b/internal/hook/wait_exec_hook_handler_test.go index 5c83352356..f12e7fef4e 100644 --- a/internal/hook/wait_exec_hook_handler_test.go +++ b/internal/hook/wait_exec_hook_handler_test.go @@ -465,7 +465,7 @@ func TestWaitExecHandleHooks(t *testing.T) { }, }). Result(), - expectedErrors: []error{errors.New("Hook my-hook-1 in container container1 in pod default/my-pod not executed: context deadline exceeded")}, + expectedErrors: []error{errors.New("hook my-hook-1 in container container1 in pod default/my-pod not executed: context deadline exceeded")}, byContainer: map[string][]PodExecRestoreHook{ "container1": { { @@ -496,7 +496,7 @@ func TestWaitExecHandleHooks(t *testing.T) { }, }). Result(), - expectedErrors: []error{errors.New("Hook my-hook-1 in container container1 in pod default/my-pod not executed: context deadline exceeded")}, + expectedErrors: []error{errors.New("hook my-hook-1 in container container1 in pod default/my-pod not executed: context deadline exceeded")}, byContainer: map[string][]PodExecRestoreHook{ "container1": { { diff --git a/internal/storage/storagelocation.go b/internal/storage/storagelocation.go index e1d00bcf73..1def65d17b 100644 --- a/internal/storage/storagelocation.go +++ b/internal/storage/storagelocation.go @@ -71,10 +71,7 @@ func IsReadyToValidate(bslValidationFrequency *metav1.Duration, lastValidationTi // We want to validate BSL only if the set validation frequency/ interval has elapsed. nextValidation := lastValidation.Add(validationFrequency) // next validation time: last validation time + validation frequency - if time.Now().UTC().Before(nextValidation) { // ready only when NOW is equal to or after the next validation time - return false - } - return true + return !time.Now().UTC().Before(nextValidation) // ready only when NOW is equal to or after the next validation time } // ListBackupStorageLocations verifies if there are any backup storage locations. diff --git a/internal/storage/storagelocation_test.go b/internal/storage/storagelocation_test.go index a8079787d6..bd5a94aa90 100644 --- a/internal/storage/storagelocation_test.go +++ b/internal/storage/storagelocation_test.go @@ -163,7 +163,7 @@ func TestListBackupStorageLocations(t *testing.T) { t.Run(tt.name, func(t *testing.T) { g := NewWithT(t) - client := fake.NewFakeClientWithScheme(scheme.Scheme, tt.backupLocations) + client := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(tt.backupLocations).Build() if tt.expectError { _, err := ListBackupStorageLocations(context.Background(), client, "ns-1") g.Expect(err).NotTo(BeNil()) diff --git a/pkg/staticcheck.conf b/pkg/staticcheck.conf new file mode 100644 index 0000000000..ba239a7083 --- /dev/null +++ b/pkg/staticcheck.conf @@ -0,0 +1 @@ +checks = [] \ No newline at end of file diff --git a/test/staticcheck.conf b/test/staticcheck.conf new file mode 100644 index 0000000000..ba239a7083 --- /dev/null +++ b/test/staticcheck.conf @@ -0,0 +1 @@ +checks = [] \ No newline at end of file