-
Notifications
You must be signed in to change notification settings - Fork 14
linters: add forbiddenmarkers and nonullable linters #111
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
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 forbiddenmarkers | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors" | ||
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" | ||
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector" | ||
"sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" | ||
"sigs.k8s.io/kube-api-linter/pkg/analysis/utils" | ||
"sigs.k8s.io/kube-api-linter/pkg/config" | ||
) | ||
|
||
const name = "forbiddenmarkers" | ||
|
||
type analyzer struct { | ||
forbiddenMarkers []string | ||
} | ||
|
||
// NewAnalyzer creates a new analysis.Analyzer for the forbiddenmarkers | ||
// linter based on the provided config.ForbiddenMarkersConfig. | ||
func NewAnalyzer(cfg config.ForbiddenMarkersConfig) *analysis.Analyzer { | ||
a := &analyzer{ | ||
forbiddenMarkers: cfg.Markers, | ||
} | ||
|
||
return &analysis.Analyzer{ | ||
Name: name, | ||
Doc: "Check that no forbidden markers are present on types and fields.", | ||
Run: a.run, | ||
Requires: []*analysis.Analyzer{inspector.Analyzer}, | ||
} | ||
} | ||
|
||
func (a *analyzer) run(pass *analysis.Pass) (any, error) { | ||
inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) | ||
if !ok { | ||
return nil, kalerrors.ErrCouldNotGetInspector | ||
} | ||
|
||
inspect.InspectFields(func(field *ast.Field, stack []ast.Node, _ extractjsontags.FieldTagInfo, markersAccess markers.Markers) { | ||
checkField(pass, field, markersAccess, a.forbiddenMarkers) | ||
}) | ||
|
||
inspect.InspectTypeSpec(func(typeSpec *ast.TypeSpec, markersAccess markers.Markers) { | ||
checkType(pass, typeSpec, markersAccess, a.forbiddenMarkers) | ||
}) | ||
|
||
return nil, nil //nolint:nilnil | ||
} | ||
|
||
func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers, forbiddenMarkers []string) { | ||
if field == nil || len(field.Names) == 0 { | ||
return | ||
} | ||
|
||
markers := utils.TypeAwareMarkerCollectionForField(pass, markersAccess, field) | ||
check(markers, forbiddenMarkers, reportField(pass, field)) | ||
} | ||
|
||
func checkType(pass *analysis.Pass, typeSpec *ast.TypeSpec, markersAccess markers.Markers, forbiddenMarkers []string) { | ||
if typeSpec == nil { | ||
return | ||
} | ||
|
||
markers := markersAccess.TypeMarkers(typeSpec) | ||
check(markers, forbiddenMarkers, reportType(pass, typeSpec)) | ||
} | ||
|
||
func check(markerSet markers.MarkerSet, forbiddenMarkers []string, reportFunc func(marker markers.Marker)) { | ||
for _, marker := range forbiddenMarkers { | ||
marks := markerSet.Get(marker) | ||
for _, mark := range marks { | ||
reportFunc(mark) | ||
} | ||
} | ||
} | ||
|
||
func reportField(pass *analysis.Pass, field *ast.Field) func(marker markers.Marker) { | ||
return func(marker markers.Marker) { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: field.Pos(), | ||
Message: fmt.Sprintf("field %s has forbidden marker %q", field.Names[0].Name, marker.Identifier), | ||
SuggestedFixes: []analysis.SuggestedFix{ | ||
{ | ||
Message: fmt.Sprintf("remove forbidden marker %q", marker.Identifier), | ||
TextEdits: []analysis.TextEdit{ | ||
{ | ||
Pos: marker.Pos, | ||
End: marker.End + 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
func reportType(pass *analysis.Pass, typeSpec *ast.TypeSpec) func(marker markers.Marker) { | ||
return func(marker markers.Marker) { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: typeSpec.Pos(), | ||
Message: fmt.Sprintf("type %s has forbidden marker %q", typeSpec.Name, marker.Identifier), | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
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 forbiddenmarkers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
"sigs.k8s.io/kube-api-linter/pkg/analysis/forbiddenmarkers" | ||
"sigs.k8s.io/kube-api-linter/pkg/config" | ||
) | ||
|
||
func TestWithConfiguration(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.RunWithSuggestedFixes(t, testdata, forbiddenmarkers.NewAnalyzer(config.ForbiddenMarkersConfig{ | ||
Markers: []string{ | ||
"forbidden", | ||
}, | ||
}), "a/...") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
/* | ||
* The `forbiddenmarkers` linter ensures that types and fields do not contain any markers | ||
* that are forbidden. | ||
* | ||
* By default, `forbiddenmarkers` is not enabled. | ||
* | ||
* It can be configured with a list of marker identifiers that are forbidden | ||
* ```yaml | ||
* lintersConfig: | ||
* forbiddenMarkers: | ||
* markers: | ||
* - some:forbidden:marker | ||
* - anotherforbidden | ||
* ``` | ||
* | ||
* Fixes are suggested to remove all markers that are forbidden. | ||
* | ||
*/ | ||
package forbiddenmarkers |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
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 forbiddenmarkers | ||
|
||
import ( | ||
"golang.org/x/tools/go/analysis" | ||
"sigs.k8s.io/kube-api-linter/pkg/config" | ||
) | ||
|
||
// Initializer returns the AnalyzerInitializer for this | ||
// Analyzer so that it can be added to the registry. | ||
func Initializer() initializer { | ||
return initializer{} | ||
} | ||
|
||
// intializer implements the AnalyzerInitializer interface. | ||
type initializer struct{} | ||
|
||
// Name returns the name of the Analyzer. | ||
func (initializer) Name() string { | ||
return name | ||
} | ||
|
||
// Init returns the intialized Analyzer. | ||
func (initializer) Init(cfg config.LintersConfig) (*analysis.Analyzer, error) { | ||
return NewAnalyzer(cfg.ForbiddenMarkers), nil | ||
} | ||
|
||
// Default determines whether this Analyzer is on by default, or not. | ||
func (initializer) Default() bool { | ||
return false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package a | ||
|
||
// +forbidden | ||
type ForbiddenMarkerType string // want `type ForbiddenMarkerType has forbidden marker "forbidden"` | ||
|
||
// +allowed | ||
type AllowedMarkerType string | ||
|
||
type Test struct { | ||
// +forbidden | ||
ForbiddenMarkerField string `json:"forbiddenMarkerField"`// want `field ForbiddenMarkerField has forbidden marker "forbidden"` | ||
|
||
ForbiddenMarkerFieldTypeAlias ForbiddenMarkerType `json:"forbiddenMarkerFieldTypeAlias"` // want `field ForbiddenMarkerFieldTypeAlias has forbidden marker "forbidden"` | ||
|
||
// +allowed | ||
AllowedMarkerField string `json:"allowedMarkerField"` | ||
|
||
AllowedMarkerFieldTypeAlias AllowedMarkerType `json:"AllowedMarkerFieldTypeAlias"` | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package a | ||
|
||
type ForbiddenMarkerType string // want `type ForbiddenMarkerType has forbidden marker "forbidden"` | ||
|
||
// +allowed | ||
type AllowedMarkerType string | ||
|
||
type Test struct { | ||
ForbiddenMarkerField string `json:"forbiddenMarkerField"`// want `field ForbiddenMarkerField has forbidden marker "forbidden"` | ||
|
||
ForbiddenMarkerFieldTypeAlias ForbiddenMarkerType `json:"forbiddenMarkerFieldTypeAlias"` // want `field ForbiddenMarkerFieldTypeAlias has forbidden marker "forbidden"` | ||
|
||
// +allowed | ||
AllowedMarkerField string `json:"allowedMarkerField"` | ||
|
||
AllowedMarkerFieldTypeAlias AllowedMarkerType `json:"AllowedMarkerFieldTypeAlias"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
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 nonullable | ||
|
||
import ( | ||
"golang.org/x/tools/go/analysis" | ||
"sigs.k8s.io/kube-api-linter/pkg/analysis/forbiddenmarkers" | ||
"sigs.k8s.io/kube-api-linter/pkg/config" | ||
) | ||
|
||
const name = "nonullable" | ||
|
||
func newAnalyzer() *analysis.Analyzer { | ||
analyzer := forbiddenmarkers.NewAnalyzer(config.ForbiddenMarkersConfig{ | ||
Markers: []string{ | ||
"nullable", | ||
}, | ||
}) | ||
|
||
analyzer.Name = name | ||
analyzer.Doc = "Check that nullable marker is not present on any types or fields." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe create a constructor on the forbidden markers side that allows you to pass options for Name and Doc, rather than overriding them here? Then this just becomes
Or even, you could just call that inside the initializer. |
||
|
||
return analyzer | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
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 nonullable | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.RunWithSuggestedFixes(t, testdata, newAnalyzer(), "a/...") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
/* | ||
* The `nonullable` linter ensures that types and fields do not have the `nullable` marker. | ||
* | ||
* Fixes are suggested to remove the `nullable` marker. | ||
* | ||
Comment on lines
+18
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the asterisks at the beginning of each line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied format from another linter doc.go - but also I think my editor does this by default. I can strip the asterisks at the beginning of each line if necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't matter to me, but normally with |
||
*/ | ||
package nonullable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if I wanted to forbid a marker only if it had a certain parameter? Or a certain value?
E.g. I want to prevent someone from using a particular
kubebuilder:validation:Format
, would this allow me to do that?Or I wanted to say, you can't use the
optionalOldSelf
property on an XValidation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good questions. This would not allow that for now. I think those are reasonable use cases and make sense to consider.
I can add some configuration options to allow for this type of customization.