Skip to content

Add golangci-lint Go plugin version of KAL #78

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

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ Kube API Linter is aimed at being an assistant to API review, by catching the me

## Installation

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

### Golangci-lint Plugin
### Golangci-lint Module

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

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

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.62.0
version: v1.64.8
name: golangci-kube-api-linter
destination: ./bin
plugins:
Expand Down Expand Up @@ -76,6 +76,37 @@ 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: {}
```

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.
Expand Down
41 changes: 41 additions & 0 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package main is meant to be compiled as a plugin for golangci-lint, see
// https://golangci-lint.run/plugins/go-plugins/.
package main

import (
"fmt"

"golang.org/x/tools/go/analysis"
kubeapilinter "sigs.k8s.io/kube-api-linter"
)

// New API, see https://github.com/golangci/golangci-lint/pull/3887.
func New(pluginSettings any) ([]*analysis.Analyzer, error) {
plugin, err := kubeapilinter.New(pluginSettings)
if err != nil {
return nil, fmt.Errorf("error creating plugin: %w", err)
}

analyzers, err := plugin.BuildAnalyzers()
if err != nil {
return nil, fmt.Errorf("error building analyzers: %w", err)
}

return analyzers, nil
}
4 changes: 4 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package kubeapilinter is a golangci-lint plugin for the Kube API Linter (KAL).
// It is built as a module to be used with golangci-lint.
// See https://golangci-lint.run/plugins/module-plugins/ for more information.
package kubeapilinter

import (
Expand Down