-
Notifications
You must be signed in to change notification settings - Fork 15
📖 Update README.md - With instructions for GolangCI 2x #109
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,66 @@ If you do not have `golangci-lint` installed, review the `golangci-lint` [instal | |
|
||
[golangci-lint-install]: https://golangci-lint.run/welcome/install/ | ||
|
||
### Using with golangci-lint v2 Plugin | ||
|
||
Kube API Linter can be used as a plugin with `golangci-lint` v2. This setup allows integrating Kube API Linter alongside other static analysis tools within the same linter workflow. | ||
|
||
More information about golangci-lint plugins can be found in the [golangci-lint plugin documentation][golangci-lint-plugin-docs]. | ||
|
||
#### Plugin Setup | ||
|
||
First, build the plugin `.so` file: | ||
|
||
```shell | ||
go build -buildmode=plugin -o ./bin/kube-api-linter.so sigs.k8s.io/kube-api-linter/pkg/plugin | ||
``` | ||
|
||
This will generate a `kube-api-linter.so` plugin in the `bin` directory. | ||
|
||
#### Configuration | ||
|
||
Update your `.golangci.yml` with the following snippet: | ||
|
||
```yaml | ||
version: "2" | ||
|
||
linters: | ||
enable: | ||
- kubeapilinter | ||
... | ||
|
||
settings: | ||
revive: | ||
rules: | ||
- name: comment-spacings | ||
- name: import-shadowing | ||
custom: | ||
kubeapilinter: | ||
path: "./bin/kube-api-linter.so" | ||
description: "Kube API Linter plugin" | ||
original-url: "sigs.k8s.io/kube-api-linter" | ||
settings: | ||
linters: {} | ||
lintersConfig: {} | ||
|
||
exclusions: | ||
rules: | ||
# Only run kubeapilinter inside the 'api/' directory | ||
# Prevents running API-specific checks on unrelated packages like controllers or tests | ||
- path-except: "^api/" | ||
linters: | ||
- kubeapilinter | ||
... | ||
``` | ||
|
||
This approach allows full integration of Kube API Linter as a plugin, scoped correctly to relevant directories, while using `golangci-lint` v2 and coexisting with other linters. | ||
|
||
**NOTE** For GolangCi Versions lower than 2x you might can do as follows: | ||
|
||
You will need to create a `.custom-gcl.yml` file to describe the custom linters you want to run. The following is an example of a `.custom-gcl.yml` file: | ||
|
||
```yaml | ||
version: v1.64.8 | ||
version: v1.64.8 # GolangCi version | ||
name: golangci-kube-api-linter | ||
destination: ./bin | ||
plugins: | ||
|
@@ -63,6 +119,12 @@ issues: | |
- kubeapilinter | ||
``` | ||
|
||
#### Optional Configurations | ||
|
||
##### Enabling Specific Linters (Valid for Versions < 2.x) | ||
|
||
> **Note**: The following configuration is only supported when using the **module integration** via `.custom-gcl.yml` and is **not valid in plugin mode** (`.so` files with `golangci-lint` v2.x+). | ||
Comment on lines
+124
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't true, you can do exactly the same configuration between both versions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried it, but it did not work out. It might changed the format. I will try out after. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying but I could not make work yet see: kubernetes-sigs/kubebuilder#4941 |
||
|
||
If you wish to only run selected linters you can do so by specifying the linters you want to enable in the `linters` section: | ||
|
||
```yaml | ||
|
@@ -93,37 +155,6 @@ Where fixes are available within a rule, these can be applied automatically with | |
golangci-kube-api-linter run path/to/api/types --fix | ||
``` | ||
|
||
### Golangci-lint Plugin | ||
|
||
The Kube API Linter can also be used as a plugin for `golangci-lint`. | ||
To do this, you will need to install the `golangci-lint` binary and then install the Kube API Linter plugin. | ||
|
||
More information about golangci-lint plugins can be found in the [golangci-lint plugin documentation][golangci-lint-plugin-docs]. | ||
|
||
[golangci-lint-plugin-docs]: https://golangci-lint.run/plugins/go-plugins/ | ||
|
||
```shell | ||
go build -buildmode=plugin -o bin/kube-api-linter.so sigs.k8s.io/kube-api-linter/pkg/plugin | ||
``` | ||
|
||
This will create a `kube-api-linter.so` file in the `bin` directory. | ||
|
||
The `golangci-lint` configuration is similar to the module configuration, however, you will need to specify the plugin path instead. | ||
|
||
```yaml | ||
linters-settings: | ||
custom: | ||
kubeapilinter: | ||
path: "bin/kube-api-linter.so" | ||
description: Kube API LInter lints Kube like APIs based on API conventions and best practices. | ||
original-url: sigs.k8s.io/kube-api-linter | ||
settings: | ||
linters: {} | ||
lintersConfig: {} | ||
Comment on lines
-114
to
-122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the diff above, it seems we just need to update this section slightly, and the rest remains true and the same? |
||
``` | ||
|
||
The rest of the configuration is the same as the module configuration, except the standard `golangci-lint` binary is invoked, rather than a custom binary. | ||
|
||
#### VSCode integration | ||
|
||
Since VSCode already integrates with `golangci-lint` via the [Go][vscode-go] extension, you can use the `golangci-kal` binary as a linter in VSCode. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the formatting off for the exclusions stanza here? It looks like it needs to be linters.exclusions when I was looking at integrating this linter in my repository that uses the 2.x golangci-lint version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks to me like this aligns with Cluster API which is also using golangci-lint version 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that's what we have as well. In this example, the
linters
andexclusions
fields are on the same level though. I think you'd have to indent theexclusions
field once so it's nested under thelinters
field.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I get your point, yes, you're correct, this whole block from exclusions down needs indenting 2 spaces