-
Notifications
You must be signed in to change notification settings - Fork 53
/
client.go
97 lines (88 loc) · 2.82 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package v1
import (
managementv1 "github.com/rancher/opni/pkg/apis/management/v1"
"github.com/rancher/opni/pkg/util/waitctx"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type OpsClientOptions struct {
listenAddr string
dialOptions []grpc.DialOption
}
type OpsClientOption func(*OpsClientOptions)
func (o *OpsClientOptions) apply(opts ...OpsClientOption) {
for _, op := range opts {
op(o)
}
}
func WithListenAddress(addr string) OpsClientOption {
return func(o *OpsClientOptions) {
o.listenAddr = addr
}
}
func WithDialOptions(options ...grpc.DialOption) OpsClientOption {
return func(o *OpsClientOptions) {
o.dialOptions = append(o.dialOptions, options...)
}
}
func NewConditionsClient(ctx waitctx.PermissiveContext, opts ...OpsClientOption) (AlertConditionsClient, error) {
options := OpsClientOptions{
listenAddr: managementv1.DefaultManagementSocket(),
dialOptions: []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithChainStreamInterceptor(otelgrpc.StreamClientInterceptor()),
grpc.WithChainUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
},
}
options.apply(opts...)
cc, err := grpc.DialContext(ctx, options.listenAddr, options.dialOptions...)
if err != nil {
return nil, err
}
waitctx.Permissive.Go(ctx, func() {
<-ctx.Done()
cc.Close()
})
return NewAlertConditionsClient(cc), nil
}
func NewNotificationsClient(ctx waitctx.PermissiveContext, opts ...OpsClientOption) (AlertNotificationsClient, error) {
options := OpsClientOptions{
listenAddr: managementv1.DefaultManagementSocket(),
dialOptions: []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithChainStreamInterceptor(otelgrpc.StreamClientInterceptor()),
grpc.WithChainUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
},
}
options.apply(opts...)
cc, err := grpc.DialContext(ctx, options.listenAddr, options.dialOptions...)
if err != nil {
return nil, err
}
waitctx.Permissive.Go(ctx, func() {
<-ctx.Done()
cc.Close()
})
return NewAlertNotificationsClient(cc), nil
}
func NewEndpointsClient(ctx waitctx.PermissiveContext, opts ...OpsClientOption) (AlertEndpointsClient, error) {
options := OpsClientOptions{
listenAddr: managementv1.DefaultManagementSocket(),
dialOptions: []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithChainStreamInterceptor(otelgrpc.StreamClientInterceptor()),
grpc.WithChainUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
},
}
options.apply(opts...)
cc, err := grpc.DialContext(ctx, options.listenAddr, options.dialOptions...)
if err != nil {
return nil, err
}
waitctx.Permissive.Go(ctx, func() {
<-ctx.Done()
cc.Close()
})
return NewAlertEndpointsClient(cc), nil
}