Skip to content

Commit 0c4de0e

Browse files
committed
Switch to pluginbase approach for custom version of KAL
1 parent 8bd8c2f commit 0c4de0e

File tree

4 files changed

+84
-75
lines changed

4 files changed

+84
-75
lines changed

pkg/plugin/base/base.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
package base
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/golangci/plugin-module-register/register"
22+
"golang.org/x/tools/go/analysis"
23+
"k8s.io/apimachinery/pkg/util/validation/field"
24+
kalanalysis "sigs.k8s.io/kube-api-linter/pkg/analysis"
25+
"sigs.k8s.io/kube-api-linter/pkg/config"
26+
"sigs.k8s.io/kube-api-linter/pkg/validation"
27+
)
28+
29+
func init() {
30+
register.Plugin("kubeapilinter", New)
31+
}
32+
33+
// New creates a new golangci-lint plugin based on the KAL analyzers.
34+
func New(settings any) (register.LinterPlugin, error) {
35+
s, err := register.DecodeSettings[config.GolangCIConfig](settings)
36+
if err != nil {
37+
return nil, fmt.Errorf("error decoding settings: %w", err)
38+
}
39+
40+
return &GolangCIPlugin{config: s}, nil
41+
}
42+
43+
// GolangCIPlugin constructs a new plugin for the golangci-lint
44+
// plugin pattern.
45+
// This allows golangci-lint to build a version of itself, containing
46+
// all of the anaylzers included in KAL.
47+
type GolangCIPlugin struct {
48+
config config.GolangCIConfig
49+
}
50+
51+
// BuildAnalyzers returns all of the analyzers to run, based on the configuration.
52+
func (f *GolangCIPlugin) BuildAnalyzers() ([]*analysis.Analyzer, error) {
53+
if err := validation.ValidateGolangCIConfig(f.config, field.NewPath("")); err != nil {
54+
return nil, fmt.Errorf("error in KAL configuration: %w", err)
55+
}
56+
57+
analyzers, err := kalanalysis.DefaultRegistry().InitializeLinters(f.config.Linters, f.config.LintersConfig)
58+
if err != nil {
59+
return nil, fmt.Errorf("error initializing analyzers: %w", err)
60+
}
61+
62+
return analyzers, nil
63+
}
64+
65+
// GetLoadMode implements the golangci-lint plugin interface.
66+
func (f *GolangCIPlugin) GetLoadMode() string {
67+
return register.LoadModeTypesInfo
68+
}

pkg/plugin/plugin.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ import (
2222
"fmt"
2323

2424
"golang.org/x/tools/go/analysis"
25-
kubeapilinter "sigs.k8s.io/kube-api-linter"
25+
pluginbase "sigs.k8s.io/kube-api-linter/pkg/plugin/base"
26+
27+
// Import the default linters.
28+
// DO NOT ADD DIRECTLY TO THIS FILE.
29+
_ "sigs.k8s.io/kube-api-linter/pkg/registration"
2630
)
2731

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

plugin.go

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,16 @@ limitations under the License.
2020
package kubeapilinter
2121

2222
import (
23-
"fmt"
23+
pluginbase "sigs.k8s.io/kube-api-linter/pkg/plugin/base"
2424

25-
"github.com/golangci/plugin-module-register/register"
26-
"golang.org/x/tools/go/analysis"
27-
"k8s.io/apimachinery/pkg/util/validation/field"
28-
kalanalysis "sigs.k8s.io/kube-api-linter/pkg/analysis"
29-
"sigs.k8s.io/kube-api-linter/pkg/config"
30-
"sigs.k8s.io/kube-api-linter/pkg/validation"
25+
// Import the default linters.
26+
// DO NOT ADD DIRECTLY TO THIS FILE.
27+
_ "sigs.k8s.io/kube-api-linter/pkg/registration"
3128
)
3229

33-
func init() {
34-
register.Plugin("kubeapilinter", New)
35-
}
36-
37-
// New creates a new golangci-lint plugin based on the KAL analyzers.
38-
func New(settings any) (register.LinterPlugin, error) {
39-
s, err := register.DecodeSettings[config.GolangCIConfig](settings)
40-
if err != nil {
41-
return nil, fmt.Errorf("error decoding settings: %w", err)
42-
}
43-
44-
return &GolangCIPlugin{config: s}, nil
45-
}
46-
47-
// GolangCIPlugin constructs a new plugin for the golangci-lint
48-
// plugin pattern.
49-
// This allows golangci-lint to build a version of itself, containing
50-
// all of the anaylzers included in KAL.
51-
type GolangCIPlugin struct {
52-
config config.GolangCIConfig
53-
}
54-
55-
// BuildAnalyzers returns all of the analyzers to run, based on the configuration.
56-
func (f *GolangCIPlugin) BuildAnalyzers() ([]*analysis.Analyzer, error) {
57-
if err := validation.ValidateGolangCIConfig(f.config, field.NewPath("")); err != nil {
58-
return nil, fmt.Errorf("error in KAL configuration: %w", err)
59-
}
60-
61-
analyzers, err := kalanalysis.DefaultRegistry().InitializeLinters(f.config.Linters, f.config.LintersConfig)
62-
if err != nil {
63-
return nil, fmt.Errorf("error initializing analyzers: %w", err)
64-
}
65-
66-
return analyzers, nil
67-
}
68-
69-
// GetLoadMode implements the golangci-lint plugin interface.
70-
func (f *GolangCIPlugin) GetLoadMode() string {
71-
return register.LoadModeTypesInfo
72-
}
30+
// New is the entrypoint for the plugin.
31+
// We import the base plugin here so that custom implementations do not need to import
32+
// this file, but can easily create their own plugin with their own custom set of analyzers.
33+
//
34+
//nolint:gochecknoglobals
35+
var New = pluginbase.New

registration.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)