[#102] Add function pass forwarding for annotating

* Expose the GetAnnotations method of the document.Document interface.
* Add the Annotate method for adding a single annotation to a
  document.Document
* Clean up the implementation of Label and GetLabels

Change-Id: I94af2380f50848bfcd64af9d5c2c991305d9746f
Relates-To: #102
This commit is contained in:
Ian Howell 2020-03-17 12:14:44 -05:00
parent 3a6332f4de
commit cdc6ba66f1
3 changed files with 42 additions and 30 deletions

View File

@ -81,10 +81,7 @@ func (infra *Infra) Deploy() error {
// also if prune is set to true, this fulfills requirement for all labeled document to be labeled. // also if prune is set to true, this fulfills requirement for all labeled document to be labeled.
// Pruning by annotation is not available, therefore we need to use label. // Pruning by annotation is not available, therefore we need to use label.
for _, doc := range docs { for _, doc := range docs {
err := doc.Label(document.DeployedByLabel, document.InitinfraIdentifier) doc.Label(document.DeployedByLabel, document.InitinfraIdentifier)
if err != nil {
return err
}
} }
return kctl.Apply(docs, ao) return kctl.Apply(docs, ao)

View File

@ -11,41 +11,46 @@ type Factory struct {
// Document interface // Document interface
type Document interface { type Document interface {
Label(key string, value string) error Annotate(key, value string)
GetLabels() map[string]string
AsYAML() ([]byte, error) AsYAML() ([]byte, error)
MarshalJSON() ([]byte, error) GetAnnotations() map[string]string
GetName() string
GetKind() string
GetNamespace() string
GetString(path string) (string, error)
GetStringSlice(path string) ([]string, error)
GetBool(path string) (bool, error) GetBool(path string) (bool, error)
GetFloat64(path string) (float64, error) GetFloat64(path string) (float64, error)
GetInt64(path string) (int64, error) GetInt64(path string) (int64, error)
GetSlice(path string) ([]interface{}, error) GetKind() string
GetStringMap(path string) (map[string]string, error) GetLabels() map[string]string
GetMap(path string) (map[string]interface{}, error) GetMap(path string) (map[string]interface{}, error)
GetName() string
GetNamespace() string
GetSlice(path string) ([]interface{}, error)
GetString(path string) (string, error)
GetStringMap(path string) (map[string]string, error)
GetStringSlice(path string) ([]string, error)
Label(key, value string)
MarshalJSON() ([]byte, error)
}
// Factory implements Document
var _ Document = &Factory{}
// Annotate document by applying annotation to it
func (d *Factory) Annotate(key string, value string) {
annotations := d.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
annotations[key] = value
d.SetAnnotations(annotations)
} }
// Label document by applying label on it // Label document by applying label on it
func (d *Factory) Label(key string, value string) error { func (d *Factory) Label(key string, value string) {
r := d.GetKustomizeResource() labels := d.GetKustomizeResource().GetLabels()
labels := r.GetLabels()
if labels == nil { if labels == nil {
labels = make(map[string]string) labels = make(map[string]string)
} }
labels[key] = value labels[key] = value
r.SetLabels(labels) d.GetKustomizeResource().SetLabels(labels)
err := d.SetKustomizeResource(&r)
return err
}
// GetLabels returns applied labels for the document
func (d *Factory) GetLabels() map[string]string {
r := d.GetKustomizeResource()
labels := r.GetLabels()
return labels
} }
// GetNamespace returns the namespace the resource thinks it's in. // GetNamespace returns the namespace the resource thinks it's in.

View File

@ -102,10 +102,20 @@ func TestDocument(t *testing.T) {
require.NoError(err, "Unexpected error trying to GetAllDocuments") require.NoError(err, "Unexpected error trying to GetAllDocuments")
for _, doc := range docs { for _, doc := range docs {
err := doc.Label("test", "testval") doc.Label("test", "testval")
require.NoError(err, "Unexpected error trying to Label")
labelList := doc.GetLabels() labelList := doc.GetLabels()
assert.Contains(labelList, "test", "Could not find expected label") assert.Contains(labelList, "test")
}
})
t.Run("Annotate", func(t *testing.T) {
docs, err := bundle.GetAllDocuments()
require.NoError(err, "Unexpected error trying to GetAllDocuments")
for _, doc := range docs {
doc.Annotate("test", "testval")
annotationList := doc.GetAnnotations()
assert.Contains(annotationList, "test")
} }
}) })
} }