Skip to content

Commit

Permalink
fix: handle x-kubernetes-preserve-unknown-fields (#325)
Browse files Browse the repository at this point in the history
* fix: handle missing type in schema when x-kubernetes-preserve-unknown-fields is present

---------

Co-authored-by: John Knutson <john.knutson@hennepin.us>
  • Loading branch information
jknutson and johnknutsonhc authored Feb 22, 2025
1 parent 4148dcf commit 534ff5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/graph/emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (e *Emulator) generateValue(schema *spec.Schema) (interface{}, error) {
return nil, fmt.Errorf("schema is nil")
}

if enabled, ok := schema.VendorExtensible.Extensions["x-kubernetes-preserve-unknown-fields"]; ok && enabled.(bool) {
// Handle x-kubernetes-preserve-unknown-fields
return e.generateObject(schema)
}

if enabled, ok := schema.VendorExtensible.Extensions["x-kubernetes-int-or-string"]; ok && enabled.(bool) {
// Default to integer for dummy CRs
return e.generateInteger(schema), nil
Expand Down
18 changes: 18 additions & 0 deletions pkg/graph/emulator/emulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,24 @@ func TestGenerateValueWithIntOrString(t *testing.T) {

}

func TestGenerateValueWithPreserveUnknownFields(t *testing.T) {
e := NewEmulator()

t.Run("x-kubernetes-preserve-unknown-fields present, does not produce error", func(t *testing.T) {
schema := &spec.Schema{
VendorExtensible: spec.VendorExtensible{
Extensions: map[string]interface{}{
"x-kubernetes-preserve-unknown-fields": true,
},
},
}

_, err := e.generateValue(schema)
require.NoError(t, err)
})

}

func ptr[T comparable](v T) *T {
return &v
}
Expand Down

0 comments on commit 534ff5e

Please sign in to comment.