Skip to content
Merged
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
2 changes: 1 addition & 1 deletion go/fn/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ func IsMetaResource() func(*KubeObject) bool {
}

func (o *KubeObject) IsEmpty() bool {
return yaml.IsYNodeEmptyMap(o.obj.Node())
return o == nil || o.obj == nil || yaml.IsYNodeEmptyMap(o.obj.Node())
}

func NewEmptyKubeObject() *KubeObject {
Expand Down
14 changes: 12 additions & 2 deletions go/fn/resourcelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package fn

import (
"bytes"
"fmt"
"reflect"
"sort"
Expand Down Expand Up @@ -90,9 +91,18 @@ func CheckResourceDuplication(rl *ResourceList) error {
// or KRM fn output
func ParseResourceList(in []byte) (*ResourceList, error) {
rl := &ResourceList{}

// handle empty input
if len(bytes.TrimSpace(in)) == 0 {
rl.Items = make(KubeObjects, 0)
rl.Results = make(Results, 0)
rl.FunctionConfig = NewEmptyKubeObject()
return rl, nil
}
Comment thread
mozesl-nokia marked this conversation as resolved.

rlObj, err := ParseKubeObject(in)
if err != nil {
return nil, fmt.Errorf("failed to parse input bytes: %w", err)
return nil, fmt.Errorf("failed to parse ResourceList as a single KubeObject: %w", err)
}
if rlObj.GetKind() != kio.ResourceListKind {
return nil, fmt.Errorf("input was of unexpected kind %q; expected ResourceList", rlObj.GetKind())
Expand Down Expand Up @@ -172,7 +182,7 @@ func (rl *ResourceList) toYNode() (*yaml.Node, error) {
return nil, err
}
}
if !rl.FunctionConfig.IsEmpty() {
if rl.FunctionConfig != nil && !rl.FunctionConfig.IsEmpty() {
if err := reMap.SetNestedMap(rl.FunctionConfig.node(), "functionConfig"); err != nil {
return nil, err
}
Expand Down
24 changes: 24 additions & 0 deletions go/fn/resourcelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package fn

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/kyaml/kio"
)

var dupResourceInput = []byte(`
Expand Down Expand Up @@ -78,3 +81,24 @@ results:
t.Fatalf("unexpected diff: %v", cmp.Diff(expected, rl.Results))
}
}

func TestParseZeroBytesResourceList(t *testing.T) {
rl, err := ParseResourceList([]byte{})
require.NoError(t, err)
require.NotNil(t, rl)
}

// This caused a panic before
func TestEmptyToYAML(t *testing.T) {
rl := &ResourceList{}

var out []byte
var err error
require.NotPanics(t, func() {
out, err = rl.ToYAML()
})

require.NoError(t, err)
expected := fmt.Sprintf("apiVersion: %s\nkind: %s\n", kio.ResourceListAPIVersion, kio.ResourceListKind)
require.Equal(t, expected, string(out))
}
35 changes: 35 additions & 0 deletions go/fn/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

package fn

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/kyaml/kio"
)

func TestRunEmptyInputBytes(t *testing.T) {
var noOpFn ResourceListProcessorFunc = func(rl *ResourceList) (bool, error) {
return true, nil
}

output, err := Run(noOpFn, []byte{})
require.NoError(t, err)
expected := fmt.Appendf(nil, "apiVersion: %s\nkind: %s\n", kio.ResourceListAPIVersion, kio.ResourceListKind)
assert.Equal(t, expected, output)
}
Loading