Skip to content

Commit

Permalink
Fix patch for duckify mode (#603)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
tamalsaha authored Oct 23, 2024
1 parent d04f1dc commit 4c772eb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
48 changes: 48 additions & 0 deletions client/duck/patch.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 6 additions & 2 deletions client/duck/typed_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,19 @@ 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
}
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 {
Expand Down
8 changes: 6 additions & 2 deletions client/duck/unstructured_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4c772eb

Please sign in to comment.