Fix selection of kustomization resource from multi doc yaml
Signed-off-by: Marcus Weiner <marcus.weiner@gmail.com>
This commit is contained in:
@@ -333,7 +333,7 @@ func (b *Builder) unMarshallKustomization() (*kustomizev1.Kustomization, error)
|
||||
k := &kustomizev1.Kustomization{}
|
||||
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(data), len(data))
|
||||
// check for kustomization in yaml with the same name and namespace
|
||||
for !(k.Name == b.name && (k.Namespace == b.namespace || k.Namespace == "")) {
|
||||
for {
|
||||
err = decoder.Decode(k)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
@@ -343,6 +343,13 @@ func (b *Builder) unMarshallKustomization() (*kustomizev1.Kustomization, error)
|
||||
return nil, fmt.Errorf("failed to unmarshall kustomization file %s: %w", b.kustomizationFile, err)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(k.APIVersion, kustomizev1.GroupVersion.Group+"/") &&
|
||||
k.Kind == kustomizev1.KustomizationKind &&
|
||||
k.Name == b.name &&
|
||||
(k.Namespace == b.namespace || k.Namespace == "") {
|
||||
break
|
||||
}
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
|
||||
@@ -189,6 +189,12 @@ func Test_unMarshallKustomization(t *testing.T) {
|
||||
wantErr: true,
|
||||
errString: "failed find kustomization with name",
|
||||
},
|
||||
{
|
||||
name: "yaml containing other resource with same name as kustomization",
|
||||
localKsFile: "testdata/local-kustomization/invalid-resource.yaml",
|
||||
wantErr: true,
|
||||
errString: "failed find kustomization with name",
|
||||
},
|
||||
}
|
||||
|
||||
b := &Builder{
|
||||
@@ -324,7 +330,10 @@ func Test_ResolveKustomization(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
b := &Builder{}
|
||||
b := &Builder{
|
||||
name: "podinfo",
|
||||
namespace: "flux-system",
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
b.kustomizationFile = tt.localKsFile
|
||||
|
||||
4
internal/build/testdata/local-kustomization/invalid-resource.yaml
vendored
Normal file
4
internal/build/testdata/local-kustomization/invalid-resource.yaml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: podinfo
|
||||
Reference in New Issue
Block a user