1
1
package io .envoyproxy .controlplane .cache ;
2
2
3
3
import static com .google .common .base .Strings .isNullOrEmpty ;
4
- import static io .envoyproxy .controlplane .cache .Resources .ApiVersion .V2 ;
5
4
import static io .envoyproxy .controlplane .cache .Resources .ApiVersion .V3 ;
6
5
import static io .envoyproxy .controlplane .cache .Resources .ResourceType .CLUSTER ;
7
6
import static io .envoyproxy .controlplane .cache .Resources .ResourceType .ENDPOINT ;
8
7
import static io .envoyproxy .controlplane .cache .Resources .ResourceType .LISTENER ;
9
8
import static io .envoyproxy .controlplane .cache .Resources .ResourceType .ROUTE ;
10
9
import static io .envoyproxy .controlplane .cache .Resources .ResourceType .SECRET ;
11
- import static io .envoyproxy .envoy .config .filter .network .http_connection_manager .v2 .HttpConnectionManager .RouteSpecifierCase .RDS ;
12
10
13
11
import com .google .common .base .Preconditions ;
14
12
import com .google .common .collect .ImmutableList ;
17
15
import com .google .protobuf .Any ;
18
16
import com .google .protobuf .InvalidProtocolBufferException ;
19
17
import com .google .protobuf .Message ;
20
- import com .google .protobuf .Struct ;
21
- import com .google .protobuf .util .JsonFormat ;
22
- import io .envoyproxy .envoy .api .v2 .Cluster ;
23
- import io .envoyproxy .envoy .api .v2 .Cluster .DiscoveryType ;
24
- import io .envoyproxy .envoy .api .v2 .ClusterLoadAssignment ;
25
- import io .envoyproxy .envoy .api .v2 .Listener ;
26
- import io .envoyproxy .envoy .api .v2 .RouteConfiguration ;
27
- import io .envoyproxy .envoy .api .v2 .auth .Secret ;
28
- import io .envoyproxy .envoy .api .v2 .listener .Filter ;
29
- import io .envoyproxy .envoy .api .v2 .listener .FilterChain ;
30
- import io .envoyproxy .envoy .config .filter .network .http_connection_manager .v2 .HttpConnectionManager ;
18
+ import io .envoyproxy .envoy .config .cluster .v3 .Cluster ;
19
+ import io .envoyproxy .envoy .config .endpoint .v3 .ClusterLoadAssignment ;
20
+ import io .envoyproxy .envoy .config .listener .v3 .Filter ;
21
+ import io .envoyproxy .envoy .config .listener .v3 .FilterChain ;
22
+ import io .envoyproxy .envoy .config .listener .v3 .Listener ;
23
+ import io .envoyproxy .envoy .config .route .v3 .RouteConfiguration ;
24
+ import io .envoyproxy .envoy .extensions .filters .network .http_connection_manager .v3 .HttpConnectionManager ;
25
+ import io .envoyproxy .envoy .extensions .filters .network .http_connection_manager .v3 .HttpConnectionManager .RouteSpecifierCase ;
26
+ import io .envoyproxy .envoy .extensions .transport_sockets .tls .v3 .Secret ;
31
27
import java .util .Collection ;
32
28
import java .util .List ;
33
29
import java .util .Map ;
38
34
public class Resources {
39
35
40
36
/**
41
- * Version-agnostic representation of a resource. This is useful when the version qualifier
42
- * isn't needed.
37
+ * Version-agnostic representation of a resource. This is useful when the version qualifier isn't
38
+ * needed.
43
39
*/
44
40
public enum ResourceType {
45
41
CLUSTER ,
@@ -50,7 +46,6 @@ public enum ResourceType {
50
46
}
51
47
52
48
public enum ApiVersion {
53
- V2 ,
54
49
V3
55
50
}
56
51
@@ -59,90 +54,47 @@ public enum ApiVersion {
59
54
static final String FILTER_ENVOY_ROUTER = "envoy.filters.http.router" ;
60
55
static final String FILTER_HTTP_CONNECTION_MANAGER = "envoy.http_connection_manager" ;
61
56
62
- public static class V2 {
63
- private static final String TYPE_URL_PREFIX = "type.googleapis.com/envoy.api.v2." ;
64
- public static final String SECRET_TYPE_URL = TYPE_URL_PREFIX + "auth.Secret" ;
65
- public static final String ROUTE_TYPE_URL = TYPE_URL_PREFIX + "RouteConfiguration" ;
66
- public static final String LISTENER_TYPE_URL = TYPE_URL_PREFIX + "Listener" ;
67
- public static final String ENDPOINT_TYPE_URL = TYPE_URL_PREFIX + "ClusterLoadAssignment" ;
68
- public static final String CLUSTER_TYPE_URL = TYPE_URL_PREFIX + "Cluster" ;
69
-
70
- public static final List <String > TYPE_URLS = ImmutableList .of (
71
- CLUSTER_TYPE_URL ,
72
- ENDPOINT_TYPE_URL ,
73
- LISTENER_TYPE_URL ,
74
- ROUTE_TYPE_URL ,
75
- SECRET_TYPE_URL );
76
- }
77
-
78
57
public static class V3 {
79
58
80
- public static final String CLUSTER_TYPE_URL = "type.googleapis.com/envoy.config.cluster.v3"
81
- + ".Cluster" ;
82
- public static final String ENDPOINT_TYPE_URL = "type.googleapis.com/envoy.config.endpoint.v3"
83
- + ".ClusterLoadAssignment" ;
84
- public static final String LISTENER_TYPE_URL = "type.googleapis.com/envoy.config.listener.v3"
85
- + ".Listener" ;
86
- public static final String ROUTE_TYPE_URL = "type.googleapis.com/envoy.config.route.v3"
87
- + ".RouteConfiguration" ;
88
- public static final String SECRET_TYPE_URL = "type.googleapis.com/envoy.extensions"
89
- + ".transport_sockets.tls.v3.Secret" ;
90
-
91
- public static final List <String > TYPE_URLS = ImmutableList .of (
92
- CLUSTER_TYPE_URL ,
93
- ENDPOINT_TYPE_URL ,
94
- LISTENER_TYPE_URL ,
95
- ROUTE_TYPE_URL ,
96
- SECRET_TYPE_URL );
59
+ public static final String CLUSTER_TYPE_URL =
60
+ "type.googleapis.com/envoy.config.cluster.v3" + ".Cluster" ;
61
+ public static final String ENDPOINT_TYPE_URL =
62
+ "type.googleapis.com/envoy.config.endpoint.v3" + ".ClusterLoadAssignment" ;
63
+ public static final String LISTENER_TYPE_URL =
64
+ "type.googleapis.com/envoy.config.listener.v3" + ".Listener" ;
65
+ public static final String ROUTE_TYPE_URL =
66
+ "type.googleapis.com/envoy.config.route.v3" + ".RouteConfiguration" ;
67
+ public static final String SECRET_TYPE_URL =
68
+ "type.googleapis.com/envoy.extensions" + ".transport_sockets.tls.v3.Secret" ;
69
+
70
+ public static final List <String > TYPE_URLS =
71
+ ImmutableList .of (
72
+ CLUSTER_TYPE_URL ,
73
+ ENDPOINT_TYPE_URL ,
74
+ LISTENER_TYPE_URL ,
75
+ ROUTE_TYPE_URL ,
76
+ SECRET_TYPE_URL );
97
77
}
98
78
99
- public static final List <ResourceType > RESOURCE_TYPES_IN_ORDER = ImmutableList .of (
100
- CLUSTER ,
101
- ENDPOINT ,
102
- LISTENER ,
103
- ROUTE ,
104
- SECRET );
105
-
106
- public static final Map <String , String > V3_TYPE_URLS_TO_V2 = ImmutableMap .of (
107
- Resources .V3 .CLUSTER_TYPE_URL , Resources .V2 .CLUSTER_TYPE_URL ,
108
- Resources .V3 .ENDPOINT_TYPE_URL , Resources .V2 .ENDPOINT_TYPE_URL ,
109
- Resources .V3 .LISTENER_TYPE_URL , Resources .V2 .LISTENER_TYPE_URL ,
110
- Resources .V3 .ROUTE_TYPE_URL , Resources .V2 .ROUTE_TYPE_URL ,
111
- Resources .V3 .SECRET_TYPE_URL , Resources .V2 .SECRET_TYPE_URL );
112
-
113
- public static final Map <String , String > V2_TYPE_URLS_TO_V3 = ImmutableMap .of (
114
- Resources .V2 .CLUSTER_TYPE_URL , Resources .V3 .CLUSTER_TYPE_URL ,
115
- Resources .V2 .ENDPOINT_TYPE_URL , Resources .V3 .ENDPOINT_TYPE_URL ,
116
- Resources .V2 .LISTENER_TYPE_URL , Resources .V3 .LISTENER_TYPE_URL ,
117
- Resources .V2 .ROUTE_TYPE_URL , Resources .V3 .ROUTE_TYPE_URL ,
118
- Resources .V2 .SECRET_TYPE_URL , Resources .V3 .SECRET_TYPE_URL );
79
+ public static final List <ResourceType > RESOURCE_TYPES_IN_ORDER =
80
+ ImmutableList .of (CLUSTER , ENDPOINT , LISTENER , ROUTE , SECRET );
119
81
120
82
public static final Map <String , ResourceType > TYPE_URLS_TO_RESOURCE_TYPE =
121
83
new ImmutableMap .Builder <String , ResourceType >()
122
84
.put (Resources .V3 .CLUSTER_TYPE_URL , CLUSTER )
123
- .put (Resources .V2 .CLUSTER_TYPE_URL , CLUSTER )
124
85
.put (Resources .V3 .ENDPOINT_TYPE_URL , ENDPOINT )
125
- .put (Resources .V2 .ENDPOINT_TYPE_URL , ENDPOINT )
126
86
.put (Resources .V3 .LISTENER_TYPE_URL , LISTENER )
127
- .put (Resources .V2 .LISTENER_TYPE_URL , LISTENER )
128
87
.put (Resources .V3 .ROUTE_TYPE_URL , ROUTE )
129
- .put (Resources .V2 .ROUTE_TYPE_URL , ROUTE )
130
88
.put (Resources .V3 .SECRET_TYPE_URL , SECRET )
131
- .put (Resources .V2 .SECRET_TYPE_URL , SECRET )
132
89
.build ();
133
90
134
91
public static final Map <String , Class <? extends Message >> RESOURCE_TYPE_BY_URL =
135
92
new ImmutableMap .Builder <String , Class <? extends Message >>()
136
- .put (Resources .V2 .CLUSTER_TYPE_URL , Cluster .class )
137
- .put (Resources .V2 .ENDPOINT_TYPE_URL , ClusterLoadAssignment .class )
138
- .put (Resources .V2 .LISTENER_TYPE_URL , Listener .class )
139
- .put (Resources .V2 .ROUTE_TYPE_URL , RouteConfiguration .class )
140
- .put (Resources .V2 .SECRET_TYPE_URL , Secret .class )
141
- .put (Resources .V3 .CLUSTER_TYPE_URL , io .envoyproxy .envoy .config .cluster .v3 .Cluster .class )
142
- .put (Resources .V3 .ENDPOINT_TYPE_URL , io .envoyproxy .envoy .config .endpoint .v3 .ClusterLoadAssignment .class )
143
- .put (Resources .V3 .LISTENER_TYPE_URL , io .envoyproxy .envoy .config .listener .v3 .Listener .class )
144
- .put (Resources .V3 .ROUTE_TYPE_URL , io .envoyproxy .envoy .config .route .v3 .RouteConfiguration .class )
145
- .put (Resources .V3 .SECRET_TYPE_URL , io .envoyproxy .envoy .extensions .transport_sockets .tls .v3 .Secret .class )
93
+ .put (Resources .V3 .CLUSTER_TYPE_URL , Cluster .class )
94
+ .put (Resources .V3 .ENDPOINT_TYPE_URL , ClusterLoadAssignment .class )
95
+ .put (Resources .V3 .LISTENER_TYPE_URL , Listener .class )
96
+ .put (Resources .V3 .ROUTE_TYPE_URL , RouteConfiguration .class )
97
+ .put (Resources .V3 .SECRET_TYPE_URL , Secret .class )
146
98
.build ();
147
99
148
100
/**
@@ -171,26 +123,6 @@ public static String getResourceName(Message resource) {
171
123
return ((Secret ) resource ).getName ();
172
124
}
173
125
174
- if (resource instanceof io .envoyproxy .envoy .config .cluster .v3 .Cluster ) {
175
- return ((io .envoyproxy .envoy .config .cluster .v3 .Cluster ) resource ).getName ();
176
- }
177
-
178
- if (resource instanceof io .envoyproxy .envoy .config .endpoint .v3 .ClusterLoadAssignment ) {
179
- return ((io .envoyproxy .envoy .config .endpoint .v3 .ClusterLoadAssignment ) resource ).getClusterName ();
180
- }
181
-
182
- if (resource instanceof io .envoyproxy .envoy .config .listener .v3 .Listener ) {
183
- return ((io .envoyproxy .envoy .config .listener .v3 .Listener ) resource ).getName ();
184
- }
185
-
186
- if (resource instanceof io .envoyproxy .envoy .config .route .v3 .RouteConfiguration ) {
187
- return ((io .envoyproxy .envoy .config .route .v3 .RouteConfiguration ) resource ).getName ();
188
- }
189
-
190
- if (resource instanceof io .envoyproxy .envoy .extensions .transport_sockets .tls .v3 .Secret ) {
191
- return ((io .envoyproxy .envoy .extensions .transport_sockets .tls .v3 .Secret ) resource ).getName ();
192
- }
193
-
194
126
return "" ;
195
127
}
196
128
@@ -223,35 +155,25 @@ public static Set<String> getResourceReferences(Collection<? extends Message> re
223
155
if (r instanceof ClusterLoadAssignment || r instanceof RouteConfiguration ) {
224
156
// Endpoints have no dependencies.
225
157
226
- // References to clusters in routes (and listeners) are not included in the result, because the clusters are
158
+ // References to clusters in routes (and listeners) are not included in the result, because
159
+ // the clusters are
227
160
// currently retrieved in bulk, and not by name.
228
161
229
162
continue ;
230
163
}
231
-
232
164
if (r instanceof Cluster ) {
233
165
Cluster c = (Cluster ) r ;
234
166
235
167
// For EDS clusters, use the cluster name or the service name override.
236
- if (c .getType () == DiscoveryType .EDS ) {
237
- if (!isNullOrEmpty (c .getEdsClusterConfig ().getServiceName ())) {
238
- refs .add (c .getEdsClusterConfig ().getServiceName ());
239
- } else {
240
- refs .add (c .getName ());
241
- }
242
- }
243
- } else if (r instanceof io .envoyproxy .envoy .config .cluster .v3 .Cluster ) {
244
- io .envoyproxy .envoy .config .cluster .v3 .Cluster c = (io .envoyproxy .envoy .config .cluster .v3 .Cluster ) r ;
245
-
246
- // For EDS clusters, use the cluster name or the service name override.
247
- if (c .getType () == io .envoyproxy .envoy .config .cluster .v3 .Cluster .DiscoveryType .EDS ) {
168
+ if (c .getType () == Cluster .DiscoveryType .EDS ) {
248
169
if (!isNullOrEmpty (c .getEdsClusterConfig ().getServiceName ())) {
249
170
refs .add (c .getEdsClusterConfig ().getServiceName ());
250
171
} else {
251
172
refs .add (c .getName ());
252
173
}
253
174
}
254
175
} else if (r instanceof Listener ) {
176
+
255
177
Listener l = (Listener ) r ;
256
178
257
179
// Extract the route configuration names from the HTTP connection manager.
@@ -262,49 +184,14 @@ public static Set<String> getResourceReferences(Collection<? extends Message> re
262
184
}
263
185
264
186
try {
265
- HttpConnectionManager config ;
266
-
267
- if (filter .hasTypedConfig ()) {
268
- config = filter .getTypedConfig ().unpack (HttpConnectionManager .class );
269
- } else {
270
- HttpConnectionManager .Builder builder = HttpConnectionManager .newBuilder ();
271
- structAsMessage (filter .getConfig (), builder );
272
- config = builder .build ();
273
- }
274
-
275
- if (config .getRouteSpecifierCase () == RDS && !isNullOrEmpty (config .getRds ().getRouteConfigName ())) {
276
- refs .add (config .getRds ().getRouteConfigName ());
277
- }
278
- } catch (InvalidProtocolBufferException e ) {
279
- LOGGER .error (
280
- "Failed to convert v2 HTTP connection manager config struct into protobuf "
281
- + "message for listener {}" ,
282
- getResourceName (l ),
283
- e );
284
- }
285
- }
286
- }
287
- } else if (r instanceof io .envoyproxy .envoy .config .listener .v3 .Listener ) {
288
-
289
- io .envoyproxy .envoy .config .listener .v3 .Listener l =
290
- (io .envoyproxy .envoy .config .listener .v3 .Listener ) r ;
291
-
292
- // Extract the route configuration names from the HTTP connection manager.
293
- for (io .envoyproxy .envoy .config .listener .v3 .FilterChain chain : l .getFilterChainsList ()) {
294
- for (io .envoyproxy .envoy .config .listener .v3 .Filter filter : chain .getFiltersList ()) {
295
- if (!filter .getName ().equals (FILTER_HTTP_CONNECTION_MANAGER )) {
296
- continue ;
297
- }
298
-
299
- try {
300
- io .envoyproxy .envoy .extensions .filters .network
301
- .http_connection_manager .v3 .HttpConnectionManager config = filter
302
- .getTypedConfig ().unpack (
303
- io .envoyproxy .envoy .extensions .filters .network
304
- .http_connection_manager .v3 .HttpConnectionManager .class );
305
-
306
- if (config .getRouteSpecifierCase () == io .envoyproxy .envoy .extensions .filters .network
307
- .http_connection_manager .v3 .HttpConnectionManager .RouteSpecifierCase .RDS
187
+ HttpConnectionManager config =
188
+ filter
189
+ .getTypedConfig ()
190
+ .unpack (
191
+ io .envoyproxy .envoy .extensions .filters .network .http_connection_manager .v3
192
+ .HttpConnectionManager .class );
193
+
194
+ if (config .getRouteSpecifierCase () == RouteSpecifierCase .RDS
308
195
&& !isNullOrEmpty (config .getRds ().getRouteConfigName ())) {
309
196
refs .add (config .getRds ().getRouteConfigName ());
310
197
}
@@ -323,28 +210,14 @@ public static Set<String> getResourceReferences(Collection<? extends Message> re
323
210
return refs .build ();
324
211
}
325
212
326
- /**
327
- * Returns the API version (v2 or v3) for a given type URL.
328
- */
213
+ /** Returns the supported API version for a given type URL. */
329
214
public static ApiVersion getResourceApiVersion (String typeUrl ) {
330
- if (Resources .V2 .TYPE_URLS .contains (typeUrl )) {
331
- return V2 ;
332
- } else if (Resources .V3 .TYPE_URLS .contains (typeUrl )) {
215
+ if (Resources .V3 .TYPE_URLS .contains (typeUrl )) {
333
216
return V3 ;
334
217
}
335
218
336
219
throw new RuntimeException (String .format ("Unsupported API version for type URL %s" , typeUrl ));
337
220
}
338
221
339
- private static void structAsMessage (Struct struct , Message .Builder messageBuilder )
340
- throws InvalidProtocolBufferException {
341
-
342
- String json = JsonFormat .printer ()
343
- .preservingProtoFieldNames ()
344
- .print (struct );
345
-
346
- JsonFormat .parser ().merge (json , messageBuilder );
347
- }
348
-
349
- private Resources () { }
222
+ private Resources () {}
350
223
}
0 commit comments