From af600b0e6c8b7a9f2e2ff17a8889011e49af5ba2 Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Thu, 14 Apr 2022 08:48:10 -0400 Subject: [PATCH] :running: Update golangci-lint to 1.45.2 The current version fails on files that are unchanged. --- .github/workflows/golangci-lint.yml | 2 +- .golangci.yml | 5 +++++ pkg/certwatcher/certwatcher_test.go | 2 +- pkg/client/client_test.go | 6 +++--- pkg/client/fake/client.go | 8 ++------ pkg/client/metadata_client.go | 4 +--- pkg/client/unstructured_client.go | 16 +++++----------- pkg/client/watch.go | 8 ++------ pkg/envtest/crd.go | 2 +- pkg/internal/flock/flock_unix.go | 1 + pkg/manager/signals/signal_posix.go | 1 + tools/setup-envtest/workflows/workflows_test.go | 2 +- 12 files changed, 24 insertions(+), 33 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 2cf235d808..8d95dc21c4 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -19,5 +19,5 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: - version: v1.40.1 + version: v1.45.2 working-directory: ${{matrix.working-directory}} diff --git a/.golangci.yml b/.golangci.yml index 9ca3fd9270..96290b8bf7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -121,6 +121,11 @@ issues: - linters: - gocritic text: "singleCaseSwitch: should rewrite switch statement to if statement" + # It considers all file access to a filename that comes from a variable problematic, + # which is naiv at best. + - linters: + - gosec + text: "G304: Potential file inclusion via variable" run: timeout: 10m diff --git a/pkg/certwatcher/certwatcher_test.go b/pkg/certwatcher/certwatcher_test.go index 287645df92..cee66dfcc1 100644 --- a/pkg/certwatcher/certwatcher_test.go +++ b/pkg/certwatcher/certwatcher_test.go @@ -167,7 +167,7 @@ func writeCerts(certPath, keyPath, ip string) error { return err } - keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) //nolint:gosec + keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return err } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index fac8c97ab8..52bec53ff0 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -1110,7 +1110,7 @@ var _ = Describe("Client", func() { By("initially creating two Deployments") dep2 := dep.DeepCopy() - dep2.Name = dep2.Name + "-2" + dep2.Name += "-2" dep, err = clientset.AppsV1().Deployments(ns).Create(ctx, dep, metav1.CreateOptions{}) Expect(err).NotTo(HaveOccurred()) @@ -1209,7 +1209,7 @@ var _ = Describe("Client", func() { By("initially creating two Deployments") dep2 := dep.DeepCopy() - dep2.Name = dep2.Name + "-2" + dep2.Name += "-2" dep, err = clientset.AppsV1().Deployments(ns).Create(ctx, dep, metav1.CreateOptions{}) Expect(err).NotTo(HaveOccurred()) @@ -1295,7 +1295,7 @@ var _ = Describe("Client", func() { By("initially creating two Deployments") dep2 := dep.DeepCopy() - dep2.Name = dep2.Name + "-2" + dep2.Name += "-2" dep, err = clientset.AppsV1().Deployments(ns).Create(ctx, dep, metav1.CreateOptions{}) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/client/fake/client.go b/pkg/client/fake/client.go index d23e1957d1..d007cd14ee 100644 --- a/pkg/client/fake/client.go +++ b/pkg/client/fake/client.go @@ -352,9 +352,7 @@ func (c *fakeClient) Watch(ctx context.Context, list client.ObjectList, opts ... return nil, err } - if strings.HasSuffix(gvk.Kind, "List") { - gvk.Kind = gvk.Kind[:len(gvk.Kind)-4] - } + gvk.Kind = strings.TrimSuffix(gvk.Kind, "List") listOpts := client.ListOptions{} listOpts.ApplyOptions(opts) @@ -371,9 +369,7 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl originalKind := gvk.Kind - if strings.HasSuffix(gvk.Kind, "List") { - gvk.Kind = gvk.Kind[:len(gvk.Kind)-4] - } + gvk.Kind = strings.TrimSuffix(gvk.Kind, "List") if _, isUnstructuredList := obj.(*unstructured.UnstructuredList); isUnstructuredList && !c.scheme.Recognizes(gvk) { // We need to register the ListKind with UnstructuredList: diff --git a/pkg/client/metadata_client.go b/pkg/client/metadata_client.go index 59747463a4..db495ca46b 100644 --- a/pkg/client/metadata_client.go +++ b/pkg/client/metadata_client.go @@ -146,9 +146,7 @@ func (mc *metadataClient) List(ctx context.Context, obj ObjectList, opts ...List } gvk := metadata.GroupVersionKind() - if strings.HasSuffix(gvk.Kind, "List") { - gvk.Kind = gvk.Kind[:len(gvk.Kind)-4] - } + gvk.Kind = strings.TrimSuffix(gvk.Kind, "List") listOpts := ListOptions{} listOpts.ApplyOptions(opts) diff --git a/pkg/client/unstructured_client.go b/pkg/client/unstructured_client.go index dcf15be275..819527e700 100644 --- a/pkg/client/unstructured_client.go +++ b/pkg/client/unstructured_client.go @@ -95,8 +95,7 @@ func (uc *unstructuredClient) Update(ctx context.Context, obj Object, opts ...Up // Delete implements client.Client. func (uc *unstructuredClient) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error { - _, ok := obj.(*unstructured.Unstructured) - if !ok { + if _, ok := obj.(*unstructured.Unstructured); !ok { return fmt.Errorf("unstructured client did not understand object: %T", obj) } @@ -118,8 +117,7 @@ func (uc *unstructuredClient) Delete(ctx context.Context, obj Object, opts ...De // DeleteAllOf implements client.Client. func (uc *unstructuredClient) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error { - _, ok := obj.(*unstructured.Unstructured) - if !ok { + if _, ok := obj.(*unstructured.Unstructured); !ok { return fmt.Errorf("unstructured client did not understand object: %T", obj) } @@ -141,8 +139,7 @@ func (uc *unstructuredClient) DeleteAllOf(ctx context.Context, obj Object, opts // Patch implements client.Client. func (uc *unstructuredClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error { - _, ok := obj.(*unstructured.Unstructured) - if !ok { + if _, ok := obj.(*unstructured.Unstructured); !ok { return fmt.Errorf("unstructured client did not understand object: %T", obj) } @@ -201,9 +198,7 @@ func (uc *unstructuredClient) List(ctx context.Context, obj ObjectList, opts ... } gvk := u.GroupVersionKind() - if strings.HasSuffix(gvk.Kind, "List") { - gvk.Kind = gvk.Kind[:len(gvk.Kind)-4] - } + gvk.Kind = strings.TrimSuffix(gvk.Kind, "List") listOpts := ListOptions{} listOpts.ApplyOptions(opts) @@ -222,8 +217,7 @@ func (uc *unstructuredClient) List(ctx context.Context, obj ObjectList, opts ... } func (uc *unstructuredClient) UpdateStatus(ctx context.Context, obj Object, opts ...UpdateOption) error { - _, ok := obj.(*unstructured.Unstructured) - if !ok { + if _, ok := obj.(*unstructured.Unstructured); !ok { return fmt.Errorf("unstructured client did not understand object: %T", obj) } diff --git a/pkg/client/watch.go b/pkg/client/watch.go index 765ca5daa6..70490664bd 100644 --- a/pkg/client/watch.go +++ b/pkg/client/watch.go @@ -69,9 +69,7 @@ func (w *watchingClient) listOpts(opts ...ListOption) ListOptions { func (w *watchingClient) metadataWatch(ctx context.Context, obj *metav1.PartialObjectMetadataList, opts ...ListOption) (watch.Interface, error) { gvk := obj.GroupVersionKind() - if strings.HasSuffix(gvk.Kind, "List") { - gvk.Kind = gvk.Kind[:len(gvk.Kind)-4] - } + gvk.Kind = strings.TrimSuffix(gvk.Kind, "List") listOpts := w.listOpts(opts...) @@ -85,9 +83,7 @@ func (w *watchingClient) metadataWatch(ctx context.Context, obj *metav1.PartialO func (w *watchingClient) unstructuredWatch(ctx context.Context, obj *unstructured.UnstructuredList, opts ...ListOption) (watch.Interface, error) { gvk := obj.GroupVersionKind() - if strings.HasSuffix(gvk.Kind, "List") { - gvk.Kind = gvk.Kind[:len(gvk.Kind)-4] - } + gvk.Kind = strings.TrimSuffix(gvk.Kind, "List") r, err := w.client.unstructuredClient.cache.getResource(obj) if err != nil { diff --git a/pkg/envtest/crd.go b/pkg/envtest/crd.go index a15c1dacaa..27e08475ec 100644 --- a/pkg/envtest/crd.go +++ b/pkg/envtest/crd.go @@ -426,7 +426,7 @@ func readCRDs(basePath string, files []os.FileInfo) ([]*apiextensionsv1.CustomRe // readDocuments reads documents from file. func readDocuments(fp string) ([][]byte, error) { - b, err := ioutil.ReadFile(fp) //nolint:gosec + b, err := ioutil.ReadFile(fp) if err != nil { return nil, err } diff --git a/pkg/internal/flock/flock_unix.go b/pkg/internal/flock/flock_unix.go index 3a904f3f5f..71ec576df2 100644 --- a/pkg/internal/flock/flock_unix.go +++ b/pkg/internal/flock/flock_unix.go @@ -1,3 +1,4 @@ +//go:build linux || darwin || freebsd || openbsd || netbsd || dragonfly // +build linux darwin freebsd openbsd netbsd dragonfly /* diff --git a/pkg/manager/signals/signal_posix.go b/pkg/manager/signals/signal_posix.go index 9bdb4e7418..a0f00a7321 100644 --- a/pkg/manager/signals/signal_posix.go +++ b/pkg/manager/signals/signal_posix.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows /* diff --git a/tools/setup-envtest/workflows/workflows_test.go b/tools/setup-envtest/workflows/workflows_test.go index 0968a69c99..352bfab8bb 100644 --- a/tools/setup-envtest/workflows/workflows_test.go +++ b/tools/setup-envtest/workflows/workflows_test.go @@ -369,7 +369,7 @@ var _ = Describe("Workflows", func() { }) It("should remove matching versions from the store & keep non-matching ones", func() { - entries, err := env.FS.ReadDir(filepath.Join(".teststore/k8s")) + entries, err := env.FS.ReadDir(".teststore/k8s") Expect(err).NotTo(HaveOccurred(), "should be able to read the store") Expect(entries).To(ConsistOf( WithTransform(fs.FileInfo.Name, Equal("1.16.2-ifonlysingularitywasstillathing-amd64")),