1+ import { Configs } from "." ;
2+
3+ const typeGetters = {
4+ "type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy" : ( obj ) => {
5+ return { type : "cluster" , value : obj . cluster } ;
6+ } ,
7+ "type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager" : ( obj ) => {
8+ return { type : "route" , value : obj . rds ?. route_config_name || 'Inline Route' } ;
9+ }
10+ }
11+
12+ const typeHandlers = {
13+ 'type.googleapis.com/envoy.config.listener.v3.Listener' : ( config , config_dump ) => {
14+ let chains = [ ] ;
15+ if ( config . default_filter_chain ) {
16+ chains . push ( config . default_filter_chain )
17+ }
18+ chains . push ( ...config . filter_chains )
19+ console . log ( chains ) ;
20+
21+ const result = [ ]
22+ chains . forEach ( chain => {
23+ console . log ( chain ) ;
24+ chain . filters . forEach ( async filter => {
25+ const fType = filter . typed_config [ "@type" ]
26+ console . log ( fType ) ;
27+ if ( ! typeGetters [ fType ] ) return
28+
29+ const target = typeGetters [ fType ] ( filter . typed_config )
30+ const links = useConfigRelationship ( findTargetConfig ( target , config_dump ) , config_dump )
31+ result . push ( ...links )
32+
33+ result . push ( { source : `listener: ${ config . name } ` , target : `${ target . type } : ${ target . value } ` , value : 1 } )
34+ } )
35+ } )
36+ return result
37+ } ,
38+ 'type.googleapis.com/envoy.config.route.v3.RouteConfiguration' : ( config , config_dump ) => {
39+ const result = [ ]
40+ config . virtual_hosts . forEach ( vh => {
41+ vh . routes . forEach ( route => {
42+ const target = route . route . cluster
43+ // console.log(target);
44+ const links = useConfigRelationship ( findTargetConfig ( { type : 'cluster' , value : target } , config_dump ) , config_dump )
45+ result . push ( ...links )
46+
47+ result . push ( { source : `route: ${ config . name } ` , target : `cluster: ${ target } ` , value : 1 } )
48+ } )
49+ } )
50+ return result
51+ } ,
52+ 'type.googleapis.com/envoy.config.cluster.v3.Cluster' : ( config , config_dump ) => {
53+ if ( config . type == 'EDS' ) {
54+ return useConfigRelationship ( findTargetConfig ( { type : 'endpoint' , value : config . eds_cluster_config . service_name } , config_dump ) , config_dump )
55+ }
56+
57+ return [ ]
58+ } ,
59+ 'type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment' : ( config , config_dump ) => {
60+ const result = [ ]
61+ config . endpoints ?. forEach ( endpoint => {
62+ endpoint . lb_endpoints . forEach ( lb_endpoint => {
63+ const target = lb_endpoint . endpoint . address . socket_address . address
64+ result . push ( { source : `cluster: ${ config . cluster_name } ` , target : `endpoint: ${ target } ` , value : 1 } )
65+ } )
66+ }
67+ )
68+ return result
69+ }
70+ }
71+
72+ export const findTargetConfig = ( target , config_dump ) => {
73+ const configTool = new Configs ( config_dump ) ;
74+ if ( target . type === 'route' ) {
75+ return configTool . getRouteConfigs ( ) . find ( c => c . name === target . value )
76+ }
77+
78+ if ( target . type === 'cluster' ) {
79+ return configTool . getClusterConfigs ( ) . find ( c => c . name === target . value )
80+ }
81+
82+ if ( target . type === 'endpoint' ) {
83+ return configTool . getEndpointConfigs ( ) . find ( c => c . cluster_name === target . value )
84+ }
85+ }
86+
87+ export const useConfigRelationship = ( cfg , config_dump ) => {
88+ const type = cfg [ "@type" ] ;
89+ console . log ( type , cfg , config_dump ) ;
90+ if ( ! typeHandlers [ type ] ) return [ ]
91+ const links = typeHandlers [ type ] ( cfg , config_dump )
92+ console . log ( links ) ;
93+ return links ;
94+ }
0 commit comments