|
| 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 | + "slices" |
| 22 | + |
| 23 | + "golang.org/x/tools/go/analysis" |
| 24 | + kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors" |
| 25 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" |
| 26 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector" |
| 27 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" |
| 28 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/utils" |
| 29 | +) |
| 30 | + |
| 31 | +const name = "forbiddenmarkers" |
| 32 | + |
| 33 | +type analyzer struct { |
| 34 | + forbiddenMarkers []Marker |
| 35 | +} |
| 36 | + |
| 37 | +// NewAnalyzer creates a new analysis.Analyzer for the forbiddenmarkers |
| 38 | +// linter based on the provided config.ForbiddenMarkersConfig. |
| 39 | +func newAnalyzer(cfg *Config) *analysis.Analyzer { |
| 40 | + a := &analyzer{ |
| 41 | + forbiddenMarkers: cfg.Markers, |
| 42 | + } |
| 43 | + |
| 44 | + analyzer := &analysis.Analyzer{ |
| 45 | + Name: name, |
| 46 | + Doc: "Check that no forbidden markers are present on types and fields.", |
| 47 | + Run: a.run, |
| 48 | + Requires: []*analysis.Analyzer{inspector.Analyzer}, |
| 49 | + } |
| 50 | + |
| 51 | + for _, marker := range a.forbiddenMarkers { |
| 52 | + markers.DefaultRegistry().Register(marker.Identifier) |
| 53 | + } |
| 54 | + |
| 55 | + return analyzer |
| 56 | +} |
| 57 | + |
| 58 | +func (a *analyzer) run(pass *analysis.Pass) (any, error) { |
| 59 | + inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) |
| 60 | + if !ok { |
| 61 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 62 | + } |
| 63 | + |
| 64 | + inspect.InspectFields(func(field *ast.Field, stack []ast.Node, _ extractjsontags.FieldTagInfo, markersAccess markers.Markers) { |
| 65 | + checkField(pass, field, markersAccess, a.forbiddenMarkers) |
| 66 | + }) |
| 67 | + |
| 68 | + inspect.InspectTypeSpec(func(typeSpec *ast.TypeSpec, markersAccess markers.Markers) { |
| 69 | + checkType(pass, typeSpec, markersAccess, a.forbiddenMarkers) |
| 70 | + }) |
| 71 | + |
| 72 | + return nil, nil //nolint:nilnil |
| 73 | +} |
| 74 | + |
| 75 | +func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers, forbiddenMarkers []Marker) { |
| 76 | + if field == nil || len(field.Names) == 0 { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + markers := utils.TypeAwareMarkerCollectionForField(pass, markersAccess, field) |
| 81 | + check(markers, forbiddenMarkers, reportField(pass, field)) |
| 82 | +} |
| 83 | + |
| 84 | +func checkType(pass *analysis.Pass, typeSpec *ast.TypeSpec, markersAccess markers.Markers, forbiddenMarkers []Marker) { |
| 85 | + if typeSpec == nil { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + markers := markersAccess.TypeMarkers(typeSpec) |
| 90 | + check(markers, forbiddenMarkers, reportType(pass, typeSpec)) |
| 91 | +} |
| 92 | + |
| 93 | +func check(markerSet markers.MarkerSet, forbiddenMarkers []Marker, reportFunc func(marker markers.Marker)) { |
| 94 | + for _, marker := range forbiddenMarkers { |
| 95 | + marks := markerSet.Get(marker.Identifier) |
| 96 | + for _, mark := range marks { |
| 97 | + if len(marker.RuleSets) == 0 { |
| 98 | + reportFunc(mark) |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + for _, ruleSet := range marker.RuleSets { |
| 103 | + if markerMatchesAttributeRules(mark, ruleSet.Attributes...) { |
| 104 | + reportFunc(mark) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func markerMatchesAttributeRules(marker markers.Marker, attrRules ...MarkerAttribute) bool { |
| 112 | + for _, attrRule := range attrRules { |
| 113 | + // if the marker doesn't contain the attribute for a specified rule it fails the AND |
| 114 | + // operation. |
| 115 | + val, ok := marker.Expressions[attrRule.Name] |
| 116 | + if !ok { |
| 117 | + return false |
| 118 | + } |
| 119 | + |
| 120 | + // if the value doesn't match one of the forbidden ones, this marker is not forbidden |
| 121 | + if len(attrRule.Values) > 0 && !slices.Contains(attrRule.Values, val) { |
| 122 | + return false |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return true |
| 127 | +} |
| 128 | + |
| 129 | +func reportField(pass *analysis.Pass, field *ast.Field) func(marker markers.Marker) { |
| 130 | + return func(marker markers.Marker) { |
| 131 | + pass.Report(analysis.Diagnostic{ |
| 132 | + Pos: field.Pos(), |
| 133 | + Message: fmt.Sprintf("field %s has forbidden marker %q", field.Names[0].Name, marker.String()), |
| 134 | + SuggestedFixes: []analysis.SuggestedFix{ |
| 135 | + { |
| 136 | + Message: fmt.Sprintf("remove forbidden marker %q", marker.String()), |
| 137 | + TextEdits: []analysis.TextEdit{ |
| 138 | + { |
| 139 | + Pos: marker.Pos, |
| 140 | + End: marker.End, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func reportType(pass *analysis.Pass, typeSpec *ast.TypeSpec) func(marker markers.Marker) { |
| 150 | + return func(marker markers.Marker) { |
| 151 | + pass.Report(analysis.Diagnostic{ |
| 152 | + Pos: typeSpec.Pos(), |
| 153 | + Message: fmt.Sprintf("type %s has forbidden marker %q", typeSpec.Name, marker.String()), |
| 154 | + SuggestedFixes: []analysis.SuggestedFix{ |
| 155 | + { |
| 156 | + Message: fmt.Sprintf("remove forbidden marker %q", marker.String()), |
| 157 | + TextEdits: []analysis.TextEdit{ |
| 158 | + { |
| 159 | + Pos: marker.Pos, |
| 160 | + End: marker.End, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
0 commit comments