From 712276176c685c05d41f1442e602bc6864811142 Mon Sep 17 00:00:00 2001 From: jregan Date: Mon, 16 Nov 2020 21:26:18 -0800 Subject: [PATCH] Gather and use some of the kyaml constants. --- kyaml/yaml/const.go | 29 +++++++++++++++++++++++++++++ kyaml/yaml/kfns.go | 18 +++++++++++------- kyaml/yaml/kfns_test.go | 27 ++++++++++++++++++++++++++- kyaml/yaml/rnode.go | 23 ----------------------- 4 files changed, 66 insertions(+), 31 deletions(-) create mode 100644 kyaml/yaml/const.go diff --git a/kyaml/yaml/const.go b/kyaml/yaml/const.go new file mode 100644 index 000000000..fd3fd7428 --- /dev/null +++ b/kyaml/yaml/const.go @@ -0,0 +1,29 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package yaml + +const ( + // NodeTagNull is the tag set for a yaml.Document that contains no data; + // e.g. it isn't a Map, Slice, Document, etc + NodeTagNull = "!!null" + NodeTagFloat = "!!float" + NodeTagString = "!!str" + NodeTagBool = "!!bool" + NodeTagInt = "!!int" + NodeTagMap = "!!map" + NodeTagSeq = "!!seq" + NodeTagEmpty = "" +) + +// Field names +const ( + AnnotationsField = "annotations" + APIVersionField = "apiVersion" + KindField = "kind" + MetadataField = "metadata" + DataField = "data" + NameField = "name" + NamespaceField = "namespace" + LabelsField = "labels" +) diff --git a/kyaml/yaml/kfns.go b/kyaml/yaml/kfns.go index 97af75905..e8c50a7da 100644 --- a/kyaml/yaml/kfns.go +++ b/kyaml/yaml/kfns.go @@ -17,7 +17,7 @@ type AnnotationClearer struct { func (c AnnotationClearer) Filter(rn *RNode) (*RNode, error) { return rn.Pipe( - PathGetter{Path: []string{"metadata", "annotations"}}, + PathGetter{Path: []string{MetadataField, AnnotationsField}}, FieldClearer{Name: c.Key}) } @@ -28,12 +28,12 @@ func ClearAnnotation(key string) AnnotationClearer { // ClearEmptyAnnotations clears the keys, annotations // and metadata if they are empty/null func ClearEmptyAnnotations(rn *RNode) error { - _, err := rn.Pipe(Lookup("metadata"), FieldClearer{ - Name: "annotations", IfEmpty: true}) + _, err := rn.Pipe(Lookup(MetadataField), FieldClearer{ + Name: AnnotationsField, IfEmpty: true}) if err != nil { return errors.Wrap(err) } - _, err = rn.Pipe(FieldClearer{Name: "metadata", IfEmpty: true}) + _, err = rn.Pipe(FieldClearer{Name: MetadataField, IfEmpty: true}) if err != nil { return errors.Wrap(err) } @@ -59,7 +59,9 @@ func (s AnnotationSetter) Filter(rn *RNode) (*RNode, error) { } return rn.Pipe( - PathGetter{Path: []string{"metadata", "annotations"}, Create: yaml.MappingNode}, + PathGetter{ + Path: []string{MetadataField, AnnotationsField}, + Create: yaml.MappingNode}, FieldSetter{Name: s.Key, Value: v}) } @@ -78,7 +80,8 @@ type AnnotationGetter struct { // AnnotationGetter returns the annotation value. // Returns "", nil if the annotation does not exist. func (g AnnotationGetter) Filter(rn *RNode) (*RNode, error) { - v, err := rn.Pipe(PathGetter{Path: []string{"metadata", "annotations", g.Key}}) + v, err := rn.Pipe( + PathGetter{Path: []string{MetadataField, AnnotationsField, g.Key}}) if v == nil || err != nil { return v, err } @@ -106,7 +109,8 @@ func (s LabelSetter) Filter(rn *RNode) (*RNode, error) { v.YNode().Tag = NodeTagString v.YNode().Style = yaml.SingleQuotedStyle return rn.Pipe( - PathGetter{Path: []string{"metadata", "labels"}, Create: yaml.MappingNode}, + PathGetter{ + Path: []string{MetadataField, LabelsField}, Create: yaml.MappingNode}, FieldSetter{Name: s.Key, Value: v}) } diff --git a/kyaml/yaml/kfns_test.go b/kyaml/yaml/kfns_test.go index fe7d63573..ceda72511 100644 --- a/kyaml/yaml/kfns_test.go +++ b/kyaml/yaml/kfns_test.go @@ -16,7 +16,7 @@ data: enableRisky: "false" ` -func TestSetLabel(t *testing.T) { +func TestSetLabel1(t *testing.T) { rn := MustParse(input) _, err := rn.Pipe(SetLabel("foo", "bar")) if err != nil { @@ -39,6 +39,31 @@ data: } } +func TestSetLabel2(t *testing.T) { + rn := MustParse(`apiVersion: v1 +kind: ConfigMap +data: + altGreeting: "Good Morning!" +`) + _, err := rn.Pipe(SetLabel("foo", "bar")) + if err != nil { + t.Fatalf("unexpected error %v", err) + } + output := rn.MustString() + + expected := `apiVersion: v1 +kind: ConfigMap +data: + altGreeting: "Good Morning!" +metadata: + labels: + foo: 'bar' +` + if output != expected { + t.Fatalf("expected \n%s\nbut got \n%s\n", expected, output) + } +} + func TestAnnotation(t *testing.T) { rn := MustParse(input) _, err := rn.Pipe(SetAnnotation("foo", "bar")) diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index 2264dd3db..75186189a 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -14,18 +14,6 @@ import ( "sigs.k8s.io/kustomize/kyaml/errors" ) -const ( - // NodeTagNull is the tag set for a yaml.Document that contains no data; - // e.g. it isn't a Map, Slice, Document, etc - NodeTagNull = "!!null" - NodeTagFloat = "!!float" - NodeTagString = "!!str" - NodeTagBool = "!!bool" - NodeTagInt = "!!int" - NodeTagMap = "!!map" - NodeTagSeq = "!!seq" - NodeTagEmpty = "" -) // MakeNullNode returns an RNode that represents an empty document. func MakeNullNode() *RNode { @@ -184,17 +172,6 @@ func (rn *RNode) Copy() *RNode { var ErrMissingMetadata = fmt.Errorf("missing Resource metadata") -// Field names -const ( - AnnotationsField = "annotations" - APIVersionField = "apiVersion" - KindField = "kind" - MetadataField = "metadata" - NameField = "name" - NamespaceField = "namespace" - LabelsField = "labels" -) - // IsNil is true if the node is nil, or its underlying YNode is nil. func (rn *RNode) IsNil() bool { return rn == nil || rn.YNode() == nil