Skip to content

Commit

Permalink
Workaround for preserve-unknown-fields arrays issue
Browse files Browse the repository at this point in the history
For CRDs with arrays the `parentPath` function doesn't walk back to the spec.
To not miss `x-kubernetes-preserve-unknown-fields` we do partial matching
which could open more fields than desired if the field names overlap.

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Dec 15, 2023
1 parent e02da77 commit c1d602d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 13 additions & 1 deletion internal/engine/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,19 @@ func convertCRD(crd cue.Value) (*IntermediateCRD, error) {
stack = append(stack[:i], pc.Node())
pathstack = append(pathstack[:i], psel)

if !preserve[cue.MakePath(pathstack...).String()] {
// Risk not closing up fields that are not marked with 'x-kubernetes-preserve-unknown-fields: true'
// if the current field name matches a path that is marked with preserve unknown.
// TODO: find a way to fix parentPath when arrays are involved.
currentPath := cue.MakePath(pathstack...).String()
found := false
for k, ok := range preserve {
if strings.HasSuffix(k, currentPath) && ok {
found = true
break
}
}

if !found {
newlist := make([]ast.Decl, 0, len(x.Elts))
for _, elt := range x.Elts {
if _, is := elt.(*ast.Ellipsis); !is {
Expand Down
4 changes: 3 additions & 1 deletion internal/engine/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ func TestConvertCRD(t *testing.T) {
spec?: {
template?: {
// Preserve unknown fields.
values?: {}
values?: {
...
}
}
}
}`,
Expand Down

0 comments on commit c1d602d

Please sign in to comment.