Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add staticcheck to verify-golangci #103723

Closed
wants to merge 21 commits into from

Conversation

hlee95
Copy link
Contributor

@hlee95 hlee95 commented Jul 15, 2021

What type of PR is this?

/kind bug

What this PR does / why we need it:

  1. Adds staticcheck to hack/verify-golangci-lint.sh
  2. Fixes or ignores staticcheck failures
  3. Removes hack/verify-staticcheck.sh and cleans up hack/tools/go.mod

Which issue(s) this PR fixes:

Fixes #103721

Special notes for your reviewer:

There were a lot of failures for SA5011 "possible nil pointer dereference". The original issue showed 3 occurrences, but after I fixed those with //nolint:staticcheck, I found that more showed up the next time I ran the lint check. All of the occurrences of this failure happened on something of the following form:

		rs, err := rc.clientSet.AppsV1().ReplicaSets(rc.nsName).Get(context.TODO(), rc.name, metav1.GetOptions{})
		framework.ExpectNoError(err)
		if rs == nil {
			framework.Failf(rsIsNil)
		}
		return int(rs.Status.ReadyReplicas)

The linter says that:

test/e2e/framework/autoscaling/autoscaling_utils.go:340:17: SA5011: possible nil pointer dereference (staticcheck)
		return int(rs.Status.ReadyReplicas)
		              ^
