mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Gather and use some of the kyaml constants.
This commit is contained in:
29
kyaml/yaml/const.go
Normal file
29
kyaml/yaml/const.go
Normal 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"
|
||||||
|
)
|
||||||
@@ -17,7 +17,7 @@ type AnnotationClearer struct {
|
|||||||
|
|
||||||
func (c AnnotationClearer) Filter(rn *RNode) (*RNode, error) {
|
func (c AnnotationClearer) Filter(rn *RNode) (*RNode, error) {
|
||||||
return rn.Pipe(
|
return rn.Pipe(
|
||||||
PathGetter{Path: []string{"metadata", "annotations"}},
|
PathGetter{Path: []string{MetadataField, AnnotationsField}},
|
||||||
FieldClearer{Name: c.Key})
|
FieldClearer{Name: c.Key})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,12 +28,12 @@ func ClearAnnotation(key string) AnnotationClearer {
|
|||||||
// ClearEmptyAnnotations clears the keys, annotations
|
// ClearEmptyAnnotations clears the keys, annotations
|
||||||
// and metadata if they are empty/null
|
// and metadata if they are empty/null
|
||||||
func ClearEmptyAnnotations(rn *RNode) error {
|
func ClearEmptyAnnotations(rn *RNode) error {
|
||||||
_, err := rn.Pipe(Lookup("metadata"), FieldClearer{
|
_, err := rn.Pipe(Lookup(MetadataField), FieldClearer{
|
||||||
Name: "annotations", IfEmpty: true})
|
Name: AnnotationsField, IfEmpty: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err)
|
return errors.Wrap(err)
|
||||||
}
|
}
|
||||||
_, err = rn.Pipe(FieldClearer{Name: "metadata", IfEmpty: true})
|
_, err = rn.Pipe(FieldClearer{Name: MetadataField, IfEmpty: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err)
|
return errors.Wrap(err)
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,9 @@ func (s AnnotationSetter) Filter(rn *RNode) (*RNode, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return rn.Pipe(
|
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})
|
FieldSetter{Name: s.Key, Value: v})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +80,8 @@ type AnnotationGetter struct {
|
|||||||
// AnnotationGetter returns the annotation value.
|
// AnnotationGetter returns the annotation value.
|
||||||
// Returns "", nil if the annotation does not exist.
|
// Returns "", nil if the annotation does not exist.
|
||||||
func (g AnnotationGetter) Filter(rn *RNode) (*RNode, error) {
|
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 {
|
if v == nil || err != nil {
|
||||||
return v, err
|
return v, err
|
||||||
}
|
}
|
||||||
@@ -106,7 +109,8 @@ func (s LabelSetter) Filter(rn *RNode) (*RNode, error) {
|
|||||||
v.YNode().Tag = NodeTagString
|
v.YNode().Tag = NodeTagString
|
||||||
v.YNode().Style = yaml.SingleQuotedStyle
|
v.YNode().Style = yaml.SingleQuotedStyle
|
||||||
return rn.Pipe(
|
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})
|
FieldSetter{Name: s.Key, Value: v})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ data:
|
|||||||
enableRisky: "false"
|
enableRisky: "false"
|
||||||
`
|
`
|
||||||
|
|
||||||
func TestSetLabel(t *testing.T) {
|
func TestSetLabel1(t *testing.T) {
|
||||||
rn := MustParse(input)
|
rn := MustParse(input)
|
||||||
_, err := rn.Pipe(SetLabel("foo", "bar"))
|
_, err := rn.Pipe(SetLabel("foo", "bar"))
|
||||||
if err != nil {
|
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) {
|
func TestAnnotation(t *testing.T) {
|
||||||
rn := MustParse(input)
|
rn := MustParse(input)
|
||||||
_, err := rn.Pipe(SetAnnotation("foo", "bar"))
|
_, err := rn.Pipe(SetAnnotation("foo", "bar"))
|
||||||
|
|||||||
@@ -14,18 +14,6 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
"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.
|
// MakeNullNode returns an RNode that represents an empty document.
|
||||||
func MakeNullNode() *RNode {
|
func MakeNullNode() *RNode {
|
||||||
@@ -184,17 +172,6 @@ func (rn *RNode) Copy() *RNode {
|
|||||||
|
|
||||||
var ErrMissingMetadata = fmt.Errorf("missing Resource metadata")
|
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.
|
// IsNil is true if the node is nil, or its underlying YNode is nil.
|
||||||
func (rn *RNode) IsNil() bool {
|
func (rn *RNode) IsNil() bool {
|
||||||
return rn == nil || rn.YNode() == nil
|
return rn == nil || rn.YNode() == nil
|
||||||
|
|||||||
Reference in New Issue
Block a user