Merge "Extend document interface with utility methods"

This commit is contained in:
Zuul 2020-06-03 16:00:57 +00:00 committed by Gerrit Code Review
commit 1e6c449a8c
4 changed files with 53 additions and 1 deletions

View File

@ -16,6 +16,7 @@ package document
import (
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/yaml"
)
// Factory holds document data
@ -30,6 +31,7 @@ type Document interface {
GetAnnotations() map[string]string
GetBool(path string) (bool, error)
GetFloat64(path string) (float64, error)
GetGroup() string
GetInt64(path string) (int64, error)
GetKind() string
GetLabels() map[string]string
@ -40,8 +42,10 @@ type Document interface {
GetString(path string) (string, error)
GetStringMap(path string) (map[string]string, error)
GetStringSlice(path string) ([]string, error)
GetVersion() string
Label(map[string]string)
MarshalJSON() ([]byte, error)
ToObject(interface{}) error
}
// Factory implements Document
@ -145,6 +149,18 @@ func (d *Factory) GetName() string {
return r.GetName()
}
// GetGroup returns api group from apiVersion field
func (d *Factory) GetGroup() string {
r := d.GetKustomizeResource()
return r.GetGvk().Group
}
// GetVersion returns api version from apiVersion field
func (d *Factory) GetVersion() string {
r := d.GetKustomizeResource()
return r.GetGvk().Version
}
// GetKind returns the Kind: field from the document.
func (d *Factory) GetKind() string {
r := d.GetKustomizeResource()
@ -162,6 +178,15 @@ func (d *Factory) SetKustomizeResource(r *resource.Resource) error {
return nil
}
// ToObject serializes document to object passed as an argument
func (d *Factory) ToObject(obj interface{}) error {
docYAML, err := d.AsYAML()
if err != nil {
return err
}
return yaml.Unmarshal(docYAML, obj)
}
// NewDocument is a convenience method to construct a new Document. Although
// an error is unlikely at this time, this provides some future proofing for
// when we want more strict airship specific validation of documents getting

View File

@ -83,6 +83,25 @@ func TestDocument(t *testing.T) {
assert.Equal(strings.TrimRight(s, "\n"), strings.TrimRight(string(fileData), "\n"))
})
t.Run("ToObject", func(t *testing.T) {
expectedObj := map[string]interface{}{
"apiVersion": "airshipit.org/v1alpha1",
"kind": "Phase",
"metadata": map[string]interface{}{
"name": "initinfra",
},
"config": map[string]interface{}{
"documentEntryPoint": "manifests/site/test-site/initinfra",
},
}
doc, err := bundle.GetByName("initinfra")
require.NoError(err)
actualObj := make(map[string]interface{})
err = doc.ToObject(&actualObj)
assert.NoError(err)
assert.Equal(expectedObj, actualObj)
})
t.Run("GetString", func(t *testing.T) {
doc, err := bundle.GetByName("some-random-deployment-we-will-filter")
require.NoError(err, "Unexpected error trying to GetByName")

View File

@ -0,0 +1,7 @@
---
apiVersion: airshipit.org/v1alpha1
kind: Phase
metadata:
name: initinfra
config:
documentEntryPoint: "manifests/site/test-site/initinfra"

View File

@ -2,4 +2,5 @@ resources:
- baremetal.yaml
- tiller.yaml
- argo.yaml
- initially_ignored.yaml
- initially_ignored.yaml
- custom_resource.yaml