From c1d602d1dbf96bf9f738f69b8279c013d2538cc7 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Fri, 15 Dec 2023 12:26:47 +0200 Subject: [PATCH] Workaround for preserve-unknown-fields arrays issue 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 --- internal/engine/importer.go | 14 +++++++++++++- internal/engine/importer_test.go | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/engine/importer.go b/internal/engine/importer.go index eefbd2d6..109a31ac 100644 --- a/internal/engine/importer.go +++ b/internal/engine/importer.go @@ -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 { diff --git a/internal/engine/importer_test.go b/internal/engine/importer_test.go index 26a68a0c..838cbde7 100644 --- a/internal/engine/importer_test.go +++ b/internal/engine/importer_test.go @@ -334,7 +334,9 @@ func TestConvertCRD(t *testing.T) { spec?: { template?: { // Preserve unknown fields. - values?: {} + values?: { + ... + } } } }`,