diff --git a/pkg/document/bundle.go b/pkg/document/bundle.go index 34671203a..c0f694cb5 100644 --- a/pkg/document/bundle.go +++ b/pkg/document/bundle.go @@ -18,6 +18,7 @@ import ( "io" "strings" + kustfs "sigs.k8s.io/kustomize/api/filesys" "sigs.k8s.io/kustomize/api/k8sdeps/kunstruct" "sigs.k8s.io/kustomize/api/krusty" "sigs.k8s.io/kustomize/api/resmap" @@ -68,6 +69,21 @@ func NewBundleByPath(rootPath string) (Bundle, error) { return NewBundle(fs.NewDocumentFs(), rootPath) } +// NewBundleFromBytes is a function which builds new document.Bundle from raw []bytes +func NewBundleFromBytes(data []byte) (Bundle, error) { + fSys := fs.Fs{ + FileSystem: kustfs.MakeFsInMemory(), + } + if err := fSys.WriteFile("/kustomization.yaml", []byte(`resources: +- data.yaml`)); err != nil { + return nil, err + } + if err := fSys.WriteFile("/data.yaml", data); err != nil { + return nil, err + } + return NewBundle(fSys, "/") +} + // NewBundle is a convenience function to create a new bundle // Over time, it will evolve to support allowing more control // for kustomize plugins @@ -101,7 +117,7 @@ func NewBundle(fSys fs.FileSystem, kustomizePath string) (Bundle, error) { kustomizer := krusty.MakeKustomizer(fSys, &o) m, err := kustomizer.Run(kustomizePath) if err != nil { - return bundle, err + return nil, err } err = bundle.SetKustomizeResourceMap(m) return bundle, err diff --git a/pkg/document/bundle_test.go b/pkg/document/bundle_test.go index 2462a269a..181f539ef 100644 --- a/pkg/document/bundle_test.go +++ b/pkg/document/bundle_test.go @@ -32,6 +32,32 @@ func TestNewBundle(t *testing.T) { require.NotNil(bundle) } +func TestNewBundleFromBytes(t *testing.T) { + require := require.New(t) + assert := assert.New(t) + + bundle, err := document.NewBundleFromBytes([]byte(`apiVersion: v1 +kind: Test +metadata: + name: test + labels: + app: test-label`)) + require.NoError(err) + require.NotNil(bundle) + + docs, err := bundle.GetAllDocuments() + require.NoError(err) + assert.NotEmpty(docs) + + doc, err := bundle.SelectOne(document.NewSelector().ByKind("Test").ByName("test")) + require.NoError(err) + assert.NotNil(doc) + + doc, err = bundle.SelectOne(document.NewSelector().ByKind("Test").ByLabel("app")) + require.NoError(err) + assert.NotNil(doc) +} + func TestBundleDocumentFiltering(t *testing.T) { assert := assert.New(t) require := require.New(t)