Keep empty map in kustomize output

This commit is contained in:
Donny Xia
2020-08-06 13:19:26 -07:00
parent 2bcece5f1e
commit d59d0401f4
14 changed files with 53 additions and 76 deletions

View File

@@ -156,7 +156,8 @@ spec:
- mountPath: /tmp/ps
name: busybox-persistent-storage
volumes:
- name: busybox-persistent-storage
- emptyDir: {}
name: busybox-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -232,7 +233,8 @@ spec:
- mountPath: /tmp/ps
name: nginx-persistent-storage
volumes:
- name: nginx-persistent-storage
- emptyDir: {}
name: nginx-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -258,7 +260,8 @@ spec:
- mountPath: /tmp/ps
name: busybox-persistent-storage
volumes:
- name: busybox-persistent-storage
- emptyDir: {}
name: busybox-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -332,7 +335,8 @@ spec:
- mountPath: /tmp/ps
name: nginx-persistent-storage
volumes:
- name: nginx-persistent-storage
- emptyDir: {}
name: nginx-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -459,7 +463,8 @@ spec:
- mountPath: /tmp/ps
name: busybox-persistent-storage
volumes:
- name: busybox-persistent-storage
- emptyDir: {}
name: busybox-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -559,7 +564,8 @@ spec:
- mountPath: /tmp/ps
name: busybox-persistent-storage
volumes:
- name: busybox-persistent-storage
- emptyDir: {}
name: busybox-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -661,7 +667,8 @@ spec:
- mountPath: /tmp/ps
name: busybox-persistent-storage
volumes:
- name: busybox-persistent-storage
- emptyDir: {}
name: busybox-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -762,7 +769,8 @@ spec:
- mountPath: /tmp/ps
name: busybox-persistent-storage
volumes:
- name: busybox-persistent-storage
- emptyDir: {}
name: busybox-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -957,7 +965,8 @@ spec:
- mountPath: /tmp/ps
name: busybox-persistent-storage
volumes:
- name: busybox-persistent-storage
- emptyDir: {}
name: busybox-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -1171,7 +1180,8 @@ spec:
- mountPath: /tmp/ps
name: busybox-persistent-storage
volumes:
- name: busybox-persistent-storage
- emptyDir: {}
name: busybox-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base

View File

@@ -79,7 +79,8 @@ spec:
- mountPath: /tmp/ps
name: nginx-persistent-storage
volumes:
- name: nginx-persistent-storage
- emptyDir: {}
name: nginx-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base
@@ -222,7 +223,8 @@ spec:
- mountPath: /tmp/ps
name: nginx-persistent-storage
volumes:
- name: nginx-persistent-storage
- emptyDir: {}
name: nginx-persistent-storage
- configMap:
name: configmap-in-base
name: configmap-in-base

View File

@@ -540,7 +540,8 @@ spec:
- mountPath: /tmp/ps
name: nginx-persistent-storage
volumes:
- name: nginx-persistent-storage
- emptyDir: {}
name: nginx-persistent-storage
- configMap:
name: staging-team-foo-configmap-in-base-798k5k7g9f
name: configmap-in-base

View File

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

View File

@@ -302,7 +302,7 @@ kind: Deployment
spec:
template:
spec:
containers: {}
containers: []
`,
dest: `
apiVersion: apps/v1

View File

@@ -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: []
`,
},
}

View File

@@ -167,8 +167,8 @@ spec: {}
expected: `
kind: Deployment
spec:
foo: bar1
baz: buz
foo: bar1
`,
},

View File

@@ -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: {}
`,
},

View File

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

View File

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

View File

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

View File

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

View File

@@ -467,7 +467,6 @@ spec:
replicas: 1
template:
metadata:
creationTimestamp: null
labels:
workload.sas.com/class: stateless
spec:
@@ -498,45 +497,6 @@ spec:
name: tmp
`
// This is the current (incorrect) result we get with kustomize 3.8.1
const currentCleanedDeployment = `apiVersion: apps/v1
kind: Deployment
metadata:
labels:
workload.sas.com/class: stateless
name: sas-crunchy-data-postgres-operator
spec:
replicas: 1
template:
metadata:
labels:
workload.sas.com/class: stateless
spec:
containers:
- envFrom: []
image: sas-crunchy-data-operator-api-server
imagePullPolicy: IfNotPresent
name: apiserver
ports:
- containerPort: 8443
volumeMounts:
- mountPath: /security-ssh
name: security-ssh
- mountPath: /tmp
name: tmp
imagePullSecrets: []
initContainers: []
serviceAccountName: postgres-operator
tolerations:
- effect: NoSchedule
key: workload.sas.com/class
operator: Equal
value: stateful
volumes:
- name: security-ssh
- name: tmp
`
func TestPatchStrategicMergeTransformerCleanupItems(t *testing.T) {
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PatchStrategicMergeTransformer")
@@ -571,7 +531,7 @@ paths:
- patch.yaml
`,
anUncleanDeploymentResource,
currentCleanedDeployment) // prefer expectedCleanedDeployment
expectedCleanedDeployment) // prefer expectedCleanedDeployment
}
func TestStrategicMergeTransformerNoSchema(t *testing.T) {

View File

@@ -9,4 +9,7 @@ require (
sigs.k8s.io/yaml v1.2.0
)
replace sigs.k8s.io/kustomize/api v0.5.1 => ../../../api
replace (
sigs.k8s.io/kustomize/api v0.5.1 => ../../../api
sigs.k8s.io/kustomize/kyaml v0.4.2 => ../../../kyaml
)