Repository: GoogleContainerTools/kpt
Pull request #2514 (2021-09-20 20:16:50)

Wrap Bare Sequence Nodes to avoid parsing errors
Wrap bare sequence nodes while reading resources. Upstream change corresponding to https://github.com/kubernetes-sigs/kustomize/pull/4189


Patch:
diff --git a/go.mod b/go.mod
index 1a9ffcd6..7ddc0895 100644
--- a/go.mod
+++ b/go.mod
@@ -24,5 +24,5 @@ require (
 	k8s.io/utils v0.0.0-20210707171843-4b05e18ac7d9
 	sigs.k8s.io/cli-utils v0.25.1-0.20210702190410-c1a7c2d0409d
 	sigs.k8s.io/kustomize/api v0.8.10
-	sigs.k8s.io/kustomize/kyaml v0.11.2-0.20210908182956-2b8a39373e88
+	sigs.k8s.io/kustomize/kyaml v0.11.2-0.20210920224623-c47fc4860720
 )
diff --git a/go.sum b/go.sum
index 8d65eebf..415e3294 100644
--- a/go.sum
+++ b/go.sum
@@ -996,6 +996,10 @@ sigs.k8s.io/kustomize/kyaml v0.11.1-0.20210715213702-35d1c3f9b418 h1:aFl1EFMI4TN
 sigs.k8s.io/kustomize/kyaml v0.11.1-0.20210715213702-35d1c3f9b418/go.mod h1:FTJxEZ86ScK184NpGSAQcfEqee0nul8oLCK30D47m4E=
 sigs.k8s.io/kustomize/kyaml v0.11.2-0.20210908182956-2b8a39373e88 h1:oAFCwINXUpDfmRrof559WnkAMIVQl6gTTrMaXsYAOrc=
 sigs.k8s.io/kustomize/kyaml v0.11.2-0.20210908182956-2b8a39373e88/go.mod h1:FTJxEZ86ScK184NpGSAQcfEqee0nul8oLCK30D47m4E=
+sigs.k8s.io/kustomize/kyaml v0.11.2-0.20210920184024-1cb93123fc3f h1:qXVD5h5HHQLrhjijxnEamdibvj7do0ZLyxueB9blxEE=
+sigs.k8s.io/kustomize/kyaml v0.11.2-0.20210920184024-1cb93123fc3f/go.mod h1:FTJxEZ86ScK184NpGSAQcfEqee0nul8oLCK30D47m4E=
+sigs.k8s.io/kustomize/kyaml v0.11.2-0.20210920224623-c47fc4860720 h1:pL/hMigoxtZ9THfFBXvwtHUeoy93h3INK/VC6oR9QwA=
+sigs.k8s.io/kustomize/kyaml v0.11.2-0.20210920224623-c47fc4860720/go.mod h1:FTJxEZ86ScK184NpGSAQcfEqee0nul8oLCK30D47m4E=
 sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
 sigs.k8s.io/structured-merge-diff/v4 v4.1.0 h1:C4r9BgJ98vrKnnVCjwCSXcWjWe0NKcUQkmzDXZXGwH8=
 sigs.k8s.io/structured-merge-diff/v4 v4.1.0/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
