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: handle x-kubernetes-preserve-unknown-fields #325

Merged
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