|
| 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 defaultorrequired |
| 17 | + |
| 18 | +import ( |
| 19 | + "golang.org/x/tools/go/analysis" |
| 20 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/conflictingmarkers" |
| 21 | + "sigs.k8s.io/kube-api-linter/pkg/markers" |
| 22 | +) |
| 23 | + |
| 24 | +const name = "defaultorrequired" |
| 25 | + |
| 26 | +// newAnalyzer creates a new analyzer that wraps conflictingmarkers with a predefined configuration |
| 27 | +// for checking default and required marker conflicts. |
| 28 | +func newAnalyzer() *analysis.Analyzer { |
| 29 | + cfg := &conflictingmarkers.ConflictingMarkersConfig{ |
| 30 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 31 | + { |
| 32 | + Name: "default_or_required", |
| 33 | + Sets: [][]string{ |
| 34 | + {markers.DefaultMarker, markers.KubebuilderDefaultMarker}, |
| 35 | + {markers.RequiredMarker, markers.KubebuilderRequiredMarker, markers.K8sRequiredMarker}, |
| 36 | + }, |
| 37 | + Description: "A field with a default value cannot be required. A required field must be provided by the user, so a default value is not meaningful.", |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + // Create the underlying conflicting markers analyzer |
| 43 | + analyzer := conflictingmarkers.NewAnalyzer(cfg) |
| 44 | + |
| 45 | + // Override the name to match this linter |
| 46 | + analyzer.Name = name |
| 47 | + analyzer.Doc = "Checks that fields marked as required do not have default values applied" |
| 48 | + |
| 49 | + return analyzer |
| 50 | +} |
0 commit comments