mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Keep empty map in kustomize output
This commit is contained in:
@@ -67,14 +67,15 @@ func (e ElementSetter) Filter(rn *RNode) (*RNode, error) {
|
||||
matchingElementFound := false
|
||||
for i := range rn.YNode().Content {
|
||||
elem := rn.Content()[i]
|
||||
newNode := NewRNode(elem)
|
||||
|
||||
// empty elements are not valid -- they at least need an associative key
|
||||
if IsEmpty(NewRNode(elem)) {
|
||||
if IsEmpty(newNode) || IsEmptyMap(newNode) {
|
||||
continue
|
||||
}
|
||||
|
||||
// check if this is the element we are matching
|
||||
val, err := NewRNode(elem).Pipe(FieldMatcher{Name: e.Key, StringValue: e.Value})
|
||||
val, err := newNode.Pipe(FieldMatcher{Name: e.Key, StringValue: e.Value})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -302,7 +302,7 @@ kind: Deployment
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers: {}
|
||||
containers: []
|
||||
`,
|
||||
dest: `
|
||||
apiVersion: apps/v1
|
||||
|
||||
@@ -262,7 +262,7 @@ kind: Deployment
|
||||
{description: `remove list -- empty in src`,
|
||||
source: `
|
||||
kind: Deployment
|
||||
items: {}
|
||||
items: []
|
||||
`,
|
||||
dest: `
|
||||
kind: Deployment
|
||||
@@ -273,7 +273,7 @@ items:
|
||||
`,
|
||||
expected: `
|
||||
kind: Deployment
|
||||
items: {}
|
||||
items: []
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -167,8 +167,8 @@ spec: {}
|
||||
expected: `
|
||||
kind: Deployment
|
||||
spec:
|
||||
foo: bar1
|
||||
baz: buz
|
||||
foo: bar1
|
||||
`,
|
||||
},
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ kind: Deployment
|
||||
{description: `remove scalar -- empty in src`,
|
||||
source: `
|
||||
kind: Deployment
|
||||
field: {}
|
||||
field: null
|
||||
`,
|
||||
dest: `
|
||||
kind: Deployment
|
||||
@@ -99,7 +99,6 @@ field: value1
|
||||
`,
|
||||
expected: `
|
||||
kind: Deployment
|
||||
field: {}
|
||||
`,
|
||||
},
|
||||
|
||||
|
||||
@@ -371,7 +371,7 @@ kind: Deployment
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers: {}
|
||||
containers: null
|
||||
`,
|
||||
update: `
|
||||
apiVersion: apps/v1
|
||||
@@ -379,7 +379,7 @@ kind: Deployment
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers: {}
|
||||
containers: null
|
||||
`,
|
||||
local: `
|
||||
apiVersion: apps/v1
|
||||
@@ -584,7 +584,7 @@ kind: Deployment
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers: {}
|
||||
containers: null
|
||||
`,
|
||||
local: `
|
||||
apiVersion: apps/v1
|
||||
|
||||
@@ -44,16 +44,17 @@ func IsMissingOrNull(node *RNode) bool {
|
||||
return node == nil || node.YNode() == nil || node.YNode().Tag == NullNodeTag
|
||||
}
|
||||
|
||||
// IsEmpty returns true if the RNode is MissingOrNull, or is either a MappingNode with
|
||||
// no fields.
|
||||
// IsEmpty returns true if the RNode is MissingOrNull
|
||||
func IsEmpty(node *RNode) bool {
|
||||
if IsMissingOrNull(node) {
|
||||
return IsMissingOrNull(node)
|
||||
}
|
||||
|
||||
// IsEmptyMap returns true if the RNode is an empty node or an empty map
|
||||
func IsEmptyMap(node *RNode) bool {
|
||||
if IsEmpty(node) {
|
||||
return true
|
||||
}
|
||||
|
||||
// Empty sequence is a special case and temporarily not considered as empty here.
|
||||
// Some users may want to keep empty sequence for compatibility reason.
|
||||
// For example, use JSON 6902 patch.
|
||||
return node.YNode().Kind == yaml.MappingNode && len(node.YNode().Content) == 0
|
||||
}
|
||||
|
||||
|
||||
@@ -221,19 +221,19 @@ func TestIsEmpty_Arrays(t *testing.T) {
|
||||
func TestIsEmpty_Maps(t *testing.T) {
|
||||
node := NewMapRNode(nil)
|
||||
// empty map
|
||||
if !IsEmpty(node) {
|
||||
if !IsEmptyMap(node) {
|
||||
t.Fatalf("input: empty map")
|
||||
}
|
||||
// map with 1 item
|
||||
node = NewMapRNode(&map[string]string{
|
||||
"foo": "bar",
|
||||
})
|
||||
if IsEmpty(node) {
|
||||
if IsEmptyMap(node) {
|
||||
t.Fatalf("input: map with 1 item")
|
||||
}
|
||||
// delete the item in map
|
||||
node.value.Content = nil
|
||||
if !IsEmpty(node) {
|
||||
if !IsEmptyMap(node) {
|
||||
t.Fatalf("input: empty map")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ func (l *Walker) walkAssociativeSequence() (*yaml.RNode, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if yaml.IsEmpty(val) {
|
||||
if yaml.IsEmpty(val) || yaml.IsEmptyMap(val) {
|
||||
_, err = dest.Pipe(yaml.ElementSetter{Key: key, Value: value})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user