-
Notifications
You must be signed in to change notification settings - Fork 562
Bug 1853601: use server-side-apply for catalog source pod update #1624
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 all 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
k8scontrollerclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
fakecontrollerclient "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
// FakeApplier provides a wrapper around the fake k8s controller client to convert the unsupported apply-type patches to merge patches. | ||
func NewFakeApplier(scheme *runtime.Scheme, owner string, objs ...runtime.Object) *ServerSideApplier { | ||
return &ServerSideApplier{ | ||
client: &fakeApplier{fakecontrollerclient.NewFakeClientWithScheme(scheme, objs...)}, | ||
Scheme: scheme, | ||
Owner: k8scontrollerclient.FieldOwner(owner), | ||
} | ||
} | ||
|
||
type fakeApplier struct { | ||
k8scontrollerclient.Client | ||
} | ||
|
||
func (c *fakeApplier) Patch(ctx context.Context, obj runtime.Object, patch k8scontrollerclient.Patch, opts ...k8scontrollerclient.PatchOption) error { | ||
patch, opts = convertApplyToMergePatch(patch, opts...) | ||
return c.Client.Patch(ctx, obj, patch, opts...) | ||
} | ||
|
||
func (c *fakeApplier) Status() k8scontrollerclient.StatusWriter { | ||
return fakeStatusWriter{c.Client.Status()} | ||
} | ||
|
||
type fakeStatusWriter struct { | ||
k8scontrollerclient.StatusWriter | ||
} | ||
|
||
func (c fakeStatusWriter) Patch(ctx context.Context, obj runtime.Object, patch k8scontrollerclient.Patch, opts ...k8scontrollerclient.PatchOption) error { | ||
patch, opts = convertApplyToMergePatch(patch, opts...) | ||
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'm not sure an apply patch can be directly converted to a merge patch (since I think you need both the old and new resource to generate the delta for a merge patch), but if this is working, I think it might be okay. I'm a little concerned that we're swapping SSA out for another patch type in our unit tests, but at least it lets us test the rest of the logic in our unit tests. |
||
return c.StatusWriter.Patch(ctx, obj, patch, opts...) | ||
} | ||
|
||
func convertApplyToMergePatch(patch k8scontrollerclient.Patch, opts ...k8scontrollerclient.PatchOption) (k8scontrollerclient.Patch, []k8scontrollerclient.PatchOption) { | ||
// Apply patch type is not supported on the fake controller | ||
if patch.Type() == types.ApplyPatchType { | ||
patch = k8scontrollerclient.Merge | ||
patchOptions := make([]k8scontrollerclient.PatchOption, 0, len(opts)) | ||
for _, opt := range opts { | ||
if opt == k8scontrollerclient.ForceOwnership { | ||
continue | ||
} | ||
patchOptions = append(patchOptions, opt) | ||
} | ||
opts = patchOptions | ||
} | ||
return patch, opts | ||
} |
Uh oh!
There was an error while loading. Please reload this page.