Add Append method to bundle interface

This method would allow us to append bundle with new documents

Change-Id: I201a2a0f2e7f707556a53495538c0f075ce6fe17
Relates-To: #238
This commit is contained in:
Kostiantyn Kalynovskyi 2020-07-01 14:04:54 -05:00 committed by Kostyantyn Kalynovskyi
parent 6a2fba6f21
commit edf09b0fd5
2 changed files with 41 additions and 0 deletions

View File

@ -18,8 +18,10 @@ import (
"io" "io"
"strings" "strings"
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/api/krusty" "sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/resmap" "sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/api/types"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
@ -53,6 +55,7 @@ type Bundle interface {
GetByAnnotation(annotationSelector string) ([]Document, error) GetByAnnotation(annotationSelector string) ([]Document, error)
GetByLabel(labelSelector string) ([]Document, error) GetByLabel(labelSelector string) ([]Document, error)
GetAllDocuments() ([]Document, error) GetAllDocuments() ([]Document, error)
Append(Document) error
} }
// NewBundleByPath helper function that returns new document.Bundle interface based on clusterType and // NewBundleByPath helper function that returns new document.Bundle interface based on clusterType and
@ -338,6 +341,20 @@ func (b *BundleFactory) GetByGvk(group, version, kind string) ([]Document, error
return b.Select(selector) return b.Select(selector)
} }
// Append bundle with the document, this only works with document interface implementation
// that is provided by this package
func (b *BundleFactory) Append(doc Document) error {
yaml, err := doc.AsYAML()
if err != nil {
return err
}
res, err := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()).FromBytes(yaml)
if err != nil {
return nil
}
return b.ResMap.Append(res)
}
// Write will write out the entire bundle resource map // Write will write out the entire bundle resource map
func (b *BundleFactory) Write(out io.Writer) error { func (b *BundleFactory) Write(out io.Writer) error {
for _, res := range b.ResMap.Resources() { for _, res := range b.ResMap.Resources() {

View File

@ -151,6 +151,30 @@ func TestBundleDocumentFiltering(t *testing.T) {
// obviously, this should be improved // obviously, this should be improved
assert.Contains(b.String(), "workflow-controller") assert.Contains(b.String(), "workflow-controller")
}) })
t.Run("BundleAppend", func(t *testing.T) {
doc, err := document.NewDocumentFromBytes([]byte(`apiVersion: v1
kind: Secret
metadata:
name: append-secret
namespace: metal3
type: Opaque
stringData:
username: append-username
password: append-password`))
require.NoError(err)
err = bundle.Append(doc)
require.NoError(err)
secretDoc, err := bundle.SelectOne(document.NewSelector().ByKind("Secret").ByName("append-secret"))
require.NoError(err)
require.NotNil(t, secretDoc)
password, err := secretDoc.GetString("stringData.password")
require.NoError(err)
username, err := secretDoc.GetString("stringData.username")
require.NoError(err)
assert.Equal(password, "append-password")
assert.Equal(username, "append-username")
})
} }
func TestBundleOrder(t *testing.T) { func TestBundleOrder(t *testing.T) {