forked from kumahq/kuma-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_mesh.go
278 lines (261 loc) · 7.64 KB
/
generate_mesh.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"flag"
"fmt"
"io"
"math/rand"
"os"
"strings"
"text/template"
"time"
)
type service struct {
idx int
edges []int
replicas int
}
var srvTemplate = template.Must(template.New("").Parse(`
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{.name}}
{{- if .namespace}}
namespace: {{.namespace}}
{{- end}}
labels:
app: {{.name}}
spec:
replicas: {{.replicas}}
selector:
matchLabels:
app: {{.name}}
serviceName: {{.name}}
template:
metadata:
labels:
app: {{.name}}
annotations:
kuma.io/mesh: {{.mesh}}
{{- if ne .reachableServices "" }}
{{- if eq .reachableServices "none" }}
kuma.io/transparent-proxying-reachable-services: ""
{{- else }}
kuma.io/transparent-proxying-reachable-services: "{{.reachableServices }}"
{{- end}}
{{- end}}
spec:
containers:
- name: service
image: {{.image}}
ports:
- containerPort: 9090
env:
- name: SERVICE
value: "{{.name}}"
- name: UPSTREAM_URIS
value: "{{.uris}}"
resources:
limits:
memory: "32Mi"
cpu: "50m"
---
apiVersion: v1
kind: Service
metadata:
name: {{.name}}
{{- if .namespace}}
namespace: {{.namespace}}
{{- end}}
annotations:
spec:
selector:
app: {{.name}}
ports:
- protocol: TCP
appProtocol: http
port: 80
targetPort: 9090
`)).Option("missingkey=error")
var clientTemplate = template.Must(template.New("").Parse(`
---
apiVersion: apps/v1
kind: Deployment
metadata:
{{- if .namespace}}
namespace: {{.namespace}}
{{- end}}
name: "fake-client"
spec:
replicas: 1
selector:
matchLabels:
app: "fake-client"
template:
metadata:
labels:
app: "fake-client"
annotations:
kuma.io/mesh: {{.mesh}}
{{- if ne .reachableServices "" }}
kuma.io/transparent-proxying-reachable-services: "{{.reachableServices }}"
{{- end}}
spec:
containers:
- name: client
image: buoyantio/slow_cooker:1.3.0
args: ["-qps", "1", "-concurrency", "10", "{{.uri}}"]
resources:
limits:
memory: "32Mi"
cpu: "200m"
`)).Option("missingkey=error")
var namespaceTemplate = template.Must(template.New("").Parse(`
apiVersion: kuma.io/v1alpha1
kind: Mesh
metadata:
name: {{.mesh}}
spec:
metrics:
backends:
- conf:
{{- if .externalPrometheus }}
skipMTLS: true
{{- end }}
path: /metrics
port: 5670
tags:
kuma.io/service: dataplane-metrics
name: prometheus-1
type: prometheus
enabledBackend: prometheus-1
mtls:
backends:
- name: ca-1
type: builtin
enabledBackend: ca-1
---
apiVersion: v1
kind: Namespace
metadata:
name: {{.namespace}}
labels:
kuma.io/sidecar-injection: enabled
`)).Option("missingkey=error")
func toUri(idx int, namespace string) string {
return fmt.Sprintf("http://%s.mesh:80", toKumaService(idx, namespace))
}
func toKumaService(idx int, namespace string) string {
return fmt.Sprintf("%s_%s_svc_80", toName(idx), namespace)
}
func toName(idx int) string {
return fmt.Sprintf("srv-%03d", idx)
}
func (s service) ToYaml(writer io.Writer, namespace, mesh, image string, withReachableServices bool) error {
opt := map[string]interface{}{
"name": toName(s.idx),
"namespace": namespace,
"mesh": mesh,
"uris": strings.Join(s.mapEdges(func(i int) string { return toUri(i, namespace) }), ","),
"image": image,
"replicas": s.replicas,
"reachableServices": "",
}
if withReachableServices {
if len(s.edges) == 0 {
opt["reachableServices"] = "none"
} else {
opt["reachableServices"] = strings.Join(s.mapEdges(func(i int) string { return toKumaService(i, namespace) }), ",")
}
}
return srvTemplate.Execute(writer, opt)
}
func (s service) mapEdges(fn func(int) string) []string {
var all []string
for _, edge := range s.edges {
all = append(all, fn(edge))
}
return all
}
type services []service
func (s services) ToDot() string {
var allEdges []string
for _, srv := range s {
for _, other := range srv.edges {
allEdges = append(allEdges, fmt.Sprintf("%d -> %d;", srv.idx, other))
}
}
return fmt.Sprintf("digraph{\n%s\n}\n", strings.Join(allEdges, "\n"))
}
func (s services) ToYaml(writer io.Writer, conf serviceConf) error {
if err := namespaceTemplate.Execute(writer, map[string]interface{}{"namespace": conf.namespace, "mesh": conf.mesh, "externalPrometheus": conf.withExternalPrometheus}); err != nil {
return err
}
if conf.withGenerator {
params := map[string]string{"namespace": conf.namespace, "mesh": conf.mesh, "uri": toUri(0, conf.namespace), "reachableServices": ""}
if conf.withReachableServices {
params["reachableServices"] = toKumaService(0, conf.namespace)
}
if err := clientTemplate.Execute(writer, params); err != nil {
return err
}
}
for _, srv := range s {
if _, err := writer.Write([]byte("---")); err != nil {
return err
}
if err := srv.ToYaml(writer, conf.namespace, conf.mesh, conf.image, conf.withReachableServices); err != nil {
return err
}
}
return nil
}
type serviceConf struct {
withFailure bool
withGenerator bool
withReachableServices bool
namespace string
mesh string
image string
withExternalPrometheus bool
}
func GenerateRandomServiceMesh(seed int64, numServices, percentEdges, minReplicas, maxReplicas int) services {
r := rand.New(rand.NewSource(seed))
srvs := services{}
for i := 0; i < numServices; i++ {
numInstances := 1
if maxReplicas > minReplicas {
numInstances = (r.Int() % (1 + maxReplicas - minReplicas)) + minReplicas
}
srvs = append(srvs, service{idx: i, replicas: numInstances})
}
// That's the whole story of DAG and topological sort with triangular matrix.
for i := 0; i < numServices; i++ {
for j := i + 1; j < numServices; j++ {
if r.Int()%(j-i) == 0 && r.Int()%100 < percentEdges {
srvs[i].edges = append(srvs[i].edges, j)
}
}
}
return srvs
}
func main() {
conf := serviceConf{}
flag.BoolVar(&conf.withGenerator, "withGenerator", false, "Whether we should start a job that generates synthetic load to the first service")
flag.StringVar(&conf.namespace, "namespace", "kuma-test", "The name of the namespace to deploy to")
flag.StringVar(&conf.mesh, "mesh", "default", "The name of the mesh to deploy to")
flag.StringVar(&conf.image, "image", "nicholasjackson/fake-service:v0.21.1", "The fake-service image")
flag.BoolVar(&conf.withReachableServices, "withReachableServices", true, "Whether we should use reachable services or not")
flag.BoolVar(&conf.withExternalPrometheus, "withExternalPrometheus", false, "Whether we should use a prometheus inside or outside the mesh")
numServices := flag.Int("numServices", 20, "The number of services to use")
minReplicas := flag.Int("minReplicas", 1, "The minimum number of replicas to use (will pick a number between min and max)")
maxReplicas := flag.Int("maxReplicas", 1, "The max number of replicas to use (will pick a number between min and max)")
percentEdge := flag.Int("percentEdge", 50, "The for an edge between 2 nodes to exist (100 == sure)")
seed := flag.Int64("seed", time.Now().Unix(), "the seed for the random generate (set to now by default)")
flag.Parse()
fmt.Printf("# Using seed: %d\n", *seed)
srvs := GenerateRandomServiceMesh(*seed, *numServices, *percentEdge, *minReplicas, *maxReplicas)
err := srvs.ToYaml(os.Stdout, conf)
if err != nil {
panic(any(err))
}
}