diff --git a/internal/cmdrender/executor.go b/internal/cmdrender/executor.go
index ec23c380..31d69f16 100644
--- a/internal/cmdrender/executor.go
+++ b/internal/cmdrender/executor.go
@@ -88,6 +88,7 @@ func (e *Executor) Execute(ctx context.Context) error {
 			PreserveSeqIndent:  true,
 			PackageFileName:    kptfilev1.KptFileName,
 			IncludeSubpackages: true,
+			WrapBareSeqNode:    true,
 		}
 		err = pkgWriter.Write(hctx.root.resources)
 		if err != nil {
diff --git a/internal/pkg/pkg.go b/internal/pkg/pkg.go
index 6ecff99e..49bc6a5b 100644
--- a/internal/pkg/pkg.go
+++ b/internal/pkg/pkg.go
@@ -491,6 +491,7 @@ func (p *Pkg) LocalResources(includeMetaResources bool) (resources []*yaml.RNode
 		SetAnnotations: map[string]string{
 			pkgPathAnnotation: string(p.UniquePath),
 		},
+		WrapBareSeqNode: true,
 	}
 	resources, err = pkgReader.Read()
 	if err != nil {
diff --git a/internal/util/addmergecomment/addmergecomment.go b/internal/util/addmergecomment/addmergecomment.go
index e650b6c3..aff70533 100644
--- a/internal/util/addmergecomment/addmergecomment.go
+++ b/internal/util/addmergecomment/addmergecomment.go
@@ -33,7 +33,7 @@ type AddMergeComment struct{}
 // Process invokes AddMergeComment kyaml filter on the resources in input packages paths
 func Process(paths ...string) error {
 	for _, path := range paths {
-		inout := &kio.LocalPackageReadWriter{PackagePath: path, PreserveSeqIndent: true}
+		inout := &kio.LocalPackageReadWriter{PackagePath: path, PreserveSeqIndent: true, WrapBareSeqNode: true}
 		amc := &AddMergeComment{}
 		err := kio.Pipeline{
 			Inputs:  []kio.Reader{inout},
diff --git a/internal/util/cmdutil/cmdutil.go b/internal/util/cmdutil/cmdutil.go
index 2b0a9594..ca946115 100644
--- a/internal/util/cmdutil/cmdutil.go
+++ b/internal/util/cmdutil/cmdutil.go
@@ -166,7 +166,7 @@ func WriteToOutput(r io.Reader, w io.Writer, outDir string) error {
 	}
 
 	return kio.Pipeline{
-		Inputs:  []kio.Reader{&kio.ByteReader{Reader: r, PreserveSeqIndent: true}},
+		Inputs:  []kio.Reader{&kio.ByteReader{Reader: r, PreserveSeqIndent: true, WrapBareSeqNode: true}},
 		Outputs: outputs}.Execute()
 }
 
diff --git a/internal/util/merge/merge3.go b/internal/util/merge/merge3.go
index 75e490b4..d25b5158 100644
--- a/internal/util/merge/merge3.go
+++ b/internal/util/merge/merge3.go
@@ -70,6 +70,7 @@ func (m Merge3) Merge() error {
 		IncludeSubpackages: m.IncludeSubPackages,
 		PackageFileName:    kptfilev1.KptFileName,
 		PreserveSeqIndent:  true,
+		WrapBareSeqNode:    true,
 	}
 	inputs = append(inputs, dest)
 
@@ -82,6 +83,7 @@ func (m Merge3) Merge() error {
 			IncludeSubpackages: m.IncludeSubPackages,
 			PackageFileName:    kptfilev1.KptFileName,
 			PreserveSeqIndent:  true,
+			WrapBareSeqNode:    true,
 		},
 		Exclusions: relPaths,
 	})
@@ -95,6 +97,7 @@ func (m Merge3) Merge() error {
 			IncludeSubpackages: m.IncludeSubPackages,
 			PackageFileName:    kptfilev1.KptFileName,
 			PreserveSeqIndent:  true,
+			WrapBareSeqNode:    true,
 		},
 		Exclusions: relPaths,
 	})
