Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multi-doc yaml for flux build #2792

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"sync"
"time"
Expand Down Expand Up @@ -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
}
Expand Down
58 changes: 58 additions & 0 deletions internal/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package build

import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -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)
}
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: infra
namespace: flux-system
spec:
path: "./clusters/test-build"
32 changes: 32 additions & 0 deletions internal/build/testdata/local-kustomization/multi-doc-valid.yaml
Original file line number Diff line number Diff line change
@@ -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"
---
6 changes: 6 additions & 0 deletions internal/build/testdata/local-kustomization/no-ns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: podinfo
spec:
path: "./clusters/test-build"
7 changes: 7 additions & 0 deletions internal/build/testdata/local-kustomization/valid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
kind: Kustomization
metadata:
name: podinfo
namespace: flux-system
spec:
path: "./clusters/test-build"