Merge "Extend document interface with utility methods"
This commit is contained in:
commit
1e6c449a8c
@ -16,6 +16,7 @@ package document
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Factory holds document data
|
// Factory holds document data
|
||||||
@ -30,6 +31,7 @@ type Document interface {
|
|||||||
GetAnnotations() map[string]string
|
GetAnnotations() map[string]string
|
||||||
GetBool(path string) (bool, error)
|
GetBool(path string) (bool, error)
|
||||||
GetFloat64(path string) (float64, error)
|
GetFloat64(path string) (float64, error)
|
||||||
|
GetGroup() string
|
||||||
GetInt64(path string) (int64, error)
|
GetInt64(path string) (int64, error)
|
||||||
GetKind() string
|
GetKind() string
|
||||||
GetLabels() map[string]string
|
GetLabels() map[string]string
|
||||||
@ -40,8 +42,10 @@ type Document interface {
|
|||||||
GetString(path string) (string, error)
|
GetString(path string) (string, error)
|
||||||
GetStringMap(path string) (map[string]string, error)
|
GetStringMap(path string) (map[string]string, error)
|
||||||
GetStringSlice(path string) ([]string, error)
|
GetStringSlice(path string) ([]string, error)
|
||||||
|
GetVersion() string
|
||||||
Label(map[string]string)
|
Label(map[string]string)
|
||||||
MarshalJSON() ([]byte, error)
|
MarshalJSON() ([]byte, error)
|
||||||
|
ToObject(interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Factory implements Document
|
// Factory implements Document
|
||||||
@ -145,6 +149,18 @@ func (d *Factory) GetName() string {
|
|||||||
return r.GetName()
|
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.
|
// GetKind returns the Kind: field from the document.
|
||||||
func (d *Factory) GetKind() string {
|
func (d *Factory) GetKind() string {
|
||||||
r := d.GetKustomizeResource()
|
r := d.GetKustomizeResource()
|
||||||
@ -162,6 +178,15 @@ func (d *Factory) SetKustomizeResource(r *resource.Resource) error {
|
|||||||
return nil
|
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
|
// NewDocument is a convenience method to construct a new Document. Although
|
||||||
// an error is unlikely at this time, this provides some future proofing for
|
// an error is unlikely at this time, this provides some future proofing for
|
||||||
// when we want more strict airship specific validation of documents getting
|
// when we want more strict airship specific validation of documents getting
|
||||||
|
@ -83,6 +83,25 @@ func TestDocument(t *testing.T) {
|
|||||||
assert.Equal(strings.TrimRight(s, "\n"), strings.TrimRight(string(fileData), "\n"))
|
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) {
|
t.Run("GetString", func(t *testing.T) {
|
||||||
doc, err := bundle.GetByName("some-random-deployment-we-will-filter")
|
doc, err := bundle.GetByName("some-random-deployment-we-will-filter")
|
||||||
require.NoError(err, "Unexpected error trying to GetByName")
|
require.NoError(err, "Unexpected error trying to GetByName")
|
||||||
|
7
pkg/document/testdata/common/custom_resource.yaml
vendored
Normal file
7
pkg/document/testdata/common/custom_resource.yaml
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
apiVersion: airshipit.org/v1alpha1
|
||||||
|
kind: Phase
|
||||||
|
metadata:
|
||||||
|
name: initinfra
|
||||||
|
config:
|
||||||
|
documentEntryPoint: "manifests/site/test-site/initinfra"
|
@ -3,3 +3,4 @@ resources:
|
|||||||
- tiller.yaml
|
- tiller.yaml
|
||||||
- argo.yaml
|
- argo.yaml
|
||||||
- initially_ignored.yaml
|
- initially_ignored.yaml
|
||||||
|
- custom_resource.yaml
|
Loading…
x
Reference in New Issue
Block a user