|
| 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 uniquemarkers |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "go/ast" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "golang.org/x/tools/go/analysis" |
| 24 | + "k8s.io/apimachinery/pkg/util/sets" |
| 25 | + kalerrors "sigs.k8s.io/kube-api-linter/pkg/analysis/errors" |
| 26 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" |
| 27 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/inspector" |
| 28 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" |
| 29 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/utils" |
| 30 | + "sigs.k8s.io/kube-api-linter/pkg/config" |
| 31 | + markersconsts "sigs.k8s.io/kube-api-linter/pkg/markers" |
| 32 | +) |
| 33 | + |
| 34 | +const name = "uniquemarkers" |
| 35 | + |
| 36 | +func init() { |
| 37 | + for _, uniqueMarker := range defaultUniqueMarkers() { |
| 38 | + markers.DefaultRegistry().Register(uniqueMarker.Identifier) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type analyzer struct { |
| 43 | + uniqueMarkers []config.UniqueMarker |
| 44 | +} |
| 45 | + |
| 46 | +func newAnalyzer(cfg config.UniqueMarkersConfig) *analysis.Analyzer { |
| 47 | + a := &analyzer{ |
| 48 | + uniqueMarkers: append(defaultUniqueMarkers(), cfg.CustomMarkers...), |
| 49 | + } |
| 50 | + |
| 51 | + return &analysis.Analyzer{ |
| 52 | + Name: name, |
| 53 | + Doc: "Check that all markers that should be unique on a field/type are only present once", |
| 54 | + Run: a.run, |
| 55 | + Requires: []*analysis.Analyzer{inspector.Analyzer}, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (a *analyzer) run(pass *analysis.Pass) (any, error) { |
| 60 | + inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) |
| 61 | + if !ok { |
| 62 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 63 | + } |
| 64 | + |
| 65 | + inspect.InspectFields(func(field *ast.Field, stack []ast.Node, _ extractjsontags.FieldTagInfo, markersAccess markers.Markers) { |
| 66 | + checkField(pass, field, markersAccess, a.uniqueMarkers) |
| 67 | + }) |
| 68 | + |
| 69 | + inspect.InspectTypeSpec(func(typeSpec *ast.TypeSpec, markersAccess markers.Markers) { |
| 70 | + checkType(pass, typeSpec, markersAccess, a.uniqueMarkers) |
| 71 | + }) |
| 72 | + |
| 73 | + return nil, nil //nolint:nilnil |
| 74 | +} |
| 75 | + |
| 76 | +func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers, uniqueMarkers []config.UniqueMarker) { |
| 77 | + if field == nil || len(field.Names) == 0 { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + markers := utils.TypeAwareMarkerCollectionForField(pass, markersAccess, field) |
| 82 | + check(markers, uniqueMarkers, reportField(pass, field)) |
| 83 | +} |
| 84 | + |
| 85 | +func checkType(pass *analysis.Pass, typeSpec *ast.TypeSpec, markersAccess markers.Markers, uniqueMarkers []config.UniqueMarker) { |
| 86 | + if typeSpec == nil { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + markers := markersAccess.TypeMarkers(typeSpec) |
| 91 | + check(markers, uniqueMarkers, reportType(pass, typeSpec)) |
| 92 | +} |
| 93 | + |
| 94 | +func check(markerSet markers.MarkerSet, uniqueMarkers []config.UniqueMarker, reportFunc func(id string)) { |
| 95 | + for _, marker := range uniqueMarkers { |
| 96 | + marks := markerSet.Get(marker.Identifier) |
| 97 | + markSet := sets.New[string]() |
| 98 | + |
| 99 | + for _, mark := range marks { |
| 100 | + id := constructIdentifier(mark, marker.Attributes...) |
| 101 | + |
| 102 | + if markSet.Has(id) { |
| 103 | + reportFunc(id) |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + markSet.Insert(id) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// constructIdentifier returns a string that serves as a unique identifier for a |
| 113 | +// marker based on the provided attributes that should be unique for the marker. |
| 114 | +func constructIdentifier(marker markers.Marker, attributes ...string) string { |
| 115 | + // if there are no unique attributes, the unique identifier is just |
| 116 | + // the base marker identifier |
| 117 | + if len(attributes) == 0 { |
| 118 | + return marker.Identifier |
| 119 | + } |
| 120 | + |
| 121 | + // If a marker doesn't specify the attribute, we should assume it is equivalent |
| 122 | + // to the empty string ("") so that we can still key on uniqueness of other attributes |
| 123 | + // effectively. |
| 124 | + id := fmt.Sprintf("%s:", marker.Identifier) |
| 125 | + for _, attr := range attributes { |
| 126 | + id += fmt.Sprintf("%s=%s,", attr, marker.Expressions[attr]) |
| 127 | + } |
| 128 | + |
| 129 | + id = strings.TrimSuffix(id, ",") |
| 130 | + |
| 131 | + return id |
| 132 | +} |
| 133 | + |
| 134 | +func reportField(pass *analysis.Pass, field *ast.Field) func(id string) { |
| 135 | + return func(id string) { |
| 136 | + pass.Report(analysis.Diagnostic{ |
| 137 | + Pos: field.Pos(), |
| 138 | + Message: fmt.Sprintf("field %s has multiple definitions of marker %s when only a single definition should exist", field.Names[0].Name, id), |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func reportType(pass *analysis.Pass, typeSpec *ast.TypeSpec) func(id string) { |
| 144 | + return func(id string) { |
| 145 | + pass.Report(analysis.Diagnostic{ |
| 146 | + Pos: typeSpec.Pos(), |
| 147 | + Message: fmt.Sprintf("type %s has multiple definitions of marker %s when only a single definition should exist", typeSpec.Name, id), |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +//nolint:funlen |
| 153 | +func defaultUniqueMarkers() []config.UniqueMarker { |
| 154 | + return []config.UniqueMarker{ |
| 155 | + // Basic unique markers |
| 156 | + // ------ |
| 157 | + { |
| 158 | + Identifier: markersconsts.DefaultMarker, |
| 159 | + }, |
| 160 | + // ------ |
| 161 | + |
| 162 | + // Kubebuilder-specific unique markers |
| 163 | + // ------ |
| 164 | + { |
| 165 | + Identifier: markersconsts.KubebuilderDefaultMarker, |
| 166 | + }, |
| 167 | + { |
| 168 | + Identifier: markersconsts.KubebuilderExampleMarker, |
| 169 | + }, |
| 170 | + { |
| 171 | + Identifier: markersconsts.KubebuilderEnumMarker, |
| 172 | + }, |
| 173 | + { |
| 174 | + Identifier: markersconsts.KubebuilderExclusiveMaximumMarker, |
| 175 | + }, |
| 176 | + { |
| 177 | + Identifier: markersconsts.KubebuilderExclusiveMinimumMarker, |
| 178 | + }, |
| 179 | + { |
| 180 | + Identifier: markersconsts.KubebuilderFormatMarker, |
| 181 | + }, |
| 182 | + { |
| 183 | + Identifier: markersconsts.KubebuilderMaxItemsMarker, |
| 184 | + }, |
| 185 | + { |
| 186 | + Identifier: markersconsts.KubebuilderMaxLengthMarker, |
| 187 | + }, |
| 188 | + { |
| 189 | + Identifier: markersconsts.KubebuilderMaxPropertiesMarker, |
| 190 | + }, |
| 191 | + { |
| 192 | + Identifier: markersconsts.KubebuilderMaximumMarker, |
| 193 | + }, |
| 194 | + { |
| 195 | + Identifier: markersconsts.KubebuilderMinItemsMarker, |
| 196 | + }, |
| 197 | + { |
| 198 | + Identifier: markersconsts.KubebuilderMinLengthMarker, |
| 199 | + }, |
| 200 | + { |
| 201 | + Identifier: markersconsts.KubebuilderMinPropertiesMarker, |
| 202 | + }, |
| 203 | + { |
| 204 | + Identifier: markersconsts.KubebuilderMinimumMarker, |
| 205 | + }, |
| 206 | + { |
| 207 | + Identifier: markersconsts.KubebuilderMultipleOfMarker, |
| 208 | + }, |
| 209 | + { |
| 210 | + Identifier: markersconsts.KubebuilderPatternMarker, |
| 211 | + }, |
| 212 | + { |
| 213 | + Identifier: markersconsts.KubebuilderTypeMarker, |
| 214 | + }, |
| 215 | + { |
| 216 | + Identifier: markersconsts.KubebuilderUniqueItemsMarker, |
| 217 | + }, |
| 218 | + |
| 219 | + { |
| 220 | + Identifier: markersconsts.KubebuilderItemsEnumMarker, |
| 221 | + }, |
| 222 | + { |
| 223 | + Identifier: markersconsts.KubebuilderItemsFormatMarker, |
| 224 | + }, |
| 225 | + { |
| 226 | + Identifier: markersconsts.KubebuilderItemsMaxLengthMarker, |
| 227 | + }, |
| 228 | + { |
| 229 | + Identifier: markersconsts.KubebuilderItemsMaxItemsMarker, |
| 230 | + }, |
| 231 | + { |
| 232 | + Identifier: markersconsts.KubebuilderItemsMaxPropertiesMarker, |
| 233 | + }, |
| 234 | + { |
| 235 | + Identifier: markersconsts.KubebuilderItemsMaximumMarker, |
| 236 | + }, |
| 237 | + { |
| 238 | + Identifier: markersconsts.KubebuilderItemsMinLengthMarker, |
| 239 | + }, |
| 240 | + { |
| 241 | + Identifier: markersconsts.KubebuilderItemsMinItemsMarker, |
| 242 | + }, |
| 243 | + { |
| 244 | + Identifier: markersconsts.KubebuilderItemsMinPropertiesMarker, |
| 245 | + }, |
| 246 | + { |
| 247 | + Identifier: markersconsts.KubebuilderItemsMinimumMarker, |
| 248 | + }, |
| 249 | + { |
| 250 | + Identifier: markersconsts.KubebuilderItemsExclusiveMaximumMarker, |
| 251 | + }, |
| 252 | + { |
| 253 | + Identifier: markersconsts.KubebuilderItemsExclusiveMinimumMarker, |
| 254 | + }, |
| 255 | + { |
| 256 | + Identifier: markersconsts.KubebuilderItemsMultipleOfMarker, |
| 257 | + }, |
| 258 | + { |
| 259 | + Identifier: markersconsts.KubebuilderItemsPatternMarker, |
| 260 | + }, |
| 261 | + { |
| 262 | + Identifier: markersconsts.KubebuilderItemsTypeMarker, |
| 263 | + }, |
| 264 | + { |
| 265 | + Identifier: markersconsts.KubebuilderItemsUniqueItemsMarker, |
| 266 | + }, |
| 267 | + { |
| 268 | + Identifier: markersconsts.KubebuilderXValidationMarker, |
| 269 | + Attributes: []string{ |
| 270 | + "rule", |
| 271 | + }, |
| 272 | + }, |
| 273 | + { |
| 274 | + Identifier: markersconsts.KubebuilderItemsXValidationMarker, |
| 275 | + Attributes: []string{ |
| 276 | + "rule", |
| 277 | + }, |
| 278 | + }, |
| 279 | + // ------ |
| 280 | + } |
| 281 | +} |
0 commit comments