...
1 package destination
2
3 import (
4 "context"
5
6 pb "github.com/linkerd/linkerd2-proxy-api/go/destination"
7 "github.com/linkerd/linkerd2/pkg/k8s"
8 "go.opencensus.io/plugin/ocgrpc"
9 "google.golang.org/grpc"
10 "google.golang.org/grpc/credentials/insecure"
11 )
12
13 const (
14 destinationPort = 8086
15 destinationDeployment = "linkerd-destination"
16 )
17
18
19
20 func NewClient(addr string) (pb.DestinationClient, *grpc.ClientConn, error) {
21 conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(&ocgrpc.ClientHandler{}))
22 if err != nil {
23 return nil, nil, err
24 }
25
26 return pb.NewDestinationClient(conn), conn, nil
27 }
28
29
30
31 func NewExternalClient(ctx context.Context, controlPlaneNamespace string, kubeAPI *k8s.KubernetesAPI, pod string) (pb.DestinationClient, *grpc.ClientConn, error) {
32 var portForward *k8s.PortForward
33 var err error
34 if pod == "" {
35 portForward, err = k8s.NewPortForward(
36 ctx,
37 kubeAPI,
38 controlPlaneNamespace,
39 destinationDeployment,
40 "localhost",
41 0,
42 destinationPort,
43 false,
44 )
45 } else {
46 portForward, err = k8s.NewPodPortForward(kubeAPI, controlPlaneNamespace, pod, "localhost", 0, destinationPort, false)
47 }
48 if err != nil {
49 return nil, nil, err
50 }
51
52 destinationAddress := portForward.AddressAndPort()
53 if err = portForward.Init(); err != nil {
54 return nil, nil, err
55 }
56
57 return NewClient(destinationAddress)
58 }
59
View as plain text