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

✨ Add client.StrategicMergeFrom #1406

Merged

Conversation

timebertt
Copy link
Contributor

@timebertt timebertt commented Mar 1, 2021

Fixes #1375

This PR adds client.StrategicMergeFrom.
The function is similar to client.{MergeFrom,MergeFromWithOptions}, but it creates a strategic-merge-patch instead of a merge-patch. See doc strings for more details about the differences of the two patch types and to figure out when to use which one.

Note, that strategic merge patches are not supported by CRDs.

@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 Mar 1, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @timebertt. 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 Mar 1, 2021
@coderanger
Copy link
Contributor

Is there any way to refactor this so it's not quite as much copy-paste from MergeFrom? It feels like a lot of duplication.

@timebertt
Copy link
Contributor Author

@coderanger Yes, you're right. It turned out to be very much the same like MergeFrom.
Deduplicated it now. PTAL.

pkg/client/patch.go Outdated Show resolved Hide resolved
}

// StrategicMergeFromWithOptions creates a Patch that patches using the strategic-merge-patch strategy with the given object as base.
func StrategicMergeFromWithOptions(obj runtime.Object, opts ...MergeFromOption) Patch {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it appropriate to use MergeFromOption here? My concern is that merge-patch and strategic-merge-patch could diverge in the options they take in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was contemplating about the same, glad you bring it up here.

The reason I chose this way, is that if options specific to either patch type will be added in the future, we could simply add another interface StrategicMergeFromOption, implement both interfaces in the options that we already have and just switch the parameter type in StrategicMergeFrom. This way, folks using the options wouldn't get any compile tim error when upgrading.
And as it's not required to differentiate right now, I chose to reuse MergeFromOption for simplicity's sake.

Let me know, if you have any concerns with this approach.

}

// StrategicMergeFrom creates a Patch that patches using the strategic-merge-patch strategy with the given object as base.
func StrategicMergeFrom(obj runtime.Object, opts ...MergeFromOption) Patch {
Copy link
Member

Choose a reason for hiding this comment

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

Can you please add godocs here and to MergeFrom that explain how StrategicMergePatch and MergePatch differ and when to use which?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, good idea. Done.
WDYT now?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 3, 2021
@timebertt
Copy link
Contributor Author

@timebertt: PR needs rebase.

Done.

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 3, 2021
@cbandy
Copy link
Contributor

cbandy commented Mar 4, 2021

I see a failure now:

Patch
controller-runtime/pkg/client/client_test.go:3242
  StrategicMergeFrom
  controller-runtime/pkg/client/client_test.go:3307
    creates a strategic merge patch with the modifications applied during the mutation, using optimistic locking [It]
    controller-runtime/pkg/client/client_test.go:3357

    Unexpected error:
        <mergepatch.ErrPreconditionFailed>: {
            message: "precondition failed for: map[metadata:map[resourceVersion:10] spec:map[template:map[spec:map[$setElementOrder/containers:[map[name:main] map[name:sidecar]] containers:[map[image:foo:v2 name:main]]]]]]",
        }
        precondition failed for: map[metadata:map[resourceVersion:10] spec:map[template:map[spec:map[$setElementOrder/containers:[map[name:main] map[name:sidecar]] containers:[map[image:foo:v2 name:main]]]]]]
    occurred

    controller-runtime/pkg/client/client_test.go:3376

@timebertt
Copy link
Contributor Author

@cbandy yes, sorry about that.
The preconditions and the respective test case are not needed anymore since your changes in #1395.
I fixed it accordingly. PTAL.

@alvaroaleman
Copy link
Member

/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 Mar 4, 2021
@alvaroaleman
Copy link
Member

/lgtm
/approve

thank you!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 4, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, timebertt

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

The pull request process is described 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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 4, 2021
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. 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.

Support creating strategic merge patch
5 participants