Skip to content

Commit 3e0a334

Browse files
committed
feat: xds lib
1 parent f6942e5 commit 3e0a334

File tree

10 files changed

+409
-197
lines changed

10 files changed

+409
-197
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files.associations": {
44
"*.pconf": "Starlark",
55
"*.mpconf": "Starlark",
6-
"*.pinc": "Starlark",
6+
"*.pinc": "starlark",
77
"*.proto-validator": "Starlark",
88
"*.materialized_JSON": "json"
99
}

CONFIGSPACE

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ratelimit = remote_repo(
1616
exclude_file_regexps=["^google/.*"],
1717
)
1818

19-
xds = remote_repo(
19+
xds_proto = remote_repo(
2020
url="github.com/cncf/xds",
2121
commit="0335649",
2222
additional_proto_dirs=["."],
@@ -38,16 +38,8 @@ googleapis = remote_repo(
3838
],
3939
)
4040

41+
# Uncomment this in order to get the xds helpers
4142
# xds = remote_repo(
4243
# url="github.com/protoconf/protoconf-xds",
43-
# branch="main",
44-
# exclude_file_regexps=[
45-
# "google/.*",
46-
# "envoy/.*",
47-
# "io/prometheus/.*",
48-
# "opencensus/.*",
49-
# "opentelemetry/.*",
50-
# "udpa/.*",
51-
# "validate/.*",
52-
# ],
44+
# tag="v0.1.2",
5345
# )

embed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import (
44
"embed"
55
)
66

7-
//go:embed src
7+
//go:embed CONFIGSPACE
88
var InitTemplate embed.FS

materialized_config/example/envoy/test-id.materialized_JSON

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"@type": "type.googleapis.com/protoconfenvoy.v1.XDSSnapshot",
55
"clusters": [
66
{
7-
"name": "remote_cluster",
7+
"name": "origin",
88
"type": "LOGICAL_DNS",
99
"connectTimeout": "5s",
10+
"lbPolicy": "LEAST_REQUEST",
1011
"loadAssignment": {
11-
"clusterName": "remote_cluster",
12+
"clusterName": "origin",
1213
"endpoints": [
1314
{
1415
"lbEndpoints": [
@@ -26,35 +27,65 @@
2627
}
2728
]
2829
},
29-
"dnsLookupFamily": "V4_ONLY",
3030
"transportSocket": {
3131
"name": "envoy.transport_sockets.tls",
3232
"typedConfig": {
3333
"@type": "type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext",
3434
"sni": "www.protoconf.dev"
3535
}
3636
}
37+
},
38+
{
39+
"name": "google",
40+
"type": "LOGICAL_DNS",
41+
"loadAssignment": {
42+
"clusterName": "google",
43+
"endpoints": [
44+
{
45+
"lbEndpoints": [
46+
{
47+
"endpoint": {
48+
"address": {
49+
"socketAddress": {
50+
"address": "google.com",
51+
"portValue": 80
52+
}
53+
}
54+
}
55+
}
56+
]
57+
}
58+
]
59+
}
3760
}
3861
],
3962
"routes": [
4063
{
41-
"name": "local_route",
64+
"name": "default",
4265
"virtualHosts": [
4366
{
44-
"name": "local_service",
67+
"name": "default",
4568
"domains": [
46-
"*",
47-
"local_service"
69+
"*"
4870
],
4971
"routes": [
5072
{
5173
"match": {
52-
"prefix": "/"
74+
"prefix": "/protoconf"
5375
},
5476
"route": {
55-
"cluster": "remote_cluster",
77+
"cluster": "origin",
5678
"hostRewriteLiteral": "www.protoconf.dev"
5779
}
80+
},
81+
{
82+
"match": {
83+
"prefix": "/google"
84+
},
85+
"route": {
86+
"cluster": "google",
87+
"hostRewriteLiteral": "google.com"
88+
}
5889
}
5990
]
6091
}
@@ -63,7 +94,7 @@
6394
],
6495
"listeners": [
6596
{
66-
"name": "listener_0",
97+
"name": "default",
6798
"address": {
6899
"socketAddress": {
69100
"address": "0.0.0.0",
@@ -94,7 +125,7 @@
94125
},
95126
"resourceApiVersion": "V3"
96127
},
97-
"routeConfigName": "local_route"
128+
"routeConfigName": "default"
98129
},
99130
"httpFilters": [
100131
{
@@ -110,22 +141,6 @@
110141
}
111142
]
112143
}
113-
],
114-
"ratelimits": [
115-
{
116-
"domain": "local_service",
117-
"descriptors": [
118-
{
119-
"key": "remote_address",
120-
"value": "blah",
121-
"rateLimit": {
122-
"unit": "MINUTE",
123-
"requestsPerUnit": 10000,
124-
"name": "local_service_limit"
125-
}
126-
}
127-
]
128-
}
129144
]
130145
}
131146
}

src/example/envoy.mpconf

Lines changed: 30 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,16 @@
11
"""
22
envoy helpers
33
"""
4-
load("//protoconfxds/v1/protoconfxds.proto", "XDSSnapshot")
5-
load(
6-
"@envoy//envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.proto",
7-
"HttpConnectionManager",
8-
)
9-
load("@envoy//envoy/config/route/v3/route.proto", "RouteConfiguration")
10-
load("@envoy//envoy/config/listener/v3/listener.proto", "Listener")
11-
load("@envoy//envoy/config/cluster/v3/cluster.proto", "Cluster")
12-
load("@envoy//envoy/extensions/transport_sockets/tls/v3/tls.proto", "UpstreamTlsContext")
13-
load(
14-
"@envoy//envoy/config/core/v3/config_source.proto",
15-
"ConfigSource",
16-
"ApiConfigSource",
17-
)
18-
load("@envoy//envoy/config/core/v3/grpc_service.proto", "GrpcService")
19-
load(
20-
"@envoy//envoy/config/listener/v3/listener_components.proto",
21-
"FilterChain",
22-
"Filter",
23-
)
24-
load(
25-
"@envoy//envoy/config/route/v3/route_components.proto",
26-
"VirtualHost",
27-
"Route",
28-
"RouteMatch",
29-
"RouteAction",
30-
)
31-
load("@envoy//envoy/config/core/v3/address.proto", "Address", "SocketAddress")
324
load("/google/protobuf/duration.proto", "Duration")
33-
load("any.star", "any")
34-
load("@envoy//envoy/extensions/filters/http/router/v3/router.proto", "Router")
35-
load(
36-
"@envoy//envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.proto",
37-
"Rds",
38-
"HttpFilter",
39-
)
40-
load("@envoy//envoy/config/core/v3/config_source.proto", "ApiVersion")
41-
load(
42-
"@envoy//envoy/config/endpoint/v3/endpoint_components.proto",
43-
"LocalityLbEndpoints",
44-
"LbEndpoint",
45-
"Endpoint",
46-
)
47-
load("@envoy//envoy/config/endpoint/v3/endpoint.proto", "ClusterLoadAssignment")
48-
load("@envoy//envoy/config/core/v3/base.proto", "TransportSocket")
495
load(
506
"@ratelimit//ratelimit/config/ratelimit/v3/rls_conf.proto",
517
"RateLimitConfig",
528
"RateLimitDescriptor",
539
"RateLimitPolicy",
5410
"RateLimitUnit",
5511
)
56-
load("//example/wellknown.pinc", "wellknown")
12+
load("//xds/lib.pinc", "xds")
13+
5714

5815
ClusterName = "remote_cluster"
5916
RouteName = "local_route"
@@ -63,111 +20,6 @@ UpstreamHost = "www.protoconf.dev"
6320
UpstreamPort = 443
6421

6522

66-
def makeCluster(clusterName):
67-
return Cluster(
68-
name=clusterName,
69-
connect_timeout=Duration(seconds=5),
70-
type=Cluster.DiscoveryType.LOGICAL_DNS,
71-
lb_policy=Cluster.LbPolicy.ROUND_ROBIN,
72-
load_assignment=makeEndpoint(clusterName),
73-
transport_socket=TransportSocket(
74-
name="envoy.transport_sockets.tls",
75-
typed_config=any.new(UpstreamTlsContext(sni=UpstreamHost,)),
76-
),
77-
dns_lookup_family=Cluster.DnsLookupFamily.V4_ONLY,
78-
)
79-
80-
81-
def makeEndpoint(clusterName):
82-
return ClusterLoadAssignment(
83-
cluster_name=clusterName,
84-
endpoints=[
85-
LocalityLbEndpoints(
86-
lb_endpoints=[
87-
LbEndpoint(
88-
endpoint=Endpoint(
89-
address=Address(
90-
socket_address=SocketAddress(
91-
protocol=SocketAddress.Protocol.TCP,
92-
address=UpstreamHost,
93-
port_value=UpstreamPort,
94-
),
95-
),
96-
),
97-
)
98-
],
99-
)
100-
],
101-
)
102-
103-
104-
def makeRoute(routeName, clusterName):
105-
return RouteConfiguration(
106-
name=routeName,
107-
virtual_hosts=[
108-
VirtualHost(
109-
name="local_service",
110-
domains=["*", "local_service"],
111-
routes=[
112-
Route(
113-
match=RouteMatch(prefix="/",),
114-
route=RouteAction(
115-
cluster=clusterName, host_rewrite_literal=UpstreamHost,
116-
),
117-
)
118-
],
119-
)
120-
],
121-
)
122-
123-
124-
def makeHTTPListener(listenerName, route):
125-
routerConfig = any.new(Router())
126-
# HTTP filter configuration
127-
manager = HttpConnectionManager(
128-
codec_type=HttpConnectionManager.CodecType.AUTO,
129-
stat_prefix="http",
130-
rds=Rds(config_source=makeConfigSource(), route_config_name=route,),
131-
http_filters=[HttpFilter(name=wellknown.Router, typed_config=routerConfig,)],
132-
)
133-
pbst = any.new(manager)
134-
135-
return Listener(
136-
name=listenerName,
137-
address=Address(
138-
socket_address=SocketAddress(
139-
protocol=SocketAddress.Protocol.TCP,
140-
address="0.0.0.0",
141-
port_value=ListenerPort,
142-
),
143-
),
144-
filter_chains=[
145-
FilterChain(
146-
filters=[
147-
Filter(name=wellknown.HTTPConnectionManager, typed_config=pbst,)
148-
],
149-
)
150-
],
151-
)
152-
153-
154-
def makeConfigSource():
155-
source = ConfigSource(
156-
resource_api_version=ApiVersion.V3,
157-
api_config_source=ApiConfigSource(
158-
transport_api_version=ApiVersion.V3,
159-
api_type=ApiConfigSource.ApiType.GRPC,
160-
set_node_on_first_message_only=True,
161-
grpc_services=[
162-
GrpcService(
163-
envoy_grpc=GrpcService.EnvoyGrpc(cluster_name="xds_cluster")
164-
)
165-
],
166-
),
167-
)
168-
return source
169-
170-
17123
def makeRateLimiters():
17224
return [
17325
RateLimitConfig(
@@ -187,15 +39,36 @@ def makeRateLimiters():
18739
]
18840

18941

190-
print(makeRateLimiters())
42+
# print(makeRateLimiters())
19143

19244

19345
def main():
19446
return {
195-
"test-id": XDSSnapshot(
196-
clusters=[makeCluster(ClusterName)],
197-
routes=[makeRoute(RouteName, ClusterName)],
198-
listeners=[makeHTTPListener(ListenerName, RouteName)],
199-
ratelimits=makeRateLimiters(),
200-
),
47+
"test-id": xds.Snapshot(
48+
xds.Cluster(
49+
"origin",
50+
xds.LeastRequest,
51+
xds.DnsDiscovery,
52+
xds.WithEndpoint(UpstreamHost, UpstreamPort),
53+
xds.WithConnectTimeout(Duration(seconds=5)),
54+
xds.UpstreamTLS(sni=UpstreamHost),
55+
),
56+
xds.Cluster(
57+
"google", xds.WithEndpoint("google.com", 80), xds.DnsDiscovery
58+
),
59+
xds.Listener(
60+
"default",
61+
xds.WithTCP("0.0.0.0", 10000),
62+
xds.WithRdsHTTPRoute("default"),
63+
),
64+
xds.VirtualHost(
65+
"default",
66+
xds.Domains("*"),
67+
xds.ToCluster(
68+
"origin", xds.IfPrefix("/protoconf"), xds.HostRewrite(UpstreamHost)
69+
),
70+
xds.ToCluster("google", xds.IfPrefix("/google"), xds.HostRewrite("google.com")),
71+
),
72+
xds.Debug,
73+
)
20174
}

0 commit comments

Comments
 (0)