From 01afa1cae4253d7dd375f1cf52d1f65e66d5ca06 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Mon, 3 Apr 2023 14:04:35 +0300 Subject: [PATCH] [#75] Make grpc tree client implementation internal Since we have pkg 'internal/frostfs/services/tree' that is downloading during build we cannot export any package that is depended on it. Signed-off-by: Denis Kirillov --- cmd/s3-gw/app.go | 3 +- .../frostfs/services}/tree_client_grpc.go | 81 ++++++++++--------- .../services}/tree_client_grpc_signature.go | 2 +- .../services}/tree_client_grpc_test.go | 2 +- 4 files changed, 45 insertions(+), 43 deletions(-) rename {pkg/service/tree => internal/frostfs/services}/tree_client_grpc.go (75%) rename {pkg/service/tree => internal/frostfs/services}/tree_client_grpc_signature.go (98%) rename {pkg/service/tree => internal/frostfs/services}/tree_client_grpc_test.go (97%) diff --git a/cmd/s3-gw/app.go b/cmd/s3-gw/app.go index 6b38a950e..d8da913db 100644 --- a/cmd/s3-gw/app.go +++ b/cmd/s3-gw/app.go @@ -21,6 +21,7 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/notifications" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/resolver" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs" + "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/version" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/wallet" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/xml" @@ -114,7 +115,7 @@ func (a *App) initLayer(ctx context.Context) { treeServiceEndpoint := a.cfg.GetString(cfgTreeServiceEndpoint) grpcDialOpt := grpc.WithTransportCredentials(insecure.NewCredentials()) - treeGRPCClient, err := tree.NewTreeServiceClientGRPC(ctx, treeServiceEndpoint, a.key, grpcDialOpt) + treeGRPCClient, err := services.NewTreeServiceClientGRPC(ctx, treeServiceEndpoint, a.key, grpcDialOpt) if err != nil { a.log.Fatal("failed to create tree service", zap.Error(err)) } diff --git a/pkg/service/tree/tree_client_grpc.go b/internal/frostfs/services/tree_client_grpc.go similarity index 75% rename from pkg/service/tree/tree_client_grpc.go rename to internal/frostfs/services/tree_client_grpc.go index e447627a2..ff582bf2d 100644 --- a/pkg/service/tree/tree_client_grpc.go +++ b/internal/frostfs/services/tree_client_grpc.go @@ -1,4 +1,4 @@ -package tree +package services import ( "context" @@ -10,14 +10,15 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox" - "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services/tree" + grpcService "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services/tree" + "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/pkg/service/tree" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "google.golang.org/grpc" ) type GetNodeByPathResponseInfoWrapper struct { - response *tree.GetNodeByPathResponse_Info + response *grpcService.GetNodeByPathResponse_Info } func (n GetNodeByPathResponseInfoWrapper) GetNodeID() uint64 { @@ -32,8 +33,8 @@ func (n GetNodeByPathResponseInfoWrapper) GetTimestamp() uint64 { return n.response.GetTimestamp() } -func (n GetNodeByPathResponseInfoWrapper) GetMeta() []Meta { - res := make([]Meta, len(n.response.Meta)) +func (n GetNodeByPathResponseInfoWrapper) GetMeta() []tree.Meta { + res := make([]tree.Meta, len(n.response.Meta)) for i, value := range n.response.Meta { res[i] = value } @@ -41,7 +42,7 @@ func (n GetNodeByPathResponseInfoWrapper) GetMeta() []Meta { } type GetSubTreeResponseBodyWrapper struct { - response *tree.GetSubTreeResponse_Body + response *grpcService.GetSubTreeResponse_Body } func (n GetSubTreeResponseBodyWrapper) GetNodeID() uint64 { @@ -56,8 +57,8 @@ func (n GetSubTreeResponseBodyWrapper) GetTimestamp() uint64 { return n.response.GetTimestamp() } -func (n GetSubTreeResponseBodyWrapper) GetMeta() []Meta { - res := make([]Meta, len(n.response.Meta)) +func (n GetSubTreeResponseBodyWrapper) GetMeta() []tree.Meta { + res := make([]tree.Meta, len(n.response.Meta)) for i, value := range n.response.Meta { res[i] = value } @@ -67,7 +68,7 @@ func (n GetSubTreeResponseBodyWrapper) GetMeta() []Meta { type ServiceClientGRPC struct { key *keys.PrivateKey conn *grpc.ClientConn - service tree.TreeServiceClient + service grpcService.TreeServiceClient } func NewTreeServiceClientGRPC(ctx context.Context, addr string, key *keys.PrivateKey, grpcOpts ...grpc.DialOption) (*ServiceClientGRPC, error) { @@ -76,8 +77,8 @@ func NewTreeServiceClientGRPC(ctx context.Context, addr string, key *keys.Privat return nil, fmt.Errorf("did not connect: %v", err) } - c := tree.NewTreeServiceClient(conn) - if _, err = c.Healthcheck(ctx, &tree.HealthcheckRequest{}); err != nil { + c := grpcService.NewTreeServiceClient(conn) + if _, err = c.Healthcheck(ctx, &grpcService.HealthcheckRequest{}); err != nil { return nil, fmt.Errorf("healthcheck: %w", err) } @@ -88,14 +89,14 @@ func NewTreeServiceClientGRPC(ctx context.Context, addr string, key *keys.Privat }, nil } -func (c *ServiceClientGRPC) GetNodes(ctx context.Context, p *GetNodesParams) ([]NodeResponse, error) { - request := &tree.GetNodeByPathRequest{ - Body: &tree.GetNodeByPathRequest_Body{ +func (c *ServiceClientGRPC) GetNodes(ctx context.Context, p *tree.GetNodesParams) ([]tree.NodeResponse, error) { + request := &grpcService.GetNodeByPathRequest{ + Body: &grpcService.GetNodeByPathRequest_Body{ ContainerId: p.BktInfo.CID[:], TreeId: p.TreeID, Path: p.Path, Attributes: p.Meta, - PathAttribute: FileNameKey, + PathAttribute: tree.FileNameKey, LatestOnly: p.LatestOnly, AllAttributes: p.AllAttrs, BearerToken: getBearer(ctx, p.BktInfo), @@ -103,7 +104,7 @@ func (c *ServiceClientGRPC) GetNodes(ctx context.Context, p *GetNodesParams) ([] } if err := c.signRequest(request.Body, func(key, sign []byte) { - request.Signature = &tree.Signature{ + request.Signature = &grpcService.Signature{ Key: key, Sign: sign, } @@ -116,7 +117,7 @@ func (c *ServiceClientGRPC) GetNodes(ctx context.Context, p *GetNodesParams) ([] return nil, handleError("failed to get node by path", err) } - res := make([]NodeResponse, len(resp.GetBody().GetNodes())) + res := make([]tree.NodeResponse, len(resp.GetBody().GetNodes())) for i, info := range resp.GetBody().GetNodes() { res[i] = GetNodeByPathResponseInfoWrapper{info} } @@ -124,9 +125,9 @@ func (c *ServiceClientGRPC) GetNodes(ctx context.Context, p *GetNodesParams) ([] return res, nil } -func (c *ServiceClientGRPC) GetSubTree(ctx context.Context, bktInfo *data.BucketInfo, treeID string, rootID uint64, depth uint32) ([]NodeResponse, error) { - request := &tree.GetSubTreeRequest{ - Body: &tree.GetSubTreeRequest_Body{ +func (c *ServiceClientGRPC) GetSubTree(ctx context.Context, bktInfo *data.BucketInfo, treeID string, rootID uint64, depth uint32) ([]tree.NodeResponse, error) { + request := &grpcService.GetSubTreeRequest{ + Body: &grpcService.GetSubTreeRequest_Body{ ContainerId: bktInfo.CID[:], TreeId: treeID, RootId: rootID, @@ -136,7 +137,7 @@ func (c *ServiceClientGRPC) GetSubTree(ctx context.Context, bktInfo *data.Bucket } if err := c.signRequest(request.Body, func(key, sign []byte) { - request.Signature = &tree.Signature{ + request.Signature = &grpcService.Signature{ Key: key, Sign: sign, } @@ -149,7 +150,7 @@ func (c *ServiceClientGRPC) GetSubTree(ctx context.Context, bktInfo *data.Bucket return nil, handleError("failed to get sub tree client", err) } - var subtree []NodeResponse + var subtree []tree.NodeResponse for { resp, err := cli.Recv() if err == io.EOF { @@ -164,8 +165,8 @@ func (c *ServiceClientGRPC) GetSubTree(ctx context.Context, bktInfo *data.Bucket } func (c *ServiceClientGRPC) AddNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, parent uint64, meta map[string]string) (uint64, error) { - request := &tree.AddRequest{ - Body: &tree.AddRequest_Body{ + request := &grpcService.AddRequest{ + Body: &grpcService.AddRequest_Body{ ContainerId: bktInfo.CID[:], TreeId: treeID, ParentId: parent, @@ -174,7 +175,7 @@ func (c *ServiceClientGRPC) AddNode(ctx context.Context, bktInfo *data.BucketInf }, } if err := c.signRequest(request.Body, func(key, sign []byte) { - request.Signature = &tree.Signature{ + request.Signature = &grpcService.Signature{ Key: key, Sign: sign, } @@ -191,19 +192,19 @@ func (c *ServiceClientGRPC) AddNode(ctx context.Context, bktInfo *data.BucketInf } func (c *ServiceClientGRPC) AddNodeByPath(ctx context.Context, bktInfo *data.BucketInfo, treeID string, path []string, meta map[string]string) (uint64, error) { - request := &tree.AddByPathRequest{ - Body: &tree.AddByPathRequest_Body{ + request := &grpcService.AddByPathRequest{ + Body: &grpcService.AddByPathRequest_Body{ ContainerId: bktInfo.CID[:], TreeId: treeID, Path: path, Meta: metaToKV(meta), - PathAttribute: FileNameKey, + PathAttribute: tree.FileNameKey, BearerToken: getBearer(ctx, bktInfo), }, } if err := c.signRequest(request.Body, func(key, sign []byte) { - request.Signature = &tree.Signature{ + request.Signature = &grpcService.Signature{ Key: key, Sign: sign, } @@ -228,8 +229,8 @@ func (c *ServiceClientGRPC) AddNodeByPath(ctx context.Context, bktInfo *data.Buc } func (c *ServiceClientGRPC) MoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID, parentID uint64, meta map[string]string) error { - request := &tree.MoveRequest{ - Body: &tree.MoveRequest_Body{ + request := &grpcService.MoveRequest{ + Body: &grpcService.MoveRequest_Body{ ContainerId: bktInfo.CID[:], TreeId: treeID, NodeId: nodeID, @@ -240,7 +241,7 @@ func (c *ServiceClientGRPC) MoveNode(ctx context.Context, bktInfo *data.BucketIn } if err := c.signRequest(request.Body, func(key, sign []byte) { - request.Signature = &tree.Signature{ + request.Signature = &grpcService.Signature{ Key: key, Sign: sign, } @@ -256,8 +257,8 @@ func (c *ServiceClientGRPC) MoveNode(ctx context.Context, bktInfo *data.BucketIn } func (c *ServiceClientGRPC) RemoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID uint64) error { - request := &tree.RemoveRequest{ - Body: &tree.RemoveRequest_Body{ + request := &grpcService.RemoveRequest{ + Body: &grpcService.RemoveRequest_Body{ ContainerId: bktInfo.CID[:], TreeId: treeID, NodeId: nodeID, @@ -265,7 +266,7 @@ func (c *ServiceClientGRPC) RemoveNode(ctx context.Context, bktInfo *data.Bucket }, } if err := c.signRequest(request.Body, func(key, sign []byte) { - request.Signature = &tree.Signature{ + request.Signature = &grpcService.Signature{ Key: key, Sign: sign, } @@ -280,11 +281,11 @@ func (c *ServiceClientGRPC) RemoveNode(ctx context.Context, bktInfo *data.Bucket return nil } -func metaToKV(meta map[string]string) []*tree.KeyValue { - result := make([]*tree.KeyValue, 0, len(meta)) +func metaToKV(meta map[string]string) []*grpcService.KeyValue { + result := make([]*grpcService.KeyValue, 0, len(meta)) for key, value := range meta { - result = append(result, &tree.KeyValue{Key: key, Value: []byte(value)}) + result = append(result, &grpcService.KeyValue{Key: key, Value: []byte(value)}) } return result @@ -303,9 +304,9 @@ func getBearer(ctx context.Context, bktInfo *data.BucketInfo) []byte { func handleError(msg string, err error) error { if strings.Contains(err.Error(), "not found") { - return fmt.Errorf("%w: %s", ErrNodeNotFound, err.Error()) + return fmt.Errorf("%w: %s", tree.ErrNodeNotFound, err.Error()) } else if strings.Contains(err.Error(), "is denied by") { - return fmt.Errorf("%w: %s", ErrNodeAccessDenied, err.Error()) + return fmt.Errorf("%w: %s", tree.ErrNodeAccessDenied, err.Error()) } return fmt.Errorf("%s: %w", msg, err) } diff --git a/pkg/service/tree/tree_client_grpc_signature.go b/internal/frostfs/services/tree_client_grpc_signature.go similarity index 98% rename from pkg/service/tree/tree_client_grpc_signature.go rename to internal/frostfs/services/tree_client_grpc_signature.go index 7eb656430..9dd38f9c6 100644 --- a/pkg/service/tree/tree_client_grpc_signature.go +++ b/internal/frostfs/services/tree_client_grpc_signature.go @@ -1,5 +1,5 @@ /*REMOVE THIS AFTER SIGNATURE WILL BE AVAILABLE IN TREE CLIENT FROM FROSTFS NODE*/ -package tree +package services import ( crypto "git.frostfs.info/TrueCloudLab/frostfs-crypto" diff --git a/pkg/service/tree/tree_client_grpc_test.go b/internal/frostfs/services/tree_client_grpc_test.go similarity index 97% rename from pkg/service/tree/tree_client_grpc_test.go rename to internal/frostfs/services/tree_client_grpc_test.go index b9f0f8c32..724b90931 100644 --- a/pkg/service/tree/tree_client_grpc_test.go +++ b/internal/frostfs/services/tree_client_grpc_test.go @@ -1,4 +1,4 @@ -package tree +package services import ( "errors"