Skip to content

Commit

Permalink
Implement GetResources for core packages API.
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 4, 2021
1 parent eebef53 commit 5f94523
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/kubeapps-apis/plugin_test/mock_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type TestPackagingPluginServer struct {
InstalledPackageSummaries []*corev1.InstalledPackageSummary
InstalledPackageDetail *corev1.InstalledPackageDetail
PackageAppVersions []*corev1.PackageAppVersion
ResourceRefs []*corev1.ResourceRef
Categories []string
NextPageToken string
Status codes.Code
Expand Down Expand Up @@ -94,6 +95,17 @@ func (s TestPackagingPluginServer) GetAvailablePackageVersions(ctx context.Conte
}, nil
}

// GetResourceRefs returns the resource references based on the request.
func (s TestPackagingPluginServer) GetResourceRefs(ctx context.Context, request *packages.GetResourceRefsRequest) (*packages.GetResourceRefsResponse, error) {
if s.Status != codes.OK {
return nil, status.Errorf(s.Status, "Non-OK response")
}
return &packages.GetResourceRefsResponse{
Context: request.GetInstalledPackageRef().GetContext(),
ResourceRefs: s.ResourceRefs,
}, nil
}

func (s TestPackagingPluginServer) CreateInstalledPackage(ctx context.Context, request *packages.CreateInstalledPackageRequest) (*packages.CreateInstalledPackageResponse, error) {
if s.Status != codes.OK {
return nil, status.Errorf(s.Status, "Non-OK response")
Expand Down
27 changes: 27 additions & 0 deletions cmd/kubeapps-apis/server/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,33 @@ func (s packagesServer) GetAvailablePackageVersions(ctx context.Context, request
}, nil
}

// GetResourceRefs returns the references for the Kubernetes resources created by
// an installed package.
func (s *packagesServer) GetResourceRefs(ctx context.Context, request *packages.GetResourceRefsRequest) (*packages.GetResourceRefsResponse, error) {
pkgRef := request.GetInstalledPackageRef()
contextMsg := fmt.Sprintf("(cluster=%q, namespace=%q)", pkgRef.GetContext().GetCluster(), pkgRef.GetContext().GetNamespace())
identifier := pkgRef.GetIdentifier()
log.Infof("+core GetResources %s %s", contextMsg, identifier)

if request.GetInstalledPackageRef().GetPlugin() == nil {
return nil, status.Errorf(codes.InvalidArgument, "Unable to retrieve the plugin (missing InstalledPackageRef.Plugin)")
}

// Retrieve the plugin with server matching the requested plugin name
pluginWithServer := s.getPluginWithServer(request.InstalledPackageRef.Plugin)
if pluginWithServer == nil {
return nil, status.Errorf(codes.InvalidArgument, "Unable to retrieve the plugin %v", request.InstalledPackageRef.Plugin)
}

// Get the response from the requested plugin
response, err := pluginWithServer.server.GetResourceRefs(ctx, request)
if err != nil {
return nil, status.Errorf(status.Convert(err).Code(), "Unable to get the resource refs for the package %q using the plugin %q: %v", request.InstalledPackageRef.Identifier, request.InstalledPackageRef.Plugin.Name, err)
}

return response, nil
}

// CreateInstalledPackage creates an installed package using configured plugins.
func (s packagesServer) CreateInstalledPackage(ctx context.Context, request *packages.CreateInstalledPackageRequest) (*packages.CreateInstalledPackageResponse, error) {
contextMsg := fmt.Sprintf("(cluster=%q, namespace=%q)", request.GetTargetContext().GetCluster(), request.GetTargetContext().GetNamespace())
Expand Down
93 changes: 93 additions & 0 deletions cmd/kubeapps-apis/server/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
corev1 "github.com/kubeapps/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1"
plugins "github.com/kubeapps/kubeapps/cmd/kubeapps-apis/gen/core/plugins/v1alpha1"
"github.com/kubeapps/kubeapps/cmd/kubeapps-apis/plugin_test"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand All @@ -41,6 +42,7 @@ var ignoreUnexportedOpts = cmpopts.IgnoreUnexported(
corev1.GetAvailablePackageDetailResponse{},
corev1.GetAvailablePackageSummariesResponse{},
corev1.GetAvailablePackageVersionsResponse{},
corev1.GetResourceRefsResponse{},
corev1.GetInstalledPackageDetailResponse{},
corev1.GetInstalledPackageSummariesResponse{},
corev1.CreateInstalledPackageResponse{},
Expand All @@ -52,6 +54,7 @@ var ignoreUnexportedOpts = cmpopts.IgnoreUnexported(
corev1.Maintainer{},
corev1.PackageAppVersion{},
corev1.VersionReference{},
corev1.ResourceRef{},
plugins.Plugin{},
)

Expand Down Expand Up @@ -819,3 +822,93 @@ func TestDeleteInstalledPackage(t *testing.T) {
})
}
}

