Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
15 changes: 15 additions & 0 deletions e2e/testdata/fn-render/preserve-document-comments/Kptfile
Original file line number Diff line number Diff line change
@@ -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"
33 changes: 33 additions & 0 deletions e2e/testdata/fn-render/preserve-document-comments/a.yaml
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions e2e/testdata/fn-render/preserve-document-comments/b.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions pkg/fn/runtime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
})
Comment on lines +390 to +396
}
}

if fr.opts.SetPkgPathAnnotation {
if pkgPathErr := setPkgPathAnnotationIfNotExist(output, fr.pkgPath); pkgPathErr != nil {
Expand Down
66 changes: 66 additions & 0 deletions pkg/fn/runtime/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package runtime
import (
"bytes"
"context"
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -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())
})
}
}
Loading