Skip to content
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

🐛 Fix SIGSERV when RawSpec is nil (due to anonymous struct) #1098

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
Properties: make(map[string]apiext.JSONSchemaProps),
}

if ctx.info.RawSpec.Type != structType {
if ctx.info.RawSpec != nil && ctx.info.RawSpec.Type != structType {
Copy link
Member

Choose a reason for hiding this comment

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

But this would allow embedded structs / anonymous structs, which does not seem to be allowed according to the error below?

Shouldn't this then be:

Suggested change
if ctx.info.RawSpec != nil && ctx.info.RawSpec.Type != structType {
if ctx.info.RawSpec == nil || ctx.info.RawSpec.Type != structType {

ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("encountered non-top-level struct (possibly embedded), those aren't allowed"), structType))
return props
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ type CronJobSpec struct {
// A struct that can only be entirely replaced via a nested type.
NestedStructWithSeveralFields NestedStructWithSeveralFields `json:"nestedStructWithSeveralFields"`

// A struct with an anonymous struct as a field.
StructWithAnonymousStruct StructWithAnonymousStruct `json:"structWithAnonymousStruct"`

// A struct that can only be entirely replaced via a nested type and
// field markers.
// +structType=atomic
Expand Down Expand Up @@ -424,6 +427,12 @@ type NestedObject struct {
Bar bool `json:"bar"`
}

type StructWithAnonymousStruct struct {
Anonymous struct {
Baz string `json:"baz"`
} `json:"anonymous"`
}

// +structType=atomic
type NestedStructWithSeveralFields NestedObject

Expand Down
10 changes: 10 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9042,6 +9042,15 @@ spec:
- messageExpression: '''Length has to be even but is '' + len(self.stringWithEvenLengthAndMessageExpression)
+ '' instead'''
rule: self.size() % 2 == 0
structWithAnonymousStruct:
description: A struct with an anonymous struct as a field.
type: object
properties:
anonymous:
type: object
properties: {}
required:
- anonymous
structWithSeveralFields:
description: A struct that can only be entirely replaced
properties:
Expand Down Expand Up @@ -9134,6 +9143,7 @@ spec:
- patternObject
- schedule
- stringPair
- structWithAnonymousStruct
- structWithSeveralFields
- twoOfAKindPart0
- twoOfAKindPart1
Expand Down