Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the remaining non-connect error functionality. #6336

Merged
merged 6 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 10 additions & 11 deletions cmd/kubeapps-apis/core/packages/v1alpha1/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/plugins/v1alpha1"

"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
log "k8s.io/klog/v2"
)

Expand Down Expand Up @@ -72,7 +71,7 @@ func (s packagesServer) GetAvailablePackageSummaries(ctx context.Context, reques
var pkgWithOffsets availableSummaryWithOffsets
for pkgWithOffsets = range summariesWithOffsets {
if pkgWithOffsets.err != nil {
return nil, connect.NewError(connect.Code(status.Convert(pkgWithOffsets.err).Code()), pkgWithOffsets.err)
return nil, err
}
pkgs = append(pkgs, pkgWithOffsets.availablePackageSummary)
categories = append(categories, pkgWithOffsets.categories...)
Expand Down Expand Up @@ -122,7 +121,7 @@ func (s packagesServer) GetAvailablePackageDetail(ctx context.Context, request *
// Get the response from the requested plugin
response, err := pluginWithServer.server.GetAvailablePackageDetail(ctx, request)
if err != nil {
return nil, connect.NewError(connect.Code(status.Convert(err).Code()), fmt.Errorf("Unable to get the available package detail for the package %q using the plugin %q: %w", request.Msg.AvailablePackageRef.Identifier, request.Msg.AvailablePackageRef.Plugin.Name, err))
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to get the available package detail for the package %q using the plugin %q: %w", request.Msg.AvailablePackageRef.Identifier, request.Msg.AvailablePackageRef.Plugin.Name, err))
}

// Validate the plugin response
Expand Down Expand Up @@ -150,7 +149,7 @@ func (s packagesServer) GetInstalledPackageSummaries(ctx context.Context, reques
var pkgWithOffsets installedSummaryWithOffsets
for pkgWithOffsets = range summariesWithOffsets {
if pkgWithOffsets.err != nil {
return nil, connect.NewError(connect.Code(status.Code(pkgWithOffsets.err)), pkgWithOffsets.err)
return nil, pkgWithOffsets.err
}
pkgs = append(pkgs, pkgWithOffsets.installedPackageSummary)
if pageSize > 0 && len(pkgs) >= int(pageSize) {
Expand Down Expand Up @@ -195,7 +194,7 @@ func (s packagesServer) GetInstalledPackageDetail(ctx context.Context, request *
// Get the response from the requested plugin
response, err := pluginWithServer.server.GetInstalledPackageDetail(ctx, request)
if err != nil {
return nil, connect.NewError(connect.Code(status.Convert(err).Code()), fmt.Errorf("Unable to get the installed package detail for the package %q using the plugin %q: %w", request.Msg.InstalledPackageRef.Identifier, request.Msg.InstalledPackageRef.Plugin.Name, err))
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to get the installed package detail for the package %q using the plugin %q: %w", request.Msg.InstalledPackageRef.Identifier, request.Msg.InstalledPackageRef.Plugin.Name, err))
}

// Validate the plugin response
Expand Down Expand Up @@ -227,7 +226,7 @@ func (s packagesServer) GetAvailablePackageVersions(ctx context.Context, request
ctxForPlugin := updateContextWithAuthz(ctx, request.Header())
response, err := pluginWithServer.server.GetAvailablePackageVersions(ctxForPlugin, request)
if err != nil {
return nil, connect.NewError(connect.Code(status.Convert(err).Code()), fmt.Errorf("Unable to get the available package versions for the package %q using the plugin %q: %w", request.Msg.AvailablePackageRef.Identifier, request.Msg.AvailablePackageRef.Plugin.Name, err))
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to get the available package versions for the package %q using the plugin %q: %w", request.Msg.AvailablePackageRef.Identifier, request.Msg.AvailablePackageRef.Plugin.Name, err))
}

// Validate the plugin response
Expand Down Expand Up @@ -264,8 +263,8 @@ func (s *packagesServer) GetInstalledPackageResourceRefs(ctx context.Context, re
if err != nil {
log.Errorf("Unable to get the resource refs for the package %q using the plugin %q: %v", request.Msg.InstalledPackageRef.Identifier, request.Msg.InstalledPackageRef.Plugin.Name, err)

errCode := connect.Code(status.Convert(err).Code())
connectError := connect.NewError(connect.Code(status.Convert(err).Code()), fmt.Errorf("Unable to get the resource refs for the package %q using the plugin %q: %v", request.Msg.InstalledPackageRef.Identifier, request.Msg.InstalledPackageRef.Plugin.Name, errCode))
errCode := connect.CodeOf(err)
connectError := connect.NewError(errCode, fmt.Errorf("Unable to get the resource refs for the package %q using the plugin %q: %v", request.Msg.InstalledPackageRef.Identifier, request.Msg.InstalledPackageRef.Plugin.Name, errCode))

// Plugins are still using gRPC here, not connect:
return nil, connect.NewError(errCode, connectError)
Expand All @@ -292,7 +291,7 @@ func (s packagesServer) CreateInstalledPackage(ctx context.Context, request *con
ctxForPlugin := updateContextWithAuthz(ctx, request.Header())
response, err := pluginWithServer.server.CreateInstalledPackage(ctxForPlugin, request)
if err != nil {
return nil, connect.NewError(connect.Code(status.Convert(err).Code()), fmt.Errorf("Unable to create the installed package for the package %q using the plugin %q: %w", request.Msg.AvailablePackageRef.Identifier, request.Msg.AvailablePackageRef.Plugin.Name, err))
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to create the installed package for the package %q using the plugin %q: %w", request.Msg.AvailablePackageRef.Identifier, request.Msg.AvailablePackageRef.Plugin.Name, err))
}

// Validate the plugin response
Expand Down Expand Up @@ -321,7 +320,7 @@ func (s packagesServer) UpdateInstalledPackage(ctx context.Context, request *con
ctxForPlugin := updateContextWithAuthz(ctx, request.Header())
response, err := pluginWithServer.server.UpdateInstalledPackage(ctxForPlugin, request)
if err != nil {
return nil, connect.NewError(connect.Code(status.Convert(err).Code()), fmt.Errorf("Unable to update the installed package for the package %q using the plugin %q: %w", request.Msg.InstalledPackageRef.Identifier, request.Msg.InstalledPackageRef.Plugin.Name, err))
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to update the installed package for the package %q using the plugin %q: %w", request.Msg.InstalledPackageRef.Identifier, request.Msg.InstalledPackageRef.Plugin.Name, err))
}

// Validate the plugin response
Expand Down Expand Up @@ -350,7 +349,7 @@ func (s packagesServer) DeleteInstalledPackage(ctx context.Context, request *con
ctxForPlugin := updateContextWithAuthz(ctx, request.Header())
response, err := pluginWithServer.server.DeleteInstalledPackage(ctxForPlugin, request)
if err != nil {
return nil, connect.NewError(connect.Code(status.Convert(err).Code()), fmt.Errorf("Unable to delete the installed packagefor the package %q using the plugin %q: %w", request.Msg.InstalledPackageRef.Identifier, request.Msg.InstalledPackageRef.Plugin.Name, err))
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to delete the installed packagefor the package %q using the plugin %q: %w", request.Msg.InstalledPackageRef.Identifier, request.Msg.InstalledPackageRef.Plugin.Name, err))
}

return response, nil
Expand Down
26 changes: 12 additions & 14 deletions cmd/kubeapps-apis/core/packages/v1alpha1/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
corev1 "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1"
plugins "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/plugins/v1alpha1"
"github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/plugin_test"

"google.golang.org/grpc/codes"
)

const (
Expand All @@ -24,7 +22,7 @@ const (
var mockedPackagingPlugin1 = makeDefaultTestPackagingPlugin("mock1")
var mockedPackagingPlugin2 = makeDefaultTestPackagingPlugin("mock2")
var mockedPackagingPlugin3 = makeDefaultTestPackagingPlugin("mock2")
var mockedNotFoundPackagingPlugin = makeOnlyStatusTestPackagingPlugin("bad-plugin", codes.NotFound)
var mockedNotFoundPackagingPlugin = makeOnlyStatusTestPackagingPlugin("bad-plugin", connect.CodeNotFound)

var ignoreUnexportedOpts = cmpopts.IgnoreUnexported(
corev1.AvailablePackageDetail{},
Expand Down Expand Up @@ -77,11 +75,11 @@ func makeDefaultTestPackagingPlugin(pluginName string) pkgPluginWithServer {
}
}

func makeOnlyStatusTestPackagingPlugin(pluginName string, statusCode codes.Code) pkgPluginWithServer {
func makeOnlyStatusTestPackagingPlugin(pluginName string, errorCode connect.Code) pkgPluginWithServer {
pluginDetails := &plugins.Plugin{Name: pluginName, Version: "v1alpha1"}
packagingPluginServer := &plugin_test.TestPackagingPluginServer{Plugin: pluginDetails}

packagingPluginServer.Status = statusCode
packagingPluginServer.ErrorCode = errorCode

return pkgPluginWithServer{
plugin: pluginDetails,
Expand All @@ -93,7 +91,7 @@ func TestGetAvailablePackageSummaries(t *testing.T) {
testCases := []struct {
name string
configuredPlugins []pkgPluginWithServer
statusCode connect.Code
errorCode connect.Code
request *corev1.GetAvailablePackageSummariesRequest
expectedResponse *corev1.GetAvailablePackageSummariesResponse
}{
Expand Down Expand Up @@ -275,7 +273,7 @@ func TestGetAvailablePackageSummaries(t *testing.T) {
AvailablePackageSummaries: []*corev1.AvailablePackageSummary{},
Categories: []string{""},
},
statusCode: connect.CodeNotFound,
errorCode: connect.CodeNotFound,
},
}

Expand All @@ -286,14 +284,14 @@ func TestGetAvailablePackageSummaries(t *testing.T) {
}
availablePackageSummaries, err := server.GetAvailablePackageSummaries(context.Background(), connect.NewRequest(tc.request))

if got, want := connect.CodeOf(err), tc.statusCode; err != nil && got != want {
if got, want := connect.CodeOf(err), tc.errorCode; err != nil && got != want {
t.Fatalf("got: %+v, want: %+v, err: %+v", got, want, err)
}
if tc.statusCode != 0 {
if tc.errorCode != 0 {
return
}

if tc.statusCode == 0 {
if tc.errorCode == 0 {
if got, want := availablePackageSummaries.Msg, tc.expectedResponse; !cmp.Equal(got, want, ignoreUnexportedOpts) {
t.Errorf("mismatch (-want +got):\n%s", cmp.Diff(want, got, ignoreUnexportedOpts))
}
Expand All @@ -306,7 +304,7 @@ func TestGetAvailablePackageDetail(t *testing.T) {
testCases := []struct {
name string
configuredPlugins []pkgPluginWithServer
statusCode connect.Code
errorCode connect.Code
request *corev1.GetAvailablePackageDetailRequest
expectedResponse *corev1.GetAvailablePackageDetailResponse
}{
Expand Down Expand Up @@ -351,7 +349,7 @@ func TestGetAvailablePackageDetail(t *testing.T) {
},

expectedResponse: &corev1.GetAvailablePackageDetailResponse{},
statusCode: connect.CodeNotFound,
errorCode: connect.CodeNotFound,
},
}

Expand All @@ -362,11 +360,11 @@ func TestGetAvailablePackageDetail(t *testing.T) {
}
availablePackageDetail, err := server.GetAvailablePackageDetail(context.Background(), connect.NewRequest(tc.request))

if got, want := connect.CodeOf(err), tc.statusCode; err != nil && got != want {
if got, want := connect.CodeOf(err), tc.errorCode; err != nil && got != want {
t.Fatalf("got: %+v, want: %+v, err: %+v", got, want, err)
}

if tc.statusCode == 0 {
if tc.errorCode == 0 {
if got, want := availablePackageDetail.Msg, tc.expectedResponse; !cmp.Equal(got, want, ignoreUnexportedOpts) {
t.Errorf("mismatch (-want +got):\n%s", cmp.Diff(want, got, ignoreUnexportedOpts))
}
Expand Down
38 changes: 18 additions & 20 deletions cmd/kubeapps-apis/core/packages/v1alpha1/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
packagesconnect "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1/v1alpha1connect"
"github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/plugins/v1alpha1"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
log "k8s.io/klog/v2"
)

Expand Down Expand Up @@ -61,24 +59,24 @@ func (s repositoriesServer) AddPackageRepository(ctx context.Context, request *c
log.InfoS("+core AddPackageRepository", "name", request.Msg.GetName(), "cluster", request.Msg.GetContext().GetCluster(), "namespace", request.Msg.GetContext().GetNamespace())

if request.Msg.GetPlugin() == nil {
return nil, status.Errorf(codes.InvalidArgument, "Unable to retrieve the plugin (missing request.Plugin)")
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Unable to retrieve the plugin (missing request.Plugin)"))
}

// Retrieve the plugin with server matching the requested plugin name
pluginWithServer := s.getPluginWithServer(request.Msg.Plugin)
if pluginWithServer == nil {
return nil, status.Errorf(codes.Internal, "Unable to get the plugin %v", request.Msg.Plugin)
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to get the plugin %v", request.Msg.Plugin))
}

// Get the response from the requested plugin
response, err := pluginWithServer.server.AddPackageRepository(ctx, request)
if err != nil {
return nil, status.Errorf(status.Convert(err).Code(), "Unable to add package repository %q using the plugin %q: %v", request.Msg.Name, request.Msg.Plugin.Name, err)
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to add package repository %q using the plugin %q: %w", request.Msg.Name, request.Msg.Plugin.Name, err))
}

// Validate the plugin response
if response.Msg.PackageRepoRef == nil {
return nil, status.Errorf(codes.Internal, "Invalid AddPackageRepository response from the plugin %v: %v", pluginWithServer.plugin.Name, err)
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Invalid AddPackageRepository response from the plugin %v: %w", pluginWithServer.plugin.Name, err))
}

return response, nil
Expand All @@ -88,24 +86,24 @@ func (s repositoriesServer) GetPackageRepositoryDetail(ctx context.Context, requ
log.InfoS("+core GetPackageRepositoryDetail", "identifier", request.Msg.GetPackageRepoRef().GetIdentifier(), "cluster", request.Msg.GetPackageRepoRef().GetContext().GetCluster(), "namespace", request.Msg.GetPackageRepoRef().GetContext().GetNamespace())

if request.Msg.GetPackageRepoRef().GetPlugin() == nil {
return nil, status.Errorf(codes.InvalidArgument, "Unable to retrieve the plugin (missing PackageRepoRef.Plugin)")
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Unable to retrieve the plugin (missing PackageRepoRef.Plugin)"))
}

// Retrieve the plugin with server matching the requested plugin name
pluginWithServer := s.getPluginWithServer(request.Msg.PackageRepoRef.Plugin)
if pluginWithServer == nil {
return nil, status.Errorf(codes.Internal, "Unable to get the plugin %v", request.Msg.PackageRepoRef.Plugin)
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to get the plugin %v", request.Msg.PackageRepoRef.Plugin))
}

// Get the response from the requested plugin
response, err := pluginWithServer.server.GetPackageRepositoryDetail(ctx, request)
if err != nil {
return nil, status.Errorf(status.Convert(err).Code(), "Unable to get the package repository detail for the repository %q using the plugin %q: %v", request.Msg.PackageRepoRef.Identifier, request.Msg.PackageRepoRef.Plugin.Name, err)
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to get the package repository detail for the repository %q using the plugin %q: %w", request.Msg.PackageRepoRef.Identifier, request.Msg.PackageRepoRef.Plugin.Name, err))
}

// Validate the plugin response
if response.Msg.GetDetail().GetPackageRepoRef() == nil {
return nil, status.Errorf(codes.Internal, "Invalid package reposirtory detail response from the plugin %v: %v", pluginWithServer.plugin.Name, err)
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Invalid package reposirtory detail response from the plugin %v: %w", pluginWithServer.plugin.Name, err))
}

// Build the response
Expand All @@ -124,7 +122,7 @@ func (s repositoriesServer) GetPackageRepositorySummaries(ctx context.Context, r
for _, p := range s.pluginsWithServers {
response, err := p.server.GetPackageRepositorySummaries(ctx, request)
if err != nil {
return nil, status.Errorf(status.Convert(err).Code(), "Invalid GetPackageRepositorySummaries response from the plugin %v: %v", p.plugin.Name, err)
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Invalid GetPackageRepositorySummaries response from the plugin %v: %w", p.plugin.Name, err))
}
if response == nil {
log.Infof("core GetPackageRepositorySummaries received nil response from plugin %s / %s", p.plugin.GetName(), p.plugin.GetVersion())
Expand Down Expand Up @@ -159,25 +157,25 @@ func (s repositoriesServer) UpdatePackageRepository(ctx context.Context, request
log.InfoS("+core UpdatePackageRepository", "cluster", request.Msg.GetPackageRepoRef().GetContext().GetCluster(), "namespace", request.Msg.GetPackageRepoRef().GetContext().GetNamespace(), "id", request.Msg.GetPackageRepoRef().GetIdentifier())

if request.Msg.GetPackageRepoRef().GetPlugin() == nil {
return nil, status.Errorf(codes.InvalidArgument, "Unable to retrieve the plugin (missing PackageRepoRef.Plugin)")
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Unable to retrieve the plugin (missing PackageRepoRef.Plugin)"))
}

// Retrieve the plugin with server matching the requested plugin name
pluginWithServer := s.getPluginWithServer(request.Msg.PackageRepoRef.Plugin)
if pluginWithServer == nil {
return nil, status.Errorf(codes.Internal, "Unable to get the plugin %v", request.Msg.PackageRepoRef.Plugin)
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to get the plugin %v", request.Msg.PackageRepoRef.Plugin))
}

// Get the response from the requested plugin
response, err := pluginWithServer.server.UpdatePackageRepository(ctx, request)
if err != nil {
return nil, status.Errorf(status.Convert(err).Code(), "Unable to update the package repository %q using the plugin %q: %v",
request.Msg.PackageRepoRef.Identifier, request.Msg.PackageRepoRef.Plugin.Name, err)
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to update the package repository %q using the plugin %q: %w",
request.Msg.PackageRepoRef.Identifier, request.Msg.PackageRepoRef.Plugin.Name, err))
}

// Validate the plugin response
if response.Msg.PackageRepoRef == nil {
return nil, status.Errorf(codes.Internal, "Invalid UpdatePackageRepository response from the plugin %v: %v", pluginWithServer.plugin.Name, err)
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Invalid UpdatePackageRepository response from the plugin %v: %w", pluginWithServer.plugin.Name, err))
}

return response, nil
Expand All @@ -188,20 +186,20 @@ func (s repositoriesServer) DeletePackageRepository(ctx context.Context, request
log.InfoS("+core DeletePackageRepository", "cluster", request.Msg.GetPackageRepoRef().GetContext().GetCluster(), "namespace", request.Msg.GetPackageRepoRef().GetContext().GetNamespace(), "id", request.Msg.GetPackageRepoRef().GetIdentifier())

if request.Msg.GetPackageRepoRef().GetPlugin() == nil {
return nil, status.Errorf(codes.InvalidArgument, "Unable to retrieve the plugin (missing PackageRepoRef.Plugin)")
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Unable to retrieve the plugin (missing PackageRepoRef.Plugin)"))
}

// Retrieve the plugin with server matching the requested plugin name
pluginWithServer := s.getPluginWithServer(request.Msg.PackageRepoRef.Plugin)
if pluginWithServer == nil {
return nil, status.Errorf(codes.Internal, "Unable to get the plugin %v", request.Msg.PackageRepoRef.Plugin)
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to get the plugin %v", request.Msg.PackageRepoRef.Plugin))
}

// Get the response from the requested plugin
response, err := pluginWithServer.server.DeletePackageRepository(ctx, request)
if err != nil {
return nil, status.Errorf(status.Convert(err).Code(), "Unable to delete the package repository %q using the plugin %q: %v",
request.Msg.PackageRepoRef.Identifier, request.Msg.PackageRepoRef.Plugin.Name, err)
return nil, connect.NewError(connect.CodeOf(err), fmt.Errorf("Unable to delete the package repository %q using the plugin %q: %w",
request.Msg.PackageRepoRef.Identifier, request.Msg.PackageRepoRef.Plugin.Name, err))
}

return response, nil
Expand Down