-
Notifications
You must be signed in to change notification settings - Fork 1.2k
⚠️ Add subresource apply support #3321
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -544,6 +544,30 @@ func (po *SubResourcePatchOptions) ApplyToSubResourcePatch(o *SubResourcePatchOp | |
| } | ||
| } | ||
|
|
||
| // SubResourceApplyOptions are the options for a subresource | ||
| // apply rquest. | ||
| type SubResourceApplyOptions struct { | ||
| ApplyOptions | ||
| SubResourceBody runtime.ApplyConfiguration | ||
| } | ||
|
|
||
| // ApplyOpts applies the given options. | ||
| func (ao *SubResourceApplyOptions) ApplyOpts(opts []SubResourceApplyOption) *SubResourceApplyOptions { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This differs from our typical naming convention of naming these |
||
| for _, o := range opts { | ||
| o.ApplyToSubResourceApply(ao) | ||
| } | ||
|
|
||
| return ao | ||
| } | ||
|
|
||
| // ApplyToSubResourceApply applies the configuration on the given patch options. | ||
| func (ao *SubResourceApplyOptions) ApplyToSubResourceApply(o *SubResourceApplyOptions) { | ||
| ao.ApplyOptions.ApplyToApply(&o.ApplyOptions) | ||
| if ao.SubResourceBody != nil { | ||
| o.SubResourceBody = ao.SubResourceBody | ||
| } | ||
| } | ||
|
|
||
| func (sc *subResourceClient) Get(ctx context.Context, obj Object, subResource Object, opts ...SubResourceGetOption) error { | ||
| switch obj.(type) { | ||
| case runtime.Unstructured: | ||
|
|
@@ -595,3 +619,13 @@ func (sc *subResourceClient) Patch(ctx context.Context, obj Object, patch Patch, | |
| return sc.client.typedClient.PatchSubResource(ctx, obj, sc.subResource, patch, opts...) | ||
| } | ||
| } | ||
|
|
||
| func (sc *subResourceClient) Apply(ctx context.Context, obj runtime.ApplyConfiguration, opts ...SubResourceApplyOption) error { | ||
| switch obj := obj.(type) { | ||
| case *unstructuredApplyConfiguration: | ||
| defer sc.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind()) | ||
| return sc.client.unstructuredClient.ApplySubResource(ctx, obj, sc.subResource, opts...) | ||
| default: | ||
| return sc.client.typedClient.ApplySubResource(ctx, obj, sc.subResource, opts...) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1335,6 +1335,38 @@ func (sw *fakeSubResourceClient) statusPatch(body client.Object, patch client.Pa | |
| return sw.client.patch(body, patch, &patchOptions.PatchOptions) | ||
| } | ||
|
|
||
| func (sw *fakeSubResourceClient) Apply(ctx context.Context, obj runtime.ApplyConfiguration, opts ...client.SubResourceApplyOption) error { | ||
| if sw.subResource != "status" { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is different from IMHO it is better to error out rather than to silenly do the wrong thing. We could fo a follow-up to do the same in Patch for consistency (and/or implement the subresources we support through
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think tracking a follow-up would be good
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a point to the tracking issue |
||
| return errors.New("fakeSubResourceClient currently only supports Apply for status subresource") | ||
| } | ||
|
|
||
| applyOpts := &client.SubResourceApplyOptions{} | ||
| applyOpts.ApplyOpts(opts) | ||
|
|
||
| data, err := json.Marshal(obj) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal apply configuration: %w", err) | ||
| } | ||
|
|
||
| u := &unstructured.Unstructured{} | ||
| if err := json.Unmarshal(data, u); err != nil { | ||
| return fmt.Errorf("failed to unmarshal apply configuration: %w", err) | ||
| } | ||
|
|
||
| patchOpts := &client.SubResourcePatchOptions{} | ||
| patchOpts.Raw = applyOpts.AsPatchOptions() | ||
|
|
||
| if applyOpts.SubResourceBody != nil { | ||
sbueringer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| subResourceBody := &unstructured.Unstructured{} | ||
| if err := json.Unmarshal(data, subResourceBody); err != nil { | ||
| return fmt.Errorf("failed to unmarshal subresource body: %w", err) | ||
| } | ||
| patchOpts.SubResourceBody = subResourceBody | ||
| } | ||
|
|
||
| return sw.Patch(ctx, u, &fakeApplyPatch{}, patchOpts) | ||
| } | ||
|
|
||
| func allowsUnconditionalUpdate(gvk schema.GroupVersionKind) bool { | ||
| switch gvk.Group { | ||
| case "apps": | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.