/
client.go
267 lines (244 loc) · 7.87 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mux
import (
"context"
"crypto/tls"
"crypto/x509"
"net/url"
"os"
"time"
)
import (
"github.com/go-logr/logr"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
import (
mesh_proto "github.com/apache/dubbo-kubernetes/api/mesh/v1alpha1"
"github.com/apache/dubbo-kubernetes/pkg/config/multizone"
"github.com/apache/dubbo-kubernetes/pkg/core"
"github.com/apache/dubbo-kubernetes/pkg/core/runtime/component"
"github.com/apache/dubbo-kubernetes/pkg/dds"
"github.com/apache/dubbo-kubernetes/pkg/version"
)
const (
DDSVersionHeaderKey = "dds-version"
DDSVersionV3 = "v3"
)
var muxClientLog = core.Log.WithName("dds-mux-client")
type client struct {
globalToZoneCb OnGlobalToZoneSyncStartedFunc
zoneToGlobalCb OnZoneToGlobalSyncStartedFunc
globalURL string
clientID string
config multizone.DdsClientConfig
ctx context.Context
}
func NewClient(
ctx context.Context,
globalURL string,
clientID string,
globalToZoneCb OnGlobalToZoneSyncStartedFunc,
zoneToGlobalCb OnZoneToGlobalSyncStartedFunc,
config multizone.DdsClientConfig,
) component.Component {
return &client{
ctx: ctx,
globalToZoneCb: globalToZoneCb,
zoneToGlobalCb: zoneToGlobalCb,
globalURL: globalURL,
clientID: clientID,
config: config,
}
}
func (c *client) Start(stop <-chan struct{}) (errs error) {
u, err := url.Parse(c.globalURL)
if errs != nil {
return err
}
dialOpts := []grpc.DialOption{}
dialOpts = append(dialOpts, grpc.WithUserAgent(version.Build.UserAgent("dds")), grpc.WithDefaultCallOptions(
grpc.MaxCallSendMsgSize(int(c.config.MaxMsgSize)),
grpc.MaxCallRecvMsgSize(int(c.config.MaxMsgSize))),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: grpcKeepAliveTime,
Timeout: grpcKeepAliveTime,
PermitWithoutStream: true,
}),
)
switch u.Scheme {
case "grpc":
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
case "grpcs":
tlsConfig, err := tlsConfig(c.config.RootCAFile, c.config.TlsSkipVerify)
if err != nil {
return errors.Wrap(err, "could not ")
}
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
default:
return errors.Errorf("unsupported scheme %q. Use one of %s", u.Scheme, []string{"grpc", "grpcs"})
}
conn, err := grpc.Dial(u.Host, dialOpts...)
if err != nil {
return err
}
defer func() {
if err := conn.Close(); err != nil {
errs = errors.Wrapf(err, "failed to close a connection")
}
}()
withDDSCtx, cancel := context.WithCancel(metadata.AppendToOutgoingContext(c.ctx,
"client-id", c.clientID,
DDSVersionHeaderKey, DDSVersionV3,
dds.FeaturesMetadataKey, dds.FeatureZonePingHealth,
dds.FeaturesMetadataKey, dds.FeatureHashSuffix,
))
defer cancel()
log := muxClientLog.WithValues("client-id", c.clientID)
errorCh := make(chan error)
go c.startHealthCheck(withDDSCtx, log, conn, errorCh)
go c.startGlobalToZoneSync(withDDSCtx, log, conn, errorCh)
go c.startZoneToGlobalSync(withDDSCtx, log, conn, errorCh)
select {
case <-stop:
cancel()
return errs
case err = <-errorCh:
cancel()
return err
}
}
func (c *client) startGlobalToZoneSync(ctx context.Context, log logr.Logger, conn *grpc.ClientConn, errorCh chan error) {
ddsClient := mesh_proto.NewDDSSyncServiceClient(conn)
log = log.WithValues("rpc", "global-to-zone")
log.Info("initializing Dubbo Discovery Service (DDS) stream for global to zone sync of resources with delta xDS")
stream, err := ddsClient.GlobalToZoneSync(ctx)
if err != nil {
errorCh <- err
return
}
processingErrorsCh := make(chan error)
c.globalToZoneCb.OnGlobalToZoneSyncStarted(stream, processingErrorsCh)
c.handleProcessingErrors(stream, log, processingErrorsCh, errorCh)
}
func (c *client) startZoneToGlobalSync(ctx context.Context, log logr.Logger, conn *grpc.ClientConn, errorCh chan error) {
ddsClient := mesh_proto.NewDDSSyncServiceClient(conn)
log = log.WithValues("rpc", "zone-to-global")
log.Info("initializing Dubbo Discovery Service (DDS) stream for zone to global sync of resources with delta xDS")
stream, err := ddsClient.ZoneToGlobalSync(ctx)
if err != nil {
errorCh <- err
return
}
processingErrorsCh := make(chan error)
c.zoneToGlobalCb.OnZoneToGlobalSyncStarted(stream, processingErrorsCh)
c.handleProcessingErrors(stream, log, processingErrorsCh, errorCh)
}
func (c *client) startHealthCheck(
ctx context.Context,
log logr.Logger,
conn *grpc.ClientConn,
errorCh chan error,
) {
client := mesh_proto.NewGlobalDDSServiceClient(conn)
log = log.WithValues("rpc", "healthcheck")
log.Info("starting")
prevInterval := 5 * time.Minute
ticker := time.NewTicker(prevInterval)
defer ticker.Stop()
for {
log.Info("sending health check")
resp, err := client.HealthCheck(ctx, &mesh_proto.ZoneHealthCheckRequest{})
if err != nil && !errors.Is(err, context.Canceled) {
if status.Code(err) == codes.Unimplemented {
log.Info("health check unimplemented in server, stopping")
return
}
log.Error(err, "health check failed")
errorCh <- errors.Wrap(err, "zone health check request failed")
} else if interval := resp.Interval.AsDuration(); interval > 0 {
if prevInterval != interval {
prevInterval = interval
log.Info("Global CP requested new healthcheck interval", "interval", interval)
}
ticker.Reset(interval)
}
select {
case <-ticker.C:
continue
case <-ctx.Done():
log.Info("stopping")
return
}
}
}
func (c *client) handleProcessingErrors(
stream grpc.ClientStream,
log logr.Logger,
processingErrorsCh chan error,
errorCh chan error,
) {
err := <-processingErrorsCh
if status.Code(err) == codes.Unimplemented {
log.Error(err, "rpc stream failed, because global CP does not implement this rpc. Upgrade remote CP.")
// backwards compatibility. Do not rethrow error, so DDS multiplex can still operate.
return
}
if errors.Is(err, context.Canceled) {
log.Info("rpc stream shutting down")
// Let's not propagate this error further as we've already cancelled the context
err = nil
} else {
log.Error(err, "rpc stream failed prematurely, will restart in background")
}
if err := stream.CloseSend(); err != nil {
log.Error(err, "CloseSend returned an error")
}
if err != nil {
errorCh <- err
}
}
func (c *client) NeedLeaderElection() bool {
return true
}
func tlsConfig(rootCaFile string, skipVerify bool) (*tls.Config, error) {
// #nosec G402 -- we let the user decide if they want to ignore verification
tlsConfig := &tls.Config{
InsecureSkipVerify: skipVerify,
MinVersion: tls.VersionTLS12,
}
if rootCaFile != "" {
roots := x509.NewCertPool()
caCert, err := os.ReadFile(rootCaFile)
if err != nil {
return nil, errors.Wrapf(err, "could not read certificate %s", rootCaFile)
}
ok := roots.AppendCertsFromPEM(caCert)
if !ok {
return nil, errors.New("failed to parse root certificate")
}
tlsConfig.RootCAs = roots
}
return tlsConfig, nil
}