Skip to content

Commit

Permalink
Merge pull request #10 from alauda/feat/add-dynamic-client-interface
Browse files Browse the repository at this point in the history
Add Get dynamic client interface by GroupKind
  • Loading branch information
jayscodeasy authored Dec 27, 2019
2 parents 1955f1f + 73276e7 commit d338503
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions pkg/client/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package client

import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"strings"
"sync"

"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/klog"

"github.com/juju/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog"
)

type APIGroupMap struct {
Expand Down Expand Up @@ -419,6 +418,24 @@ func (c *KubeClient) DynamicClientForResource(resource string, version string) (
return c.ic.Resource(gvr), nil
}

// DynamicClientForResource get dynamic client for resource
func (c *KubeClient) DynamicClientForGroupKind(gk metav1.GroupKind) (dynamic.NamespaceableResourceInterface, error) {
version, err := c.GetVersionByGroup(gk.Group)
if err != nil {
return nil, err
}
resource, err := c.GetResourceTypeByGroupKind(gk)
if err != nil {
return nil, err
}
gv := schema.GroupVersion{
Group: gk.Group,
Version: version,
}
gvr := gv.WithResource(resource)
return c.ic.Resource(gvr), nil
}

func (c *KubeClient) ClientForGVK(gvk schema.GroupVersionKind) (dynamic.NamespaceableResourceInterface, error) {
resource, err := c.GetResourceTypeByKind(gvk.Kind)
if err != nil {
Expand Down

0 comments on commit d338503

Please sign in to comment.