Skip to content

Commit

Permalink
Update resources plugin to support resource refs with namespace.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Nelson <minelson@vmware.com>
  • Loading branch information
absoludity committed Nov 18, 2021
1 parent cbfc108 commit f6dffc7
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmd/kubeapps-apis/plugins/resources/v1alpha1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ func (s *Server) GetResources(r *v1alpha1.GetResourcesRequest, stream v1alpha1.R
}

if !r.GetWatch() {
resource, err := dynamicClient.Resource(gvr).Namespace(namespace).Get(stream.Context(), ref.GetName(), metav1.GetOptions{})
var resource interface{}
if ref.Namespace != "" {
resource, err = dynamicClient.Resource(gvr).Namespace(ref.Namespace).Get(stream.Context(), ref.GetName(), metav1.GetOptions{})
} else {
resource, err = dynamicClient.Resource(gvr).Get(stream.Context(), ref.GetName(), metav1.GetOptions{})
}
if err != nil {
return status.Errorf(codes.Internal, "unable to get resource referenced by %+v: %s", ref, err.Error())
}
Expand Down
92 changes: 92 additions & 0 deletions cmd/kubeapps-apis/plugins/resources/v1alpha1/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"google.golang.org/grpc/test/bufconn"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -62,6 +63,7 @@ func resourceRefsForObjects(t *testing.T, objects ...runtime.Object) []*pkgsGRPC
ApiVersion: gvk.GroupVersion().String(),
Kind: gvk.Kind,
Name: objMeta.Name,
Namespace: objMeta.Namespace,
})
}
return refs
Expand Down Expand Up @@ -91,6 +93,7 @@ func getResourcesClient(t *testing.T, objects ...runtime.Object) (v1alpha1.Resou
pkgsGRPCv1alpha1.RegisterPackagesServiceServer(s, fakePkgsPluginServer)

scheme := runtime.NewScheme()
t.Logf("loading fake client with objects: %+v", objects)
fakeDynamicClient := dynfake.NewSimpleDynamicClient(
scheme,
objects...,
Expand Down Expand Up @@ -195,13 +198,15 @@ func TestGetResources(t *testing.T) {
ApiVersion: "apps/v1",
Kind: "Deployment",
Name: "some-deployment",
Namespace: "default",
},
},
{
ResourceRef: &pkgsGRPCv1alpha1.ResourceRef{
ApiVersion: "core/v1",
Kind: "Service",
Name: "some-service",
Namespace: "default",
},
},
},
Expand All @@ -222,6 +227,7 @@ func TestGetResources(t *testing.T) {
ApiVersion: "core/v1",
Kind: "Service",
Name: "some-service",
Namespace: "default",
},
},
},
Expand Down Expand Up @@ -254,6 +260,7 @@ func TestGetResources(t *testing.T) {
ApiVersion: "core/v1",
Kind: "Service",
Name: "some-service",
Namespace: "default",
},
},
},
Expand Down Expand Up @@ -306,6 +313,91 @@ func TestGetResources(t *testing.T) {
},
expectedErrorCode: codes.InvalidArgument,
},
{
name: "it gets requested resources from different namespaces when they belong to the installed package",
request: &v1alpha1.GetResourcesRequest{
InstalledPackageRef: &pkgsGRPCv1alpha1.InstalledPackageReference{
Context: &pkgsGRPCv1alpha1.Context{
Cluster: "default",
Namespace: "default",
},
Identifier: "some-package",
Plugin: fakePkgsPlugin,
},
ResourceRefs: []*pkgsGRPCv1alpha1.ResourceRef{
{
ApiVersion: "core/v1",
Kind: "Service",
Name: "some-service",
Namespace: "other-namespace",
},
},
},
clusterObjects: []runtime.Object{
&core.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "core/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "some-service",
Namespace: "other-namespace",
},
},
},
expectedErrorCode: codes.OK,
expectedResources: []*v1alpha1.GetResourcesResponse{
{
ResourceRef: &pkgsGRPCv1alpha1.ResourceRef{
ApiVersion: "core/v1",
Kind: "Service",
Name: "some-service",
Namespace: "other-namespace",
},
},
},
},
{
name: "it gets non-namespaced requested resources when they belong to the installed package",
request: &v1alpha1.GetResourcesRequest{
InstalledPackageRef: &pkgsGRPCv1alpha1.InstalledPackageReference{
Context: &pkgsGRPCv1alpha1.Context{
Cluster: "default",
Namespace: "default",
},
Identifier: "some-package",
Plugin: fakePkgsPlugin,
},
ResourceRefs: []*pkgsGRPCv1alpha1.ResourceRef{
{
ApiVersion: "rbac/v1",
Kind: "ClusterRole",
Name: "some-cluster-role",
},
},
},
clusterObjects: []runtime.Object{
&rbac.ClusterRole{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRole",
APIVersion: "rbac/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "some-cluster-role",
},
},
},
expectedErrorCode: codes.OK,
expectedResources: []*v1alpha1.GetResourcesResponse{
{
ResourceRef: &pkgsGRPCv1alpha1.ResourceRef{
ApiVersion: "rbac/v1",
Kind: "ClusterRole",
Name: "some-cluster-role",
},
},
},
},
// TODO(minelson): test a watch request also. I've spent quite a bit of
// time trying to do so by putting the call to `GetResources` in a go
// routine (and passing the results out via response and error channels)
Expand Down

0 comments on commit f6dffc7

Please sign in to comment.