From 5ecb7e0a692ef4139156790cc5696bfab66a4d6d Mon Sep 17 00:00:00 2001 From: Michael Nelson Date: Mon, 19 Jun 2023 15:54:12 +1000 Subject: [PATCH] Switch resources plugin over to connect error. (#6320) ### Description of the change Follows on from #6319, cleaning up the resources plugin so that it uses the connect error. ### Applicable issues - ref #6269 ### Additional information PRs will follow for the helm and flux plugins, before removing the original status error code. Signed-off-by: Michael Nelson --- .../plugins/pkg/connecterror/connecterror.go | 2 +- .../plugins/resources/v1alpha1/namespaces.go | 4 ++-- .../resources/v1alpha1/namespaces_test.go | 10 ++++----- .../plugins/resources/v1alpha1/secrets.go | 13 +++++------ .../resources/v1alpha1/secrets_test.go | 22 +++++++++---------- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/cmd/kubeapps-apis/plugins/pkg/connecterror/connecterror.go b/cmd/kubeapps-apis/plugins/pkg/connecterror/connecterror.go index 7cdb7e5a249..46806d250ee 100644 --- a/cmd/kubeapps-apis/plugins/pkg/connecterror/connecterror.go +++ b/cmd/kubeapps-apis/plugins/pkg/connecterror/connecterror.go @@ -10,7 +10,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" ) -// FromK8sResourceError generates a grpc status error from a Kubernetes error +// FromK8sResourceError generates a grpc connect error from a Kubernetes error // when querying a resource. func FromK8sError(verb, resource, identifier string, err error) error { if identifier == "" { diff --git a/cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces.go b/cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces.go index 89e4b3ba852..1ab63338d03 100644 --- a/cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces.go +++ b/cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces.go @@ -59,7 +59,7 @@ func (s *Server) CreateNamespace(ctx context.Context, r *connect.Request[v1alpha typedClient, err := s.clientGetter.Typed(r.Header(), cluster) if err != nil { - return nil, status.Errorf(codes.Internal, "unable to get the k8s client: '%v'", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("unable to get the k8s client: '%w'", err)) } _, err = typedClient.CoreV1().Namespaces().Create(ctx, &core.Namespace{ @@ -73,7 +73,7 @@ func (s *Server) CreateNamespace(ctx context.Context, r *connect.Request[v1alpha }, }, metav1.CreateOptions{}) if err != nil { - return nil, statuserror.FromK8sError("get", "Namespace", namespace, err) + return nil, connecterror.FromK8sError("get", "Namespace", namespace, err) } return connect.NewResponse(&v1alpha1.CreateNamespaceResponse{}), nil diff --git a/cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces_test.go b/cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces_test.go index 6f289022633..8a539fa4402 100644 --- a/cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces_test.go +++ b/cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces_test.go @@ -155,7 +155,7 @@ func TestCreateNamespace(t *testing.T) { request *v1alpha1.CreateNamespaceRequest k8sError error expectedResponse *v1alpha1.CreateNamespaceResponse - expectedErrorCode codes.Code + expectedErrorCode connect.Code existingObjects []runtime.Object validator func(action clientGoTesting.Action) (handled bool, ret runtime.Object, err error) }{ @@ -208,7 +208,7 @@ func TestCreateNamespace(t *testing.T) { Group: "v1", Resource: "namespaces", }, "default", errors.New("Bang")), - expectedErrorCode: codes.PermissionDenied, + expectedErrorCode: connect.CodePermissionDenied, }, { name: "returns already exists if k8s returns an already exists error", @@ -222,7 +222,7 @@ func TestCreateNamespace(t *testing.T) { Group: "v1", Resource: "namespaces", }, "default"), - expectedErrorCode: codes.AlreadyExists, + expectedErrorCode: connect.CodeAlreadyExists, }, { name: "returns an internal error if k8s returns an unexpected error", @@ -233,7 +233,7 @@ func TestCreateNamespace(t *testing.T) { }, }, k8sError: k8serrors.NewInternalError(errors.New("Bang")), - expectedErrorCode: codes.Internal, + expectedErrorCode: connect.CodeInternal, }, } @@ -257,7 +257,7 @@ func TestCreateNamespace(t *testing.T) { response, err := s.CreateNamespace(context.Background(), connect.NewRequest(tc.request)) - if got, want := status.Code(err), tc.expectedErrorCode; got != want { + if got, want := connect.CodeOf(err), tc.expectedErrorCode; err != nil && got != want { t.Fatalf("got: %d, want: %d, err: %+v", got, want, err) } if tc.expectedErrorCode != 0 { diff --git a/cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets.go b/cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets.go index c4aea9c0169..74d43633875 100644 --- a/cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets.go +++ b/cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets.go @@ -5,12 +5,11 @@ package main import ( "context" + "fmt" "github.com/bufbuild/connect-go" "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/plugins/resources/v1alpha1" - "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/plugins/pkg/statuserror" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" + "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/plugins/pkg/connecterror" core "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" log "k8s.io/klog/v2" @@ -25,7 +24,7 @@ func (s *Server) CreateSecret(ctx context.Context, r *connect.Request[v1alpha1.C typedClient, err := s.clientGetter.Typed(r.Header(), cluster) if err != nil { - return nil, status.Errorf(codes.Internal, "unable to get the k8s client: '%v'", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("unable to get the k8s client: '%w'", err)) } _, err = typedClient.CoreV1().Secrets(namespace).Create(ctx, &core.Secret{ @@ -41,7 +40,7 @@ func (s *Server) CreateSecret(ctx context.Context, r *connect.Request[v1alpha1.C StringData: r.Msg.GetStringData(), }, metav1.CreateOptions{}) if err != nil { - return nil, statuserror.FromK8sError("get", "Namespace", namespace, err) + return nil, connecterror.FromK8sError("get", "Namespace", namespace, err) } return connect.NewResponse(&v1alpha1.CreateSecretResponse{}), nil @@ -100,12 +99,12 @@ func (s *Server) GetSecretNames(ctx context.Context, r *connect.Request[v1alpha1 typedClient, err := s.clientGetter.Typed(r.Header(), cluster) if err != nil { - return nil, status.Errorf(codes.Internal, "unable to get the k8s client: '%v'", err) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("unable to get the k8s client: '%w'", err)) } secretList, err := typedClient.CoreV1().Secrets(namespace).List(ctx, metav1.ListOptions{}) if err != nil { - return nil, statuserror.FromK8sError("list", "Secrets", "", err) + return nil, connecterror.FromK8sError("list", "Secrets", "", err) } secrets := map[string]v1alpha1.SecretType{} diff --git a/cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets_test.go b/cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets_test.go index 2995cc70651..3c37a1f6e1e 100644 --- a/cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets_test.go +++ b/cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets_test.go @@ -15,8 +15,6 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" pkgsGRPCv1alpha1 "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/core/packages/v1alpha1" "github.com/vmware-tanzu/kubeapps/cmd/kubeapps-apis/gen/plugins/resources/v1alpha1" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" core "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -40,7 +38,7 @@ func TestCreateSecret(t *testing.T) { request *v1alpha1.CreateSecretRequest k8sError error expectedResponse *v1alpha1.CreateSecretResponse - expectedErrorCode codes.Code + expectedErrorCode connect.Code existingObjects []runtime.Object }{ { @@ -68,7 +66,7 @@ func TestCreateSecret(t *testing.T) { Group: "v1", Resource: "secrets", }, "default", errors.New("Bang")), - expectedErrorCode: codes.PermissionDenied, + expectedErrorCode: connect.CodePermissionDenied, }, { name: "returns already exists if k8s returns an already exists error", @@ -82,7 +80,7 @@ func TestCreateSecret(t *testing.T) { Group: "v1", Resource: "secrets", }, "default"), - expectedErrorCode: codes.AlreadyExists, + expectedErrorCode: connect.CodeAlreadyExists, }, { name: "returns an internal error if k8s returns an unexpected error", @@ -93,7 +91,7 @@ func TestCreateSecret(t *testing.T) { }, }, k8sError: k8serrors.NewInternalError(errors.New("Bang")), - expectedErrorCode: codes.Internal, + expectedErrorCode: connect.CodeInternal, }, } @@ -114,11 +112,11 @@ func TestCreateSecret(t *testing.T) { response, err := s.CreateSecret(context.Background(), connect.NewRequest(tc.request)) - if got, want := status.Code(err), tc.expectedErrorCode; got != want { + if got, want := connect.CodeOf(err), tc.expectedErrorCode; err != nil && got != want { t.Fatalf("got: %d, want: %d, err: %+v", got, want, err) } - if tc.expectedErrorCode != codes.OK { + if tc.expectedErrorCode != 0 { return } @@ -148,7 +146,7 @@ func TestGetSecretNames(t *testing.T) { request *v1alpha1.GetSecretNamesRequest k8sError error expectedResponse *v1alpha1.GetSecretNamesResponse - expectedErrorCode codes.Code + expectedErrorCode connect.Code existingObjects []runtime.Object }{ { @@ -216,7 +214,7 @@ func TestGetSecretNames(t *testing.T) { Group: "v1", Resource: "secrets", }, "default", errors.New("Bang")), - expectedErrorCode: codes.PermissionDenied, + expectedErrorCode: connect.CodePermissionDenied, }, { name: "returns an internal error if k8s returns an unexpected error", @@ -227,7 +225,7 @@ func TestGetSecretNames(t *testing.T) { }, }, k8sError: k8serrors.NewInternalError(errors.New("Bang")), - expectedErrorCode: codes.Internal, + expectedErrorCode: connect.CodeInternal, }, } @@ -248,7 +246,7 @@ func TestGetSecretNames(t *testing.T) { response, err := s.GetSecretNames(context.Background(), connect.NewRequest(tc.request)) - if got, want := status.Code(err), tc.expectedErrorCode; got != want { + if got, want := connect.CodeOf(err), tc.expectedErrorCode; err != nil && got != want { t.Fatalf("got: %d, want: %d, err: %+v", got, want, err) } if tc.expectedErrorCode != 0 {