Merge pull request #3242 from monopole/useConst

Gather and use some of the kyaml constants.
This commit is contained in:
Jeff Regan
2020-11-16 21:31:13 -08:00
committed by GitHub
4 changed files with 66 additions and 31 deletions

29
kyaml/yaml/const.go Normal file
View File

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

View File

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

View File

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

View File

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