11package io .envoyproxy .controlplane .cache ;
22
33import static com .google .common .base .Strings .isNullOrEmpty ;
4- import static io .envoyproxy .controlplane .cache .Resources .ApiVersion .V2 ;
54import static io .envoyproxy .controlplane .cache .Resources .ApiVersion .V3 ;
65import static io .envoyproxy .controlplane .cache .Resources .ResourceType .CLUSTER ;
76import static io .envoyproxy .controlplane .cache .Resources .ResourceType .ENDPOINT ;
87import static io .envoyproxy .controlplane .cache .Resources .ResourceType .LISTENER ;
98import static io .envoyproxy .controlplane .cache .Resources .ResourceType .ROUTE ;
109import static io .envoyproxy .controlplane .cache .Resources .ResourceType .SECRET ;
11- import static io .envoyproxy .envoy .config .filter .network .http_connection_manager .v2 .HttpConnectionManager .RouteSpecifierCase .RDS ;
1210
1311import com .google .common .base .Preconditions ;
1412import com .google .common .collect .ImmutableList ;
1715import com .google .protobuf .Any ;
1816import com .google .protobuf .InvalidProtocolBufferException ;
1917import 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 ;
3127import java .util .Collection ;
3228import java .util .List ;
3329import java .util .Map ;
3834public class Resources {
3935
4036 /**
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.
4339 */
4440 public enum ResourceType {
4541 CLUSTER ,
@@ -50,7 +46,6 @@ public enum ResourceType {
5046 }
5147
5248 public enum ApiVersion {
53- V2 ,
5449 V3
5550 }
5651
@@ -59,90 +54,47 @@ public enum ApiVersion {
5954 static final String FILTER_ENVOY_ROUTER = "envoy.filters.http.router" ;
6055 static final String FILTER_HTTP_CONNECTION_MANAGER = "envoy.http_connection_manager" ;
6156
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-
7857 public static class V3 {
7958
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 );
9777 }
9878
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 );
11981
12082 public static final Map <String , ResourceType > TYPE_URLS_TO_RESOURCE_TYPE =
12183 new ImmutableMap .Builder <String , ResourceType >()
12284 .put (Resources .V3 .CLUSTER_TYPE_URL , CLUSTER )
123- .put (Resources .V2 .CLUSTER_TYPE_URL , CLUSTER )
12485 .put (Resources .V3 .ENDPOINT_TYPE_URL , ENDPOINT )
125- .put (Resources .V2 .ENDPOINT_TYPE_URL , ENDPOINT )
12686 .put (Resources .V3 .LISTENER_TYPE_URL , LISTENER )
127- .put (Resources .V2 .LISTENER_TYPE_URL , LISTENER )
12887 .put (Resources .V3 .ROUTE_TYPE_URL , ROUTE )
129- .put (Resources .V2 .ROUTE_TYPE_URL , ROUTE )
13088 .put (Resources .V3 .SECRET_TYPE_URL , SECRET )
131- .put (Resources .V2 .SECRET_TYPE_URL , SECRET )
13289 .build ();
13390
13491 public static final Map <String , Class <? extends Message >> RESOURCE_TYPE_BY_URL =
13592 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 )
14698 .build ();
14799
148100 /**
@@ -171,26 +123,6 @@ public static String getResourceName(Message resource) {
171123 return ((Secret ) resource ).getName ();
172124 }
173125
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-
194126 return "" ;
195127 }
196128
@@ -223,35 +155,25 @@ public static Set<String> getResourceReferences(Collection<? extends Message> re
223155 if (r instanceof ClusterLoadAssignment || r instanceof RouteConfiguration ) {
224156 // Endpoints have no dependencies.
225157
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
227160 // currently retrieved in bulk, and not by name.
228161
229162 continue ;
230163 }
231-
232164 if (r instanceof Cluster ) {
233165 Cluster c = (Cluster ) r ;
234166
235167 // 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 ) {
248169 if (!isNullOrEmpty (c .getEdsClusterConfig ().getServiceName ())) {
249170 refs .add (c .getEdsClusterConfig ().getServiceName ());
250171 } else {
251172 refs .add (c .getName ());
252173 }
253174 }
254175 } else if (r instanceof Listener ) {
176+
255177 Listener l = (Listener ) r ;
256178
257179 // Extract the route configuration names from the HTTP connection manager.
@@ -262,49 +184,14 @@ public static Set<String> getResourceReferences(Collection<? extends Message> re
262184 }
263185
264186 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
308195 && !isNullOrEmpty (config .getRds ().getRouteConfigName ())) {
309196 refs .add (config .getRds ().getRouteConfigName ());
310197 }
@@ -323,28 +210,14 @@ public static Set<String> getResourceReferences(Collection<? extends Message> re
323210 return refs .build ();
324211 }
325212
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. */
329214 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 )) {
333216 return V3 ;
334217 }
335218
336219 throw new RuntimeException (String .format ("Unsupported API version for type URL %s" , typeUrl ));
337220 }
338221
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 () {}
350223}
0 commit comments