diff --git a/internal/build/build.go b/internal/build/build.go index 74fd34d7..02a97c30 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -23,6 +23,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "os" "sync" "time" @@ -250,12 +251,19 @@ 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)) - err = decoder.Decode(k) - if err != nil { - return nil, fmt.Errorf("failed to unmarshall kustomization file %s: %w", b.kustomizationFile, err) + // 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 } diff --git a/internal/build/build_test.go b/internal/build/build_test.go index 5e1cfffd..df4a7099 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -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) + } + } + }) + } +} diff --git a/internal/build/testdata/local-kustomization/different-name.yaml b/internal/build/testdata/local-kustomization/different-name.yaml new file mode 100644 index 00000000..d2c675e8 --- /dev/null +++ b/internal/build/testdata/local-kustomization/different-name.yaml @@ -0,0 +1,7 @@ +apiVersion: kustomize.toolkit.fluxcd.io/v1beta2 +kind: Kustomization +metadata: + name: infra + namespace: flux-system +spec: + path: "./clusters/test-build" diff --git a/internal/build/testdata/local-kustomization/multi-doc-valid.yaml b/internal/build/testdata/local-kustomization/multi-doc-valid.yaml new file mode 100644 index 00000000..d5928463 --- /dev/null +++ b/internal/build/testdata/local-kustomization/multi-doc-valid.yaml @@ -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" +--- \ No newline at end of file diff --git a/internal/build/testdata/local-kustomization/no-ns.yaml b/internal/build/testdata/local-kustomization/no-ns.yaml new file mode 100644 index 00000000..d7292d24 --- /dev/null +++ b/internal/build/testdata/local-kustomization/no-ns.yaml @@ -0,0 +1,6 @@ +apiVersion: kustomize.toolkit.fluxcd.io/v1beta2 +kind: Kustomization +metadata: + name: podinfo +spec: + path: "./clusters/test-build" diff --git a/internal/build/testdata/local-kustomization/valid.yaml b/internal/build/testdata/local-kustomization/valid.yaml new file mode 100644 index 00000000..41adab26 --- /dev/null +++ b/internal/build/testdata/local-kustomization/valid.yaml @@ -0,0 +1,7 @@ +apiVersion: kustomize.toolkit.fluxcd.io/v1beta2 +kind: Kustomization +metadata: + name: podinfo + namespace: flux-system +spec: + path: "./clusters/test-build"