Skip to content

Commit

Permalink
Detect GVK properly in kubecbuilder CreateOrPatch helper
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
tamalsaha committed Dec 26, 2023
1 parent 16b1d22 commit a76fa40
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ type (
)

func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, transform TransformFunc, opts ...client.PatchOption) (kutil.VerbType, error) {
gvk, err := apiutil.GVKForObject(obj, c.Scheme())
if err != nil {
return kutil.VerbUnchanged, errors.Wrapf(err, "failed to get GVK for object %T", obj)
}

cur := obj.DeepCopyObject().(client.Object)
key := types.NamespacedName{
Namespace: cur.GetNamespace(),
Name: cur.GetName(),
}
err := c.Get(ctx, key, cur)
err = c.Get(ctx, key, cur)
if kerr.IsNotFound(err) {
klog.V(3).Infof("Creating %+v %s/%s.", obj.GetObjectKind().GroupVersionKind(), key.Namespace, key.Name)
klog.V(3).Infof("Creating %+v %s/%s.", gvk, key.Namespace, key.Name)

createOpts := make([]client.CreateOption, 0, len(opts))
for i := range opts {
Expand All @@ -96,11 +101,6 @@ func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, tran
return kutil.VerbUnchanged, err
}

gvk, err := apiutil.GVKForObject(obj, c.Scheme())
if err != nil {
return kutil.VerbUnchanged, errors.Wrapf(err, "failed to get GVK for object %T", obj)
}

_, unstructuredObj := obj.(*unstructured.Unstructured)

var patch client.Patch
Expand Down

0 comments on commit a76fa40

Please sign in to comment.