Add ability to create bundle from bytes

This patch implements new constructor of document.Bundle type
using raw bytes as source.

Change-Id: Ie7742bcf245083ac988541aa9987448f5827fec3
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
This commit is contained in:
Ruslan Aliev 2021-02-08 22:14:57 -06:00
parent a435109673
commit cc25bcf52e
2 changed files with 43 additions and 1 deletions

View File

@ -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

View File

@ -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)