Skip to content

Commit

Permalink
Merge pull request #20 from iawia002/typedclient
Browse files Browse the repository at this point in the history
kubernetes/client: use new object instance in Get/List method
  • Loading branch information
iawia002 authored Sep 12, 2023
2 parents 9280b3d + 37530d7 commit 434bbd1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 50 deletions.
56 changes: 26 additions & 30 deletions kubernetes/client/typed/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (

type typedClient struct {
client client.Reader
scheme *runtime.Scheme
gvk schema.GroupVersionKind
obj runtime.Object
listObj runtime.Object
listGVK schema.GroupVersionKind
}

// NewTypedClient returns a new Client implementation.
Expand Down Expand Up @@ -46,54 +46,50 @@ func NewTypedClient(gvk schema.GroupVersionKind, opts ...func(*options)) (Client
o.cache = cache
}

var (
obj runtime.Object
listObj runtime.Object
)
if o.scheme.Recognizes(gvk) {
obj, _ = o.scheme.New(gvk)
}
listGVK := schema.GroupVersionKind{
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind + "List",
}
if o.scheme.Recognizes(listGVK) {
listObj, _ = o.scheme.New(listGVK)
}

return &typedClient{
client: o.cache,
gvk: gvk,
obj: obj,
listObj: listObj,
client: o.cache,
scheme: o.scheme,
gvk: gvk,
listGVK: schema.GroupVersionKind{
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind + "List",
},
}, nil
}

var resourceNotRegisteredError = "kind %s is not registered in scheme"

// Get retrieves an object for the given object key.
func (t *typedClient) Get(ctx context.Context, key types.NamespacedName, opts ...client.GetOption) (client.Object, error) {
if t.obj == nil {
if !t.scheme.Recognizes(t.gvk) {
return nil, fmt.Errorf(resourceNotRegisteredError, t.gvk.String())
}

obj := t.obj.(client.Object)
if err := t.client.Get(ctx, key, obj, opts...); err != nil {
obj, err := t.scheme.New(t.gvk)
if err != nil {
return nil, err
}
return obj, nil
clientObj := obj.(client.Object)
if err = t.client.Get(ctx, key, clientObj, opts...); err != nil {
return nil, err
}
return clientObj, nil
}

// List retrieves list of objects for a given namespace and list options.
func (t *typedClient) List(ctx context.Context, namespace string, opts ...client.ListOption) (client.ObjectList, error) {
if t.listObj == nil {
if !t.scheme.Recognizes(t.listGVK) {
return nil, fmt.Errorf(resourceNotRegisteredError, t.gvk.String())
}

listObj := t.listObj.(client.ObjectList)
if err := t.client.List(ctx, listObj, append(opts, client.InNamespace(namespace))...); err != nil {
listObj, err := t.scheme.New(t.listGVK)
if err != nil {
return nil, err
}
clientObj := listObj.(client.ObjectList)
if err = t.client.List(ctx, clientObj, append(opts, client.InNamespace(namespace))...); err != nil {
return nil, err
}
return listObj, nil
return clientObj, nil
}
31 changes: 12 additions & 19 deletions kubernetes/client/typed/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
Expand All @@ -16,8 +15,7 @@ import (
type unstructuredTypedClient struct {
client client.Reader
gvk schema.GroupVersionKind
obj runtime.Object
listObj runtime.Object
listGVK schema.GroupVersionKind
}

// NewUnstructuredTypedClient returns a new Client implementation that returns all objects as Unstructured objects.
Expand All @@ -42,27 +40,21 @@ func NewUnstructuredTypedClient(gvk schema.GroupVersionKind, opts ...func(*optio
o.cache = cache
}

obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(gvk)

listObj := &unstructured.UnstructuredList{}
listObj.SetGroupVersionKind(schema.GroupVersionKind{
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind + "List",
})

return &unstructuredTypedClient{
client: o.cache,
gvk: gvk,
obj: obj,
listObj: listObj,
client: o.cache,
gvk: gvk,
listGVK: schema.GroupVersionKind{
Group: gvk.Group,
Version: gvk.Version,
Kind: gvk.Kind + "List",
},
}, nil
}

// Get retrieves an object for the given object key.
func (t *unstructuredTypedClient) Get(ctx context.Context, key types.NamespacedName, opts ...client.GetOption) (client.Object, error) {
obj := t.obj.(client.Object)
obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(t.gvk)
if err := t.client.Get(ctx, key, obj, opts...); err != nil {
return nil, err
}
Expand All @@ -71,7 +63,8 @@ func (t *unstructuredTypedClient) Get(ctx context.Context, key types.NamespacedN

// List retrieves list of objects for a given namespace and list options.
func (t *unstructuredTypedClient) List(ctx context.Context, namespace string, opts ...client.ListOption) (client.ObjectList, error) {
listObj := t.listObj.(client.ObjectList)
listObj := &unstructured.UnstructuredList{}
listObj.SetGroupVersionKind(t.listGVK)
if err := t.client.List(ctx, listObj, append(opts, client.InNamespace(namespace))...); err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/client/typed/unstructured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func TestUnstructuredList(t *testing.T) {
if tt.isErr {
return
}
pods := got.(*unstructured.UnstructuredList)
pods := &corev1.PodList{}
_ = unstructuredutils.ConvertToTyped(got.(*unstructured.UnstructuredList), pods)
if len(pods.Items) != tt.wanted {
t.Errorf("List() = %v, want %v", len(pods.Items), tt.wanted)
}
Expand Down

0 comments on commit 434bbd1

Please sign in to comment.