-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
EnvoyProtoData.java
587 lines (509 loc) · 16.4 KB
/
EnvoyProtoData.java
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
* Copyright 2019 The gRPC Authors
*
* Licensed 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 io.grpc.xds;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import io.envoyproxy.envoy.type.FractionalPercent;
import io.envoyproxy.envoy.type.FractionalPercent.DenominatorType;
import io.grpc.EquivalentAddressGroup;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* Defines gRPC data types for Envoy protobuf messages used in xDS protocol. Each data type has
* the same name as Envoy's corresponding protobuf message, but only with fields used by gRPC.
*
* <p>Each data type should define a {@code fromEnvoyProtoXXX} static method to convert an Envoy
* proto message to an instance of that data type.
*
* <p>For data types that need to be sent as protobuf messages, a {@code toEnvoyProtoXXX} instance
* method is defined to convert an instance to Envoy proto message.
*/
final class EnvoyProtoData {
// Prevent instantiation.
private EnvoyProtoData() {
}
/**
* See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.core.Locality}.
*/
static final class Locality {
private final String region;
private final String zone;
private final String subZone;
Locality(String region, String zone, String subZone) {
this.region = region;
this.zone = zone;
this.subZone = subZone;
}
static Locality fromEnvoyProtoLocality(io.envoyproxy.envoy.api.v2.core.Locality locality) {
return new Locality(
/* region = */ locality.getRegion(),
/* zone = */ locality.getZone(),
/* subZone = */ locality.getSubZone());
}
io.envoyproxy.envoy.api.v2.core.Locality toEnvoyProtoLocality() {
return io.envoyproxy.envoy.api.v2.core.Locality.newBuilder()
.setRegion(region)
.setZone(zone)
.setSubZone(subZone)
.build();
}
String getRegion() {
return region;
}
String getZone() {
return zone;
}
String getSubZone() {
return subZone;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Locality locality = (Locality) o;
return Objects.equals(region, locality.region)
&& Objects.equals(zone, locality.zone)
&& Objects.equals(subZone, locality.subZone);
}
@Override
public int hashCode() {
return Objects.hash(region, zone, subZone);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("region", region)
.add("zone", zone)
.add("subZone", subZone)
.toString();
}
}
/**
* See corresponding Envoy proto message {@link
* io.envoyproxy.envoy.api.v2.endpoint.LocalityLbEndpoints}.
*/
static final class LocalityLbEndpoints {
private final List<LbEndpoint> endpoints;
private final int localityWeight;
private final int priority;
/** Must only be used for testing. */
@VisibleForTesting
LocalityLbEndpoints(List<LbEndpoint> endpoints, int localityWeight, int priority) {
this.endpoints = endpoints;
this.localityWeight = localityWeight;
this.priority = priority;
}
static LocalityLbEndpoints fromEnvoyProtoLocalityLbEndpoints(
io.envoyproxy.envoy.api.v2.endpoint.LocalityLbEndpoints proto) {
List<LbEndpoint> endpoints = new ArrayList<>(proto.getLbEndpointsCount());
for (io.envoyproxy.envoy.api.v2.endpoint.LbEndpoint endpoint : proto.getLbEndpointsList()) {
endpoints.add(LbEndpoint.fromEnvoyProtoLbEndpoint(endpoint));
}
return
new LocalityLbEndpoints(
endpoints,
proto.getLoadBalancingWeight().getValue(),
proto.getPriority());
}
List<LbEndpoint> getEndpoints() {
return Collections.unmodifiableList(endpoints);
}
int getLocalityWeight() {
return localityWeight;
}
int getPriority() {
return priority;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LocalityLbEndpoints that = (LocalityLbEndpoints) o;
return localityWeight == that.localityWeight
&& priority == that.priority
&& Objects.equals(endpoints, that.endpoints);
}
@Override
public int hashCode() {
return Objects.hash(endpoints, localityWeight, priority);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("endpoints", endpoints)
.add("localityWeight", localityWeight)
.add("priority", priority)
.toString();
}
}
/**
* See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.endpoint.LbEndpoint}.
*/
static final class LbEndpoint {
private final EquivalentAddressGroup eag;
private final int loadBalancingWeight;
private final boolean isHealthy;
@VisibleForTesting
LbEndpoint(String address, int port, int loadBalancingWeight, boolean isHealthy) {
this(
new EquivalentAddressGroup(
new InetSocketAddress(address, port)),
loadBalancingWeight, isHealthy);
}
@VisibleForTesting
LbEndpoint(EquivalentAddressGroup eag, int loadBalancingWeight, boolean isHealthy) {
this.eag = eag;
this.loadBalancingWeight = loadBalancingWeight;
this.isHealthy = isHealthy;
}
static LbEndpoint fromEnvoyProtoLbEndpoint(
io.envoyproxy.envoy.api.v2.endpoint.LbEndpoint proto) {
io.envoyproxy.envoy.api.v2.core.SocketAddress socketAddress =
proto.getEndpoint().getAddress().getSocketAddress();
InetSocketAddress addr =
new InetSocketAddress(socketAddress.getAddress(), socketAddress.getPortValue());
return
new LbEndpoint(
new EquivalentAddressGroup(ImmutableList.<java.net.SocketAddress>of(addr)),
proto.getLoadBalancingWeight().getValue(),
proto.getHealthStatus() == io.envoyproxy.envoy.api.v2.core.HealthStatus.HEALTHY
|| proto.getHealthStatus() == io.envoyproxy.envoy.api.v2.core.HealthStatus.UNKNOWN
);
}
EquivalentAddressGroup getAddress() {
return eag;
}
int getLoadBalancingWeight() {
return loadBalancingWeight;
}
boolean isHealthy() {
return isHealthy;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LbEndpoint that = (LbEndpoint) o;
return loadBalancingWeight == that.loadBalancingWeight
&& Objects.equals(eag, that.eag)
&& isHealthy == that.isHealthy;
}
@Override
public int hashCode() {
return Objects.hash(eag, loadBalancingWeight, isHealthy);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("eag", eag)
.add("loadBalancingWeight", loadBalancingWeight)
.add("isHealthy", isHealthy)
.toString();
}
}
/**
* See corresponding Envoy proto message {@link
* io.envoyproxy.envoy.api.v2.ClusterLoadAssignment.Policy.DropOverload}.
*/
static final class DropOverload {
private final String category;
private final int dropsPerMillion;
/** Must only be used for testing. */
@VisibleForTesting
DropOverload(String category, int dropsPerMillion) {
this.category = category;
this.dropsPerMillion = dropsPerMillion;
}
static DropOverload fromEnvoyProtoDropOverload(
io.envoyproxy.envoy.api.v2.ClusterLoadAssignment.Policy.DropOverload proto) {
FractionalPercent percent = proto.getDropPercentage();
int numerator = percent.getNumerator();
DenominatorType type = percent.getDenominator();
switch (type) {
case TEN_THOUSAND:
numerator *= 100;
break;
case HUNDRED:
numerator *= 100_00;
break;
case MILLION:
break;
default:
throw new IllegalArgumentException("Unknown denominator type of " + percent);
}
if (numerator > 1_000_000) {
numerator = 1_000_000;
}
return new DropOverload(proto.getCategory(), numerator);
}
String getCategory() {
return category;
}
int getDropsPerMillion() {
return dropsPerMillion;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DropOverload that = (DropOverload) o;
return dropsPerMillion == that.dropsPerMillion && Objects.equals(category, that.category);
}
@Override
public int hashCode() {
return Objects.hash(category, dropsPerMillion);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("category", category)
.add("dropsPerMillion", dropsPerMillion)
.toString();
}
}
/** See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.route.Route}. */
static final class Route {
private final RouteMatch routeMatch;
@Nullable
private final RouteAction routeAction;
@VisibleForTesting
Route(RouteMatch routeMatch, @Nullable RouteAction routeAction) {
this.routeMatch = routeMatch;
this.routeAction = routeAction;
}
RouteMatch getRouteMatch() {
return routeMatch;
}
@Nullable
RouteAction getRouteAction() {
return routeAction;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Route route = (Route) o;
return Objects.equals(routeMatch, route.routeMatch)
&& Objects.equals(routeAction, route.routeAction);
}
@Override
public int hashCode() {
return Objects.hash(routeMatch, routeAction);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("routeMatch", routeMatch)
.add("routeAction", routeAction)
.toString();
}
static Route fromEnvoyProtoRoute(io.envoyproxy.envoy.api.v2.route.Route proto) {
RouteMatch routeMatch = RouteMatch.fromEnvoyProtoRouteMatch(proto.getMatch());
RouteAction routeAction = null;
if (proto.hasRoute()) {
routeAction = RouteAction.fromEnvoyProtoRouteAction(proto.getRoute());
}
return new Route(routeMatch, routeAction);
}
}
/** See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.route.RouteMatch}. */
static final class RouteMatch {
private final String prefix;
private final String path;
private final boolean hasRegex;
@VisibleForTesting
RouteMatch(String prefix, String path, boolean hasRegex) {
this.prefix = prefix;
this.path = path;
this.hasRegex = hasRegex;
}
String getPrefix() {
return prefix;
}
String getPath() {
return path;
}
boolean hasRegex() {
return hasRegex;
}
boolean isDefaultMatcher() {
if (hasRegex) {
return false;
}
if (!path.isEmpty()) {
return false;
}
return prefix.isEmpty() || prefix.equals("/");
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RouteMatch that = (RouteMatch) o;
return hasRegex == that.hasRegex
&& Objects.equals(prefix, that.prefix)
&& Objects.equals(path, that.path);
}
@Override
public int hashCode() {
return Objects.hash(prefix, path, hasRegex);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("prefix", prefix)
.add("path", path)
.add("hasRegex", hasRegex)
.toString();
}
private static RouteMatch fromEnvoyProtoRouteMatch(
io.envoyproxy.envoy.api.v2.route.RouteMatch proto) {
return new RouteMatch(
proto.getPrefix(), proto.getPath(), !proto.getRegex().isEmpty() || proto.hasSafeRegex());
}
}
/** See corresponding Envoy proto message {@link io.envoyproxy.envoy.api.v2.route.RouteAction}. */
static final class RouteAction {
private final String cluster;
private final String clusterHeader;
private final List<ClusterWeight> weightedCluster;
@VisibleForTesting
RouteAction(String cluster, String clusterHeader, List<ClusterWeight> weightedCluster) {
this.cluster = cluster;
this.clusterHeader = clusterHeader;
this.weightedCluster = Collections.unmodifiableList(weightedCluster);
}
String getCluster() {
return cluster;
}
String getClusterHeader() {
return clusterHeader;
}
List<ClusterWeight> getWeightedCluster() {
return weightedCluster;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RouteAction that = (RouteAction) o;
return Objects.equals(cluster, that.cluster)
&& Objects.equals(clusterHeader, that.clusterHeader)
&& Objects.equals(weightedCluster, that.weightedCluster);
}
@Override
public int hashCode() {
return Objects.hash(cluster, clusterHeader, weightedCluster);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("cluster", cluster)
.add("clusterHeader", clusterHeader)
.add("weightedCluster", weightedCluster)
.toString();
}
private static RouteAction fromEnvoyProtoRouteAction(
io.envoyproxy.envoy.api.v2.route.RouteAction proto) {
List<ClusterWeight> weightedCluster = new ArrayList<>();
List<io.envoyproxy.envoy.api.v2.route.WeightedCluster.ClusterWeight> clusterWeights
= proto.getWeightedClusters().getClustersList();
for (io.envoyproxy.envoy.api.v2.route.WeightedCluster.ClusterWeight clusterWeight
: clusterWeights) {
weightedCluster.add(ClusterWeight.fromEnvoyProtoClusterWeight(clusterWeight));
}
return new RouteAction(proto.getCluster(), proto.getClusterHeader(), weightedCluster);
}
}
/**
* See corresponding Envoy proto message {@link
* io.envoyproxy.envoy.api.v2.route.WeightedCluster.ClusterWeight}.
*/
static final class ClusterWeight {
private final String name;
private final int weight;
@VisibleForTesting
ClusterWeight(String name, int weight) {
this.name = name;
this.weight = weight;
}
String getName() {
return name;
}
int getWeight() {
return weight;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ClusterWeight that = (ClusterWeight) o;
return weight == that.weight && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name, weight);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", name)
.add("weight", weight)
.toString();
}
private static ClusterWeight fromEnvoyProtoClusterWeight(
io.envoyproxy.envoy.api.v2.route.WeightedCluster.ClusterWeight proto) {
return new ClusterWeight(proto.getName(), proto.getWeight().getValue());
}
}
}