-
Notifications
You must be signed in to change notification settings - Fork 432
/
mesh.go
271 lines (229 loc) · 6.77 KB
/
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
package services
import (
"fmt"
"net/http"
"os"
"os/exec"
static_plugin_gloo "github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/static"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/faultinjection"
)
// for each service we will create an envoy config and run it with
// env vars describing the location of the upstreams
// and run envoy along with them.
type Service struct {
Name string
Process string
// these will be filled by Start
Port uint32
MeshPort uint32
}
var envoyPort = 8300
type QuoteUnquoteMesh struct {
clients TestClients
portfor func(i, j int) uint32
proxies []*gloov1.Proxy
upstreams []gloov1.Upstream
meshupstreams []gloov1.Upstream
Envoys []*EnvoyInstance
}
func proxyForService(i int) string {
return fmt.Sprintf("proxy-%d", i)
}
func (m *QuoteUnquoteMesh) getSelfListener(svcIndex int) *gloov1.Listener {
return &gloov1.Listener{
Name: "listener-self",
BindAddress: "127.0.0.1",
BindPort: m.portfor(svcIndex, svcIndex),
ListenerType: &gloov1.Listener_HttpListener{
HttpListener: &gloov1.HttpListener{
VirtualHosts: []*gloov1.VirtualHost{{
Name: "virt-self",
Domains: []string{"*"},
Routes: []*gloov1.Route{{
Action: &gloov1.Route_RouteAction{
RouteAction: &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Single{
Single: &gloov1.Destination{
DestinationType: &gloov1.Destination_Upstream{
Upstream: m.upstreams[svcIndex].Metadata.Ref(),
},
},
},
},
},
}},
}},
},
},
}
}
func (m *QuoteUnquoteMesh) AddFault(svcIndex int, percent float32) {
// the first listener is the self listener
l := m.getSelfListener(svcIndex)
route := l.ListenerType.(*gloov1.Listener_HttpListener).HttpListener.VirtualHosts[0].Routes[0]
route.Options = &gloov1.RouteOptions{
Faults: &faultinjection.RouteFaults{
Abort: &faultinjection.RouteAbort{
HttpStatus: http.StatusServiceUnavailable,
Percentage: percent,
},
},
}
m.UpdateSelfListener(svcIndex, l)
}
func (m *QuoteUnquoteMesh) RemoveFault(svcIndex int) {
// the first listener is the self listener
l := m.getSelfListener(svcIndex)
m.UpdateSelfListener(svcIndex, l)
}
func (m *QuoteUnquoteMesh) UpdateSelfListener(svcIndex int, l *gloov1.Listener) {
var ropts clients.ReadOpts
proxy := m.proxies[svcIndex]
existingproxy, err := m.clients.ProxyClient.Read(proxy.Metadata.Namespace, proxy.Metadata.Name, ropts)
Expect(err).NotTo(HaveOccurred())
existingproxy.Listeners[0] = l
var opts clients.WriteOpts
opts.OverwriteExisting = true
_, err = m.clients.ProxyClient.Write(existingproxy, opts)
Expect(err).NotTo(HaveOccurred())
}
func (m *QuoteUnquoteMesh) Start(ef *EnvoyFactory, testClients TestClients, services []*Service) {
m.clients = testClients
numServices := len(services)
m.portfor = func(i, j int) uint32 {
return uint32(envoyPort + i*numServices + j)
}
envoyPort += numServices * numServices
// fill in ports
for i, svc := range services {
svc.Port = uint32(7100 + i)
svc.MeshPort = m.portfor(i, i)
}
for i, s := range services {
u := gloov1.Upstream{
Metadata: &core.Metadata{
Name: fmt.Sprintf("local-%d", i),
Namespace: "default",
},
UpstreamType: &gloov1.Upstream_Static{
Static: &static_plugin_gloo.UpstreamSpec{
Hosts: []*static_plugin_gloo.Host{{
Addr: "localhost",
Port: s.Port,
}},
},
},
}
m.upstreams = append(m.upstreams, u)
}
for i := range services {
u := gloov1.Upstream{
Metadata: &core.Metadata{
Name: fmt.Sprintf("mesh-local-%d", i),
Namespace: "default",
},
UpstreamType: &gloov1.Upstream_Static{
Static: &static_plugin_gloo.UpstreamSpec{
Hosts: []*static_plugin_gloo.Host{{
Addr: "localhost",
Port: m.portfor(i, i),
}},
},
},
}
m.meshupstreams = append(m.meshupstreams, u)
}
for i := range services {
// proxy object for service i
proxy := &gloov1.Proxy{
Metadata: &core.Metadata{
Name: proxyForService(i),
Namespace: "default",
},
Listeners: []*gloov1.Listener{m.getSelfListener(i)},
}
for j := range services {
if i == j {
continue
}
proxy.Listeners = append(proxy.Listeners, &gloov1.Listener{
Name: fmt.Sprintf("listener-%d-to-%d", i, j),
BindAddress: "127.0.0.1",
BindPort: m.portfor(i, j),
ListenerType: &gloov1.Listener_HttpListener{
HttpListener: &gloov1.HttpListener{
VirtualHosts: []*gloov1.VirtualHost{{
Name: fmt.Sprintf("virt-%d-to-%d", i, j),
Domains: []string{"*"},
Routes: []*gloov1.Route{{
Action: &gloov1.Route_RouteAction{
RouteAction: &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Single{
Single: &gloov1.Destination{
DestinationType: &gloov1.Destination_Upstream{
Upstream: m.meshupstreams[j].Metadata.Ref(),
},
},
},
},
},
}},
}},
},
},
})
}
m.proxies = append(m.proxies, proxy)
}
var opts clients.WriteOpts
for _, u := range m.upstreams {
_, err := m.clients.UpstreamClient.Write(&u, opts)
Expect(err).NotTo(HaveOccurred())
}
for _, u := range m.meshupstreams {
_, err := m.clients.UpstreamClient.Write(&u, opts)
Expect(err).NotTo(HaveOccurred())
}
for _, p := range m.proxies {
_, err := m.clients.ProxyClient.Write(p, opts)
Expect(err).NotTo(HaveOccurred())
}
// now start all the envoys
for _, p := range m.proxies {
ei, err := ef.NewEnvoyInstance()
Expect(err).NotTo(HaveOccurred())
ei.RunWithRole(p.Metadata.Namespace+"~"+p.Metadata.Name, m.clients.GlooPort)
m.Envoys = append(m.Envoys, ei)
}
// now start all the services!
var cmds []*exec.Cmd
for i, svc := range services {
var envvars []string
envvars = append(envvars, "SELF="+fmt.Sprintf("%d", i))
envvars = append(envvars, "TOTAL="+fmt.Sprintf("%d", numServices))
envvars = append(envvars, "PORT="+fmt.Sprintf("%d", svc.Port))
for j := 0; j < numServices; j++ {
if i == j {
// help services error if they have a bug.
envvars = append(envvars, fmt.Sprintf("SVC%d=0", j))
} else {
envvars = append(envvars, fmt.Sprintf("SVC%d=%d", j, m.portfor(i, j)))
}
}
cmd := exec.Command(svc.Process)
cmd.Env = append(os.Environ(), envvars...)
_, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
cmds = append(cmds, cmd)
}
// wait for services to die so we exit
// for _, cmd := range cmds {
// cmd.Wait()
// }
}