mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
Merge pull request #4235 from Goodwine/kyaml-wrap-bug
Fix kyaml readwriter inconsistencies when wrapping resources
This commit is contained in:
@@ -67,12 +67,12 @@ func (rw *ByteReadWriter) Read() ([]*yaml.RNode, error) {
|
|||||||
WrapBareSeqNode: rw.WrapBareSeqNode,
|
WrapBareSeqNode: rw.WrapBareSeqNode,
|
||||||
}
|
}
|
||||||
val, err := b.Read()
|
val, err := b.Read()
|
||||||
|
rw.Results = b.Results
|
||||||
|
|
||||||
if rw.FunctionConfig == nil {
|
if rw.FunctionConfig == nil {
|
||||||
rw.FunctionConfig = b.FunctionConfig
|
rw.FunctionConfig = b.FunctionConfig
|
||||||
}
|
}
|
||||||
rw.Results = b.Results
|
if !rw.NoWrap && rw.WrappingKind == "" {
|
||||||
|
|
||||||
if !rw.NoWrap {
|
|
||||||
rw.WrappingAPIVersion = b.WrappingAPIVersion
|
rw.WrappingAPIVersion = b.WrappingAPIVersion
|
||||||
rw.WrappingKind = b.WrappingKind
|
rw.WrappingKind = b.WrappingKind
|
||||||
}
|
}
|
||||||
@@ -80,15 +80,18 @@ func (rw *ByteReadWriter) Read() ([]*yaml.RNode, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rw *ByteReadWriter) Write(nodes []*yaml.RNode) error {
|
func (rw *ByteReadWriter) Write(nodes []*yaml.RNode) error {
|
||||||
return ByteWriter{
|
w := ByteWriter{
|
||||||
Writer: rw.Writer,
|
Writer: rw.Writer,
|
||||||
KeepReaderAnnotations: rw.KeepReaderAnnotations,
|
KeepReaderAnnotations: rw.KeepReaderAnnotations,
|
||||||
Style: rw.Style,
|
Style: rw.Style,
|
||||||
FunctionConfig: rw.FunctionConfig,
|
FunctionConfig: rw.FunctionConfig,
|
||||||
Results: rw.Results,
|
Results: rw.Results,
|
||||||
WrappingAPIVersion: rw.WrappingAPIVersion,
|
}
|
||||||
WrappingKind: rw.WrappingKind,
|
if !rw.NoWrap {
|
||||||
}.Write(nodes)
|
w.WrappingAPIVersion = rw.WrappingAPIVersion
|
||||||
|
w.WrappingKind = rw.WrappingKind
|
||||||
|
}
|
||||||
|
return w.Write(nodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseAll reads all of the inputs into resources
|
// ParseAll reads all of the inputs into resources
|
||||||
|
|||||||
@@ -747,3 +747,122 @@ func TestByteReadWriter_WrapBareSeqNode(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestByteReadWriter_ResourceListWrapping(t *testing.T) {
|
||||||
|
singleDeployment := `kind: Deployment
|
||||||
|
apiVersion: v1
|
||||||
|
metadata:
|
||||||
|
name: tester
|
||||||
|
namespace: default
|
||||||
|
spec:
|
||||||
|
replicas: 0`
|
||||||
|
resourceList := `apiVersion: config.kubernetes.io/v1
|
||||||
|
kind: ResourceList
|
||||||
|
items:
|
||||||
|
- kind: Deployment
|
||||||
|
apiVersion: v1
|
||||||
|
metadata:
|
||||||
|
name: tester
|
||||||
|
namespace: default
|
||||||
|
spec:
|
||||||
|
replicas: 0`
|
||||||
|
resourceListWithError := `apiVersion: config.kubernetes.io/v1
|
||||||
|
kind: ResourceList
|
||||||
|
items:
|
||||||
|
- kind: Deployment
|
||||||
|
apiVersion: v1
|
||||||
|
metadata:
|
||||||
|
name: tester
|
||||||
|
namespace: default
|
||||||
|
spec:
|
||||||
|
replicas: 0
|
||||||
|
results:
|
||||||
|
- message: some error
|
||||||
|
severity: error`
|
||||||
|
resourceListDifferentWrapper := strings.NewReplacer(
|
||||||
|
"kind: ResourceList", "kind: SomethingElse",
|
||||||
|
"apiVersion: config.kubernetes.io/v1", "apiVersion: fakeVersion",
|
||||||
|
).Replace(resourceList)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
noWrap bool
|
||||||
|
wrapKind string
|
||||||
|
wrapAPIVersion string
|
||||||
|
input string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "resource list",
|
||||||
|
input: resourceList,
|
||||||
|
want: resourceList,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "individual resources",
|
||||||
|
input: singleDeployment,
|
||||||
|
want: singleDeployment,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "no nested wrapping",
|
||||||
|
wrapKind: kio.ResourceListKind,
|
||||||
|
wrapAPIVersion: kio.ResourceListAPIVersion,
|
||||||
|
input: resourceList,
|
||||||
|
want: resourceList,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "unwrap resource list",
|
||||||
|
noWrap: true,
|
||||||
|
input: resourceList,
|
||||||
|
want: singleDeployment,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "wrap individual resources",
|
||||||
|
wrapKind: kio.ResourceListKind,
|
||||||
|
wrapAPIVersion: kio.ResourceListAPIVersion,
|
||||||
|
input: singleDeployment,
|
||||||
|
want: resourceList,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "NoWrap has precedence",
|
||||||
|
noWrap: true,
|
||||||
|
wrapKind: kio.ResourceListKind,
|
||||||
|
wrapAPIVersion: kio.ResourceListAPIVersion,
|
||||||
|
input: singleDeployment,
|
||||||
|
want: singleDeployment,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "honor specified wrapping kind",
|
||||||
|
wrapKind: "SomethingElse",
|
||||||
|
wrapAPIVersion: "fakeVersion",
|
||||||
|
input: resourceList,
|
||||||
|
want: resourceListDifferentWrapper,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "passthrough results",
|
||||||
|
input: resourceListWithError,
|
||||||
|
want: resourceListWithError,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range testCases {
|
||||||
|
tc := testCases[i]
|
||||||
|
t.Run(tc.desc, func(t *testing.T) {
|
||||||
|
var got bytes.Buffer
|
||||||
|
rw := kio.ByteReadWriter{
|
||||||
|
Reader: strings.NewReader(tc.input),
|
||||||
|
Writer: &got,
|
||||||
|
NoWrap: tc.noWrap,
|
||||||
|
WrappingAPIVersion: tc.wrapAPIVersion,
|
||||||
|
WrappingKind: tc.wrapKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
rnodes, err := rw.Read()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
err = rw.Write(rnodes)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, tc.want, strings.TrimSpace(got.String()))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user