diff --git a/client/duck/patch.go b/client/duck/patch.go new file mode 100644 index 000000000..968df35d9 --- /dev/null +++ b/client/duck/patch.go @@ -0,0 +1,48 @@ +/* +Copyright AppsCode Inc. and Contributors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package duck + +import ( + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +type RawPatch struct { + pt types.PatchType + data []byte +} + +var _ client.Patch = &RawPatch{} + +func NewRawPatch(obj client.Object, patch client.Patch) (client.Patch, error) { + data, err := patch.Data(obj) + if err != nil { + return nil, err + } + return &RawPatch{ + pt: patch.Type(), + data: data, + }, nil +} + +func (r *RawPatch) Type() types.PatchType { + return r.pt +} + +func (r *RawPatch) Data(obj client.Object) ([]byte, error) { + return r.data, nil +} diff --git a/client/duck/typed_client.go b/client/duck/typed_client.go index bcebd01e6..9ad6235a6 100644 --- a/client/duck/typed_client.go +++ b/client/duck/typed_client.go @@ -185,6 +185,11 @@ func (d *typedClient) Patch(ctx context.Context, obj client.Object, patch client return d.c.Patch(ctx, obj, patch, opts...) } + rawPatch, err := NewRawPatch(obj, patch) + if err != nil { + return err + } + ll, err := d.c.Scheme().New(d.rawGVK) if err != nil { return err @@ -192,8 +197,7 @@ func (d *typedClient) Patch(ctx context.Context, obj client.Object, patch client llo := ll.(client.Object) llo.SetNamespace(obj.GetNamespace()) llo.SetName(obj.GetName()) - llo.SetLabels(obj.GetLabels()) - return d.c.Patch(ctx, llo, patch, opts...) + return d.c.Patch(ctx, llo, rawPatch, opts...) } func (d *typedClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error { diff --git a/client/duck/unstructured_client.go b/client/duck/unstructured_client.go index 12c7bc2f2..bf7549d3b 100755 --- a/client/duck/unstructured_client.go +++ b/client/duck/unstructured_client.go @@ -133,12 +133,16 @@ func (uc *unstructuredClient) Patch(ctx context.Context, obj client.Object, patc return uc.c.Patch(ctx, obj, patch, opts...) } + rawPatch, err := NewRawPatch(obj, patch) + if err != nil { + return err + } + var llo unstructured.Unstructured llo.GetObjectKind().SetGroupVersionKind(uc.rawGVK) llo.SetNamespace(obj.GetNamespace()) llo.SetName(obj.GetName()) - llo.SetLabels(obj.GetLabels()) - return uc.c.Patch(ctx, &llo, patch, opts...) + return uc.c.Patch(ctx, &llo, rawPatch, opts...) } // Get implements client.Client.