Skip to content

Commit

Permalink
Merge pull request #148 from a-hilaly/emulation/rawextensions
Browse files Browse the repository at this point in the history
parser: Add support for `x-kubernetes-preserve-unknown-fields` schema extension
  • Loading branch information
michaelhtm authored Nov 19, 2024
2 parents 81ddf9c + 52e9b28 commit 006e1d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/graph/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
"github.com/awslabs/kro/internal/graph/variable"
)

const (
xKubernetesPreserveUnknownFields = "x-kubernetes-preserve-unknown-fields"
)

// ParseResource extracts CEL expressions from a resource based on
// the schema. The resource is expected to be a map[string]interface{}.
//
Expand Down Expand Up @@ -88,6 +92,15 @@ func parseObject(field map[string]interface{}, schema *spec.Schema, path, expect
return nil, fmt.Errorf("expected object type or AdditionalProperties allowed for path %s, got %v", path, field)
}

// Look for vendor schema extensions first
if len(schema.VendorExtensible.Extensions) > 0 {
// If the schema has the x-kubernetes-preserve-unknown-fields extension, we should not
// parse the object and return an empty list of expressions.
if enabled, ok := schema.VendorExtensible.Extensions[xKubernetesPreserveUnknownFields]; ok && enabled.(bool) {
return nil, nil
}
}

var expressionsFields []variable.FieldDescriptor
for fieldName, value := range field {
fieldSchema, err := getFieldSchema(schema, fieldName)
Expand Down
15 changes: 15 additions & 0 deletions internal/graph/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,21 @@ func TestParserEdgeCases(t *testing.T) {
},
expectedError: "",
},
{
name: "schema with x-kubernetes-preserve-unknown-fields",
schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
},
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-preserve-unknown-fields": true,
},
},
},
resource: map[string]interface{}{"name": "John", "age": 30},
expectedError: "",
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 006e1d0

Please sign in to comment.