From ea008274c96c3a9a100b30c1e2d8c7d9229f2467 Mon Sep 17 00:00:00 2001 From: grothej Date: Thu, 11 Dec 2025 02:17:11 +0100 Subject: [PATCH 1/9] feat: add field for retaining first doc separator --- kyaml/kio/byteio_reader.go | 7 +++++++ kyaml/kio/kioutil/kioutil.go | 3 +++ 2 files changed, 10 insertions(+) diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 230ab891bb..ddf36def22 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -160,6 +160,9 @@ type ByteReader struct { // AnchorsAweigh set to true attempts to replace all YAML anchor aliases // with their definitions (anchor values) immediately after the read. AnchorsAweigh bool + + // PreserveInitialDocSep if true adds the kioutil.PreserveInitialDocSep annotation to the first resource + PreserveInitialDocSep bool } var _ Reader = &ByteReader{} @@ -333,6 +336,10 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode r.SetAnnotations[kioutil.SeqIndentAnnotation] = seqIndentStyle } } + + if index == 0 && r.PreserveInitialDocSep && node.Kind == yaml.DocumentNode { + r.SetAnnotations[kioutil.InitialDocSepAnnotation] = "true" + } } var keys []string for k := range r.SetAnnotations { diff --git a/kyaml/kio/kioutil/kioutil.go b/kyaml/kio/kioutil/kioutil.go index 818d6ca006..bc3d299d4e 100644 --- a/kyaml/kio/kioutil/kioutil.go +++ b/kyaml/kio/kioutil/kioutil.go @@ -30,6 +30,9 @@ const ( // SeqIndentAnnotation records the sequence nodes indentation of the input resource SeqIndentAnnotation AnnotationKey = internalPrefix + "seqindent" + // InitialDocSepAnnotation indicates that the initial document separator should be kept + InitialDocSepAnnotation AnnotationKey = internalPrefix + "initial-doc-sep" + // IdAnnotation records the id of the resource to map inputs to outputs IdAnnotation AnnotationKey = internalPrefix + "id" From 6082ed611e4232c5377c59bf18bc592dfe812677 Mon Sep 17 00:00:00 2001 From: grothej Date: Tue, 16 Dec 2025 00:09:45 +0100 Subject: [PATCH 2/9] refactor: extract annotation setting logic --- kyaml/kio/byteio_reader.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index ddf36def22..80cf9f2787 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -322,24 +322,10 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode r.SetAnnotations = map[string]string{} } if !r.OmitReaderAnnotations { - err := kioutil.CopyLegacyAnnotations(n) + err := r.setResourceAnnotations(n, index, originalYAML) if err != nil { return nil, err } - r.SetAnnotations[kioutil.IndexAnnotation] = fmt.Sprintf("%d", index) - r.SetAnnotations[kioutil.LegacyIndexAnnotation] = fmt.Sprintf("%d", index) - - if r.PreserveSeqIndent { - // derive and add the seqindent annotation - seqIndentStyle := yaml.DeriveSeqIndentStyle(originalYAML) - if seqIndentStyle != "" { - r.SetAnnotations[kioutil.SeqIndentAnnotation] = seqIndentStyle - } - } - - if index == 0 && r.PreserveInitialDocSep && node.Kind == yaml.DocumentNode { - r.SetAnnotations[kioutil.InitialDocSepAnnotation] = "true" - } } var keys []string for k := range r.SetAnnotations { @@ -354,3 +340,21 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode } return n, nil } + +func (r *ByteReader) setResourceAnnotations(n *yaml.RNode, index int, originalYAML string) error { + err := kioutil.CopyLegacyAnnotations(n) + if err != nil { + return err + } + r.SetAnnotations[kioutil.IndexAnnotation] = fmt.Sprintf("%d", index) + r.SetAnnotations[kioutil.LegacyIndexAnnotation] = fmt.Sprintf("%d", index) + + if r.PreserveSeqIndent { + // derive and add the seqindent annotation + seqIndentStyle := yaml.DeriveSeqIndentStyle(originalYAML) + if seqIndentStyle != "" { + r.SetAnnotations[kioutil.SeqIndentAnnotation] = seqIndentStyle + } + } + return nil +} From 5aacfaf64e260c9930a6a828aeffa3c7c9b3b30a Mon Sep 17 00:00:00 2001 From: grothej Date: Wed, 17 Dec 2025 00:33:25 +0100 Subject: [PATCH 3/9] feat: set annotation for preserving initial doc sep --- kyaml/kio/byteio_reader.go | 6 +++ kyaml/kio/byteio_reader_test.go | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 80cf9f2787..87cc3c95d9 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -356,5 +356,11 @@ func (r *ByteReader) setResourceAnnotations(n *yaml.RNode, index int, originalYA r.SetAnnotations[kioutil.SeqIndentAnnotation] = seqIndentStyle } } + if index == 0 && r.PreserveInitialDocSep && + strings.HasPrefix(originalYAML, "---") { + r.SetAnnotations[kioutil.InitialDocSepAnnotation] = "true" + } else { + delete(r.SetAnnotations, kioutil.InitialDocSepAnnotation) + } return nil } diff --git a/kyaml/kio/byteio_reader_test.go b/kyaml/kio/byteio_reader_test.go index 7670cbf6b8..df5b0b0172 100644 --- a/kyaml/kio/byteio_reader_test.go +++ b/kyaml/kio/byteio_reader_test.go @@ -1103,3 +1103,74 @@ env: }) } } + +// TestByteReader_AddPreserveInitialDocSepAnnotation tests if the internal.config.kubernetes.io/initial-doc-sep +// annotation is added to resources appropriately +func TestByteReader_AddPreserveInitialDocSepAnnotation(t *testing.T) { + type testCase struct { + name string + err string + input string + expectedAnnoValue string + OmitReaderAnnotations bool + } + + testCases := []testCase{ + { + name: "read with initial document separator", + input: `--- +apiVersion: apps/v1 +kind: Deployment +spec: + - foo + - bar + - baz +`, + expectedAnnoValue: "true", + }, + { + name: "read with initial document separator after commments", + input: `#a comment +--- +apiVersion: apps/v1 +kind: Deployment +spec: + - foo + - bar + - baz +`, + expectedAnnoValue: "", + }, + { + name: "read without initial document separator", + input: ` +apiVersion: apps/v1 +kind: Deployment +spec: + - foo + - bar + - baz +`, + expectedAnnoValue: "", + }, + } + + for i := range testCases { + tc := testCases[i] + t.Run(tc.name, func(t *testing.T) { + rNodes, err := (&ByteReader{ + OmitReaderAnnotations: tc.OmitReaderAnnotations, + PreserveInitialDocSep: true, + Reader: bytes.NewBuffer([]byte(tc.input)), + }).Read() + if tc.err != "" { + require.Error(t, err) + assert.Equal(t, tc.err, err.Error()) + return + } + require.NoError(t, err) + actual := rNodes[0].GetAnnotations()[kioutil.InitialDocSepAnnotation] + assert.Equal(t, tc.expectedAnnoValue, actual) + }) + } +} From 1bed36375134b1f4b79bd183133f3b372b4f6dc1 Mon Sep 17 00:00:00 2001 From: grothej Date: Wed, 17 Dec 2025 00:34:44 +0100 Subject: [PATCH 4/9] feat: return error in case of OmitReaderAnnotations --- kyaml/kio/byteio_reader.go | 5 +++-- kyaml/kio/byteio_reader_test.go | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 87cc3c95d9..036bafa88b 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -199,8 +199,8 @@ func splitDocuments(s string) ([]string, error) { } func (r *ByteReader) Read() ([]*yaml.RNode, error) { - if r.PreserveSeqIndent && r.OmitReaderAnnotations { - return nil, errors.Errorf(`"PreserveSeqIndent" option adds a reader annotation, please set "OmitReaderAnnotations" to false`) + if (r.PreserveSeqIndent || r.PreserveInitialDocSep) && r.OmitReaderAnnotations { + return nil, errors.Errorf(`"PreserveSeqIndent" and "PreserveInitialDocSep" options add a reader annotation, please set "OmitReaderAnnotations" to false`) } output := ResourceNodeSlice{} @@ -356,6 +356,7 @@ func (r *ByteReader) setResourceAnnotations(n *yaml.RNode, index int, originalYA r.SetAnnotations[kioutil.SeqIndentAnnotation] = seqIndentStyle } } + // if index == 0 && r.PreserveInitialDocSep && strings.HasPrefix(originalYAML, "---") { r.SetAnnotations[kioutil.InitialDocSepAnnotation] = "true" diff --git a/kyaml/kio/byteio_reader_test.go b/kyaml/kio/byteio_reader_test.go index df5b0b0172..b0cd7dec23 100644 --- a/kyaml/kio/byteio_reader_test.go +++ b/kyaml/kio/byteio_reader_test.go @@ -1080,7 +1080,7 @@ env: - bar `, OmitReaderAnnotations: true, - err: `"PreserveSeqIndent" option adds a reader annotation, please set "OmitReaderAnnotations" to false`, + err: `"PreserveSeqIndent" and "PreserveInitialDocSep" options add a reader annotation, please set "OmitReaderAnnotations" to false`, }, } @@ -1153,6 +1153,21 @@ spec: `, expectedAnnoValue: "", }, + { + name: "error if conflicting options", + input: `apiVersion: apps/v1 + kind: Deployment + spec: + - foo + - bar + - baz + env: + - foo + - bar + `, + OmitReaderAnnotations: true, + err: `"PreserveSeqIndent" and "PreserveInitialDocSep" options add a reader annotation, please set "OmitReaderAnnotations" to false`, + }, } for i := range testCases { From 107fe18f2569199ad57c9f6b7d78b59dec9bb2a6 Mon Sep 17 00:00:00 2001 From: grothej Date: Mon, 19 Jan 2026 00:03:43 +0100 Subject: [PATCH 5/9] feat: write initial doc separator when annotation present --- kyaml/kio/byteio_reader.go | 1 - kyaml/kio/byteio_writer.go | 19 ++++++++++ kyaml/kio/byteio_writer_test.go | 65 +++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 036bafa88b..4b9ab8cbce 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -356,7 +356,6 @@ func (r *ByteReader) setResourceAnnotations(n *yaml.RNode, index int, originalYA r.SetAnnotations[kioutil.SeqIndentAnnotation] = seqIndentStyle } } - // if index == 0 && r.PreserveInitialDocSep && strings.HasPrefix(originalYAML, "---") { r.SetAnnotations[kioutil.InitialDocSepAnnotation] = "true" diff --git a/kyaml/kio/byteio_writer.go b/kyaml/kio/byteio_writer.go index ec4cafe303..bb002db4a1 100644 --- a/kyaml/kio/byteio_writer.go +++ b/kyaml/kio/byteio_writer.go @@ -63,6 +63,14 @@ func (w ByteWriter) Write(inputNodes []*yaml.RNode) error { // Even though we use the this value further down we must check this before removing annotations jsonEncodeSingleBareNode := w.shouldJSONEncodeSingleBareNode(nodes) + // Check for InitialDocSepAnnotation on a node + var retainInitialDocSep bool + for i := range nodes { + if len(nodes[i].GetAnnotations(kioutil.InitialDocSepAnnotation)) > 0 { + retainInitialDocSep = true + break + } + } // store seqindent annotation value for each node in order to set the encoder indentation var seqIndentsForNodes []string for i := range nodes { @@ -85,6 +93,11 @@ func (w ByteWriter) Write(inputNodes []*yaml.RNode) error { if err != nil { return errors.Wrap(err) } + + _, err = nodes[i].Pipe(yaml.ClearAnnotation(kioutil.InitialDocSepAnnotation)) + if err != nil { + return errors.Wrap(err) + } } for _, a := range w.ClearAnnotations { _, err := nodes[i].Pipe(yaml.ClearAnnotation(a)) @@ -108,7 +121,13 @@ func (w ByteWriter) Write(inputNodes []*yaml.RNode) error { return errors.Wrap(encoder.Encode(nodes[0])) } + if retainInitialDocSep { + if _, err := w.Writer.Write([]byte("---\n")); err != nil { + return errors.Wrap(err) + } + } encoder := yaml.NewEncoder(w.Writer) + defer encoder.Close() // don't wrap the elements if w.WrappingKind == "" { diff --git a/kyaml/kio/byteio_writer_test.go b/kyaml/kio/byteio_writer_test.go index 4897287b04..900380a879 100644 --- a/kyaml/kio/byteio_writer_test.go +++ b/kyaml/kio/byteio_writer_test.go @@ -380,6 +380,71 @@ metadata: `, }, + // + // Test Case + // + { + name: "add_initial_doc_sep", + instance: ByteWriter{KeepReaderAnnotations: true}, + items: []string{ + `a: b +metadata: + annotations: + internal.config.kubernetes.io/index: 0 + internal.config.kubernetes.io/path: "a/b/a_test.yaml" + internal.config.kubernetes.io/initial-doc-sep: "true" +`, + }, + + expectedOutput: `--- +a: b +metadata: + annotations: + internal.config.kubernetes.io/index: 0 + internal.config.kubernetes.io/path: "a/b/a_test.yaml" + internal.config.kubernetes.io/initial-doc-sep: "true" +`, + }, + // + // Test Case + // + { + name: "multiple_nodes_with_initial_doc_sep_annotations", + instance: ByteWriter{KeepReaderAnnotations: true}, + items: []string{ + `a: b +metadata: + annotations: + internal.config.kubernetes.io/index: 0 + internal.config.kubernetes.io/path: "a/b/a_test.yaml" + internal.config.kubernetes.io/initial-doc-sep: "true" +`, + `c: d +metadata: + annotations: + internal.config.kubernetes.io/index: 0 + internal.config.kubernetes.io/path: "a/b/b_test.yaml" + internal.config.kubernetes.io/inital-doc-sep: "true" +`, + }, + + expectedOutput: `--- +a: b +metadata: + annotations: + internal.config.kubernetes.io/index: 0 + internal.config.kubernetes.io/path: "a/b/a_test.yaml" + internal.config.kubernetes.io/initial-doc-sep: "true" +--- +c: d +metadata: + annotations: + internal.config.kubernetes.io/index: 0 + internal.config.kubernetes.io/path: "a/b/b_test.yaml" + internal.config.kubernetes.io/initial-doc-sep: "true" +`, + }, + // // Test Case // From 66d6cd7f2643b19dda812d1972396b08119aa416 Mon Sep 17 00:00:00 2001 From: grothej Date: Mon, 19 Jan 2026 00:46:06 +0100 Subject: [PATCH 6/9] feat: propagate "PreserveInitialDocSep" to ByteReadWriter --- kyaml/kio/byteio_reader.go | 4 ++ kyaml/kio/byteio_readwriter_test.go | 84 +++++++++++++++++++++++++++++ kyaml/kio/byteio_writer_test.go | 2 +- 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 4b9ab8cbce..c8db74c3be 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -37,6 +37,9 @@ type ByteReadWriter struct { // the Resources, otherwise they will be cleared. KeepReaderAnnotations bool + // PreserveInitialDocSep if true adds kioutil.InitialDocSepAnnotation to the first resource + PreserveInitialDocSep bool + // PreserveSeqIndent if true adds kioutil.SeqIndentAnnotation to each resource PreserveSeqIndent bool @@ -63,6 +66,7 @@ func (rw *ByteReadWriter) Read() ([]*yaml.RNode, error) { b := &ByteReader{ Reader: rw.Reader, OmitReaderAnnotations: rw.OmitReaderAnnotations, + PreserveInitialDocSep: rw.PreserveInitialDocSep, PreserveSeqIndent: rw.PreserveSeqIndent, WrapBareSeqNode: rw.WrapBareSeqNode, } diff --git a/kyaml/kio/byteio_readwriter_test.go b/kyaml/kio/byteio_readwriter_test.go index 070afac890..64c7144055 100644 --- a/kyaml/kio/byteio_readwriter_test.go +++ b/kyaml/kio/byteio_readwriter_test.go @@ -593,6 +593,90 @@ env: } } +func TestByteReadWriter_PreserveInitialDocSep(t *testing.T) { + type testCase struct { + name string + err string + input string + expectedOutput string + instance kio.ByteReadWriter + preserveDocSep bool + } + + testCases := []testCase{ + { + name: "preserve initial document separator", + input: `--- +apiVersion: apps/v1 +kind: Deployment +spec: +- foo +- bar +`, + expectedOutput: `--- +apiVersion: apps/v1 +kind: Deployment +spec: +- foo +- bar +`, + preserveDocSep: true, + }, + { + name: "disregard initial document separator, when annotation isn't present", + input: `--- +apiVersion: apps/v1 +kind: Deployment +spec: +- foo +- bar +`, + expectedOutput: ` +apiVersion: apps/v1 +kind: Deployment +spec: +- foo +- bar +`, + }, + } + + for i := range testCases { + tc := testCases[i] + t.Run(tc.name, func(t *testing.T) { + var in, out bytes.Buffer + in.WriteString(tc.input) + w := tc.instance + w.Writer = &out + w.Reader = &in + w.PreserveInitialDocSep = tc.preserveDocSep + + nodes, err := w.Read() + if !assert.NoError(t, err) { + t.FailNow() + } + + w.WrappingKind = "" + err = w.Write(nodes) + if !assert.NoError(t, err) { + t.FailNow() + } + + if tc.err != "" { + if !assert.EqualError(t, err, tc.err) { + t.FailNow() + } + return + } + + if !assert.Equal(t, + strings.TrimSpace(tc.expectedOutput), strings.TrimSpace(out.String())) { + t.FailNow() + } + }) + } +} + func TestByteReadWriter_WrapBareSeqNode(t *testing.T) { type testCase struct { name string diff --git a/kyaml/kio/byteio_writer_test.go b/kyaml/kio/byteio_writer_test.go index 900380a879..5444a94162 100644 --- a/kyaml/kio/byteio_writer_test.go +++ b/kyaml/kio/byteio_writer_test.go @@ -424,7 +424,7 @@ metadata: annotations: internal.config.kubernetes.io/index: 0 internal.config.kubernetes.io/path: "a/b/b_test.yaml" - internal.config.kubernetes.io/inital-doc-sep: "true" + internal.config.kubernetes.io/initial-doc-sep: "true" `, }, From 7cea9b2ccd72501d9346fd0a77248748af11f5dc Mon Sep 17 00:00:00 2001 From: grothej Date: Sat, 7 Feb 2026 19:26:14 +0100 Subject: [PATCH 7/9] feat: propagate "PreserveInitialDocSep" to LocalPackageReadWriter --- kyaml/kio/pkgio_reader.go | 28 +++++++++++------ kyaml/kio/pkgio_reader_test.go | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/kyaml/kio/pkgio_reader.go b/kyaml/kio/pkgio_reader.go index d7eeda7932..e4b27dc91a 100644 --- a/kyaml/kio/pkgio_reader.go +++ b/kyaml/kio/pkgio_reader.go @@ -41,6 +41,9 @@ type LocalPackageReadWriter struct { KeepReaderAnnotations bool `yaml:"keepReaderAnnotations,omitempty"` + // PreserveInitialDocSep if true adds kioutil.InitialDocSepAnnotation to the first resource + PreserveInitialDocSep bool + // PreserveSeqIndent if true adds kioutil.SeqIndentAnnotation to each resource PreserveSeqIndent bool @@ -93,16 +96,17 @@ type LocalPackageReadWriter struct { func (r *LocalPackageReadWriter) Read() ([]*yaml.RNode, error) { nodes, err := LocalPackageReader{ - PackagePath: r.PackagePath, - MatchFilesGlob: r.MatchFilesGlob, - IncludeSubpackages: r.IncludeSubpackages, - ErrorIfNonResources: r.ErrorIfNonResources, - SetAnnotations: r.SetAnnotations, - PackageFileName: r.PackageFileName, - FileSkipFunc: r.FileSkipFunc, - PreserveSeqIndent: r.PreserveSeqIndent, - FileSystem: r.FileSystem, - WrapBareSeqNode: r.WrapBareSeqNode, + PackagePath: r.PackagePath, + MatchFilesGlob: r.MatchFilesGlob, + IncludeSubpackages: r.IncludeSubpackages, + ErrorIfNonResources: r.ErrorIfNonResources, + SetAnnotations: r.SetAnnotations, + PackageFileName: r.PackageFileName, + FileSkipFunc: r.FileSkipFunc, + PreserveInitialDocSep: r.PreserveInitialDocSep, + PreserveSeqIndent: r.PreserveSeqIndent, + FileSystem: r.FileSystem, + WrapBareSeqNode: r.WrapBareSeqNode, }.Read() if err != nil { return nil, errors.Wrap(err) @@ -196,6 +200,9 @@ type LocalPackageReader struct { // the file FileSkipFunc LocalPackageSkipFileFunc + // PreserveInitialDocSep if true adds kioutil.InitialDocSepAnnotation to the first resource + PreserveInitialDocSep bool + // PreserveSeqIndent if true adds kioutil.SeqIndentAnnotation to each resource PreserveSeqIndent bool @@ -301,6 +308,7 @@ func (r *LocalPackageReader) readFile(path string, _ os.FileInfo) ([]*yaml.RNode Reader: f, OmitReaderAnnotations: r.OmitReaderAnnotations, SetAnnotations: r.SetAnnotations, + PreserveInitialDocSep: r.PreserveInitialDocSep, PreserveSeqIndent: r.PreserveSeqIndent, WrapBareSeqNode: r.WrapBareSeqNode, } diff --git a/kyaml/kio/pkgio_reader_test.go b/kyaml/kio/pkgio_reader_test.go index 721f15c604..f23bddc2ca 100644 --- a/kyaml/kio/pkgio_reader_test.go +++ b/kyaml/kio/pkgio_reader_test.go @@ -674,3 +674,59 @@ metadata: } }) } + +func TestLocalPackageReader_Read_PreserveInitialDocSep(t *testing.T) { + testOnDiskAndOnMem(t, []mockFile{ + {path: "a/b"}, + {path: "a/c"}, + {path: "a_test.yaml", content: readFileA}, + {path: "b_test.yaml", content: readFileB}, + }, func(t *testing.T, path string, mockFS filesys.FileSystem) { + t.Helper() + rfr := LocalPackageReader{ + PackagePath: path, + PreserveInitialDocSep: true, + FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS}, + } + nodes, err := rfr.Read() + require.NoError(t, err) + require.Len(t, nodes, 3) + expected := []string{ + `a: b #first +metadata: + annotations: + config.kubernetes.io/index: '0' + config.kubernetes.io/path: 'a_test.yaml' + internal.config.kubernetes.io/index: '0' + internal.config.kubernetes.io/initial-doc-sep: 'true' + internal.config.kubernetes.io/path: 'a_test.yaml' +`, + `c: d # second +metadata: + annotations: + config.kubernetes.io/index: '1' + config.kubernetes.io/path: 'a_test.yaml' + internal.config.kubernetes.io/index: '1' + internal.config.kubernetes.io/path: 'a_test.yaml' +`, + `# second thing +e: f +g: + h: + - i # has a list + - j +metadata: + annotations: + config.kubernetes.io/index: '0' + config.kubernetes.io/path: 'b_test.yaml' + internal.config.kubernetes.io/index: '0' + internal.config.kubernetes.io/path: 'b_test.yaml' +`, + } + for i := range nodes { + val, err := nodes[i].String() + require.NoError(t, err) + require.Equal(t, expected[i], val) + } + }) +} From f031e6b39dc581f0f602795d0f1db706c5f7b654 Mon Sep 17 00:00:00 2001 From: grothej Date: Mon, 2 Mar 2026 23:21:07 +0100 Subject: [PATCH 8/9] fix: linting violations --- kyaml/kio/byteio_reader.go | 2 +- kyaml/kio/byteio_reader_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index c8db74c3be..6531cd5ee4 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -348,7 +348,7 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode func (r *ByteReader) setResourceAnnotations(n *yaml.RNode, index int, originalYAML string) error { err := kioutil.CopyLegacyAnnotations(n) if err != nil { - return err + return fmt.Errorf("copying legacy annotations failed: %w", err) } r.SetAnnotations[kioutil.IndexAnnotation] = fmt.Sprintf("%d", index) r.SetAnnotations[kioutil.LegacyIndexAnnotation] = fmt.Sprintf("%d", index) diff --git a/kyaml/kio/byteio_reader_test.go b/kyaml/kio/byteio_reader_test.go index b0cd7dec23..315c724815 100644 --- a/kyaml/kio/byteio_reader_test.go +++ b/kyaml/kio/byteio_reader_test.go @@ -1129,7 +1129,7 @@ spec: expectedAnnoValue: "true", }, { - name: "read with initial document separator after commments", + name: "read with initial document separator after comments", input: `#a comment --- apiVersion: apps/v1 From f69e858dd50246e957ddf7c7b5eed96ce8cf28f9 Mon Sep 17 00:00:00 2001 From: grothej Date: Mon, 2 Mar 2026 23:49:45 +0100 Subject: [PATCH 9/9] refactor: update reader creation in byteio_reader_test.go --- kyaml/kio/byteio_reader_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/kyaml/kio/byteio_reader_test.go b/kyaml/kio/byteio_reader_test.go index 315c724815..b4cee817c2 100644 --- a/kyaml/kio/byteio_reader_test.go +++ b/kyaml/kio/byteio_reader_test.go @@ -4,7 +4,6 @@ package kio_test import ( - "bytes" "strings" "testing" @@ -443,7 +442,7 @@ c: d tc := testCases[i] t.Run(tc.name, func(t *testing.T) { r := tc.instance - r.Reader = bytes.NewBufferString(tc.input) + r.Reader = strings.NewReader(tc.input) nodes, err := r.Read() if tc.err != "" { if !assert.EqualError(t, err, tc.err) { @@ -885,7 +884,7 @@ data: rNodes, err := (&ByteReader{ OmitReaderAnnotations: true, AnchorsAweigh: false, - Reader: bytes.NewBuffer([]byte(input)), + Reader: strings.NewReader(input), }).Read() require.NoError(t, err) assert.Equal(t, 1, len(rNodes)) @@ -947,7 +946,7 @@ data: rNodes, err := (&ByteReader{ OmitReaderAnnotations: true, AnchorsAweigh: true, - Reader: bytes.NewBuffer([]byte(input)), + Reader: strings.NewReader(input), }).Read() require.NoError(t, err) assert.Equal(t, 1, len(rNodes)) @@ -1090,7 +1089,7 @@ env: rNodes, err := (&ByteReader{ OmitReaderAnnotations: tc.OmitReaderAnnotations, PreserveSeqIndent: true, - Reader: bytes.NewBuffer([]byte(tc.input)), + Reader: strings.NewReader(tc.input), }).Read() if tc.err != "" { require.Error(t, err) @@ -1176,7 +1175,7 @@ spec: rNodes, err := (&ByteReader{ OmitReaderAnnotations: tc.OmitReaderAnnotations, PreserveInitialDocSep: true, - Reader: bytes.NewBuffer([]byte(tc.input)), + Reader: strings.NewReader(tc.input), }).Read() if tc.err != "" { require.Error(t, err)