|
| 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 | + "errors" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "golang.org/x/tools/go/analysis" |
| 23 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 24 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/conflictingmarkers" |
| 25 | + "sigs.k8s.io/kube-api-linter/pkg/analysis/initializer" |
| 26 | + "sigs.k8s.io/kube-api-linter/pkg/markers" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + name = "defaultorrequired" |
| 31 | + doc = "Checks that fields marked as required do not have default values applied" |
| 32 | +) |
| 33 | + |
| 34 | +var errUnexpectedInitializerType = errors.New("expected conflictingmarkers.Initializer() to be of type initializer.ConfigurableAnalyzerInitializer, but was not") |
| 35 | + |
| 36 | +// newAnalyzer creates a new analyzer that wraps conflictingmarkers with a predefined configuration |
| 37 | +// for checking default and required marker conflicts. |
| 38 | +func newAnalyzer() *analysis.Analyzer { |
| 39 | + cfg := &conflictingmarkers.ConflictingMarkersConfig{ |
| 40 | + Conflicts: []conflictingmarkers.ConflictSet{ |
| 41 | + { |
| 42 | + Name: "default_or_required", |
| 43 | + Sets: [][]string{ |
| 44 | + {markers.DefaultMarker, markers.KubebuilderDefaultMarker}, |
| 45 | + {markers.RequiredMarker, markers.KubebuilderRequiredMarker, markers.K8sRequiredMarker}, |
| 46 | + }, |
| 47 | + 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.", |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + configInit, ok := conflictingmarkers.Initializer().(initializer.ConfigurableAnalyzerInitializer) |
| 53 | + if !ok { |
| 54 | + panic(fmt.Errorf("getting initializer: %w", errUnexpectedInitializerType)) |
| 55 | + } |
| 56 | + |
| 57 | + errs := configInit.ValidateConfig(cfg, field.NewPath("defaultorrequired")) |
| 58 | + if err := errs.ToAggregate(); err != nil { |
| 59 | + panic(fmt.Errorf("defaultorrequired linter has an invalid conflictingmarkers configuration: %w", err)) |
| 60 | + } |
| 61 | + |
| 62 | + analyzer, err := configInit.Init(cfg) |
| 63 | + if err != nil { |
| 64 | + panic(fmt.Errorf("defaultorrequired linter encountered an error initializing wrapped conflictingmarkers analyzer: %w", err)) |
| 65 | + } |
| 66 | + |
| 67 | + analyzer.Name = name |
| 68 | + analyzer.Doc = doc |
| 69 | + |
| 70 | + return analyzer |
| 71 | +} |
0 commit comments