diff --git a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/getresourcerefs_test.go b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/getresourcerefs_test.go index 64e175fc8ca..214f0929701 100644 --- a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/getresourcerefs_test.go +++ b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/getresourcerefs_test.go @@ -13,8 +13,6 @@ import ( "github.com/vmware-tanzu/kubeapps/cmd/apprepository-controller/pkg/apis/apprepository/v1alpha1" corev1 "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1" "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/plugins/pkg/resourcerefs/resourcerefstest" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -26,17 +24,17 @@ func TestGetInstalledPackageResourceRefs(t *testing.T) { } type testCase struct { - baseTestCase resourcerefstest.TestCase - request *corev1.GetInstalledPackageResourceRefsRequest - expectedResponse *corev1.GetInstalledPackageResourceRefsResponse - expectedStatusCode codes.Code + baseTestCase resourcerefstest.TestCase + request *corev1.GetInstalledPackageResourceRefsRequest + expectedResponse *corev1.GetInstalledPackageResourceRefsResponse + expectedErrorCode connect.Code } // newTestCase is a function to take an existing test-case // (a so-called baseTestCase in pkg/resourcerefs module, which contains a LOT of useful data) // and "enrich" it with some new fields to create a different kind of test case // that tests server.GetInstalledPackageResourceRefs() func - newTestCase := func(tc int, identifier string, response bool, code codes.Code) testCase { + newTestCase := func(tc int, identifier string, response bool, code connect.Code) testCase { newCase := testCase{ baseTestCase: resourcerefstest.TestCases1[tc], request: &corev1.GetInstalledPackageResourceRefsRequest{ @@ -58,25 +56,25 @@ func TestGetInstalledPackageResourceRefs(t *testing.T) { ResourceRefs: resourcerefstest.TestCases1[tc].ExpectedResourceRefs, } } - newCase.expectedStatusCode = code + newCase.expectedErrorCode = code return newCase } testCases := []testCase{ - newTestCase(0, "my-apache", true, codes.OK), - newTestCase(1, "my-apache", true, codes.OK), - newTestCase(2, "my-apache", true, codes.OK), - newTestCase(3, "my-apache", true, codes.OK), - newTestCase(4, "my-iis", false, codes.NotFound), - newTestCase(5, "my-apache", false, codes.Internal), + newTestCase(0, "my-apache", true, 0), + newTestCase(1, "my-apache", true, 0), + newTestCase(2, "my-apache", true, 0), + newTestCase(3, "my-apache", true, 0), + newTestCase(4, "my-iis", false, connect.CodeNotFound), + newTestCase(5, "my-apache", false, connect.CodeInternal), // See https://github.com/vmware-tanzu/kubeapps/issues/632 - newTestCase(6, "my-apache", true, codes.OK), - newTestCase(7, "my-apache", true, codes.OK), - newTestCase(8, "my-apache", true, codes.OK), + newTestCase(6, "my-apache", true, 0), + newTestCase(7, "my-apache", true, 0), + newTestCase(8, "my-apache", true, 0), // See https://kubernetes.io/docs/reference/kubernetes-api/authorization-resources/role-v1/#RoleList - newTestCase(9, "my-apache", true, codes.OK), + newTestCase(9, "my-apache", true, 0), // See https://kubernetes.io/docs/reference/kubernetes-api/authorization-resources/cluster-role-v1/#ClusterRoleList - newTestCase(10, "my-apache", true, codes.OK), + newTestCase(10, "my-apache", true, 0), } ignoredFields := cmpopts.IgnoreUnexported( @@ -112,10 +110,10 @@ func TestGetInstalledPackageResourceRefs(t *testing.T) { response, err := server.GetInstalledPackageResourceRefs(context.Background(), connect.NewRequest(tc.request)) - if got, want := status.Code(err), tc.expectedStatusCode; got != want { + if got, want := connect.CodeOf(err), tc.expectedErrorCode; err != nil && got != want { t.Fatalf("got: %+v, want: %+v, err: %+v", got, want, err) } - if tc.expectedStatusCode != codes.OK { + if tc.expectedErrorCode != 0 { return } diff --git a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories.go b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories.go index a9f49d881a5..569cba22513 100644 --- a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories.go +++ b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories.go @@ -17,8 +17,6 @@ import ( "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/plugins/pkg/connecterror" "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/plugins/pkg/resources" "github.com/vmware-tanzu/kubeapps/pkg/helm" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/anypb" k8scorev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -654,7 +652,7 @@ func (s *Server) GetPackageRepositoryPermissions(ctx context.Context, request *c cluster := request.Msg.GetContext().GetCluster() namespace := request.Msg.GetContext().GetNamespace() if cluster == "" && namespace != "" { - return nil, status.Errorf(codes.InvalidArgument, "cluster must be specified when namespace is present: %s", namespace) + return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("cluster must be specified when namespace is present: %s", namespace)) } typedClient, err := s.clientGetter.Typed(request.Header(), cluster) if err != nil { diff --git a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_auth.go b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_auth.go index 11efb0873fa..49002047255 100644 --- a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_auth.go +++ b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_auth.go @@ -17,8 +17,6 @@ import ( corev1 "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1" "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/plugins/helm/packages/v1alpha1" "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/plugins/pkg/connecterror" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" k8scorev1 "k8s.io/api/core/v1" k8sErrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -114,13 +112,13 @@ func handleAuthSecretForUpdate( // if we have both ref config and data config, it is an invalid mixed configuration if (hasCaRef || hasAuthRef) && (hasCaData || hasAuthData) { - return nil, false, false, status.Errorf(codes.InvalidArgument, "Package repository cannot mix referenced secrets and user provided secret data") + return nil, false, false, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Package repository cannot mix referenced secrets and user provided secret data")) } // check we cannot change mode (per design spec) if secret != nil && (hasCaRef || hasCaData || hasAuthRef || hasAuthData) { if isAuthSecretKubeappsManaged(repo, secret) != (hasAuthData || hasCaData) { - return nil, false, false, status.Errorf(codes.InvalidArgument, "Auth management mode cannot be changed") + return nil, false, false, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Auth management mode cannot be changed")) } } @@ -181,7 +179,7 @@ func handleImagesPullSecretForUpdate( // check we are not changing mode if secret != nil && (hasRef || hasData) { if isImagesPullSecretKubeappsManaged(repo, secret) != hasData { - return nil, false, false, status.Errorf(codes.InvalidArgument, "Auth management mode cannot be changed") + return nil, false, false, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("Auth management mode cannot be changed")) } } @@ -591,8 +589,8 @@ func validateUserManagedRepoSecret( var secretRef string if secretRefTls != "" && secretRefAuth != "" && secretRefTls != secretRefAuth { - return nil, status.Errorf( - codes.InvalidArgument, "TLS config secret and Auth secret must be the same") + return nil, connect.NewError( + connect.CodeInvalidArgument, fmt.Errorf("TLS config secret and Auth secret must be the same")) } else if secretRefTls != "" { secretRef = secretRefTls } else if secretRefAuth != "" { diff --git a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_test.go b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_test.go index bfca404c92e..15bdf06fde5 100644 --- a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_test.go +++ b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_test.go @@ -21,8 +21,6 @@ import ( plugins "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/plugins/v1alpha1" "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/plugins/helm/packages/v1alpha1" "github.com/vmware-tanzu/kubeapps/pkg/helm" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/anypb" authorizationv1 "k8s.io/api/authorization/v1" apiv1 "k8s.io/api/core/v1" @@ -893,7 +891,7 @@ func TestGetPackageRepositorySummaries(t *testing.T) { request *corev1.GetPackageRepositorySummariesRequest existingRepos []k8sruntime.Object existingNamespaces []*apiv1.Namespace - expectedStatusCode codes.Code + expectedErrorCode connect.Code expectedResponse *corev1.GetPackageRepositorySummariesResponse reactors []*ClientReaction }{ @@ -906,7 +904,6 @@ func TestGetPackageRepositorySummaries(t *testing.T) { &repo1b, &repo2b, }, - expectedStatusCode: codes.OK, expectedResponse: &corev1.GetPackageRepositorySummariesResponse{ PackageRepositorySummaries: []*corev1.PackageRepositorySummary{ repo1Summary, @@ -975,7 +972,6 @@ func TestGetPackageRepositorySummaries(t *testing.T) { }, }, }, - expectedStatusCode: codes.OK, expectedResponse: &corev1.GetPackageRepositorySummariesResponse{ PackageRepositorySummaries: []*corev1.PackageRepositorySummary{ { @@ -1000,7 +996,6 @@ func TestGetPackageRepositorySummaries(t *testing.T) { &repo1b, &repo2b, }, - expectedStatusCode: codes.OK, expectedResponse: &corev1.GetPackageRepositorySummariesResponse{ PackageRepositorySummaries: []*corev1.PackageRepositorySummary{ repo2Summary, @@ -1015,7 +1010,6 @@ func TestGetPackageRepositorySummaries(t *testing.T) { existingRepos: []k8sruntime.Object{ &repo3, }, - expectedStatusCode: codes.OK, expectedResponse: &corev1.GetPackageRepositorySummariesResponse{ PackageRepositorySummaries: []*corev1.PackageRepositorySummary{ repo3Summary, @@ -1045,12 +1039,12 @@ func TestGetPackageRepositorySummaries(t *testing.T) { response, err := s.GetPackageRepositorySummaries(context.Background(), connect.NewRequest(tc.request)) - if got, want := status.Code(err), tc.expectedStatusCode; got != want { + if got, want := connect.CodeOf(err), tc.expectedErrorCode; err != nil && got != want { t.Fatalf("got: %+v, want: %+v, err: %+v", got, want, err) } // We don't need to check anything else for non-OK codes. - if tc.expectedStatusCode != codes.OK { + if tc.expectedErrorCode != 0 { return } @@ -1114,20 +1108,19 @@ func TestGetPackageRepositoryDetail(t *testing.T) { request *corev1.GetPackageRepositoryDetailRequest repositoryCustomizer func(repository *appRepov1alpha1.AppRepository) *appRepov1alpha1.AppRepository expectedResponse *corev1.GetPackageRepositoryDetailResponse - expectedStatusCode codes.Code + expectedErrorCode connect.Code existingSecret *apiv1.Secret }{ { - name: "not found", - request: buildRequest("ns-1", "foo"), - expectedStatusCode: codes.NotFound, + name: "not found", + request: buildRequest("ns-1", "foo"), + expectedErrorCode: connect.CodeNotFound, }, { name: "check ref", request: buildRequest("ns-1", "repo-1"), expectedResponse: buildResponse("ns-1", "repo-1", "helm", "https://test-repo", "description 1", nil, nil, nil), - expectedStatusCode: codes.OK, }, { name: "check values with auth", @@ -1145,8 +1138,7 @@ func TestGetPackageRepositoryDetail(t *testing.T) { }, nil, nil), - existingSecret: newBasicAuthSecret(helm.SecretNameForRepo("repo-3"), globalPackagingNamespace, "baz-user", "zot-pwd"), - expectedStatusCode: codes.OK, + existingSecret: newBasicAuthSecret(helm.SecretNameForRepo("repo-3"), globalPackagingNamespace, "baz-user", "zot-pwd"), }, { name: "check values without auth", @@ -1168,8 +1160,7 @@ func TestGetPackageRepositoryDetail(t *testing.T) { Variables: map[string]string{"$1": "value1"}, }, }), - existingSecret: newTlsSecret(helm.SecretNameForRepo("repo-4"), "ns-4", nil, nil, ca), - expectedStatusCode: codes.OK, + existingSecret: newTlsSecret(helm.SecretNameForRepo("repo-4"), "ns-4", nil, nil, ca), }, { name: "check values with imagesPullSecret", @@ -1190,7 +1181,6 @@ func TestGetPackageRepositoryDetail(t *testing.T) { }), existingSecret: newAuthDockerSecret(imagesPullSecretName("repo-5"), "ns-5", dockerAuthJson("https://myfooserver.com", "username", "password", "foo@bar.com", "dXNlcm5hbWU6cGFzc3dvcmQ=")), - expectedStatusCode: codes.OK, }, } @@ -1216,9 +1206,9 @@ func TestGetPackageRepositoryDetail(t *testing.T) { actualResponse, err := s.GetPackageRepositoryDetail(context.Background(), connect.NewRequest(tc.request)) // checks - if got, want := status.Code(err), tc.expectedStatusCode; got != want { + if got, want := connect.CodeOf(err), tc.expectedErrorCode; err != nil && got != want { t.Fatalf("got error: %d, want: %d, err: %+v", got, want, err) - } else if got != codes.OK { + } else if got != 0 { return } @@ -2109,11 +2099,11 @@ func TestDeletePackageRepository(t *testing.T) { func TestGetPackageRepositoryPermissions(t *testing.T) { testCases := []struct { - name string - request *corev1.GetPackageRepositoryPermissionsRequest - expectedStatusCode codes.Code - expectedResponse *corev1.GetPackageRepositoryPermissionsResponse - reactors []*ClientReaction + name string + request *corev1.GetPackageRepositoryPermissionsRequest + expectedErrorCode connect.Code + expectedResponse *corev1.GetPackageRepositoryPermissionsResponse + reactors []*ClientReaction }{ { name: "returns permissions for global package repositories", @@ -2139,7 +2129,6 @@ func TestGetPackageRepositoryPermissions(t *testing.T) { }, }, }, - expectedStatusCode: codes.OK, expectedResponse: &corev1.GetPackageRepositoryPermissionsResponse{ Permissions: []*corev1.PackageRepositoriesPermissions{ { @@ -2169,7 +2158,6 @@ func TestGetPackageRepositoryPermissions(t *testing.T) { }, }, }, - expectedStatusCode: codes.OK, expectedResponse: &corev1.GetPackageRepositoryPermissionsResponse{ Permissions: []*corev1.PackageRepositoriesPermissions{ { @@ -2192,7 +2180,7 @@ func TestGetPackageRepositoryPermissions(t *testing.T) { request: &corev1.GetPackageRepositoryPermissionsRequest{ Context: &corev1.Context{Namespace: "my-ns"}, }, - expectedStatusCode: codes.InvalidArgument, + expectedErrorCode: connect.CodeInvalidArgument, }, { name: "returns permissions for namespaced package repositories", @@ -2218,7 +2206,6 @@ func TestGetPackageRepositoryPermissions(t *testing.T) { }, }, }, - expectedStatusCode: codes.OK, expectedResponse: &corev1.GetPackageRepositoryPermissionsResponse{ Permissions: []*corev1.PackageRepositoriesPermissions{ { @@ -2251,12 +2238,12 @@ func TestGetPackageRepositoryPermissions(t *testing.T) { response, err := s.GetPackageRepositoryPermissions(context.Background(), connect.NewRequest(tc.request)) - if got, want := status.Code(err), tc.expectedStatusCode; got != want { + if got, want := connect.CodeOf(err), tc.expectedErrorCode; err != nil && got != want { t.Fatalf("got: %+v, want: %+v, err: %+v", got, want, err) } // We don't need to check anything else for non-OK codes. - if tc.expectedStatusCode != codes.OK { + if tc.expectedErrorCode != 0 { return } diff --git a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_validation.go b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_validation.go index 1f7d6a69f53..cf32157810a 100644 --- a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_validation.go +++ b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/repositories_validation.go @@ -18,8 +18,6 @@ import ( apprepov1alpha1 "github.com/vmware-tanzu/kubeapps/cmd/apprepository-controller/pkg/apis/apprepository/v1alpha1" httpclient "github.com/vmware-tanzu/kubeapps/pkg/http-client" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" corev1 "k8s.io/api/core/v1" ) @@ -85,7 +83,7 @@ func getValidator(appRepo *apprepov1alpha1.AppRepository) (HttpValidator, error) func newRepositoryClient(appRepo *apprepov1alpha1.AppRepository, secret *corev1.Secret) (httpclient.Client, error) { if cli, err := helm.InitNetClient(appRepo, secret, secret, nil); err != nil { - return nil, status.Errorf(codes.FailedPrecondition, "Unable to create HTTP client for repository: %v", err) + return nil, connect.NewError(connect.CodeFailedPrecondition, fmt.Errorf("Unable to create HTTP client for repository: %w", err)) } else { return cli, nil } diff --git a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/server.go b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/server.go index d7ed40cf8ef..8bf173fdc06 100644 --- a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/server.go +++ b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/server.go @@ -182,7 +182,7 @@ func (s *Server) MaxWorkers() int { // GetManager ensures a manager is available and returns it. func (s *Server) GetManager() (utils.AssetManager, error) { if s.manager == nil { - return nil, status.Errorf(codes.Internal, "server not configured with manager") + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("server not configured with manager")) } manager := s.manager return manager, nil @@ -395,7 +395,7 @@ func AvailablePackageDetailFromChart(chart *models.Chart, chartFiles *models.Cha isValid, err := pkgutils.IsValidChart(chart) if !isValid || err != nil { - return nil, status.Errorf(codes.Internal, "invalid chart: %s", err.Error()) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("invalid chart: %w", err)) } pkg.DisplayName = chart.Name @@ -472,7 +472,7 @@ func (s *Server) GetInstalledPackageSummaries(ctx context.Context, request *conn actionConfig, err := s.actionConfigGetter(request.Header(), request.Msg.GetContext()) if err != nil { - return nil, status.Errorf(codes.Internal, "Unable to create Helm action config: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to create Helm action config: %w", err)) } cmd := action.NewList(actionConfig) if request.Msg.GetContext().GetNamespace() == "" { @@ -489,7 +489,7 @@ func (s *Server) GetInstalledPackageSummaries(ctx context.Context, request *conn releases, err := cmd.Run() if err != nil { - return nil, status.Errorf(codes.Internal, "Unable to run Helm List action: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to run Helm List action: %w", err)) } installedPkgSummaries := make([]*corev1.InstalledPackageSummary, len(releases)) @@ -520,7 +520,7 @@ func (s *Server) GetInstalledPackageSummaries(ctx context.Context, request *conn } charts, err := s.manager.GetPaginatedChartListWithFilters(cq, 0, 0) if err != nil { - return nil, status.Errorf(codes.Internal, "Error while fetching related charts: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Error while fetching related charts: %w", err)) } // TODO(agamez): deal with multiple matches, perhaps returning []AvailablePackageRef ? // Example: global + namespaced repo including an overlapping subset of packages. @@ -594,7 +594,7 @@ func (s *Server) GetInstalledPackageDetail(ctx context.Context, request *connect actionConfig, err := s.actionConfigGetter(request.Header(), request.Msg.GetInstalledPackageRef().GetContext()) if err != nil { - return nil, status.Errorf(codes.Internal, "Unable to create Helm action config: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to create Helm action config: %w", err)) } // First grab the release. @@ -602,24 +602,24 @@ func (s *Server) GetInstalledPackageDetail(ctx context.Context, request *connect release, err := getcmd.Run(identifier) if err != nil { if err == driver.ErrReleaseNotFound { - return nil, status.Errorf(codes.NotFound, "Unable to find Helm release %q in namespace %q: %+v", identifier, namespace, err) + return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("Unable to find Helm release %q in namespace %q: %w", identifier, namespace, err)) } - return nil, status.Errorf(codes.Internal, "Unable to run Helm get action: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to run Helm get action: %w", err)) } installedPkgDetail, err := installedPkgDetailFromRelease(release, request.Msg.GetInstalledPackageRef()) if err != nil { - return nil, status.Errorf(codes.Internal, "Unable to create installed package detail from release: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to create installed package detail from release: %w", err)) } // Grab the released values. valuescmd := action.NewGetValues(actionConfig) values, err := valuescmd.Run(identifier) if err != nil { - return nil, status.Errorf(codes.Internal, "Unable to get Helm release values: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to get Helm release values: %w", err)) } valuesMarshalled, err := json.Marshal(values) if err != nil { - return nil, status.Errorf(codes.Internal, "Unable to marshal Helm release values: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Unable to marshal Helm release values: %w", err)) } installedPkgDetail.ValuesApplied = string(valuesMarshalled) @@ -635,7 +635,7 @@ func (s *Server) GetInstalledPackageDetail(ctx context.Context, request *connect } charts, err := s.manager.GetPaginatedChartListWithFilters(cq, 0, 0) if err != nil { - return nil, status.Errorf(codes.Internal, "Error while fetching related chart: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("Error while fetching related chart: %w", err)) } // Getting zero charts here @@ -1031,7 +1031,13 @@ func (s *Server) GetInstalledPackageResourceRefs(ctx context.Context, request *c refs, err := resourcerefs.GetInstalledPackageResourceRefs( request.Header(), types.NamespacedName{Name: identifier, Namespace: pkgRef.Context.Namespace}, fn) if err != nil { - return nil, err + // TODO(minelson): Remove once 6269 is complete. + switch status.Code(err) { + case codes.NotFound: + return nil, connect.NewError(connect.CodeNotFound, err) + default: + return nil, connect.NewError(connect.CodeInternal, err) + } } else { return connect.NewResponse(&corev1.GetInstalledPackageResourceRefsResponse{ Context: pkgRef.GetContext(), @@ -1107,11 +1113,11 @@ func (s *Server) getClient(headers http.Header, cluster string, namespace string func (s *Server) GetPackageRepositoryDetail(ctx context.Context, request *connect.Request[corev1.GetPackageRepositoryDetailRequest]) (*connect.Response[corev1.GetPackageRepositoryDetailResponse], error) { if request == nil || request.Msg.PackageRepoRef == nil { - return nil, status.Errorf(codes.InvalidArgument, "no request PackageRepoRef provided") + return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("no request PackageRepoRef provided")) } repoRef := request.Msg.GetPackageRepoRef() if repoRef.GetContext() == nil || repoRef.GetContext().GetNamespace() == "" { - return nil, status.Errorf(codes.InvalidArgument, "no valid context provided") + return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("no valid context provided")) } log.Infof("+helm GetPackageRepositoryDetail '%s' in context [%v]", repoRef.Identifier, repoRef.Context) @@ -1124,13 +1130,13 @@ func (s *Server) GetPackageRepositoryDetail(ctx context.Context, request *connec // Retrieve repository appRepo, caCertSecret, authSecret, imagesPullSecret, err := s.getAppRepoAndRelatedSecrets(ctx, request.Header(), cluster, name, namespace) if err != nil { - return nil, status.Errorf(codes.NotFound, "Unable to retrieve AppRepository '%s/%s' due to [%v]", namespace, name, err) + return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("Unable to retrieve AppRepository '%s/%s' due to [%w]", namespace, name, err)) } // Map to target struct repositoryDetail, err := s.mapToPackageRepositoryDetail(appRepo, cluster, namespace, caCertSecret, authSecret, imagesPullSecret) if err != nil { - return nil, status.Errorf(codes.Internal, "unable to convert the AppRepository: %v", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("unable to convert the AppRepository: %w", err)) } // response diff --git a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/server_test.go b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/server_test.go index 12b232306f3..1b6e89ff95a 100644 --- a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/server_test.go +++ b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/server_test.go @@ -102,14 +102,14 @@ func TestGetClient(t *testing.T) { manager utils.AssetManager clientGetter clientgetter.ClientProviderInterface statusCodeClient codes.Code - statusCodeManager codes.Code + statusCodeManager connect.Code }{ { name: "it returns internal error status when no manager configured", manager: nil, clientGetter: clientGetter, statusCodeClient: codes.OK, - statusCodeManager: codes.Internal, + statusCodeManager: connect.CodeInternal, }, { name: "it returns whatever error the clients getter function returns", @@ -117,15 +117,13 @@ func TestGetClient(t *testing.T) { clientGetter: &clientgetter.ClientProvider{ClientsFunc: func(headers http.Header, cluster string) (*clientgetter.ClientGetter, error) { return nil, status.Errorf(codes.FailedPrecondition, "Bang!") }}, - statusCodeClient: codes.FailedPrecondition, - statusCodeManager: codes.OK, + statusCodeClient: codes.FailedPrecondition, }, { - name: "it returns failed-precondition when clients getter function is not set", - manager: manager, - clientGetter: &clientgetter.ClientProvider{ClientsFunc: nil}, - statusCodeClient: codes.FailedPrecondition, - statusCodeManager: codes.OK, + name: "it returns failed-precondition when clients getter function is not set", + manager: manager, + clientGetter: &clientgetter.ClientProvider{ClientsFunc: nil}, + statusCodeClient: codes.FailedPrecondition, }, { name: "it returns client without error when configured correctly", @@ -145,7 +143,7 @@ func TestGetClient(t *testing.T) { _, errManager := s.GetManager() - if got, want := status.Code(errManager), tc.statusCodeManager; got != want { + if got, want := connect.CodeOf(errManager), tc.statusCodeManager; errManager != nil && got != want { t.Errorf("got: %+v, want: %+v", got, want) } @@ -1906,11 +1904,11 @@ func TestGetInstalledPackageDetail(t *testing.T) { releaseNotes = "some notes" ) testCases := []struct { - name string - existingReleases []releaseStub - request *corev1.GetInstalledPackageDetailRequest - expectedResponse *corev1.GetInstalledPackageDetailResponse - expectedStatusCode codes.Code + name string + existingReleases []releaseStub + request *corev1.GetInstalledPackageDetailRequest + expectedResponse *corev1.GetInstalledPackageDetailResponse + expectedErrorCode connect.Code }{ { name: "returns an installed package detail", @@ -1984,7 +1982,6 @@ func TestGetInstalledPackageDetail(t *testing.T) { CustomDetail: customDetailRevision2, }, }, - expectedStatusCode: codes.OK, }, { name: "returns a 404 if the installed package is not found", @@ -1996,7 +1993,7 @@ func TestGetInstalledPackageDetail(t *testing.T) { Identifier: releaseName, }, }, - expectedStatusCode: codes.NotFound, + expectedErrorCode: connect.CodeNotFound, }, } @@ -2007,18 +2004,18 @@ func TestGetInstalledPackageDetail(t *testing.T) { server, mock, cleanup := makeServer(t, authorized, actionConfig) defer cleanup() - if tc.expectedStatusCode == codes.OK { + if tc.expectedErrorCode == 0 { populateAssetDBWithDetail(t, mock, tc.expectedResponse.InstalledPackageDetail) } response, err := server.GetInstalledPackageDetail(context.Background(), connect.NewRequest(tc.request)) - if got, want := status.Code(err), tc.expectedStatusCode; got != want { + if got, want := connect.CodeOf(err), tc.expectedErrorCode; err != nil && got != want { t.Fatalf("got: %+v, want: %+v, err: %+v", got, want, err) } // We don't need to check anything else for non-OK codes. - if tc.expectedStatusCode != codes.OK { + if tc.expectedErrorCode != 0 { return } diff --git a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/update_test.go b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/update_test.go index ed822af2bc5..a369b702c3d 100644 --- a/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/update_test.go +++ b/cmd/kubeapps-apis/plugins/helm/packages/v1alpha1/update_test.go @@ -14,8 +14,6 @@ import ( "github.com/vmware-tanzu/kubeapps/cmd/apprepository-controller/pkg/apis/apprepository/v1alpha1" 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" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/release" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -23,12 +21,12 @@ import ( func TestUpdateInstalledPackage(t *testing.T) { testCases := []struct { - name string - existingReleases []releaseStub - request *corev1.UpdateInstalledPackageRequest - expectedResponse *corev1.UpdateInstalledPackageResponse - expectedStatusCode codes.Code - expectedRelease *release.Release + name string + existingReleases []releaseStub + request *corev1.UpdateInstalledPackageRequest + expectedResponse *corev1.UpdateInstalledPackageResponse + expectedErrorCode connect.Code + expectedRelease *release.Release }{ { name: "updates the installed package from repo without credentials", @@ -65,7 +63,6 @@ func TestUpdateInstalledPackage(t *testing.T) { Plugin: GetPluginDetail(), }, }, - expectedStatusCode: codes.OK, expectedRelease: &release.Release{ Name: "my-apache", Info: &release.Info{ @@ -118,7 +115,6 @@ func TestUpdateInstalledPackage(t *testing.T) { Plugin: GetPluginDetail(), }, }, - expectedStatusCode: codes.OK, expectedRelease: &release.Release{ Name: "my-apache", Info: &release.Info{ @@ -147,7 +143,7 @@ func TestUpdateInstalledPackage(t *testing.T) { Identifier: "not-a-valid-identifier", }, }, - expectedStatusCode: codes.NotFound, + expectedErrorCode: connect.CodeNotFound, }, } @@ -176,7 +172,7 @@ func TestUpdateInstalledPackage(t *testing.T) { } response, err := server.UpdateInstalledPackage(context.Background(), connect.NewRequest(tc.request)) - if got, want := status.Code(err), tc.expectedStatusCode; got != want { + if got, want := connect.CodeOf(err), tc.expectedErrorCode; err != nil && got != want { t.Fatalf("got: %+v, want: %+v, err: %+v", got, want, err) } if err != nil {