Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep empty map in kustomize output #2805

Merged
merged 1 commit into from
Aug 7, 2020

Conversation

Shell32-Natsu
Copy link
Contributor

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 6, 2020
@k8s-ci-robot
Copy link
Contributor

Hi @Shell32-Natsu. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 6, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Shell32-Natsu
To complete the pull request process, please assign mengqiy
You can assign the PR to them by writing /assign @mengqiy in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Shell32-Natsu
Copy link
Contributor Author

/assign @monopole

@monopole
Copy link
Contributor

monopole commented Aug 6, 2020

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 6, 2020
@@ -91,15 +91,14 @@ kind: Deployment
{description: `remove scalar -- empty in src`,
source: `
kind: Deployment
field: {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this PR also retains scalar nulls?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This null is in input. Because {} is not considered as empty so only null can be used as empty in input.

Copy link
Contributor

@monopole monopole left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, but i think the API is getting odd

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It now seems we have:

IsMissingOrNull(n) => n == nil || n.YNode() == nil || n.YNode().Tag == NullNodeTag
        IsEmpty(n) => IsMissingOrNull(n)
         IsNull(n) => n != nil && n.YNode() != nil && n.YNode().Tag == NullNodeTag
     IsEmptyMap(n) => IsEmpty(n) ||
                          (n.YNode().Kind == yaml.MappingNode && len(n.YNode().Content) == 0)

This feels random. And we have this strange contest between
Kind and Tag, which i'm not sure how to resolve yet.

We've not hit 1.0 yet, so we can change the API.

How about we try:

  IsNil(n)        => n == nil || n.YNode() == nil
  IsEmptyDoc(n)   => !IsNodeNil(n) && n.YNode().Tag == NodeTagNull
  IsEmptyMap(n)   => !IsNodeNil(n) &&
                        n.YNode().Kind == yaml.MappingNode &&
                        len(n.YNode().Content) == 0

with this, the tags are hidden, and we start a pattern where
IsEmptyFoo means the object is non-nil with a defined type -
so empty docs, lists and maps are different things.

Translation:

  • IsNull becomes IsEmptyDoc
  • IsEmptyand IsMissingOrNull are written as IsNil || IsEmptyDoc

The compiler can optimize away redundancy.

So L54 in kyaml/yaml/walk/associative_sequence.go which currently reads

if yaml.IsEmpty(val) || yaml.IsEmptyMap(val) {

becomes

if yaml.IsNil(val) || yaml.IsEmptyDoc(val) || yaml.IsEmptyMap(val) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good. But because these methods are fundamental and widely used so if we change them, we need to update a lot of files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about create another issue about this refactoring?

@monopole
Copy link
Contributor

monopole commented Aug 7, 2020

ack

/lgtm

Fixes #2734

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 7, 2020
@monopole monopole merged commit b7d913b into kubernetes-sigs:master Aug 7, 2020
@Shell32-Natsu Shell32-Natsu deleted the keep-empty-map branch August 25, 2020 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants