1
1
"""
2
2
envoy helpers
3
3
"""
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")
32
4
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")
49
5
load(
50
6
"@ratelimit//ratelimit/config/ratelimit/v3/rls_conf.proto",
51
7
"RateLimitConfig",
52
8
"RateLimitDescriptor",
53
9
"RateLimitPolicy",
54
10
"RateLimitUnit",
55
11
)
56
- load("//example/wellknown.pinc", "wellknown")
12
+ load("//xds/lib.pinc", "xds")
13
+
57
14
58
15
ClusterName = "remote_cluster"
59
16
RouteName = "local_route"
@@ -63,111 +20,6 @@ UpstreamHost = "www.protoconf.dev"
63
20
UpstreamPort = 443
64
21
65
22
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
-
171
23
def makeRateLimiters():
172
24
return [
173
25
RateLimitConfig(
@@ -187,15 +39,36 @@ def makeRateLimiters():
187
39
]
188
40
189
41
190
- print(makeRateLimiters())
42
+ # print(makeRateLimiters())
191
43
192
44
193
45
def main():
194
46
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
+ )
201
74
}
0 commit comments