Skip to content

Commit

Permalink
Fix SIGSERV when RawSpec is nil
Browse files Browse the repository at this point in the history
Co-authored-by: roelarents <[email protected]>
  • Loading branch information
rkettelerij and roelarents committed Nov 28, 2024
1 parent 425b7d7 commit 47ff504
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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 {
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

0 comments on commit 47ff504

Please sign in to comment.