diff --git a/pkg/analysis/helpers/inspector/inspector.go b/pkg/analysis/helpers/inspector/inspector.go index af2860aa..7342eadb 100644 --- a/pkg/analysis/helpers/inspector/inspector.go +++ b/pkg/analysis/helpers/inspector/inspector.go @@ -24,6 +24,7 @@ import ( "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/extractjsontags" "sigs.k8s.io/kube-api-linter/pkg/analysis/helpers/markers" "sigs.k8s.io/kube-api-linter/pkg/analysis/utils" + markersconsts "sigs.k8s.io/kube-api-linter/pkg/markers" ) // Inspector is an interface that allows for the inspection of fields in structs. @@ -105,6 +106,14 @@ func (i *inspector) InspectFields(inspectField func(field *ast.Field, stack []as return false } + markerSet := i.markers.FieldMarkers(field) + + schemalessMarker := markerSet.Get(markersconsts.SchemaLessMarker) + if len(schemalessMarker) > 0 { + // If the field is marked as schemaless, we don't need to inspect it. + return false + } + defer func() { if r := recover(); r != nil { // If the inspectField function panics, we recover and log information that will help identify the issue. diff --git a/pkg/analysis/maxlength/testdata/src/a/a.go b/pkg/analysis/maxlength/testdata/src/a/a.go index 50a3011c..b2ce71de 100644 --- a/pkg/analysis/maxlength/testdata/src/a/a.go +++ b/pkg/analysis/maxlength/testdata/src/a/a.go @@ -86,6 +86,11 @@ type MaxLength struct { StringWithoutMaxLength string // want "field StringWithoutMaxLength must have a maximum length, add kubebuilder:validation:MaxLength marker" } `json:"struct"` + + // +optional + // +kubebuilder:pruning:PreserveUnknownFields + // +kubebuilder:validation:Schemaless + AllOf []JSONSchemaProps `json:"allOf,omitempty"` } // StringAlias is a string without a MaxLength. @@ -105,3 +110,6 @@ type ByteSliceAlias []byte // ByteSliceAliasWithMaxLength is a byte slice with a MaxLength. // +kubebuilder:validation:MaxLength:=512 type ByteSliceAliasWithMaxLength []byte + +// JSONSchemaProps is a placeholder for the JSON schema properties. +type JSONSchemaProps struct{} diff --git a/pkg/analysis/ssatags/testdata/src/a/a.go b/pkg/analysis/ssatags/testdata/src/a/a.go index 42fa15cf..7a367b17 100644 --- a/pkg/analysis/ssatags/testdata/src/a/a.go +++ b/pkg/analysis/ssatags/testdata/src/a/a.go @@ -190,4 +190,12 @@ type SSATagsTestSpec struct { // Byte array with markers - should be ignored even if markers are present // +listType=atomic ByteArrayWithMarker []byte `json:"byteArrayWithMarker,omitempty"` // want "ByteArrayWithMarker is a byte array, which does not support the listType marker. Remove the listType marker" + + // +optional + // +kubebuilder:pruning:PreserveUnknownFields + // +kubebuilder:validation:Schemaless + AllOf []JSONSchemaProps `json:"allOf,omitempty"` } + +// JSONSchemaProps is a placeholder for the JSON schema properties. +type JSONSchemaProps struct{} diff --git a/pkg/analysis/ssatags/testdata/src/a/a.go.golden b/pkg/analysis/ssatags/testdata/src/a/a.go.golden index 34c58a1a..bcf806b4 100644 --- a/pkg/analysis/ssatags/testdata/src/a/a.go.golden +++ b/pkg/analysis/ssatags/testdata/src/a/a.go.golden @@ -189,4 +189,12 @@ type SSATagsTestSpec struct { // Byte array with markers - should be ignored even if markers are present ByteArrayWithMarker []byte `json:"byteArrayWithMarker,omitempty"` // want "ByteArrayWithMarker is a byte array, which does not support the listType marker. Remove the listType marker" + + // +optional + // +kubebuilder:pruning:PreserveUnknownFields + // +kubebuilder:validation:Schemaless + AllOf []JSONSchemaProps `json:"allOf,omitempty"` } + +// JSONSchemaProps is a placeholder for the JSON schema properties. +type JSONSchemaProps struct{} diff --git a/pkg/analysis/ssatags/testdata/src/b/b.go b/pkg/analysis/ssatags/testdata/src/b/b.go index 63b70d43..a03d84c8 100644 --- a/pkg/analysis/ssatags/testdata/src/b/b.go +++ b/pkg/analysis/ssatags/testdata/src/b/b.go @@ -185,4 +185,12 @@ type SSATagsTestSpec struct { // Byte array with markers - should be ignored even if markers are present // +listType=atomic ByteArrayWithMarker []byte `json:"byteArrayWithMarker,omitempty"` // want "ByteArrayWithMarker is a byte array, which does not support the listType marker. Remove the listType marker" + + // +optional + // +kubebuilder:pruning:PreserveUnknownFields + // +kubebuilder:validation:Schemaless + AllOf []JSONSchemaProps `json:"allOf,omitempty"` } + +// JSONSchemaProps is a placeholder for the JSON schema properties. +type JSONSchemaProps struct{} diff --git a/pkg/analysis/ssatags/testdata/src/b/b.go.golden b/pkg/analysis/ssatags/testdata/src/b/b.go.golden index 510b7a07..7ac19fa5 100644 --- a/pkg/analysis/ssatags/testdata/src/b/b.go.golden +++ b/pkg/analysis/ssatags/testdata/src/b/b.go.golden @@ -184,5 +184,12 @@ type SSATagsTestSpec struct { // Byte array with markers - should be ignored even if markers are present ByteArrayWithMarker []byte `json:"byteArrayWithMarker,omitempty"` // want "ByteArrayWithMarker is a byte array, which does not support the listType marker. Remove the listType marker" + + // +optional + // +kubebuilder:pruning:PreserveUnknownFields + // +kubebuilder:validation:Schemaless + AllOf []JSONSchemaProps `json:"allOf,omitempty"` } +// JSONSchemaProps is a placeholder for the JSON schema properties. +type JSONSchemaProps struct{} diff --git a/pkg/markers/markers.go b/pkg/markers/markers.go index 0d20ad04..2de13b16 100644 --- a/pkg/markers/markers.go +++ b/pkg/markers/markers.go @@ -164,3 +164,8 @@ const ( // K8sRequiredMarker is the marker that indicates that a field is required in k8s declarative validation. K8sRequiredMarker = "k8s:required" ) + +const ( + // SchemaLessMarker is the marker that indicates that a struct is schemaless. + SchemaLessMarker = "kubebuilder:validation:Schemaless" +)