Skip to content

Commit e60d9c6

Browse files
committed
linters: add forbiddenmarkers and nonullable linters
Signed-off-by: Bryce Palmer <[email protected]>
1 parent 1b29e82 commit e60d9c6

File tree

12 files changed

+411
-0
lines changed

12 files changed

+411
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 forbiddenmarkers
17+
18+
import (
19+
"fmt"
20+
"go/ast"
21+
22+
"golang.org/x/tools/go/analysis"
23+
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors"
24+
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags"
25+
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector"
26+
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers"
27+
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils"
28+
"sigs.k8s.io/kube-api-linter/pkg/config"
29+
)
30+
31+
const name = "forbiddenmarkers"
32+
33+
type analyzer struct {
34+
forbiddenMarkers []string
35+
}
36+
37+
func NewAnalyzer(cfg config.ForbiddenMarkersConfig) *analysis.Analyzer {
38+
a := &analyzer{
39+
forbiddenMarkers: cfg.Markers,
40+
}
41+
42+
return &analysis.Analyzer{
43+
Name: name,
44+
Doc: "Check that no forbidden markers are present on types and fields.",
45+
Run: a.run,
46+
Requires: []*analysis.Analyzer{inspector.Analyzer},
47+
}
48+
}
49+
50+
func (a *analyzer) run(pass *analysis.Pass) (any, error) {
51+
inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector)
52+
if !ok {
53+
return nil, kalerrors.ErrCouldNotGetInspector
54+
}
55+
56+
inspect.InspectFields(func(field *ast.Field, stack []ast.Node, _ extractjsontags.FieldTagInfo, markersAccess markers.Markers) {
57+
checkField(pass, field, markersAccess, a.forbiddenMarkers)
58+
})
59+
60+
inspect.InspectTypeSpec(func(typeSpec *ast.TypeSpec, markersAccess markers.Markers) {
61+
checkType(pass, typeSpec, markersAccess, a.forbiddenMarkers)
62+
})
63+
64+
return nil, nil //nolint:nilnil
65+
}
66+
67+
func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers, forbiddenMarkers []string) {
68+
if field == nil || len(field.Names) == 0 {
69+
return
70+
}
71+
72+
markers := utils.TypeAwareMarkerCollectionForField(pass, markersAccess, field)
73+
check(markers, forbiddenMarkers, reportField(pass, field))
74+
}
75+
76+
func checkType(pass *analysis.Pass, typeSpec *ast.TypeSpec, markersAccess markers.Markers, forbiddenMarkers []string) {
77+
if typeSpec == nil {
78+
return
79+
}
80+
81+
markers := markersAccess.TypeMarkers(typeSpec)
82+
check(markers, forbiddenMarkers, reportType(pass, typeSpec))
83+
}
84+
85+
func check(markerSet markers.MarkerSet, forbiddenMarkers []string, reportFunc func(marker markers.Marker)) {
86+
for _, marker := range forbiddenMarkers {
87+
marks := markerSet.Get(marker)
88+
for _, mark := range marks {
89+
reportFunc(mark)
90+
}
91+
}
92+
}
93+
94+
func reportField(pass *analysis.Pass, field *ast.Field) func(marker markers.Marker) {
95+
return func(marker markers.Marker) {
96+
pass.Report(analysis.Diagnostic{
97+
Pos: field.Pos(),
98+
Message: fmt.Sprintf("field %s has forbidden marker %q", field.Names[0].Name, marker.Identifier),
99+
SuggestedFixes: []analysis.SuggestedFix{
100+
{
101+
Message: fmt.Sprintf("remove forbidden marker %q", marker.Identifier),
102+
TextEdits: []analysis.TextEdit{
103+
{
104+
Pos: marker.Pos,
105+
End: marker.End + 1,
106+
},
107+
},
108+
},
109+
},
110+
})
111+
}
112+
}
113+
114+
func reportType(pass *analysis.Pass, typeSpec *ast.TypeSpec) func(marker markers.Marker) {
115+
return func(marker markers.Marker) {
116+
pass.Report(analysis.Diagnostic{
117+
Pos: typeSpec.Pos(),
118+
Message: fmt.Sprintf("type %s has forbidden marker %q", typeSpec.Name, marker.Identifier),
119+
})
120+
}
121+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package forbiddenmarkers_test
2+
3+
import (
4+
"testing"
5+
6+
"golang.org/x/tools/go/analysis/analysistest"
7+
"sigs.k8s.io/kube-api-linter/pkg/analysis/forbiddenmarkers"
8+
"sigs.k8s.io/kube-api-linter/pkg/config"
9+
)
10+
11+
func TestWithConfiguration(t *testing.T) {
12+
testdata := analysistest.TestData()
13+
analysistest.RunWithSuggestedFixes(t, testdata, forbiddenmarkers.NewAnalyzer(config.ForbiddenMarkersConfig{
14+
Markers: []string{
15+
"forbidden",
16+
},
17+
}), "a/...")
18+
}

pkg/analysis/forbiddenmarkers/doc.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/*
18+
* The `forbiddenmarkers` linter ensures that types and fields do not contain any markers
19+
* that are forbidden.
20+
*
21+
* By default, `forbiddenmarkers` is not enabled.
22+
*
23+
* It can be configured with a list of marker identifiers that are forbidden
24+
* ```yaml
25+
* lintersConfig:
26+
* forbiddenMarkers:
27+
* markers:
28+
* - some:forbidden:marker
29+
* - anotherforbidden
30+
* ```
31+
*
32+
* Fixes are suggested to remove all markers that are forbidden.
33+
*
34+
*/
35+
package forbiddenmarkers
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 forbiddenmarkers
17+
18+
import (
19+
"golang.org/x/tools/go/analysis"
20+
"sigs.k8s.io/kube-api-linter/pkg/config"
21+
)
22+
23+
// Initializer returns the AnalyzerInitializer for this
24+
// Analyzer so that it can be added to the registry.
25+
func Initializer() initializer {
26+
return initializer{}
27+
}
28+
29+
// intializer implements the AnalyzerInitializer interface.
30+
type initializer struct{}
31+
32+
// Name returns the name of the Analyzer.
33+
func (initializer) Name() string {
34+
return name
35+
}
36+
37+
// Init returns the intialized Analyzer.
38+
func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) {
39+
return NewAnalyzer(cfg.ForbiddenMarkers), nil
40+
}
41+
42+
// Default determines whether this Analyzer is on by default, or not.
43+
func (initializer) Default() bool {
44+
return false
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package a
2+
3+
// +forbidden
4+
type ForbiddenMarkerType string // want `type ForbiddenMarkerType has forbidden marker "forbidden"`
5+
6+
// +allowed
7+
type AllowedMarkerType string
8+
9+
type Test struct {
10+
// +forbidden
11+
ForbiddenMarkerField string `json:"forbiddenMarkerField"`// want `field ForbiddenMarkerField has forbidden marker "forbidden"`
12+
13+
ForbiddenMarkerFieldTypeAlias ForbiddenMarkerType `json:"forbiddenMarkerFieldTypeAlias"` // want `field ForbiddenMarkerFieldTypeAlias has forbidden marker "forbidden"`
14+
15+
// +allowed
16+
AllowedMarkerField string `json:"allowedMarkerField"`
17+
18+
AllowedMarkerFieldTypeAlias AllowedMarkerType `json:"AllowedMarkerFieldTypeAlias"`
19+
}
20+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package a
2+
3+
type ForbiddenMarkerType string // want `type ForbiddenMarkerType has forbidden marker "forbidden"`
4+
5+
// +allowed
6+
type AllowedMarkerType string
7+
8+
type Test struct {
9+
ForbiddenMarkerField string `json:"forbiddenMarkerField"`// want `field ForbiddenMarkerField has forbidden marker "forbidden"`
10+
11+
ForbiddenMarkerFieldTypeAlias ForbiddenMarkerType `json:"forbiddenMarkerFieldTypeAlias"` // want `field ForbiddenMarkerFieldTypeAlias has forbidden marker "forbidden"`
12+
13+
// +allowed
14+
AllowedMarkerField string `json:"allowedMarkerField"`
15+
16+
AllowedMarkerFieldTypeAlias AllowedMarkerType `json:"AllowedMarkerFieldTypeAlias"`
17+
}

pkg/analysis/nonullable/analyzer.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 nonullable
17+
18+
import (
19+
"golang.org/x/tools/go/analysis"
20+
"sigs.k8s.io/kube-api-linter/pkg/analysis/forbiddenmarkers"
21+
"sigs.k8s.io/kube-api-linter/pkg/config"
22+
)
23+
24+
const name = "nonullable"
25+
26+
func newAnalyzer() *analysis.Analyzer {
27+
analyzer := forbiddenmarkers.NewAnalyzer(config.ForbiddenMarkersConfig{
28+
Markers: []string{
29+
"nullable",
30+
},
31+
})
32+
33+
analyzer.Name = name
34+
analyzer.Doc = "Check that nullable marker is not present on any types or fields."
35+
36+
return analyzer
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package nonullable
2+
3+
import (
4+
"testing"
5+
6+
"golang.org/x/tools/go/analysis/analysistest"
7+
)
8+
9+
func Test(t *testing.T) {
10+
testdata := analysistest.TestData()
11+
analysistest.RunWithSuggestedFixes(t, testdata, newAnalyzer(), "a/...")
12+
}

pkg/analysis/nonullable/doc.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
/*
18+
* The `nonullable` linter ensures that types and fields do not have the `nullable` marker.
19+
*
20+
* Fixes are suggested to remove the `nullable` marker.
21+
*
22+
*/
23+
package nonullable
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 nonullable
17+
18+
import (
19+
"golang.org/x/tools/go/analysis"
20+
"sigs.k8s.io/kube-api-linter/pkg/config"
21+
)
22+
23+
// Initializer returns the AnalyzerInitializer for this
24+
// Analyzer so that it can be added to the registry.
25+
func Initializer() initializer {
26+
return initializer{}
27+
}
28+
29+
// intializer implements the AnalyzerInitializer interface.
30+
type initializer struct{}
31+
32+
// Name returns the name of the Analyzer.
33+
func (initializer) Name() string {
34+
return name
35+
}
36+
37+
// Init returns the intialized Analyzer.
38+
func (initializer) Init(_ config.LintersConfig) (*analysis.Analyzer, error) {
39+
return newAnalyzer(), nil
40+
}
41+
42+
// Default determines whether this Analyzer is on by default, or not.
43+
func (initializer) Default() bool {
44+
return true
45+
}

0 commit comments

Comments
 (0)