-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
client.go
52 lines (44 loc) · 1.37 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package destination
import (
"context"
pb "github.com/linkerd/linkerd2-proxy-api/go/destination"
"github.com/linkerd/linkerd2/pkg/k8s"
"go.opencensus.io/plugin/ocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
destinationPort = 8086
destinationDeployment = "linkerd-destination"
)
// NewClient creates a client for the control plane Destination API that
// implements the Destination service.
func NewClient(addr string) (pb.DestinationClient, *grpc.ClientConn, error) {
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithStatsHandler(&ocgrpc.ClientHandler{}))
if err != nil {
return nil, nil, err
}
return pb.NewDestinationClient(conn), conn, nil
}
// NewExternalClient creates a client for the control plane Destination API
// to run from outside a Kubernetes cluster.
func NewExternalClient(ctx context.Context, controlPlaneNamespace string, kubeAPI *k8s.KubernetesAPI) (pb.DestinationClient, *grpc.ClientConn, error) {
portforward, err := k8s.NewPortForward(
ctx,
kubeAPI,
controlPlaneNamespace,
destinationDeployment,
"localhost",
0,
destinationPort,
false,
)
if err != nil {
return nil, nil, err
}
destinationAddress := portforward.AddressAndPort()
if err = portforward.Init(); err != nil {
return nil, nil, err
}
return NewClient(destinationAddress)
}