forked from k8sgateway/k8sgateway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
180 lines (153 loc) · 4.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/gogo/protobuf/types"
v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoy_api_v2_core1 "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
envoylistener "github.com/envoyproxy/go-control-plane/envoy/api/v2/listener"
envoyhttp "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/http_connection_manager/v2"
envoyutil "github.com/envoyproxy/go-control-plane/pkg/util"
"github.com/ghodss/yaml"
"github.com/gogo/protobuf/proto"
"github.com/k0kubun/pp"
"github.com/solo-io/go-utils/protoutils"
"google.golang.org/grpc"
)
var dr v2.DiscoveryRequest
func init() {
dr.Node = new(envoy_api_v2_core1.Node)
}
func GetYaml(pb proto.Message) []byte {
jsn, err := protoutils.MarshalBytes(pb)
data, err := yaml.JSONToYAML(jsn)
if err != nil {
panic(err)
}
return data
}
func main() {
role := flag.String("r", "gloo-system~clusteringress-proxy", "role to register with")
port := flag.String("p", "9977", "gloo port")
//out := flag.String("o", "gostructs", "output fmt gostructs|yaml")
flag.Parse()
dr.Node.Metadata = &types.Struct{
Fields: map[string]*types.Value{"role": {Kind: &types.Value_StringValue{StringValue: *role}}}}
conn, err := grpc.Dial("localhost:"+*port, grpc.WithInsecure())
if err != nil {
log.Fatalf("dial err: %v", err)
}
ctx := context.Background()
clusters := listClusters(ctx, conn)
listeners := listListeners(ctx, conn)
var hcms []envoyhttp.HttpConnectionManager
for _, l := range listeners {
for _, fc := range l.FilterChains {
for _, filter := range fc.Filters {
if filter.Name == "envoy.http_connection_manager" {
var hcm envoyhttp.HttpConnectionManager
switch config := filter.ConfigType.(type) {
case *envoylistener.Filter_Config:
if err := envoyutil.StructToMessage(config.Config, &hcm); err == nil {
hcms = append(hcms, hcm)
}
case *envoylistener.Filter_TypedConfig:
if err := types.UnmarshalAny(config.TypedConfig, &hcm); err == nil {
hcms = append(hcms, hcm)
}
}
}
}
}
}
var routes []string
for _, hcm := range hcms {
routes = append(routes, hcm.RouteSpecifier.(*envoyhttp.HttpConnectionManager_Rds).Rds.RouteConfigName)
}
rds := listRoutes(ctx, conn, routes)
eds := listendpoints(ctx, conn)
fmt.Printf("\n\n#clusters")
for _, c := range clusters {
fmt.Printf("\n%s", GetYaml(&c))
}
fmt.Printf("\n\n#listeners")
for _, c := range listeners {
fmt.Printf("\n%s", GetYaml(&c))
}
fmt.Printf("\n\n#rds")
for _, c := range rds {
fmt.Printf("\n%s", GetYaml(&c))
}
fmt.Printf("\n\n#eds")
for _, c := range eds {
fmt.Printf("\n%s", GetYaml(&c))
}
}
func listClusters(ctx context.Context, conn *grpc.ClientConn) []v2.Cluster {
// clusters
cdsc := v2.NewClusterDiscoveryServiceClient(conn)
dresp, err := cdsc.FetchClusters(ctx, &dr)
if err != nil {
log.Fatalf("clusters err: %v", err)
}
var clusters []v2.Cluster
for _, anyCluster := range dresp.Resources {
var cluster v2.Cluster
cluster.Unmarshal(anyCluster.Value)
clusters = append(clusters, cluster)
pp.Printf("%v\n", cluster)
}
return clusters
}
func listendpoints(ctx context.Context, conn *grpc.ClientConn) []v2.ClusterLoadAssignment {
eds := v2.NewEndpointDiscoveryServiceClient(conn)
dresp, err := eds.FetchEndpoints(ctx, &dr)
if err != nil {
log.Fatalf("endpoints err: %v", err)
}
var class []v2.ClusterLoadAssignment
pp.Printf("version info: %v\n", dresp.VersionInfo)
for _, anyCla := range dresp.Resources {
var cla v2.ClusterLoadAssignment
cla.Unmarshal(anyCla.Value)
pp.Printf("%v\n", cla)
class = append(class, cla)
}
return class
}
func listListeners(ctx context.Context, conn *grpc.ClientConn) []v2.Listener {
// listeners
ldsc := v2.NewListenerDiscoveryServiceClient(conn)
dresp, err := ldsc.FetchListeners(ctx, &dr)
if err != nil {
log.Fatalf("listeners err: %v", err)
}
var listeners []v2.Listener
for _, anylistener := range dresp.Resources {
var listener v2.Listener
listener.Unmarshal(anylistener.Value)
pp.Printf("%v\n", listener)
listeners = append(listeners, listener)
}
return listeners
}
func listRoutes(ctx context.Context, conn *grpc.ClientConn, routenames []string) []v2.RouteConfiguration {
// listeners
ldsc := v2.NewRouteDiscoveryServiceClient(conn)
drr := dr
drr.ResourceNames = routenames
dresp, err := ldsc.FetchRoutes(ctx, &drr)
if err != nil {
log.Fatalf("routes err: %v", err)
}
var routes []v2.RouteConfiguration
for _, anyRoute := range dresp.Resources {
var route v2.RouteConfiguration
route.Unmarshal(anyRoute.Value)
pp.Printf("%v\n", route)
routes = append(routes, route)
}
return routes
}