github.com/argoproj/argo-cd/v3@v3.2.1/cmpserver/apiclient/clientset.go (about) 1 package apiclient 2 3 import ( 4 "context" 5 "math" 6 "time" 7 8 "github.com/argoproj/argo-cd/v3/common" 9 "github.com/argoproj/argo-cd/v3/util/env" 10 11 grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry" 12 log "github.com/sirupsen/logrus" 13 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" 14 "google.golang.org/grpc" 15 "google.golang.org/grpc/credentials/insecure" 16 17 grpc_util "github.com/argoproj/argo-cd/v3/util/grpc" 18 utilio "github.com/argoproj/argo-cd/v3/util/io" 19 ) 20 21 // MaxGRPCMessageSize contains max grpc message size 22 var MaxGRPCMessageSize = env.ParseNumFromEnv(common.EnvGRPCMaxSizeMB, 100, 0, math.MaxInt32) * 1024 * 1024 23 24 // Clientset represents config management plugin server api clients 25 type Clientset interface { 26 NewConfigManagementPluginClient() (utilio.Closer, ConfigManagementPluginServiceClient, error) 27 } 28 29 type clientSet struct { 30 address string 31 } 32 33 func (c *clientSet) NewConfigManagementPluginClient() (utilio.Closer, ConfigManagementPluginServiceClient, error) { 34 conn, err := NewConnection(c.address) 35 if err != nil { 36 return nil, nil, err 37 } 38 return conn, NewConfigManagementPluginServiceClient(conn), nil 39 } 40 41 func NewConnection(address string) (*grpc.ClientConn, error) { 42 retryOpts := []grpc_retry.CallOption{ 43 grpc_retry.WithMax(3), 44 grpc_retry.WithBackoff(grpc_retry.BackoffLinear(1000 * time.Millisecond)), 45 } 46 unaryInterceptors := []grpc.UnaryClientInterceptor{grpc_retry.UnaryClientInterceptor(retryOpts...)} 47 dialOpts := []grpc.DialOption{ 48 grpc.WithStreamInterceptor(grpc_util.RetryOnlyForServerStreamInterceptor(retryOpts...)), 49 grpc.WithChainUnaryInterceptor(unaryInterceptors...), 50 grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(MaxGRPCMessageSize), grpc.MaxCallSendMsgSize(MaxGRPCMessageSize)), 51 grpc.WithStatsHandler(otelgrpc.NewClientHandler()), 52 } 53 54 dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials())) 55 conn, err := grpc_util.BlockingNewClient(context.Background(), "unix", address, nil, dialOpts...) 56 if err != nil { 57 log.Errorf("Unable to connect to config management plugin service with address %s", address) 58 return nil, err 59 } 60 return conn, nil 61 } 62 63 // NewConfigManagementPluginClientSet creates new instance of config management plugin server Clientset 64 func NewConfigManagementPluginClientSet(address string) Clientset { 65 return &clientSet{address: address} 66 }