2022-10-10 15:35:40 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
2024-09-13 08:32:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/tree"
|
2023-05-31 09:26:54 +00:00
|
|
|
metrics "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics/grpc"
|
|
|
|
tracing "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing/grpc"
|
2024-09-13 08:32:03 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-10-10 15:35:40 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
)
|
|
|
|
|
|
|
|
// _client returns grpc Tree service client. Should be removed
|
|
|
|
// after making Tree API public.
|
2024-09-13 08:32:03 +00:00
|
|
|
func _client() (tree.TreeServiceClient, error) {
|
2022-10-10 15:35:40 +00:00
|
|
|
var netAddr network.Address
|
|
|
|
err := netAddr.FromString(viper.GetString(commonflags.RPC))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:01:43 +00:00
|
|
|
opts := []grpc.DialOption{
|
|
|
|
grpc.WithChainUnaryInterceptor(
|
2023-05-31 09:26:54 +00:00
|
|
|
metrics.NewUnaryClientInterceptor(),
|
|
|
|
tracing.NewUnaryClientInteceptor(),
|
2023-03-13 11:01:43 +00:00
|
|
|
),
|
|
|
|
grpc.WithChainStreamInterceptor(
|
2023-05-31 09:26:54 +00:00
|
|
|
metrics.NewStreamClientInterceptor(),
|
|
|
|
tracing.NewStreamClientInterceptor(),
|
2023-03-13 11:01:43 +00:00
|
|
|
),
|
|
|
|
}
|
2022-10-10 15:35:40 +00:00
|
|
|
|
|
|
|
if !strings.HasPrefix(netAddr.URIAddr(), "grpcs:") {
|
|
|
|
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
}
|
|
|
|
|
2024-09-13 08:32:03 +00:00
|
|
|
cc, err := grpc.NewClient(netAddr.URIAddr(), opts...)
|
2022-10-10 15:35:40 +00:00
|
|
|
return tree.NewTreeServiceClient(cc), err
|
|
|
|
}
|
2024-09-13 08:32:03 +00:00
|
|
|
|
|
|
|
func contextWithTimeout(cmd *cobra.Command) (context.Context, context.CancelFunc) {
|
|
|
|
if timeout := viper.GetDuration(commonflags.Timeout); timeout > 0 {
|
|
|
|
common.PrintVerbose(cmd, "Set request timeout to %s.", timeout)
|
|
|
|
return context.WithTimeout(cmd.Context(), timeout)
|
|
|
|
}
|
|
|
|
return context.WithTimeout(cmd.Context(), commonflags.TimeoutDefault)
|
|
|
|
}
|