diff --git a/e2e/testdata/fn-render/preserve-document-comments/.expected/diff.patch b/e2e/testdata/fn-render/preserve-document-comments/.expected/diff.patch new file mode 100644 index 0000000000..0dcf75b1f8 --- /dev/null +++ b/e2e/testdata/fn-render/preserve-document-comments/.expected/diff.patch @@ -0,0 +1,51 @@ +diff --git a/Kptfile b/Kptfile +index a860b3f..b2f0776 100644 +--- a/Kptfile ++++ b/Kptfile +@@ -13,3 +13,22 @@ pipeline: + - image: ghcr.io/kptdev/krm-functions-catalog/apply-setters:latest + configMap: + test: "3" ++status: ++ conditions: ++ - type: Rendered ++ status: "True" ++ reason: RenderSuccess ++ renderStatus: ++ mutationSteps: ++ - image: ghcr.io/kptdev/krm-functions-catalog/apply-setters:latest ++ exitCode: 0 ++ results: ++ - message: no matches for input setter(s) ++ - image: ghcr.io/kptdev/krm-functions-catalog/apply-setters:latest ++ exitCode: 0 ++ results: ++ - message: no matches for input setter(s) ++ - image: ghcr.io/kptdev/krm-functions-catalog/apply-setters:latest ++ exitCode: 0 ++ results: ++ - message: no matches for input setter(s) +diff --git a/a.yaml b/a.yaml +index 7550684..df38cb7 100644 +--- a/a.yaml ++++ b/a.yaml +@@ -11,7 +11,6 @@ + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +- + # START A1 + apiVersion: v1 + kind: Pod +diff --git a/b.yaml b/b.yaml +index 36c5728..e7fcfb9 100644 +--- a/b.yaml ++++ b/b.yaml +@@ -11,7 +11,6 @@ + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + # See the License for the specific language governing permissions and + # limitations under the License. +- + # START B1 + apiVersion: v1 + kind: Pod diff --git a/e2e/testdata/fn-render/preserve-document-comments/.krmignore b/e2e/testdata/fn-render/preserve-document-comments/.krmignore new file mode 100644 index 0000000000..9d7a4007d6 --- /dev/null +++ b/e2e/testdata/fn-render/preserve-document-comments/.krmignore @@ -0,0 +1 @@ +.expected diff --git a/e2e/testdata/fn-render/preserve-document-comments/Kptfile b/e2e/testdata/fn-render/preserve-document-comments/Kptfile new file mode 100644 index 0000000000..a860b3f922 --- /dev/null +++ b/e2e/testdata/fn-render/preserve-document-comments/Kptfile @@ -0,0 +1,15 @@ +apiVersion: kpt.dev/v1 +kind: Kptfile +metadata: + name: preserve-document-comments +pipeline: + mutators: + - image: ghcr.io/kptdev/krm-functions-catalog/apply-setters:latest + configMap: + test: "3" + - image: ghcr.io/kptdev/krm-functions-catalog/apply-setters:latest + configMap: + test: "3" + - image: ghcr.io/kptdev/krm-functions-catalog/apply-setters:latest + configMap: + test: "3" diff --git a/e2e/testdata/fn-render/preserve-document-comments/a.yaml b/e2e/testdata/fn-render/preserve-document-comments/a.yaml new file mode 100644 index 0000000000..7550684767 --- /dev/null +++ b/e2e/testdata/fn-render/preserve-document-comments/a.yaml @@ -0,0 +1,33 @@ +# Copyright 2026 The kpt Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# START A1 +apiVersion: v1 +kind: Pod +metadata: + name: a1 +spec: + containers: + - name: nginx + image: nginx:latest # inline + +# END A1 +--- +# START A2 +apiVersion: v1 +kind: Pod +metadata: + name: a2 + +# END A2 diff --git a/e2e/testdata/fn-render/preserve-document-comments/b.yaml b/e2e/testdata/fn-render/preserve-document-comments/b.yaml new file mode 100644 index 0000000000..36c5728e5a --- /dev/null +++ b/e2e/testdata/fn-render/preserve-document-comments/b.yaml @@ -0,0 +1,29 @@ +# Copyright 2026 The kpt Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# START B1 +apiVersion: v1 +kind: Pod +metadata: + name: b1 + +# END B1 +--- +# START B2 +apiVersion: v1 +kind: Pod +metadata: + name: b2 + +# END B2 diff --git a/pkg/fn/runtime/runner.go b/pkg/fn/runtime/runner.go index 722d87b50e..ed8114180c 100644 --- a/pkg/fn/runtime/runner.go +++ b/pkg/fn/runtime/runner.go @@ -382,6 +382,20 @@ func (fr *FunctionRunner) do(input []*yaml.RNode) (output []*yaml.RNode, err err fnResult := fr.fnResult output, err = fr.filter.Filter(input) + for _, node := range output { + root := node.Document() + if root.Kind != yaml.DocumentNode && root.FootComment != "" { + // Keep document foot comments outside the ResourceList's items sequence. + // Otherwise YAML decoding attaches them to the following resource. + content := *root + content.FootComment = "" + node.SetYNode(&yaml.Node{ + Kind: yaml.DocumentNode, + FootComment: root.FootComment, + Content: []*yaml.Node{&content}, + }) + } + } if fr.opts.SetPkgPathAnnotation { if pkgPathErr := setPkgPathAnnotationIfNotExist(output, fr.pkgPath); pkgPathErr != nil { diff --git a/pkg/fn/runtime/runner_test.go b/pkg/fn/runtime/runner_test.go index 9208aa4dc2..b8315cf4d7 100644 --- a/pkg/fn/runtime/runner_test.go +++ b/pkg/fn/runtime/runner_test.go @@ -19,6 +19,7 @@ package runtime import ( "bytes" "context" + "errors" "io" "os" "path/filepath" @@ -1155,3 +1156,68 @@ items: [] _, err = fr.do(nodes) assert.NoError(t, err, "a nameless *List resource must not be rejected as non-KRM") } + +func TestFunctionRunnerPreservesDocumentComments(t *testing.T) { + input := `# START A +apiVersion: v1 +kind: Pod +metadata: + name: a +spec: + containers: + - name: nginx + image: nginx:latest # inline + +# END A +--- +# START B +apiVersion: v1 +kind: Pod +metadata: + name: b + +# END B +` + fnErr := errors.New("function failed") + for _, tc := range []struct { + name string + input string + err error + }{ + {name: "multiple mutators", input: input}, + {name: "empty input"}, + {name: "failed mutators", input: input, err: fnErr}, + } { + t.Run(tc.name, func(t *testing.T) { + nodes, err := (&kio.ByteReader{Reader: strings.NewReader(tc.input)}).Read() + require.NoError(t, err) + fr := &FunctionRunner{ + filter: &runtimeutil.FunctionFilter{ + GlobalScope: true, + Run: func(reader io.Reader, writer io.Writer) error { + _, err := io.Copy(writer, reader) + if err != nil { + return err + } + return tc.err + }, + }, + fnResult: &fnresultv1.Result{}, + fnResults: fnresultv1.NewResultList(), + } + for range 3 { + nodes, err = fr.do(nodes) + require.ErrorIs(t, err, tc.err) + } + var out bytes.Buffer + err = (kio.ByteWriter{ + Writer: &out, + ClearAnnotations: []string{ + PathAnnotation, "config.kubernetes.io/path", + }, + }).Write(nodes) + require.NoError(t, err) + assert.Equal(t, tc.input, out.String()) + }) + } +}