From 155db7dca59dfd3a75a5ea838b8544b6f7808b3a Mon Sep 17 00:00:00 2001 From: Dmitry Ukov Date: Tue, 2 Jun 2020 14:24:07 +0400 Subject: [PATCH] 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 --- pkg/document/document.go | 25 +++++++++++++++++++ pkg/document/document_test.go | 19 ++++++++++++++ .../testdata/common/custom_resource.yaml | 7 ++++++ .../testdata/common/kustomization.yaml | 3 ++- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 pkg/document/testdata/common/custom_resource.yaml diff --git a/pkg/document/document.go b/pkg/document/document.go index a1365c7c2..9b3c3744f 100644 --- a/pkg/document/document.go +++ b/pkg/document/document.go @@ -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 diff --git a/pkg/document/document_test.go b/pkg/document/document_test.go index 55fb2ec99..dee13be7d 100644 --- a/pkg/document/document_test.go +++ b/pkg/document/document_test.go @@ -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") diff --git a/pkg/document/testdata/common/custom_resource.yaml b/pkg/document/testdata/common/custom_resource.yaml new file mode 100644 index 000000000..ee871883d --- /dev/null +++ b/pkg/document/testdata/common/custom_resource.yaml @@ -0,0 +1,7 @@ +--- +apiVersion: airshipit.org/v1alpha1 +kind: Phase +metadata: + name: initinfra +config: + documentEntryPoint: "manifests/site/test-site/initinfra" \ No newline at end of file diff --git a/pkg/document/testdata/common/kustomization.yaml b/pkg/document/testdata/common/kustomization.yaml index 65bdb16bb..185967316 100644 --- a/pkg/document/testdata/common/kustomization.yaml +++ b/pkg/document/testdata/common/kustomization.yaml @@ -2,4 +2,5 @@ resources: - baremetal.yaml - tiller.yaml - argo.yaml - - initially_ignored.yaml \ No newline at end of file + - initially_ignored.yaml + - custom_resource.yaml \ No newline at end of file