[AIR-97] Moving WriteOut function from bundle pkg
This commit moves Write function to separate yaml package, and makes it available to anyone for dumping documents in single file, with correct yaml separators '---', '...'. This will allow, for example dumping of filtered from bundle documents to disk or any writer, like Stdout, for debugging purposes. Specifically it will be used as part of `airshipctl cluster initinfra` command as a buffer for delivering resources to kubernetes cluster. Change-Id: I780e3f2d2ff446b8787153f500d04d10487ed71b
This commit is contained in:
parent
7eeb529053
commit
f8f6f8be27
2
go.mod
2
go.mod
@ -79,7 +79,7 @@ require (
|
||||
gotest.tools v2.2.0+incompatible // indirect
|
||||
k8s.io/api v0.0.0-20190516230258-a675ac48af67 // indirect
|
||||
k8s.io/apiextensions-apiserver v0.0.0-20190516231611-bf6753f2aa24 // indirect
|
||||
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d // indirect
|
||||
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
|
||||
k8s.io/apiserver v0.0.0-20190516230822-f89599b3f645 // indirect
|
||||
k8s.io/cli-runtime v0.0.0-20190516231937-17bc0b7fcef5 // indirect
|
||||
k8s.io/client-go v11.0.1-0.20190516230509-ae8359b20417+incompatible
|
||||
|
@ -15,7 +15,8 @@ import (
|
||||
"sigs.k8s.io/kustomize/v3/pkg/resource"
|
||||
"sigs.k8s.io/kustomize/v3/pkg/target"
|
||||
"sigs.k8s.io/kustomize/v3/pkg/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
utilyaml "opendev.org/airship/airshipctl/pkg/util/yaml"
|
||||
)
|
||||
|
||||
// KustomizeBuildOptions contain the options for running a Kustomize build on a bundle
|
||||
@ -230,26 +231,10 @@ func (b *BundleFactory) GetByGvk(group, version, kind string) ([]Document, error
|
||||
// Write will write out the entire bundle resource map
|
||||
func (b *BundleFactory) Write(out io.Writer) error {
|
||||
for _, res := range b.ResMap.Resources() {
|
||||
|
||||
yamlOut, err := yaml.Marshal(res.Map())
|
||||
err := utilyaml.WriteOut(out, res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// add separator for each document
|
||||
_, err = out.Write([]byte("---\n"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = out.Write(yamlOut)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// add separator for each document
|
||||
_, err = out.Write([]byte("...\n"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
32
pkg/util/yaml/writer.go
Normal file
32
pkg/util/yaml/writer.go
Normal file
@ -0,0 +1,32 @@
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// WriteOut dumps any yaml competible document to writer, adding yaml separator `---`
|
||||
// at the beginning of the document, and `...` at the end
|
||||
func WriteOut(dst io.Writer, src interface{}) error {
|
||||
|
||||
yamlOut, err := yaml.Marshal(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// add separator for each document
|
||||
_, err = dst.Write([]byte("---\n"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = dst.Write(yamlOut)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// add separator for each document
|
||||
_, err = dst.Write([]byte("...\n"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
55
pkg/util/yaml/writer_test.go
Normal file
55
pkg/util/yaml/writer_test.go
Normal file
@ -0,0 +1,55 @@
|
||||
package yaml_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
utilyaml "opendev.org/airship/airshipctl/pkg/util/yaml"
|
||||
)
|
||||
|
||||
func TestWriteOut(t *testing.T) {
|
||||
|
||||
// Create some object, that can be marshaled into yaml
|
||||
ob := &metav1.ObjectMeta{
|
||||
Name: "RandomName",
|
||||
Namespace: "uniqueNamespace",
|
||||
CreationTimestamp: metav1.NewTime(time.Unix(10, 0)),
|
||||
Labels: map[string]string{
|
||||
"airshiplabel": "airshipit.org",
|
||||
"app": "foo",
|
||||
},
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
|
||||
// WriteOut to buffer
|
||||
err := utilyaml.WriteOut(&b, ob)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to write out yaml: %v", err)
|
||||
}
|
||||
|
||||
// Verify result contents
|
||||
// TODO (kkalynovskyi) make more reliable tests
|
||||
assert.Contains(t, b.String(), ob.Name)
|
||||
assert.Contains(t, b.String(), "airshiplabel: airshipit.org")
|
||||
assert.Regexp(t, regexp.MustCompile(`^---.*`), b.String())
|
||||
assert.Regexp(t, regexp.MustCompile(`.*\.\.\.\n$`), b.String())
|
||||
|
||||
// Create new ObjectMeta for reverse marshaling test.
|
||||
var rob metav1.ObjectMeta
|
||||
|
||||
// Check if you can marshal the results of writeout back to the Object
|
||||
err = yaml.Unmarshal(b.Bytes(), &rob)
|
||||
if err != nil {
|
||||
t.Fatalf("Result of write out can not be transformed back into original object: %v", err)
|
||||
}
|
||||
|
||||
// Compare original object with reverse marshaled
|
||||
assert.Equal(t, ob, &rob)
|
||||
}
|
Loading…
Reference in New Issue
Block a user