Skip to content

Commit

Permalink
Switch resources plugin over to connect error. (#6320)
Browse files Browse the repository at this point in the history
### 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 <[email protected]>
  • Loading branch information
absoludity authored Jun 19, 2023
1 parent ab50c95 commit 5ecb7e0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cmd/kubeapps-apis/plugins/pkg/connecterror/connecterror.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions cmd/kubeapps-apis/plugins/resources/v1alpha1/namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}{
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -233,7 +233,7 @@ func TestCreateNamespace(t *testing.T) {
},
},
k8sError: k8serrors.NewInternalError(errors.New("Bang")),
expectedErrorCode: codes.Internal,
expectedErrorCode: connect.CodeInternal,
},
}

Expand All @@ -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 {
Expand Down
13 changes: 6 additions & 7 deletions cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand All @@ -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
Expand Down Expand Up @@ -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{}
Expand Down
22 changes: 10 additions & 12 deletions cmd/kubeapps-apis/plugins/resources/v1alpha1/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}{
{
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -93,7 +91,7 @@ func TestCreateSecret(t *testing.T) {
},
},
k8sError: k8serrors.NewInternalError(errors.New("Bang")),
expectedErrorCode: codes.Internal,
expectedErrorCode: connect.CodeInternal,
},
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}{
{
Expand Down Expand Up @@ -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",
Expand All @@ -227,7 +225,7 @@ func TestGetSecretNames(t *testing.T) {
},
},
k8sError: k8serrors.NewInternalError(errors.New("Bang")),
expectedErrorCode: codes.Internal,
expectedErrorCode: connect.CodeInternal,
},
}

Expand All @@ -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 {
Expand Down

0 comments on commit 5ecb7e0

Please sign in to comment.