check for correct kustomization in multi-doc yaml

Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
pull/2792/head
Somtochi Onyekwere 3 years ago
parent 56c5e784fb
commit 355ed94852

@ -23,6 +23,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"sync"
"time"
@ -250,13 +251,20 @@ func (b *Builder) unMarshallKustomization() (*kustomizev1.Kustomization, error)
if err != nil {
return nil, fmt.Errorf("failed to read kustomization file %s: %w", b.kustomizationFile, err)
}
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 == "")) {
err = decoder.Decode(k)
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("failed find kustomization with name '%s' and namespace '%s' in file '%s'",
b.name, b.namespace, b.kustomizationFile)
} else {
return nil, fmt.Errorf("failed to unmarshall kustomization file %s: %w", b.kustomizationFile, err)
}
}
}
return k, nil
}

@ -17,6 +17,7 @@ limitations under the License.
package build
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
@ -157,3 +158,60 @@ type: kubernetes.io/dockerconfigjson
})
}
}
func Test_unMarshallKustomization(t *testing.T) {
tests := []struct {
name string
localKsFile string
wantErr bool
errString string
}{
{
name: "valid kustomization",
localKsFile: "testdata/local-kustomization/valid.yaml",
},
{
name: "Multi-doc yaml containing kustomization and other resources",
localKsFile: "testdata/local-kustomization/multi-doc-valid.yaml",
},
{
name: "no namespace",
localKsFile: "testdata/local-kustomization/no-ns.yaml",
},
{
name: "kustomization with a different name",
localKsFile: "testdata/local-kustomization/different-name.yaml",
wantErr: true,
errString: "failed find kustomization with name",
},
}
b := &Builder{
name: "podinfo",
namespace: "flux-system",
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b.kustomizationFile = tt.localKsFile
ks, err := b.unMarshallKustomization()
if !tt.wantErr {
if err != nil {
t.Fatalf("unexpected err '%s'", err)
}
if ks.Name != b.name && ks.Namespace != b.namespace {
t.Errorf("expected kustomization '%s/%s' to match '%s/%s'",
ks.Name, ks.Namespace, b.name, b.namespace)
}
} else {
if err == nil {
t.Fatal("expected error but got nil")
}
if !strings.Contains(err.Error(), tt.errString) {
t.Errorf("expected error '%s' to contain string '%s'", err.Error(), tt.errString)
}
}
})
}
}

@ -0,0 +1,7 @@
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: infra
namespace: flux-system
spec:
path: "./clusters/test-build"

@ -0,0 +1,32 @@
---
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: infra-namespace
namespace: flux-system
labels:
component.kutara.io/part-of: definitions
spec:
path: "./k8s/base/infra"
prune: true
---
---
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: GitRepository
metadata:
name: podinfo
namespace: flux-system
spec:
interval: 30s
ref:
branch: master
url: https://github.com/stefanprodan/podinfo
---
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: podinfo
namespace: flux-system
spec:
path: "./clusters/test-build"
---

@ -0,0 +1,6 @@
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: podinfo
spec:
path: "./clusters/test-build"

@ -0,0 +1,7 @@
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: podinfo
namespace: flux-system
spec:
path: "./clusters/test-build"
Loading…
Cancel
Save