Extend document interface with utility methods

* GetGroup, GetVersion and GetKind now can be used to construct GVK
  structs
* ToObject deserialize document to a particular object. This is
  needed to precess custom resources which were defined for airship

Change-Id: I527e2c73288e865455853c5553bffd508a2d2eb6
This commit is contained in:
Dmitry Ukov 2020-06-02 14:24:07 +04:00
parent 6577d3fc64
commit 155db7dca5
4 changed files with 53 additions and 1 deletions

View File

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

View File

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

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 - baremetal.yaml
- tiller.yaml - tiller.yaml
- argo.yaml - argo.yaml
- initially_ignored.yaml - initially_ignored.yaml
- custom_resource.yaml