Skip to content

Commit

Permalink
Add package to discover API resources. (#5213)
Browse files Browse the repository at this point in the history
* Add package to discover API resources.

* Change package name discover -> discovery
  • Loading branch information
tdmanv authored and Ilya Kislenko committed Mar 18, 2019
1 parent 6f456ec commit c40235b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package discovery

import (
"context"

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
)

func AllGVRs(ctx context.Context, cli discovery.DiscoveryInterface) ([]schema.GroupVersionResource, error) {
arls, err := cli.ServerPreferredResources()
if err != nil {
return nil, errors.Wrap(err, "Failed to list APIResources")
}
return apiToGroupVersion(arls)
}

func NamespacedGVRs(ctx context.Context, cli discovery.DiscoveryInterface) ([]schema.GroupVersionResource, error) {
arls, err := cli.ServerPreferredNamespacedResources()
if err != nil {
return nil, errors.Wrap(err, "Failed to list APIResources")
}
return apiToGroupVersion(arls)
}

func apiToGroupVersion(arls []*metav1.APIResourceList) ([]schema.GroupVersionResource, error) {
gvrs := make([]schema.GroupVersionResource, 0, len(arls))
for _, arl := range arls {
gv, err := schema.ParseGroupVersion(arl.GroupVersion)
if err != nil {
return nil, errors.Wrapf(err, "Failed to Parse GroupVersion %s", arl.GroupVersion)
}
for _, ar := range arl.APIResources {
// Although APIResources have Group and Version fields they're empty as of client-go v1.13.1
gvrs = append(gvrs, schema.GroupVersionResource{
Group: gv.Group,
Version: gv.Version,
Resource: ar.Name,
})
}
}
return gvrs, nil
}
40 changes: 40 additions & 0 deletions pkg/discovery/discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package discovery

import (
"context"
"testing"

. "gopkg.in/check.v1"

"github.com/kanisterio/kanister/pkg/kube"
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type DiscoverSuite struct{}

var _ = Suite(&DiscoverSuite{})

func (s *DiscoverSuite) TestDiscover(c *C) {
ctx := context.Background()
cli, err := kube.NewClient()
c.Assert(err, IsNil)
gvrs, err := AllGVRs(ctx, cli.Discovery())
c.Assert(err, IsNil)
c.Assert(gvrs, Not(HasLen), 0)
for _, gvr := range gvrs {
c.Assert(gvr.Empty(), Equals, false)
c.Assert(gvr.Version, Not(Equals), "")
c.Assert(gvr.Resource, Not(Equals), "")
}

gvrs, err = NamespacedGVRs(ctx, cli.Discovery())
c.Assert(err, IsNil)
c.Assert(gvrs, Not(HasLen), 0)
for _, gvr := range gvrs {
c.Assert(gvr.Empty(), Equals, false)
c.Assert(gvr.Version, Not(Equals), "")
c.Assert(gvr.Resource, Not(Equals), "")
}
}

0 comments on commit c40235b

Please sign in to comment.