Skip to content

Ignore field that has schemaless marker #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/analysis/helpers/inspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"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.
Expand Down Expand Up @@ -54,7 +55,7 @@
// InspectFields iterates over fields in structs, ignoring any struct that is not a type declaration, and any field that is ignored and
// therefore would not be included in the CRD spec.
// For the remaining fields, it calls the provided inspectField function to apply analysis logic.
func (i *inspector) InspectFields(inspectField func(field *ast.Field, stack []ast.Node, jsonTagInfo extractjsontags.FieldTagInfo, markersAccess markers.Markers)) {

Check failure on line 58 in pkg/analysis/helpers/inspector/inspector.go

View workflow job for this annotation

GitHub Actions / golangci-lint

calculated cyclomatic complexity for function InspectFields is 11, max is 10 (cyclop)
// Filter to fields so that we can iterate over fields in a struct.
nodeFilter := []ast.Node{
(*ast.Field)(nil),
Expand Down Expand Up @@ -105,6 +106,14 @@
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.
Expand Down
8 changes: 8 additions & 0 deletions pkg/analysis/maxlength/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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{}
8 changes: 8 additions & 0 deletions pkg/analysis/ssatags/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
8 changes: 8 additions & 0 deletions pkg/analysis/ssatags/testdata/src/a/a.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
8 changes: 8 additions & 0 deletions pkg/analysis/ssatags/testdata/src/b/b.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
7 changes: 7 additions & 0 deletions pkg/analysis/ssatags/testdata/src/b/b.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
5 changes: 5 additions & 0 deletions pkg/markers/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be part of the kubebuilder set of markers, and include the Kubebuilder prefix to be consistent please

)
Loading