Skip to content

Set GVK in an object while calling typed-client #2946

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 67 additions & 9 deletions pkg/client/typed_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package client

import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

"k8s.io/apimachinery/pkg/runtime"
)
Expand All @@ -30,6 +31,18 @@ type typedClient struct {
paramCodec runtime.ParameterCodec
}

// gvkInto set GVK/TypeMeta into objects.
// GVK of an object becomes nil because of codec convention.
func (c *typedClient) gvkInto(obj runtime.Object) error {
scheme := c.resources.scheme
gvk, err := apiutil.GVKForObject(obj, scheme)
if err != nil {
return err
}
obj.GetObjectKind().SetGroupVersionKind(gvk)
return nil
}

// Create implements client.Client.
func (c *typedClient) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
o, err := c.resources.getObjMeta(obj)
Expand All @@ -40,13 +53,18 @@ func (c *typedClient) Create(ctx context.Context, obj Object, opts ...CreateOpti
createOpts := &CreateOptions{}
createOpts.ApplyOptions(opts)

return o.Post().
err = o.Post().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Body(obj).
VersionedParams(createOpts.AsCreateOptions(), c.paramCodec).
Do(ctx).
Into(obj)
if err != nil {
return err
}

return c.gvkInto(obj)
}

// Update implements client.Client.
Expand All @@ -59,14 +77,19 @@ func (c *typedClient) Update(ctx context.Context, obj Object, opts ...UpdateOpti
updateOpts := &UpdateOptions{}
updateOpts.ApplyOptions(opts)

return o.Put().
err = o.Put().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
Body(obj).
VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).
Do(ctx).
Into(obj)
if err != nil {
return err
}

return c.gvkInto(obj)
}

// Delete implements client.Client.
Expand Down Expand Up @@ -122,14 +145,19 @@ func (c *typedClient) Patch(ctx context.Context, obj Object, patch Patch, opts .
patchOpts := &PatchOptions{}
patchOpts.ApplyOptions(opts)

return o.Patch(patch.Type()).
err = o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
VersionedParams(patchOpts.AsPatchOptions(), c.paramCodec).
Body(data).
Do(ctx).
Into(obj)
if err != nil {
return err
}

return c.gvkInto(obj)
}

// Get implements client.Client.
Expand All @@ -140,11 +168,16 @@ func (c *typedClient) Get(ctx context.Context, key ObjectKey, obj Object, opts .
}
getOpts := GetOptions{}
getOpts.ApplyOptions(opts)
return r.Get().
err = r.Get().
NamespaceIfScoped(key.Namespace, r.isNamespaced()).
Resource(r.resource()).
VersionedParams(getOpts.AsGetOptions(), c.paramCodec).
Name(key.Name).Do(ctx).Into(obj)
if err != nil {
return err
}

return c.gvkInto(obj)
}

// List implements client.Client.
Expand All @@ -157,12 +190,17 @@ func (c *typedClient) List(ctx context.Context, obj ObjectList, opts ...ListOpti
listOpts := ListOptions{}
listOpts.ApplyOptions(opts)

return r.Get().
err = r.Get().
NamespaceIfScoped(listOpts.Namespace, r.isNamespaced()).
Resource(r.resource()).
VersionedParams(listOpts.AsListOptions(), c.paramCodec).
Do(ctx).
Into(obj)
if err != nil {
return err
}

return c.gvkInto(obj)
}

func (c *typedClient) GetSubResource(ctx context.Context, obj, subResourceObj Object, subResource string, opts ...SubResourceGetOption) error {
Expand All @@ -178,14 +216,19 @@ func (c *typedClient) GetSubResource(ctx context.Context, obj, subResourceObj Ob
getOpts := &SubResourceGetOptions{}
getOpts.ApplyOptions(opts)

return o.Get().
err = o.Get().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
SubResource(subResource).
VersionedParams(getOpts.AsGetOptions(), c.paramCodec).
Do(ctx).
Into(subResourceObj)
if err != nil {
return err
}

return c.gvkInto(obj)
}

func (c *typedClient) CreateSubResource(ctx context.Context, obj Object, subResourceObj Object, subResource string, opts ...SubResourceCreateOption) error {
Expand All @@ -201,7 +244,7 @@ func (c *typedClient) CreateSubResource(ctx context.Context, obj Object, subReso
createOpts := &SubResourceCreateOptions{}
createOpts.ApplyOptions(opts)

return o.Post().
err = o.Post().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
Expand All @@ -210,6 +253,11 @@ func (c *typedClient) CreateSubResource(ctx context.Context, obj Object, subReso
VersionedParams(createOpts.AsCreateOptions(), c.paramCodec).
Do(ctx).
Into(subResourceObj)
if err != nil {
return err
}

return c.gvkInto(obj)
}

// UpdateSubResource used by SubResourceWriter to write status.
Expand All @@ -236,7 +284,7 @@ func (c *typedClient) UpdateSubResource(ctx context.Context, obj Object, subReso
body.SetNamespace(obj.GetNamespace())
}

return o.Put().
err = o.Put().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
Expand All @@ -245,6 +293,11 @@ func (c *typedClient) UpdateSubResource(ctx context.Context, obj Object, subReso
VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).
Do(ctx).
Into(body)
if err != nil {
return err
}

return c.gvkInto(obj)
}

// PatchSubResource used by SubResourceWriter to write subresource.
Expand All @@ -267,7 +320,7 @@ func (c *typedClient) PatchSubResource(ctx context.Context, obj Object, subResou
return err
}

return o.Patch(patch.Type()).
err = o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
Expand All @@ -276,4 +329,9 @@ func (c *typedClient) PatchSubResource(ctx context.Context, obj Object, subResou
VersionedParams(patchOpts.AsPatchOptions(), c.paramCodec).
Do(ctx).
Into(body)
if err != nil {
return err
}

return c.gvkInto(obj)
}
Loading