test/e2e/framework/autoscaling/autoscaling_utils.go:337:6: SA5011(related information): this check suggests that the pointer can be nil (staticcheck)
		if rs == nil {
		   ^

I read here that the linter is unable to detect that our framework.Failf() function results in a panic further down the call chain, which causes the linter to flag all of these cases as a violation of SA5011. The proposed solution from that link is to add a panic("unreachable") to framework.Failf() so that the linter will understand that a panic occurs, so I did that and it fixed all of the errors except for two. Those two used Skipf instead of Failf, so I added the panic("unreachable") to Skipf as well. The other option I thought of was to add -e SA5011 in hack/verify-golangci-lint.sh but that seemed less ideal because it could hide real errors.

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/bug Categorizes issue or PR as related to a bug. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jul 15, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @hlee95!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 15, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @hlee95. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. area/dependency Issues or PRs related to dependency changes area/e2e-test-framework Issues or PRs related to refactoring the kubernetes e2e test framework area/kubelet labels Jul 15, 2021
@k8s-ci-robot k8s-ci-robot added area/test sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/network Categorizes an issue or PR as relevant to SIG Network. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. sig/testing Categorizes an issue or PR as relevant to SIG Testing. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jul 15, 2021
@hlee95
Copy link
Contributor Author

hlee95 commented Jul 15, 2021

/assign nikhita

@dims
Copy link
Member

dims commented Jul 15, 2021

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 15, 2021
@dims dims changed the title Add staticcheck to verify-golangci [WIP] Add staticcheck to verify-golangci Jul 15, 2021
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 15, 2021
@k8s-ci-robot k8s-ci-robot added the sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. label Nov 11, 2021
@hlee95
Copy link
Contributor Author

hlee95 commented Nov 11, 2021

I moved to separate commits - @hlee95 do you want to steal these commits?

thockin@6931cba thockin@64a6770

Yup I cherry-picked those.

The second does NOT move all the options from the staticcheck script or delete it - I assume you can cover that?

I copied over the checks and ignore_pattern options from the old script; please let me know if I did it correctly and if I missed anything.

@hlee95
Copy link
Contributor Author

hlee95 commented Nov 11, 2021

/retest

@BenTheElder
Copy link
Member

unrelated to this PR I think but https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/103723/pull-kubernetes-verify/1458620020010520576 does not capture useful results in the junit, only the go mod downloads (which ... are honestly noise ...), I think the golangci-lint errors are not winding up in stdout instead of stderr, so to find them you have to expand the whole build log and read through it.

we should fix that

@aojea
Copy link
Member

aojea commented Nov 11, 2021

it's a bit ironic 🙃

In ./hack/verify-golangci-lint.sh line 48:
if [[ "$#" > 0 ]]; then
           ^-- SC2071: > is for string comparisons. Use -gt instead.


In ./hack/verify-golangci-lint.sh line 53:
        pushd ./vendor/k8s.io/$(basename "$d") >/dev/null
                              ^--------------^ SC2046: Quote this to prevent word splitting.

.golangci.yaml Outdated
"-ST1*", # Mostly stylistic, redundant w/ golint
"-SA5011" # Possible nil pointer dereference
]
ignore_pattern: [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a thing? I don't see it in https://golangci-lint.run/usage/configuration/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed not a thing, I will remove it.

@thockin
Copy link
Member

thockin commented Nov 11, 2021

@aojea comments fixed in thockin@395bcb8

@fedebongio
Copy link
Contributor

/triage accepted
/remove-sig api-machinery

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Nov 11, 2021
@thockin
Copy link
Member

thockin commented Nov 11, 2021

I found another bug in script - testing fix now. Basically errexit terminates if any of the runs fails, and doesn't run the rest.

@thockin
Copy link
Member

thockin commented Nov 11, 2021

thockin@5758c48

@aojea
Copy link
Member

aojea commented Nov 12, 2021

/remove priority-backlog
/priority important-soon

Once this is in we have to spend some time getting this right, is easier to prevent than to find needless in haystacks

@k8s-ci-robot k8s-ci-robot added the priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. label Nov 12, 2021
@aojea
Copy link
Member

aojea commented Nov 12, 2021

why the linter doesn't catch these "capture variable on loop" things ?
#106373

@thockin
Copy link
Member

thockin commented Nov 15, 2021

Code freeze looming - anything we can do to help make this work?

@BenTheElder
Copy link
Member

@aojea #104835 will cover those.

@aojea
Copy link
Member

aojea commented Nov 15, 2021

can we justify to get this during the test-freeze period? I think is legit

@k8s-ci-robot
Copy link
Contributor

@hlee95: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 16, 2021
@aojea aojea mentioned this pull request Nov 16, 2021
@aojea
Copy link
Member

aojea commented Nov 16, 2021

trying to get this to the finish line #106448
keeping all the author on the commits of course, great job here

hlee95 and others added 2 commits November 16, 2021 17:26
Make verify-golangci-lint.sh work across modules and take an optional
argument for a package.
@k8s-ci-robot k8s-ci-robot added the sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. label Nov 16, 2021
@k8s-ci-robot
Copy link
Contributor

@hlee95: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-e2e-gce-ubuntu-containerd 69b6c28 link true /test pull-kubernetes-e2e-gce-ubuntu-containerd
pull-kubernetes-node-e2e-containerd 69b6c28 link true /test pull-kubernetes-node-e2e-containerd
pull-kubernetes-e2e-gce-csi-serial 69b6c28 link false /test pull-kubernetes-e2e-gce-csi-serial
pull-kubernetes-e2e-gce-storage-snapshot 69b6c28 link false /test pull-kubernetes-e2e-gce-storage-snapshot
pull-kubernetes-verify-govet-levee 69b6c28 link true /test pull-kubernetes-verify-govet-levee
pull-kubernetes-e2e-kind 69b6c28 link true /test pull-kubernetes-e2e-kind
pull-kubernetes-typecheck 69b6c28 link true /test pull-kubernetes-typecheck
pull-kubernetes-unit 69b6c28 link true /test pull-kubernetes-unit
pull-kubernetes-e2e-gce-100-performance 69b6c28 link true /test pull-kubernetes-e2e-gce-100-performance
pull-kubernetes-integration 69b6c28 link true /test pull-kubernetes-integration
pull-kubernetes-verify 69b6c28 link true /test pull-kubernetes-verify
pull-kubernetes-dependencies 69b6c28 link true /test pull-kubernetes-dependencies
pull-kubernetes-conformance-kind-ga-only-parallel 69b6c28 link true /test pull-kubernetes-conformance-kind-ga-only-parallel
pull-kubernetes-e2e-kind-ipv6 69b6c28 link true /test pull-kubernetes-e2e-kind-ipv6
pull-kubernetes-e2e-gce-storage-slow 69b6c28 link false /test pull-kubernetes-e2e-gce-storage-slow
pull-kubernetes-conformance-kind-ipv6-parallel 69b6c28 link false /test pull-kubernetes-conformance-kind-ipv6-parallel

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@thockin
Copy link
Member

thockin commented Nov 17, 2021

#106448 supercedes.

@thockin thockin closed this Nov 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/code-organization Issues or PRs related to kubernetes code organization area/dependency Issues or PRs related to dependency changes area/e2e-test-framework Issues or PRs related to refactoring the kubernetes e2e test framework area/kubeadm area/kubelet area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/backlog Higher priority than priority/awaiting-more-evidence. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/network Categorizes an issue or PR as relevant to SIG Network. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. sig/storage Categorizes an issue or PR as relevant to SIG Storage. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Archived in project
SIG Node PR Triage
Waiting on Author
Development

Successfully merging this pull request may close these issues.

Drop hack/verify-staticcheck.sh in favor of enablingstaticcheck plugin in hack/verify-golangci-lint.sh