|
| 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 conflictingmarkers |
| 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 | +) |
| 31 | + |
| 32 | +const name = "conflictingmarkers" |
| 33 | + |
| 34 | +type analyzer struct { |
| 35 | + conflictSets []ConflictSet |
| 36 | +} |
| 37 | + |
| 38 | +func newAnalyzer(cfg *ConflictingMarkersConfig) *analysis.Analyzer { |
| 39 | + if cfg == nil { |
| 40 | + cfg = &ConflictingMarkersConfig{} |
| 41 | + } |
| 42 | + |
| 43 | + // Register markers from configuration |
| 44 | + for _, conflictSet := range cfg.Conflicts { |
| 45 | + for _, set := range conflictSet.Sets { |
| 46 | + for _, markerID := range set { |
| 47 | + markers.DefaultRegistry().Register(markerID) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + a := &analyzer{ |
| 53 | + conflictSets: cfg.Conflicts, |
| 54 | + } |
| 55 | + |
| 56 | + // Use configured documentation or fall back to default |
| 57 | + doc := cfg.Doc |
| 58 | + if doc == "" { |
| 59 | + doc = "Check that fields do not have conflicting markers from mutually exclusive sets" |
| 60 | + } |
| 61 | + |
| 62 | + return &analysis.Analyzer{ |
| 63 | + Name: name, |
| 64 | + Doc: doc, |
| 65 | + Run: a.run, |
| 66 | + Requires: []*analysis.Analyzer{inspector.Analyzer}, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func (a *analyzer) run(pass *analysis.Pass) (any, error) { |
| 71 | + inspect, ok := pass.ResultOf[inspector.Analyzer].(inspector.Inspector) |
| 72 | + if !ok { |
| 73 | + return nil, kalerrors.ErrCouldNotGetInspector |
| 74 | + } |
| 75 | + |
| 76 | + inspect.InspectFields(func(field *ast.Field, stack []ast.Node, _ extractjsontags.FieldTagInfo, markersAccess markers.Markers) { |
| 77 | + checkField(pass, field, markersAccess, a.conflictSets) |
| 78 | + }) |
| 79 | + |
| 80 | + return nil, nil //nolint:nilnil |
| 81 | +} |
| 82 | + |
| 83 | +func checkField(pass *analysis.Pass, field *ast.Field, markersAccess markers.Markers, conflictSets []ConflictSet) { |
| 84 | + if field == nil || len(field.Names) == 0 { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + markers := utils.TypeAwareMarkerCollectionForField(pass, markersAccess, field) |
| 89 | + |
| 90 | + for _, conflictSet := range conflictSets { |
| 91 | + checkConflict(pass, field, markers, conflictSet) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func checkConflict(pass *analysis.Pass, field *ast.Field, markers markers.MarkerSet, conflictSet ConflictSet) { |
| 96 | + // Track which sets have markers present |
| 97 | + conflictingMarkers := make([]sets.Set[string], 0) |
| 98 | + |
| 99 | + for _, set := range conflictSet.Sets { |
| 100 | + foundMarkers := sets.New[string]() |
| 101 | + |
| 102 | + for _, markerID := range set { |
| 103 | + if markers.Has(markerID) { |
| 104 | + foundMarkers.Insert(markerID) |
| 105 | + } |
| 106 | + } |
| 107 | + // Only add the set if it has at least one marker |
| 108 | + if foundMarkers.Len() > 0 { |
| 109 | + conflictingMarkers = append(conflictingMarkers, foundMarkers) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // If two or more sets have markers, report the conflict |
| 114 | + if len(conflictingMarkers) >= 2 { |
| 115 | + reportConflict(pass, field, conflictSet, conflictingMarkers) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func reportConflict(pass *analysis.Pass, field *ast.Field, conflictSet ConflictSet, conflictingMarkers []sets.Set[string]) { |
| 120 | + // Build a descriptive message showing which sets conflict |
| 121 | + setDescriptions := make([]string, 0, len(conflictingMarkers)) |
| 122 | + |
| 123 | + for _, set := range conflictingMarkers { |
| 124 | + markersList := sets.List(set) |
| 125 | + setDescriptions = append(setDescriptions, fmt.Sprintf("%v", markersList)) |
| 126 | + } |
| 127 | + |
| 128 | + message := fmt.Sprintf("field %s has conflicting markers: %s: {%s}. %s", |
| 129 | + field.Names[0].Name, |
| 130 | + conflictSet.Name, |
| 131 | + strings.Join(setDescriptions, ", "), |
| 132 | + conflictSet.Description) |
| 133 | + |
| 134 | + pass.Report(analysis.Diagnostic{ |
| 135 | + Pos: field.Pos(), |
| 136 | + Message: message, |
| 137 | + }) |
| 138 | +} |
0 commit comments