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

Fix premature return #49834

Merged
merged 1 commit into from
Aug 8, 2017
Merged

Conversation

guoshimin
Copy link
Contributor

@guoshimin guoshimin commented Jul 29, 2017

What this PR does / why we need it: Fixes a bug where the loop is prematurely terminated.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #50040

Special notes for your reviewer:

Release note:

Fixed a bug in strategic merge patch that caused kubectl apply to error under some conditions

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://github.com/kubernetes/kubernetes/wiki/CLA-FAQ to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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. I understand the commands that are listed here.

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

Hi @guoshimin. Thanks for your PR.

I'm waiting for a kubernetes 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.

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. I understand the commands that are listed here.

@k8s-github-robot k8s-github-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Jul 29, 2017
@guoshimin
Copy link
Contributor Author

Signed the CLA

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jul 29, 2017
@apelisse
Copy link
Member

/ok-to-test

Would you mind explaining how you found that bug? This code is complicated to understand and I'd really feel more confident if we could have better tests. I have the feeling that this is a good opportunity.

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 30, 2017
@guoshimin
Copy link
Contributor Author

I wrote a tool which used the strategicpatch package. I encountered the error unable to find api field in struct Container for the json field "$setElementOrder/env" for some input, but only some of the times. From there, by tracing successful runs and failed runs, I was able to find the problem. The nondeterminism was caused by the random map traversal order.

Although I found the bug via my tool, I believe the bug affects kubectl apply too. The condition to trigger the bug is when you are

  • deleting one or more elements from an array,
  • modifying one or more elements from the same array, and
  • modifying one or more elements from another array

Here's a minimum example:

import "k8s.io/api/core/v1"

func TestMergeMap(t *testing.T) {
        for i := 0; i < 100; i++ {
                original := map[string]interface{}{
                        "env": []interface{}{
                                map[string]interface{}{
                                        "$patch": "delete",
                                        "name":   "AAA",
                                },
                        },
                        "$setElementOrder/env": []interface{}{
                                map[string]interface{}{
                                        "name": "BBB",
                                },
                        },
                }
                patch := map[string]interface{}{
                        "$setElementOrder/volumeMounts": []interface{}{},
                        "env": []interface{}{
                                map[string]interface{}{
                                        "name":  "BBB",
                                        "value": "BBB",
                                },
                        },
                        "$setElementOrder/env": []interface{}{
                                map[string]interface{}{
                                        "name": "BBB",
                                },
                        },
                }
                typ := reflect.TypeOf(v1.Container{})
                _, err := mergeMap(original, patch, typ, MergeOptions{})
                if err != nil {
                        t.Errorf(err.Error())
                        t.FailNow()
                }
        }
}

@apelisse
Copy link
Member

Awesome @guoshimin! @mengqiy can you take a look and see if this can be included in the unit-test suite?

@mengqiy
Copy link
Member

mengqiy commented Jul 31, 2017

LGTM
@guoshimin Thanks for fixing this bug. We need a test to cover this.
You can include the test in the existing test framework:https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/util/strategicpatch/patch_test.go#L1114.

IMO we don't need a release note for this fix, do we? Can you update the release-note section in the PR body.

@guoshimin
Copy link
Contributor Author

Updated the release note

@mengqiy
Copy link
Member

mengqiy commented Aug 1, 2017

Can you add the test case in #49834 (comment) to prevent regression in the future?

@guoshimin
Copy link
Contributor Author

guoshimin commented Aug 1, 2017 via email

@k8s-github-robot k8s-github-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Aug 1, 2017
@guoshimin
Copy link
Contributor Author

Added test. PTAL.

@mengqiy
Copy link
Member

mengqiy commented Aug 2, 2017

/lgtm
Please squash commits for merging.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 2, 2017
@guoshimin
Copy link
Contributor Author

/assign @pwittrock

@k8s-github-robot k8s-github-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 2, 2017
@guoshimin
Copy link
Contributor Author

/test pull-kubernetes-e2e-gce-etcd3

@mengqiy
Copy link
Member

mengqiy commented Aug 3, 2017

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 3, 2017
@guoshimin
Copy link
Contributor Author

/retest

@guoshimin
Copy link
Contributor Author

gentle ping @pwittrock

@pwittrock
Copy link
Member

/approve

@pwittrock
Copy link
Member

Thanks!

@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: guoshimin, mengqiy, pwittrock

Associated issue: 50040

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

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@k8s-github-robot k8s-github-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 7, 2017
@k8s-github-robot
Copy link

/test all [submit-queue is verifying that this PR is safe to merge]

@guoshimin
Copy link
Contributor Author

/retest

@k8s-github-robot
Copy link

Automatic merge from submit-queue

@k8s-github-robot k8s-github-robot merged commit 9606457 into kubernetes:master Aug 8, 2017
@guoshimin
Copy link
Contributor Author

Now that this is merged, how do we include it in the next patch release?

@mengqiy
Copy link
Member

mengqiy commented Aug 17, 2017

This will be included in v1.8.0.
It you want it in 1.7, you will need to backport it to branch release-1.7.

@wojtek-t wojtek-t added this to the v1.7 milestone Aug 23, 2017
@wojtek-t wojtek-t added the cherry-pick-approved Indicates a cherry-pick PR into a release branch has been approved by the release branch manager. label Aug 24, 2017
k8s-github-robot pushed a commit that referenced this pull request Aug 24, 2017
Automatic merge from submit-queue

Automated cherry pick of #49834 upstream release 1.7

Cherry pick of #49834  on release-1.7.

#49834: Fix premature return
@k8s-cherrypick-bot
Copy link

Commit found in the "release-1.7" branch appears to be this PR. Removing the "cherrypick-candidate" label. If this is an error find help to get your PR picked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cherry-pick-approved Indicates a cherry-pick PR into a release branch has been approved by the release branch manager. 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. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kubectl apply error
8 participants