diff --git a/api/krusty/extendedpatch_test.go b/api/krusty/extendedpatch_test.go index e8c4fd2a0..84c7accba 100644 --- a/api/krusty/extendedpatch_test.go +++ b/api/krusty/extendedpatch_test.go @@ -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 diff --git a/api/krusty/inlinepatch_test.go b/api/krusty/inlinepatch_test.go index eb318d1f4..b49297c6c 100644 --- a/api/krusty/inlinepatch_test.go +++ b/api/krusty/inlinepatch_test.go @@ -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 diff --git a/api/krusty/multiplepatch_test.go b/api/krusty/multiplepatch_test.go index e130b28b3..240b9b80a 100644 --- a/api/krusty/multiplepatch_test.go +++ b/api/krusty/multiplepatch_test.go @@ -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 diff --git a/kyaml/yaml/fns.go b/kyaml/yaml/fns.go index 9de3e26eb..a889bae29 100644 --- a/kyaml/yaml/fns.go +++ b/kyaml/yaml/fns.go @@ -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 } diff --git a/kyaml/yaml/merge2/element_test.go b/kyaml/yaml/merge2/element_test.go index 6a5442b04..26ed10dca 100644 --- a/kyaml/yaml/merge2/element_test.go +++ b/kyaml/yaml/merge2/element_test.go @@ -302,7 +302,7 @@ kind: Deployment spec: template: spec: - containers: {} + containers: [] `, dest: ` apiVersion: apps/v1 diff --git a/kyaml/yaml/merge2/list_test.go b/kyaml/yaml/merge2/list_test.go index 8e137fa69..ad21a7e47 100644 --- a/kyaml/yaml/merge2/list_test.go +++ b/kyaml/yaml/merge2/list_test.go @@ -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: [] `, }, } diff --git a/kyaml/yaml/merge2/map_test.go b/kyaml/yaml/merge2/map_test.go index 49c3165f8..cc687e336 100644 --- a/kyaml/yaml/merge2/map_test.go +++ b/kyaml/yaml/merge2/map_test.go @@ -167,8 +167,8 @@ spec: {} expected: ` kind: Deployment spec: - foo: bar1 baz: buz + foo: bar1 `, }, diff --git a/kyaml/yaml/merge2/scalar_test.go b/kyaml/yaml/merge2/scalar_test.go index 029a55683..4af6f894f 100644 --- a/kyaml/yaml/merge2/scalar_test.go +++ b/kyaml/yaml/merge2/scalar_test.go @@ -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: {} `, }, diff --git a/kyaml/yaml/merge3/element_test.go b/kyaml/yaml/merge3/element_test.go index 84dc84154..9b685c1c8 100644 --- a/kyaml/yaml/merge3/element_test.go +++ b/kyaml/yaml/merge3/element_test.go @@ -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 diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index ac2c63efb..74120526b 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -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 } diff --git a/kyaml/yaml/types_test.go b/kyaml/yaml/types_test.go index 56fd9a0a2..7d6d768e0 100644 --- a/kyaml/yaml/types_test.go +++ b/kyaml/yaml/types_test.go @@ -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") } } diff --git a/kyaml/yaml/walk/associative_sequence.go b/kyaml/yaml/walk/associative_sequence.go index 8d64e7a49..1c709cb77 100644 --- a/kyaml/yaml/walk/associative_sequence.go +++ b/kyaml/yaml/walk/associative_sequence.go @@ -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 diff --git a/plugin/builtin/patchstrategicmergetransformer/PatchStrategicMergeTransformer_test.go b/plugin/builtin/patchstrategicmergetransformer/PatchStrategicMergeTransformer_test.go index 319899586..069782391 100644 --- a/plugin/builtin/patchstrategicmergetransformer/PatchStrategicMergeTransformer_test.go +++ b/plugin/builtin/patchstrategicmergetransformer/PatchStrategicMergeTransformer_test.go @@ -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) { diff --git a/plugin/builtin/patchstrategicmergetransformer/go.mod b/plugin/builtin/patchstrategicmergetransformer/go.mod index aabba5056..04c9a9c25 100644 --- a/plugin/builtin/patchstrategicmergetransformer/go.mod +++ b/plugin/builtin/patchstrategicmergetransformer/go.mod @@ -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 +)