forked from kubernetes-sigs/kube-api-linter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.go
More file actions
132 lines (107 loc) · 3.78 KB
/
Copy pathanalyzer.go
File metadata and controls
132 lines (107 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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",
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,
})
}