-
Notifications
You must be signed in to change notification settings - Fork 39.3k
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 error for strategic merge patch of custom resources #53558
Fix error for strategic merge patch of custom resources #53558
Conversation
Hi @nikhita. 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 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. |
Now, the error message is shown as: $ cat crd.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: crontabs.stable.example.com
spec:
group: stable.example.com
version: v1
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ct
$ kubectl create -f crd.yaml
customresourcedefinition "crontabs.stable.example.com" created
$ cat crontab-object.yaml
apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
name: my-new-cron-object
spec:
cronSpec: "* * * * */5"
image: my-awesome-cron-image
$ kubectl create -f crontab-object.yaml
crontab "my-new-cron-object" created
$ kubectl patch crontab my-new-cron-object -p '{"spec":{"image":"new-image"}}'
Error from server: cannot apply strategic merge patch for custom resources, try --type merge |
/ok-to-test |
/retest |
// each field in a strategic merge patch, we can't easily do a strategic merge for custom resources. | ||
// So we should fail fast and return an error. | ||
if t.Name() == "Unstructured" { | ||
return nil, fmt.Errorf("cannot apply strategic merge patch for custom resources, try --type merge") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the strategy patch logic without any metadata just the classical non-strategic patch? What does the user see now?
In general, I would prefer not to mention custom resources here in apimachinery. An error constant in vendor/k8s.io/apimachinery/pkg/util/mergepatch/errors.go with "ErrUnsupportedPatchFormat" would make more sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
7507ac4
to
e93baef
Compare
The user sees: After this update, it is |
@nikhita feel like adding a unit test? |
@ncdc Will do! Also, maybe we should hold this off until #53379 (comment) is resolved (working on it). |
Looks good to me. |
e93baef
to
2ae617e
Compare
Added a test and updated the comment to add more detail into why we can't support strategic merge patch for custom resources. |
// Because custom resources are decoded as Unstructured and because we're missing the metadata about how to handle | ||
// each field in a strategic merge patch, we can't find the go struct tags. Hence, we can't easily do a strategic merge | ||
// for custom resources. So we should fail fast and return an error. | ||
if t.Name() == "Unstructured" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would _, ok := dataStruct.(Unstructured); ok
be the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_, ok := dataStruct.(*unstructured.Unstructured); ok
would be the same (with the pointer). 👍
updated.
We need the go struct tags `patchMergeKey` and `patchStrategy` for fields that support a strategic merge patch. For native resources, we can easily figure out these tags since we know the fields. Because custom resources are decoded as Unstructured and because we're missing the metadata about how to handle each field in a strategic merge patch, we can't find the go struct tags. Hence, we can't easily do a strategic merge for custom resources. So we should fail fast and return an error.
2ae617e
to
79349c9
Compare
/retest |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: nikhita, sttts Associated issue: 50037 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 |
Automatic merge from submit-queue (batch tested with PRs 54800, 53898, 54812, 54921, 53558). If you want to cherry-pick this change to another branch, please follow the instructions here. |
Fixes #50037.
We need the go struct tags
patchMergeKey
andpatchStrategy
for fields that support a strategic merge patch. For native resources, we can easily figure out these tags since we know the fields.Because custom resources are decoded as Unstructured and because we're missing the metadata about how to handle each field in a strategic merge patch, we can't find the go struct tags. Hence, we can't easily do a strategic merge for custom resources.
So we should fail fast and return an error.
Release note:
/cc @sttts @deads2k @ncdc