Skip to content

Commit 7db890f

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

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ linters:
106106
- all
107107
path: testdata
108108
- path: (.+)\.go$
109-
text: Analyzer is a global variable
109+
text: Analyzer(.*) is a global variable
110110
paths:
111111
- third_party$
112112
- builtin$

README.md

Lines changed: 31 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,33 @@ 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+
```shell
85+
go build -buildmode=plugin -o bin/kube-api-linter.so sigs.k8s.io/kube-api-linter/pkg/plugin
86+
```
87+
88+
This will create a `kube-api-linter.so` file in the `bin` directory.
89+
90+
The `golangci-lint` configuration is similar to the module configuration, however, you will need to specify the plugin path instead.
91+
92+
```yaml
93+
linters-settings:
94+
custom:
95+
kubeapilinter:
96+
path: "bin/kube-api-linter.so"
97+
description: Kube API LInter lints Kube like APIs based on API conventions and best practices.
98+
original-url: sigs.k8s.io/kube-api-linter
99+
settings:
100+
linters: {}
101+
lintersConfig: {}
102+
```
103+
104+
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.
105+
79106
#### VSCode integration
80107

81108
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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
type analyzerPlugin struct{}
29+
30+
// GetAnalyzers returns all analyzers to be used by golangci-lint.
31+
func (*analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
32+
analyzers, _ := New(nil)
33+
34+
return analyzers
35+
}
36+
37+
// AnalyzerPlugin is the entry point for golangci-lint.
38+
var AnalyzerPlugin analyzerPlugin
39+
40+
// New API, see https://github.com/golangci/golangci-lint/pull/3887.
41+
func New(pluginSettings any) ([]*analysis.Analyzer, error) {
42+
plugin, err := kubeapilinter.New(pluginSettings)
43+
if err != nil {
44+
return nil, fmt.Errorf("error creating plugin: %w", err)
45+
}
46+
47+
analyzers, err := plugin.BuildAnalyzers()
48+
if err != nil {
49+
return nil, fmt.Errorf("error building analyzers: %w", err)
50+
}
51+
52+
return analyzers, nil
53+
}

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)