-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathmain.go
More file actions
205 lines (179 loc) · 5.03 KB
/
main.go
File metadata and controls
205 lines (179 loc) · 5.03 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"context"
"flag"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/runconduit/conduit/controller/api/proxy"
common "github.com/runconduit/conduit/controller/gen/common"
pb "github.com/runconduit/conduit/controller/gen/proxy/telemetry"
"github.com/runconduit/conduit/controller/k8s"
"github.com/runconduit/conduit/controller/util"
"google.golang.org/grpc/codes"
k8sV1 "k8s.io/client-go/pkg/api/v1"
)
/* A simple script for posting simulated telemetry data to the proxy api */
var (
responseCodes = []codes.Code{
codes.OK,
codes.PermissionDenied,
codes.Unavailable,
}
streamSummary = &pb.StreamSummary{
BytesSent: 12345,
DurationMs: 10,
FramesSent: 4,
}
ports = []uint32{3333, 6262}
)
func randomPort() uint32 {
return ports[rand.Intn(len(ports))]
}
func randomCount() uint32 {
return uint32(rand.Int31n(100) + 1)
}
func randomLatencies(count uint32) (latencies []*pb.Latency) {
for i := uint32(0); i < count; i++ {
// The latency value with precision to 100µs.
latencyValue := uint32(rand.Int31n(int32(time.Second / (time.Millisecond * 10))))
latency := pb.Latency{
Latency: latencyValue,
Count: 1,
}
latencies = append(latencies, &latency)
}
return
}
func randomEos(count uint32) (eos []*pb.EosScope) {
responseCodes := make(map[uint32]uint32)
for i := uint32(0); i < count; i++ {
responseCodes[randomResponseCode()] += 1
}
for code, streamCount := range responseCodes {
eos = append(eos, &pb.EosScope{
Ctx: &pb.EosCtx{End: &pb.EosCtx_GrpcStatusCode{GrpcStatusCode: code}},
Streams: streamSummaries(streamCount),
})
}
return
}
func randomResponseCode() uint32 {
return uint32(responseCodes[rand.Intn(len(responseCodes))])
}
func streamSummaries(count uint32) (summaries []*pb.StreamSummary) {
for i := uint32(0); i < count; i++ {
summaries = append(summaries, streamSummary)
}
return
}
func stringToIp(str string) *common.IPAddress {
octets := make([]uint8, 0)
for _, num := range strings.Split(str, ".") {
oct, _ := strconv.Atoi(num)
octets = append(octets, uint8(oct))
}
return util.IPV4(octets[0], octets[1], octets[2], octets[3])
}
func podIndexFunc(obj interface{}) ([]string, error) {
return nil, nil
}
func randomPod(pods []*k8sV1.Pod, prvPodIp *common.IPAddress) *common.IPAddress {
var podIp *common.IPAddress
for {
if podIp != nil {
break
}
randomPod := pods[rand.Intn(len(pods))]
podIp = stringToIp(randomPod.Status.PodIP)
if prvPodIp != nil && podIp.GetIpv4() == prvPodIp.GetIpv4() {
podIp = nil
}
}
return podIp
}
func main() {
rand.Seed(time.Now().UnixNano())
addr := flag.String("addr", ":8086", "address of proxy api")
requestCount := flag.Int("requests", 0, "number of api requests to make (default: infinite)")
sleep := flag.Duration("sleep", time.Second, "time to sleep between requests")
maxPods := flag.Int("max-pods", 0, "total number of pods to simulate (default unlimited)")
kubeConfigPath := flag.String("kubeconfig", "", "path to kube config - required")
flag.Parse()
if len(flag.Args()) > 0 {
log.Fatal("Unable to parse command line arguments")
return
}
client, conn, err := proxy.NewTelemetryClient(*addr)
if err != nil {
log.Fatal(err.Error())
}
defer conn.Close()
clientSet, err := k8s.NewClientSet(*kubeConfigPath)
if err != nil {
log.Fatal(err.Error())
}
pods, err := k8s.NewPodIndex(clientSet, podIndexFunc)
if err != nil {
log.Fatal(err.Error())
}
pods.Run()
// required for pods.List() to work -> otherwise the list of pods returned is empty
time.Sleep(2 * time.Second)
podList, err := pods.List()
if err != nil {
log.Fatal(err.Error())
}
allPods := make([]*k8sV1.Pod, 0)
for _, pod := range podList {
if pod.Status.PodIP != "" && (*maxPods == 0 || len(allPods) < *maxPods) {
allPods = append(allPods, pod)
}
}
for i := 0; (*requestCount == 0) || (i < *requestCount); i++ {
count := randomCount()
sourceIp := randomPod(allPods, nil)
targetIp := randomPod(allPods, sourceIp)
req := &pb.ReportRequest{
Process: &pb.Process{
ScheduledInstance: "hello-1mfa0",
ScheduledNamespace: "people",
},
ClientTransports: []*pb.ClientTransport{},
ServerTransports: []*pb.ServerTransport{},
Proxy: pb.ReportRequest_INBOUND,
Requests: []*pb.RequestScope{
&pb.RequestScope{
Ctx: &pb.RequestCtx{
SourceIp: sourceIp,
TargetAddr: &common.TcpAddress{
Ip: targetIp,
Port: randomPort(),
},
Authority: "world.greeting:7778",
Method: &common.HttpMethod{Type: &common.HttpMethod_Registered_{Registered: common.HttpMethod_GET}},
Path: "/World/Greeting",
},
Count: count,
Responses: []*pb.ResponseScope{
&pb.ResponseScope{
Ctx: &pb.ResponseCtx{
HttpStatusCode: http.StatusOK,
},
ResponseLatencies: randomLatencies(count),
Ends: randomEos(count),
},
},
},
},
}
_, err = client.Report(context.Background(), req)
if err != nil {
log.Fatal(err.Error())
}
time.Sleep(*sleep)
}
}