func TestGetResourceRefs(t *testing.T) {
installedPlugin := &plugins.Plugin{Name: "plugin-1", Version: "v1alpha1"}

testCases := []struct {
name string
statusCode codes.Code
pluginResourceRefs []*corev1.ResourceRef
request *corev1.GetResourceRefsRequest
expectedResponse *corev1.GetResourceRefsResponse
}{
{
name: "it should successfully call the plugins GetResourceRefs endpoint",
pluginResourceRefs: []*corev1.ResourceRef{
{
Version: "apps/v1",
Kind: "Deployment",
Name: "some-deployment",
},
},
request: &corev1.GetResourceRefsRequest{
InstalledPackageRef: &corev1.InstalledPackageReference{
Context: &corev1.Context{Cluster: "default", Namespace: "my-ns"},
Identifier: "installed-pkg-1",
Plugin: installedPlugin,
},
},
expectedResponse: &corev1.GetResourceRefsResponse{
Context: &corev1.Context{Cluster: "default", Namespace: "my-ns"},
ResourceRefs: []*corev1.ResourceRef{
{
Version: "apps/v1",
Kind: "Deployment",
Name: "some-deployment",
},
},
},
statusCode: codes.OK,
},
{
name: "it should return an invalid argument if the plugin is not specified",
request: &corev1.GetResourceRefsRequest{
InstalledPackageRef: &corev1.InstalledPackageReference{
Context: &corev1.Context{Cluster: "default", Namespace: "my-ns"},
Identifier: "installed-pkg-1",
},
},
statusCode: codes.InvalidArgument,
},
{
name: "it should return an invalid argument if the plugin cannot be found",
request: &corev1.GetResourceRefsRequest{
InstalledPackageRef: &corev1.InstalledPackageReference{
Context: &corev1.Context{Cluster: "default", Namespace: "my-ns"},
Identifier: "installed-pkg-1",
Plugin: &plugins.Plugin{Name: "other-plugin.packages", Version: "v1alpha1"},
},
},
statusCode: codes.InvalidArgument,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
server := &packagesServer{
plugins: []*pkgsPluginWithServer{
{
plugin: installedPlugin,
server: &plugin_test.TestPackagingPluginServer{
Plugin: installedPlugin,
ResourceRefs: tc.pluginResourceRefs,
},
},
},
}

resourceRefs, err := server.GetResourceRefs(context.Background(), tc.request)

if got, want := status.Code(err), tc.statusCode; got != want {
t.Fatalf("got: %+v, want: %+v, err: %+v", got, want, err)
}

if tc.statusCode == codes.OK {
if got, want := resourceRefs, tc.expectedResponse; !cmp.Equal(got, want, ignoreUnexportedOpts) {
t.Errorf("mismatch (-want +got):\n%s", cmp.Diff(want, got, ignoreUnexportedOpts))
}
}
})
}
}

0 comments on commit 5f94523

Please sign in to comment.