diff --git a/internal/util/pkgutil/pkgutil.go b/internal/util/pkgutil/pkgutil.go
index a2944be9..088148e1 100644
--- a/internal/util/pkgutil/pkgutil.go
+++ b/internal/util/pkgutil/pkgutil.go
@@ -287,6 +287,7 @@ func FormatPackage(pkgPath string) {
 		PackagePath:       pkgPath,
 		MatchFilesGlob:    append(kio.DefaultMatch, kptfilev1.KptFileName),
 		PreserveSeqIndent: true,
+		WrapBareSeqNode:   true,
 	}
 	f := &filters.FormatFilter{
 		UseSchema: true,
diff --git a/pkg/api/kptfile/v1/validation.go b/pkg/api/kptfile/v1/validation.go
index 098d7e43..9c08abfe 100644
--- a/pkg/api/kptfile/v1/validation.go
+++ b/pkg/api/kptfile/v1/validation.go
@@ -162,7 +162,7 @@ func GetValidatedFnConfigFromPath(pkgPath types.UniquePath, configPath string) (
 	if err != nil {
 		return nil, fmt.Errorf("functionConfig must exist in the current package")
 	}
-	reader := kio.ByteReader{Reader: file, PreserveSeqIndent: true}
+	reader := kio.ByteReader{Reader: file, PreserveSeqIndent: true, WrapBareSeqNode: true}
 	nodes, err := reader.Read()
 	if err != nil {
 		return nil, fmt.Errorf("failed to read functionConfig %q: %w", configPath, err)
diff --git a/pkg/live/load.go b/pkg/live/load.go
index 20b3a1dc..82532a34 100644
--- a/pkg/live/load.go
+++ b/pkg/live/load.go
@@ -99,7 +99,8 @@ func readInvInfoFromStream(in io.Reader) (kptfilev1.Inventory, error) {
 	if err := (&kio.Pipeline{
 		Inputs: []kio.Reader{
 			&kio.ByteReader{
-				Reader: in,
+				Reader:          in,
+				WrapBareSeqNode: true,
 			},
 		},
 		Filters: []kio.Filter{
diff --git a/pkg/live/rgpath.go b/pkg/live/rgpath.go
index 44b355d4..de050bbd 100644
--- a/pkg/live/rgpath.go
+++ b/pkg/live/rgpath.go
@@ -41,7 +41,8 @@ func (r *ResourceGroupPathManifestReader) Read() ([]*unstructured.Unstructured,
 
 	var objs []*unstructured.Unstructured
 	nodes, err := (&kio.LocalPackageReader{
-		PackagePath: r.PkgPath,
+		PackagePath:     r.PkgPath,
+		WrapBareSeqNode: true,
 	}).Read()
 	if err != nil {
 		return objs, err
diff --git a/pkg/live/rgstream.go b/pkg/live/rgstream.go
index b62a77a0..a5f1999e 100644
--- a/pkg/live/rgstream.go
+++ b/pkg/live/rgstream.go
@@ -43,7 +43,8 @@ type ResourceGroupStreamManifestReader struct {
 func (p *ResourceGroupStreamManifestReader) Read() ([]*unstructured.Unstructured, error) {
 	var objs []*unstructured.Unstructured
 	nodes, err := (&kio.ByteReader{
-		Reader: p.Reader,
+		Reader:          p.Reader,
+		WrapBareSeqNode: true,
 	}).Read()
 	if err != nil {
 		return objs, err
diff --git a/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go b/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go
index 4238edae..b43cacd5 100644
--- a/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go
+++ b/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go
@@ -74,7 +74,7 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error {
 	// if there is a function-config specified, emit it
 	var functionConfig *yaml.RNode
 	if r.FunctionConfig != "" {
-		configs, err := kio.LocalPackageReader{PackagePath: r.FunctionConfig, PreserveSeqIndent: true}.Read()
+		configs, err := kio.LocalPackageReader{PackagePath: r.FunctionConfig, PreserveSeqIndent: true, WrapBareSeqNode: true}.Read()
 		if err != nil {
 			return err
 		}
@@ -109,6 +109,7 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error {
 			PreserveSeqIndent:  true,
 			PackageFileName:    kptfile.KptFileName,
 			IncludeSubpackages: true,
+			WrapBareSeqNode:    true,
 		})
 	}
 
diff --git a/thirdparty/cmdconfig/commands/cmdtree/cmdtree.go b/thirdparty/cmdconfig/commands/cmdtree/cmdtree.go
index 151f0f9d..61058310 100644
--- a/thirdparty/cmdconfig/commands/cmdtree/cmdtree.go
+++ b/thirdparty/cmdconfig/commands/cmdtree/cmdtree.go
@@ -59,6 +59,7 @@ func (r *TreeRunner) runE(c *cobra.Command, args []string) error {
 		PackagePath:       resolvedPath,
 		MatchFilesGlob:    r.getMatchFilesGlob(),
 		PreserveSeqIndent: true,
+		WrapBareSeqNode:   true,
 	}
 	fltrs := []kio.Filter{&filters.IsLocalConfig{
 		IncludeLocalConfig: true,
diff --git a/thirdparty/kyaml/runfn/runfn.go b/thirdparty/kyaml/runfn/runfn.go
index e613929d..049d4b47 100644
--- a/thirdparty/kyaml/runfn/runfn.go
+++ b/thirdparty/kyaml/runfn/runfn.go
@@ -134,13 +134,14 @@ func (r RunFns) getNodesAndFilters() (
 			PreserveSeqIndent:  true,
 			PackageFileName:    kptfile.KptFileName,
 			IncludeSubpackages: true,
+			WrapBareSeqNode:    true,
 		}
 	}
 
 	if r.Input == nil {
 		p.Inputs = []kio.Reader{outputPkg}
 	} else {
-		p.Inputs = []kio.Reader{&kio.ByteReader{Reader: r.Input, PreserveSeqIndent: true}}
+		p.Inputs = []kio.Reader{&kio.ByteReader{Reader: r.Input, PreserveSeqIndent: true, WrapBareSeqNode: true}}
 	}
 	if err := p.Execute(); err != nil {
 		return nil, nil, outputPkg, err


Test patch:
diff --git a/internal/fnruntime/runner_test.go b/internal/fnruntime/runner_test.go
index 7aa8bf0c..9c7494e3 100644
--- a/internal/fnruntime/runner_test.go
+++ b/internal/fnruntime/runner_test.go
@@ -412,6 +412,7 @@ spec:
 			Writer:                out,
 			KeepReaderAnnotations: true,
 			OmitReaderAnnotations: true,
+			WrapBareSeqNode:       true,
 		}
 		n, err := r.Read()
 		if err != nil {
diff --git a/internal/util/addmergecomment/addmergecomment_test.go b/internal/util/addmergecomment/addmergecomment_test.go
index 14662e41..521f9fda 100644
--- a/internal/util/addmergecomment/addmergecomment_test.go
+++ b/internal/util/addmergecomment/addmergecomment_test.go
@@ -123,6 +123,19 @@ apiVersion: apps/v1
 kind: MyKind
 spec:
   replicas: 3
+ `,
+		},
+		{
+			name: "Skip adding kpt merge comment if non-KRM resource",
+			input: `- op: replace
+  path: /spec
+  value:
+    group: kubeflow.org
+ `,
+			expected: `- op: replace
+  path: /spec
+  value:
+    group: kubeflow.org
  `,
 		},
 	}
diff --git a/internal/util/cmdutil/cmdutil_test.go b/internal/util/cmdutil/cmdutil_test.go
index 19fe143b..93c740fe 100644
--- a/internal/util/cmdutil/cmdutil_test.go
+++ b/internal/util/cmdutil/cmdutil_test.go
@@ -287,6 +287,7 @@ metadata:
 			in := &kio.LocalPackageReader{
 				PackagePath:       baseDir,
 				PreserveSeqIndent: true,
+				WrapBareSeqNode:   true,
 			}
 			out := &bytes.Buffer{}
 
diff --git a/internal/util/get/get_test.go b/internal/util/get/get_test.go
index d29ae12f..236d8cab 100644
--- a/internal/util/get/get_test.go
+++ b/internal/util/get/get_test.go
@@ -1342,6 +1342,7 @@ func TestCommand_Run_subpackages(t *testing.T) {
 				PackagePath:       w.FullPackagePath(),
 				MatchFilesGlob:    []string{kptfilev1.KptFileName},
 				PreserveSeqIndent: true,
+				WrapBareSeqNode:   true,
 			}
 			err = kio.Pipeline{
 				Inputs:  []kio.Reader{rw},
diff --git a/internal/util/update/update_test.go b/internal/util/update/update_test.go
index b140abfe..eecf897d 100644
--- a/internal/util/update/update_test.go
+++ b/internal/util/update/update_test.go
@@ -2150,6 +2150,7 @@ func TestCommand_Run_local_subpackages(t *testing.T) {
 					PackagePath:       g.LocalWorkspace.FullPackagePath(),
 					MatchFilesGlob:    []string{kptfilev1.KptFileName},
 					PreserveSeqIndent: true,
+					WrapBareSeqNode:   true,
 				}
 				err = kio.Pipeline{
 					Inputs:  []kio.Reader{rw},
@@ -3683,6 +3684,7 @@ func TestRun_remote_subpackages(t *testing.T) {
 				PackagePath:       g.LocalWorkspace.FullPackagePath(),
 				MatchFilesGlob:    []string{kptfilev1.KptFileName},
 				PreserveSeqIndent: true,
+				WrapBareSeqNode:   true,
 			}
 			err = kio.Pipeline{
 				Inputs:  []kio.Reader{rw},
diff --git a/pkg/live/load_test.go b/pkg/live/load_test.go
index 4f6530de..919eaca0 100644
--- a/pkg/live/load_test.go
+++ b/pkg/live/load_test.go
@@ -319,6 +319,7 @@ func TestLoad_StdIn(t *testing.T) {
 						OmitReaderAnnotations: true,
 						MatchFilesGlob:        append([]string{kptfile.KptFileName}, kio.DefaultMatch...),
 						IncludeSubpackages:    true,
+						WrapBareSeqNode:       true,
 					},
 				},
 				Outputs: []kio.Writer{