Remove delegation to RNode in Resource.

This commit is contained in:
monopole
2021-05-07 15:44:52 -07:00
parent 1e3ce57077
commit 5c4e363f11
14 changed files with 188 additions and 288 deletions

View File

@@ -345,6 +345,11 @@ func (rn *RNode) GetKind() string {
return ""
}
// SetKind sets the kind.
func (rn *RNode) SetKind(k string) {
rn.SetMapField(NewScalarRNode(k), KindField)
}
// GetApiVersion returns the apiversion, if it exists, else empty string.
func (rn *RNode) GetApiVersion() string {
if node := rn.getMapFieldValue(APIVersionField); node != nil {
@@ -353,6 +358,11 @@ func (rn *RNode) GetApiVersion() string {
return ""
}
// SetApiVersion sets the apiVersion.
func (rn *RNode) SetApiVersion(av string) {
rn.SetMapField(NewScalarRNode(av), APIVersionField)
}
// getMapFieldValue returns the value (*yaml.Node) of a mapping field.
// The value might be nil. Also, the function returns nil, not an error,
// if this node is not a mapping node, or if this node does not have the
@@ -367,17 +377,43 @@ func (rn *RNode) getMapFieldValue(field string) *yaml.Node {
return nil
}
// GetName returns the name.
// GetName returns the name, or empty string if
// field not found. The setter is more restrictive.
func (rn *RNode) GetName() string {
f := rn.Field(MetadataField)
return rn.getMetaStringField(NameField)
}
// getMetaStringField returns the value of a string field in metadata.
func (rn *RNode) getMetaStringField(fName string) string {
md := rn.getMetaData()
if md == nil {
return ""
}
f := md.Field(fName)
if f.IsNilOrEmpty() {
return ""
}
f = f.Value.Field(NameField)
if f.IsNilOrEmpty() {
return ""
return GetValue(f.Value)
}
// getMetaData returns the RNode holding the value of the metadata field.
// Return nil if field not found (no error).
func (rn *RNode) getMetaData() *RNode {
if IsMissingOrNull(rn) {
return nil
}
return f.Value.YNode().Value
var n *RNode
if rn.YNode().Kind == DocumentNode {
// get the content if this is the document node
n = NewRNode(rn.Content()[0])
} else {
n = rn
}
mf := n.Field(MetadataField)
if mf.IsNilOrEmpty() {
return nil
}
return mf.Value
}
// SetName sets the metadata name field.
@@ -385,16 +421,14 @@ func (rn *RNode) SetName(name string) error {
return rn.SetMapField(NewScalarRNode(name), MetadataField, NameField)
}
// GetNamespace gets the metadata namespace field.
func (rn *RNode) GetNamespace() (string, error) {
meta, err := rn.GetMeta()
if err != nil {
return "", err
}
return meta.Namespace, nil
// GetNamespace gets the metadata namespace field, or empty string if
// field not found. The setter is more restrictive.
func (rn *RNode) GetNamespace() string {
return rn.getMetaStringField(NamespaceField)
}
// SetNamespace tries to set the metadata namespace field.
// SetNamespace tries to set the metadata namespace field. If the argument
// is empty, the field is dropped.
func (rn *RNode) SetNamespace(ns string) error {
meta, err := rn.Pipe(Lookup(MetadataField))
if err != nil {
@@ -411,12 +445,14 @@ func (rn *RNode) SetNamespace(ns string) error {
}
// GetAnnotations gets the metadata annotations field.
func (rn *RNode) GetAnnotations() (map[string]string, error) {
meta, err := rn.GetMeta()
if err != nil {
return nil, err
// If the field is missing, returns an empty map.
// Use another method to check for missing metadata.
func (rn *RNode) GetAnnotations() map[string]string {
meta := rn.getMetaData()
if meta == nil {
return make(map[string]string)
}
return meta.Annotations, nil
return rn.getMapFromMeta(meta, AnnotationsField)
}
// SetAnnotations tries to set the metadata annotations field.
@@ -425,12 +461,26 @@ func (rn *RNode) SetAnnotations(m map[string]string) error {
}
// GetLabels gets the metadata labels field.
func (rn *RNode) GetLabels() (map[string]string, error) {
meta, err := rn.GetMeta()
if err != nil {
return nil, err
// If the field is missing, returns an empty map.
// Use another method to check for missing metadata.
func (rn *RNode) GetLabels() map[string]string {
meta := rn.getMetaData()
if meta == nil {
return make(map[string]string)
}
return meta.Labels, nil
return rn.getMapFromMeta(meta, LabelsField)
}
// getMapFromMeta returns map, sometimes empty, from metadata.
func (rn *RNode) getMapFromMeta(meta *RNode, fName string) map[string]string {
result := make(map[string]string)
if f := meta.Field(fName); !f.IsNilOrEmpty() {
_ = f.Value.VisitFields(func(node *MapNode) error {
result[GetValue(node.Key)] = GetValue(node.Value)
return nil
})
}
return result
}
// SetLabels sets the metadata labels field.
@@ -440,7 +490,7 @@ func (rn *RNode) SetLabels(m map[string]string) error {
// This established proper quoting on string values, and sorts by key.
func (rn *RNode) setMapInMetadata(m map[string]string, field string) error {
meta, err := rn.Pipe(Lookup(MetadataField))
meta, err := rn.Pipe(LookupCreate(MappingNode, MetadataField))
if err != nil {
return err
}
@@ -807,11 +857,7 @@ func (rn *RNode) MatchesAnnotationSelector(selector string) (bool, error) {
if err != nil {
return false, err
}
slice, err := rn.GetAnnotations()
if err != nil {
return false, err
}
return s.Matches(labels.Set(slice)), nil
return s.Matches(labels.Set(rn.GetAnnotations())), nil
}
// MatchesLabelSelector returns true on a selector match to labels.
@@ -820,11 +866,7 @@ func (rn *RNode) MatchesLabelSelector(selector string) (bool, error) {
if err != nil {
return false, err
}
slice, err := rn.GetLabels()
if err != nil {
return false, err
}
return s.Matches(labels.Set(slice)), nil
return s.Matches(labels.Set(rn.GetLabels())), nil
}
// HasNilEntryInList returns true if the RNode contains a list which has

View File

@@ -954,41 +954,29 @@ const deploymentJSON = `
}
`
func getNamespaceOrDie(t *testing.T, n *RNode) string {
m, err := n.GetNamespace()
assert.NoError(t, err)
return m
}
func TestRNodeSetNamespace(t *testing.T) {
n := NewRNode(nil)
assert.Equal(t, "", getNamespaceOrDie(t, n))
assert.Equal(t, "", n.GetNamespace())
assert.NoError(t, n.SetNamespace(""))
assert.Equal(t, "", getNamespaceOrDie(t, n))
assert.Equal(t, "", n.GetNamespace())
if err := n.UnmarshalJSON([]byte(deploymentJSON)); err != nil {
t.Fatalf("unexpected unmarshaljson err: %v", err)
}
assert.NoError(t, n.SetNamespace("flanders"))
assert.Equal(t, "flanders", getNamespaceOrDie(t, n))
}
func getLabelsOrDie(t *testing.T, n *RNode) map[string]string {
m, err := n.GetLabels()
assert.NoError(t, err)
return m
assert.Equal(t, "flanders", n.GetNamespace())
}
func TestRNodeSetLabels(t *testing.T) {
n := NewRNode(nil)
assert.Equal(t, 0, len(getLabelsOrDie(t, n)))
assert.Equal(t, 0, len(n.GetLabels()))
assert.NoError(t, n.SetLabels(map[string]string{}))
assert.Equal(t, 0, len(getLabelsOrDie(t, n)))
assert.Equal(t, 0, len(n.GetLabels()))
n = NewRNode(nil)
if err := n.UnmarshalJSON([]byte(deploymentJSON)); err != nil {
t.Fatalf("unexpected unmarshaljson err: %v", err)
}
labels := getLabelsOrDie(t, n)
labels := n.GetLabels()
assert.Equal(t, 2, len(labels))
assert.Equal(t, "apple", labels["fruit"])
assert.Equal(t, "carrot", labels["veggie"])
@@ -996,31 +984,25 @@ func TestRNodeSetLabels(t *testing.T) {
"label1": "foo",
"label2": "bar",
}))
labels = getLabelsOrDie(t, n)
labels = n.GetLabels()
assert.Equal(t, 2, len(labels))
assert.Equal(t, "foo", labels["label1"])
assert.Equal(t, "bar", labels["label2"])
assert.NoError(t, n.SetLabels(map[string]string{}))
assert.Equal(t, 0, len(getLabelsOrDie(t, n)))
}
func getAnnotationsOrDie(t *testing.T, n *RNode) map[string]string {
m, err := n.GetAnnotations()
assert.NoError(t, err)
return m
assert.Equal(t, 0, len(n.GetLabels()))
}
func TestRNodeGetAnnotations(t *testing.T) {
n := NewRNode(nil)
assert.Equal(t, 0, len(getAnnotationsOrDie(t, n)))
assert.Equal(t, 0, len(n.GetAnnotations()))
assert.NoError(t, n.SetAnnotations(map[string]string{}))
assert.Equal(t, 0, len(getAnnotationsOrDie(t, n)))
assert.Equal(t, 0, len(n.GetAnnotations()))
n = NewRNode(nil)
if err := n.UnmarshalJSON([]byte(deploymentJSON)); err != nil {
t.Fatalf("unexpected unmarshaljson err: %v", err)
}
annotations := getAnnotationsOrDie(t, n)
annotations := n.GetAnnotations()
assert.Equal(t, 2, len(annotations))
assert.Equal(t, "51", annotations["area"])
assert.Equal(t, "Take me to your leader.", annotations["greeting"])
@@ -1028,12 +1010,12 @@ func TestRNodeGetAnnotations(t *testing.T) {
"annotation1": "foo",
"annotation2": "bar",
}))
annotations = getAnnotationsOrDie(t, n)
annotations = n.GetAnnotations()
assert.Equal(t, 2, len(annotations))
assert.Equal(t, "foo", annotations["annotation1"])
assert.Equal(t, "bar", annotations["annotation2"])
assert.NoError(t, n.SetAnnotations(map[string]string{}))
assert.Equal(t, 0, len(getAnnotationsOrDie(t, n)))
assert.Equal(t, 0, len(n.GetAnnotations()))
}
func TestRNodeMatchesAnnotationSelector(t *testing.T) {
@@ -1644,18 +1626,12 @@ func TestGettingFields(t *testing.T) {
if expected != actual {
t.Fatalf("expected '%s', got '%s'", expected, actual)
}
actualMap, err := rn.GetLabels()
if err != nil {
t.Fatalf("unexpected err '%v'", err)
}
actualMap := rn.GetLabels()
v, ok := actualMap["fruit"]
if !ok || v != "apple" {
t.Fatalf("unexpected labels '%v'", actualMap)
}
actualMap, err = rn.GetAnnotations()
if err != nil {
t.Fatalf("unexpected err '%v'", err)
}
actualMap = rn.GetAnnotations()
v, ok = actualMap["greeting"]
if !ok || v != "Take me to your leader." {
t.Fatalf("unexpected annotations '%v'", actualMap)
@@ -1717,12 +1693,11 @@ func TestSetLabels(t *testing.T) {
if err := rn.UnmarshalJSON([]byte(deploymentBiggerJson)); err != nil {
t.Fatalf("unexpected unmarshaljson err: %v", err)
}
rn.SetLabels(map[string]string{
assert.NoError(t, rn.SetLabels(map[string]string{
"label1": "foo",
"label2": "bar",
})
labels, err := rn.GetLabels()
assert.NoError(t, err)
}))
labels := rn.GetLabels()
if expected, actual := 2, len(labels); expected != actual {
t.Fatalf("expected '%d', got '%d'", expected, actual)
}
@@ -1739,12 +1714,11 @@ func TestGetAnnotations(t *testing.T) {
if err := rn.UnmarshalJSON([]byte(deploymentBiggerJson)); err != nil {
t.Fatalf("unexpected unmarshaljson err: %v", err)
}
rn.SetAnnotations(map[string]string{
assert.NoError(t, rn.SetAnnotations(map[string]string{
"annotation1": "foo",
"annotation2": "bar",
})
annotations, err := rn.GetAnnotations()
assert.NoError(t, err)
}))
annotations := rn.GetAnnotations()
if expected, actual := 2, len(annotations); expected != actual {
t.Fatalf("expected '%d', got '%d'", expected, actual)
}