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

Use golangci-lint #111

Merged
merged 1 commit into from
Nov 28, 2022
Merged

Conversation

olivierlemasle
Copy link
Member

@olivierlemasle olivierlemasle commented Nov 21, 2022

Original PR:

Adapt gofmt-all.sh to make it compatible with gofmt for both Go >= 1.19 and Go < 1.19.

Also apply ShellCheck recommendations.


Switch to golangci-lint

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Nov 21, 2022
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Nov 21, 2022
@dgrisonnet
Copy link
Member

Ideally, I would prefer to get rid of this legacy file in favor of golangci-lint, which is the tool that we are using in all the other subprojects. Do you think you could do the migration in this PR?

@olivierlemasle
Copy link
Member Author

Ok, I also prefer golangci-lint a lot. I'll update the PR (and the prometheus-adapter's one) to use it.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 23, 2022
@olivierlemasle olivierlemasle changed the title Fix gofmt-all.sh Use golangci-lint Nov 23, 2022
@olivierlemasle
Copy link
Member Author

olivierlemasle commented Nov 23, 2022

PR updated with golangci-lint.

Most changes are just formatting / style / variables renaming (or adding //nolint for false positives).
"Real" changes are:

  • Export pkg/apiserver/completedConfig (which is returned by exported function Complete();
  • More error checking in test-adapter;
  • Fix net/http's ListenAndServe usage in test-adapter.

Copy link
Member

@dgrisonnet dgrisonnet left a comment

Choose a reason for hiding this comment

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

The project really needed it 😆, thank you for looking into that @olivierlemasle

.golangci.yml Outdated
issues:
exclude-use-default: true
exclude-rules:
# We don't check duplicate code in the tests.
Copy link
Member

Choose a reason for hiding this comment

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

This is odd, why do we need it?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, I should remove this exclusion.
It was for duplicated code between handleCustomMetrics and handleExternalMetrics; but I could refactor that.

Makefile Outdated
# Format and lint
# ---------------

GOLANGCI_VERSION:=1.50.1
Copy link
Member

Choose a reason for hiding this comment

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

nit: this would be more convenient to have at the top-level for the future since it would allow having all the versions of the tools defined in one block

@@ -41,7 +41,7 @@ func init() {
eminstall.Install(Scheme)

// we need custom conversion functions to list resources with options
installer.RegisterConversions(Scheme)
installer.RegisterConversions(Scheme) //nolint: errcheck
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a todo here to fix this errcheck, not checking errors is pretty bad.

More generally, this piece of code shouldn't be in a init() function, also it installs both custom-metrics and external-metrics every time even though some projects might only want to import one API.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok. I added a nolint exception because I thought it was the same thing that was all over Kubernetes with these zz_generated.conversion.go files (e.g. here), but with a closer look, it's not.

I agree that this code needs to be reworked because custom-metrics and external-metrics should not be both statically installed unconditionally.

Copy link
Member

Choose a reason for hiding this comment

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

Oh correct, I didn't check what RegisterConversions was doing when commenting but now that you've explained that is written the same way as zz_generated.conversion.go generated by the code-generator from k8s, I am fine with keeping the exception.

Copy link
Member Author

Choose a reason for hiding this comment

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

I switched to k8s.io/apimachinery/pkg/util/runtime.Must(), which is used in this kind of cases in Kubernetes. It would panic the same way in case of error, but at least it is not linter-specific.

pkg/apiserver/endpoints/handlers/get.go Show resolved Hide resolved
pkg/apiserver/installer/apiserver_test.go Outdated Show resolved Hide resolved
pkg/apiserver/installer/cmhandlers.go Show resolved Hide resolved
pkg/apiserver/installer/emhandlers.go Show resolved Hide resolved
Copy link
Contributor

@zroubalik zroubalik left a comment

Choose a reason for hiding this comment

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

lgtm

@zroubalik
Copy link
Contributor

@dgrisonnet can we please release a new version once this PR is merged? So we can consume k8s 0.25 deps? Thanks!

@dgrisonnet
Copy link
Member

Sure

@dgrisonnet
Copy link
Member

Thanks @olivierlemasle!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 28, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dgrisonnet, olivierlemasle, zroubalik

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 28, 2022
@k8s-ci-robot k8s-ci-robot merged commit c435b42 into kubernetes-sigs:master Nov 28, 2022
@dgrisonnet dgrisonnet mentioned this pull request Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants