-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add Conflictingmarkers #126
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
k8s-ci-robot
merged 5 commits into
kubernetes-sigs:main
from
yongruilin:conflictmarkers
Jul 29, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14a91e2
feat: add conflictingmarkers linter to detect mutually exclusive markers
yongruilin 9be033a
test: add unit tests for conflicting markers analyzer and custom conf…
yongruilin ebda0ef
docs: update linters documentation to include conflictingmarkers lint…
yongruilin ed134ff
test: add suite and initializer tests for conflicting markers analysis
yongruilin 90a97b3
Address comments
yongruilin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
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 conflictingmarkers | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
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" | ||
) | ||
|
||
const name = "conflictingmarkers" | ||
|
||
type analyzer struct { | ||
conflictSets []ConflictSet | ||
} | ||
|
||
func newAnalyzer(cfg *ConflictingMarkersConfig) *analysis.Analyzer { | ||
if cfg == nil { | ||
cfg = &ConflictingMarkersConfig{} | ||
} | ||
|
||
// Register markers from configuration | ||
for _, conflictSet := range cfg.Conflicts { | ||
for _, set := range conflictSet.Sets { | ||
for _, markerID := range set { | ||
markers.DefaultRegistry().Register(markerID) | ||
} | ||
} | ||
} | ||
|
||
a := &analyzer{ | ||
conflictSets: cfg.Conflicts, | ||
} | ||
|
||
return &analysis.Analyzer{ | ||
Name: name, | ||
Doc: "Check that fields do not have conflicting markers from mutually exclusive sets", | ||
JoelSpeed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.conflictSets) | ||
}) | ||
|
||
return nil, nil //nolint:nilnil | ||
} | ||
|
||
func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers, conflictSets []ConflictSet) { | ||
if field == nil || len(field.Names) == 0 { | ||
return | ||
} | ||
|
||
markers := utils.TypeAwareMarkerCollectionForField(pass, markersAccess, field) | ||
|
||
for _, conflictSet := range conflictSets { | ||
checkConflict(pass, field, markers, conflictSet) | ||
} | ||
} | ||
|
||
func checkConflict(pass *analysis.Pass, field *ast.Field, markers markers.MarkerSet, conflictSet ConflictSet) { | ||
// Track which sets have markers present | ||
conflictingMarkers := make([]sets.Set[string], 0) | ||
|
||
for _, set := range conflictSet.Sets { | ||
foundMarkers := sets.New[string]() | ||
|
||
for _, markerID := range set { | ||
if markers.Has(markerID) { | ||
foundMarkers.Insert(markerID) | ||
} | ||
} | ||
// Only add the set if it has at least one marker | ||
if foundMarkers.Len() > 0 { | ||
conflictingMarkers = append(conflictingMarkers, foundMarkers) | ||
} | ||
} | ||
|
||
// If two or more sets have markers, report the conflict | ||
if len(conflictingMarkers) >= 2 { | ||
reportConflict(pass, field, conflictSet, conflictingMarkers) | ||
} | ||
} | ||
|
||
func reportConflict(pass *analysis.Pass, field *ast.Field, conflictSet ConflictSet, conflictingMarkers []sets.Set[string]) { | ||
// Build a descriptive message showing which sets conflict | ||
setDescriptions := make([]string, 0, len(conflictingMarkers)) | ||
|
||
for _, set := range conflictingMarkers { | ||
markersList := sets.List(set) | ||
setDescriptions = append(setDescriptions, fmt.Sprintf("%v", markersList)) | ||
} | ||
|
||
message := fmt.Sprintf("field %s has conflicting markers: %s: {%s}. %s", | ||
field.Names[0].Name, | ||
conflictSet.Name, | ||
strings.Join(setDescriptions, ", "), | ||
conflictSet.Description) | ||
|
||
pass.Report(analysis.Diagnostic{ | ||
Pos: field.Pos(), | ||
Message: message, | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
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 conflictingmarkers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
"sigs.k8s.io/kube-api-linter/pkg/analysis/conflictingmarkers" | ||
) | ||
|
||
func TestConflictingMarkersAnalyzer(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
|
||
config := &conflictingmarkers.ConflictingMarkersConfig{ | ||
Conflicts: []conflictingmarkers.ConflictSet{ | ||
{ | ||
Name: "test_conflict", | ||
Sets: [][]string{{"marker1", "marker2"}, {"marker3", "marker4"}}, | ||
Description: "Test markers conflict with each other", | ||
}, | ||
{ | ||
Name: "three_way_conflict", | ||
Sets: [][]string{{"marker5", "marker6"}, {"marker7", "marker8"}, {"marker9", "marker10"}}, | ||
Description: "Three-way conflict between marker sets", | ||
}, | ||
}, | ||
} | ||
|
||
initializer := conflictingmarkers.Initializer() | ||
|
||
analyzer, err := initializer.Init(config) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
analysistest.Run(t, testdata, analyzer, "a") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
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 conflictingmarkers | ||
|
||
// ConflictingMarkersConfig contains the configuration for the conflictingmarkers linter. | ||
type ConflictingMarkersConfig struct { | ||
// Conflicts allows users to define sets of conflicting markers. | ||
// Each entry defines a conflict between multiple sets of markers. | ||
Conflicts []ConflictSet `json:"conflicts"` | ||
} | ||
|
||
// ConflictSet represents a conflict between multiple sets of markers. | ||
// Markers within each set are mutually exclusive with markers in all other sets. | ||
// The linter will emit a diagnostic when a field has markers from two or more sets. | ||
type ConflictSet struct { | ||
// Name is a human-readable name for this conflict set. | ||
JoelSpeed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// This name will appear in diagnostic messages to identify the type of conflict. | ||
Name string `json:"name"` | ||
// Sets contains the sets of markers that are mutually exclusive with each other. | ||
// Each set is a slice of marker identifiers. | ||
// The linter will emit a diagnostic when a field has markers from two or more sets. | ||
Sets [][]string `json:"sets"` | ||
// Description provides a description of why these markers conflict. | ||
JoelSpeed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// The linter will include this description in the diagnostic message when a conflict is detected. | ||
Description string `json:"description"` | ||
} |
29 changes: 29 additions & 0 deletions
29
pkg/analysis/conflictingmarkers/conflictingmarkers_suite_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
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 conflictingmarkers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestConflictingMarkers(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "conflictingmarkers") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
/* | ||
JoelSpeed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
conflictingmarkers is a linter that detects and reports when mutually exclusive markers are used on the same field. | ||
This prevents common configuration errors and unexpected behavior in Kubernetes API types. | ||
|
||
The linter reports issues when markers from two or more sets of a conflict definition are present on the same field. | ||
It does NOT report issues when multiple markers from the same set are present - only when markers from | ||
different sets within the same conflict definition are found together. | ||
|
||
The linter is fully configurable and requires users to define all conflict sets they want to check. | ||
There are no built-in conflict sets - all conflicts must be explicitly configured. | ||
|
||
Each conflict set must specify: | ||
- A unique name for the conflict | ||
- Multiple sets of markers that are mutually exclusive with each other (at least 2 sets) | ||
- A description explaining why the markers conflict | ||
|
||
Example configuration: | ||
```yaml | ||
lintersConfig: | ||
|
||
conflictingmarkers: | ||
conflicts: | ||
- name: "optional_vs_required" | ||
sets: | ||
- ["optional", "+kubebuilder:validation:Optional", "+k8s:validation:optional"] | ||
- ["required", "+kubebuilder:validation:Required", "+k8s:validation:required"] | ||
description: "A field cannot be both optional and required" | ||
- name: "my_custom_conflict" | ||
sets: | ||
- ["custom:marker1", "custom:marker2"] | ||
- ["custom:marker3", "custom:marker4"] | ||
- ["custom:marker5", "custom:marker6"] | ||
description: "These markers define different storage backends that cannot be used simultaneously" | ||
|
||
``` | ||
|
||
Configuration options: | ||
- `conflicts`: Required list of conflict set definitions. | ||
|
||
Note: This linter is not enabled by default and must be explicitly enabled in the configuration. | ||
|
||
The linter does not provide automatic fixes as it cannot determine which conflicting marker should be removed. | ||
*/ | ||
package conflictingmarkers |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.