NewClient makes a new Client, given a kubernetes service address.
(address string)
| 172 | |
| 173 | // NewClient makes a new Client, given a kubernetes service address. |
| 174 | func NewClient(address string) (*Client, error) { |
| 175 | kuberesolver.RegisterInCluster() |
| 176 | |
| 177 | address, err := ParseURL(address) |
| 178 | if err != nil { |
| 179 | return nil, err |
| 180 | } |
| 181 | const grpcServiceConfig = `{"loadBalancingPolicy":"round_robin"}` |
| 182 | |
| 183 | var unaryInterceptors []grpc.UnaryClientInterceptor |
| 184 | if opentracing.IsGlobalTracerRegistered() { |
| 185 | unaryInterceptors = append(unaryInterceptors, otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer())) |
| 186 | } |
| 187 | unaryInterceptors = append(unaryInterceptors, middleware.ClientUserHeaderInterceptor) |
| 188 | |
| 189 | dialOptions := []grpc.DialOption{ |
| 190 | grpc.WithDefaultServiceConfig(grpcServiceConfig), |
| 191 | grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 192 | grpc.WithChainUnaryInterceptor(unaryInterceptors...), |
| 193 | } |
| 194 | if !opentracing.IsGlobalTracerRegistered() { // Note: I'm not sure whether this condition is required, feel free to question it. |
| 195 | dialOptions = append(dialOptions, grpc.WithStatsHandler(otelgrpc.NewClientHandler())) |
| 196 | } |
| 197 | |
| 198 | conn, err := grpc.NewClient(address, dialOptions...) |
| 199 | if err != nil { |
| 200 | return nil, err |
| 201 | } |
| 202 | |
| 203 | return &Client{ |
| 204 | client: httpgrpc.NewHTTPClient(conn), |
| 205 | conn: conn, |
| 206 | }, nil |
| 207 | } |
| 208 | |
| 209 | // ServeHTTP implements http.Handler |
| 210 | func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) { |