Skip to content

Commit f27c222

Browse files
committed
Add golangci-lint Go plugin version of KAL
1 parent 2c83ed3 commit f27c222

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ Kube API Linter is aimed at being an assistant to API review, by catching the me
1010

1111
## Installation
1212

13-
Kube API Linter ships as a golangci-lint plugin.
13+
Kube API Linter ships as a golangci-lint plugin, and a golangci-lint module.
1414

15-
### Golangci-lint Plugin
15+
### Golangci-lint Module
1616

17-
To install the `golangci-lint` plugin, first you must have `golangci-lint` installed.
17+
To install the `golangci-lint` module, first you must have `golangci-lint` installed.
1818
If you do not have `golangci-lint` installed, review the `golangci-lint` [install guide][golangci-lint-install].
1919

2020
[golangci-lint-install]: https://golangci-lint.run/welcome/install/
2121

2222
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:
2323

2424
```yaml
25-
version: v1.62.0
25+
version: v1.64.8
2626
name: golangci-kube-api-linter
2727
destination: ./bin
2828
plugins:
@@ -76,6 +76,37 @@ Where fixes are available within a rule, these can be applied automatically with
7676
golangci-kube-api-linter run path/to/api/types --fix
7777
```
7878

79+
### Golangci-lint Plugin
80+
81+
The Kube API Linter can also be used as a plugin for `golangci-lint`.
82+
To do this, you will need to install the `golangci-lint` binary and then install the Kube API Linter plugin.
83+
84+
More information about golangci-lint plugins can be found in the [golangci-lint plugin documentation][golangci-lint-plugin-docs].
85+
86+
[golangci-lint-plugin-docs]: https://golangci-lint.run/plugins/go-plugins/
87+
88+
```shell
89+
go build -buildmode=plugin -o bin/kube-api-linter.so sigs.k8s.io/kube-api-linter/pkg/plugin
90+
```
91+
92+
This will create a `kube-api-linter.so` file in the `bin` directory.
93+
94+
The `golangci-lint` configuration is similar to the module configuration, however, you will need to specify the plugin path instead.
95+
96+
```yaml
97+
linters-settings:
98+
custom:
99+
kubeapilinter:
100+
path: "bin/kube-api-linter.so"
101+
description: Kube API LInter lints Kube like APIs based on API conventions and best practices.
102+
original-url: sigs.k8s.io/kube-api-linter
103+
settings:
104+
linters: {}
105+
lintersConfig: {}
106+
```
107+
108+
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.
109+
79110
#### VSCode integration
80111

81112
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.

pkg/plugin/plugin.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package main is meant to be compiled as a plugin for golangci-lint, see
18+
// https://golangci-lint.run/plugins/go-plugins/.
19+
package main
20+
21+
import (
22+
"fmt"
23+
24+
"golang.org/x/tools/go/analysis"
25+
kubeapilinter "sigs.k8s.io/kube-api-linter"
26+
)
27+
28+
// New API, see https://github.com/golangci/golangci-lint/pull/3887.
29+
func New(pluginSettings any) ([]*analysis.Analyzer, error) {
30+
plugin, err := kubeapilinter.New(pluginSettings)
31+
if err != nil {
32+
return nil, fmt.Errorf("error creating plugin: %w", err)
33+
}
34+
35+
analyzers, err := plugin.BuildAnalyzers()
36+
if err != nil {
37+
return nil, fmt.Errorf("error building analyzers: %w", err)
38+
}
39+
40+
return analyzers, nil
41+
}

plugin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
17+
// Package kubeapilinter is a golangci-lint plugin for the Kube API Linter (KAL).
18+
// It is built as a module to be used with golangci-lint.
19+
// See https://golangci-lint.run/plugins/module-plugins/ for more information.
1620
package kubeapilinter
1721

1822
import (

0 commit comments

Comments
 (0)