-
Notifications
You must be signed in to change notification settings - Fork 444
/
envoy.go
602 lines (521 loc) · 14 KB
/
envoy.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package services
import (
"bufio"
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"text/template"
"github.com/solo-io/gloo/projects/envoyinit/cmd/utils"
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
"bytes"
"io"
"github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
. "github.com/onsi/gomega"
errors "github.com/rotisserie/eris"
"github.com/solo-io/go-utils/log"
)
const (
containerName = "e2e_envoy"
)
var adminPort = uint32(20000)
var bindPort = uint32(10080)
func NextBindPort() uint32 {
return AdvanceBindPort(&bindPort)
}
func AdvanceBindPort(p *uint32) uint32 {
return atomic.AddUint32(p, 1) + uint32(config.GinkgoConfig.ParallelNode*1000)
}
func (ei *EnvoyInstance) buildBootstrap() string {
var b bytes.Buffer
if err := parsedTemplate.Execute(&b, ei); err != nil {
panic(err)
}
return b.String()
}
func (ei *EnvoyInstance) buildBootstrapFromConfig(configFile string) (string, error) {
// Will error on missing file or invalid config
config, err := utils.GetConfig(configFile)
if err != nil {
return "", err
}
addr, err := localAddr()
if err != nil {
return "", err
}
// When running envoy in docker, replace localhost
// loopback address with the docker routable IP.
if addr != "" && addr != "127.0.0.1" {
config = strings.ReplaceAll(config, "127.0.0.1", addr)
}
return config, nil
}
const envoyConfigTemplate = `
node:
cluster: ingress
id: {{.ID}}
metadata:
role: {{.Role}}
{{if .MetricsAddr}}
stats_sinks:
- name: envoy.stat_sinks.metrics_service
typed_config:
"@type": type.googleapis.com/envoy.config.metrics.v3.MetricsServiceConfig
grpc_service:
envoy_grpc: {cluster_name: metrics_cluster}
{{end}}
static_resources:
clusters:
- name: xds_cluster
connect_timeout: 5.000s
load_assignment:
cluster_name: xds_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: {{.GlooAddr}}
port_value: {{.Port}}
http2_protocol_options: {}
type: STATIC
{{if .RatelimitAddr}}
- name: ratelimit_cluster
connect_timeout: 5.000s
load_assignment:
cluster_name: ratelimit_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: {{.RatelimitAddr}}
port_value: {{.RatelimitPort}}
http2_protocol_options: {}
type: STATIC
{{end}}
{{if .AccessLogAddr}}
- name: access_log_cluster
connect_timeout: 5.000s
load_assignment:
cluster_name: access_log_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: {{.AccessLogAddr}}
port_value: {{.AccessLogPort}}
http2_protocol_options: {}
type: STATIC
{{end}}
- name: aws_sts_cluster
connect_timeout: 5.000s
type: LOGICAL_DNS
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: aws_sts_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
port_value: 443
address: sts.amazonaws.com
transport_socket:
name: envoy.transport_sockets.tls
typed_config:
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
sni: sts.amazonaws.com
{{if .MetricsAddr}}
- name: metrics_cluster
connect_timeout: 5.000s
load_assignment:
cluster_name: metrics_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: {{.MetricsAddr}}
port_value: {{.MetricsPort}}
http2_protocol_options: {}
type: STATIC
{{end}}
dynamic_resources:
ads_config:
api_type: GRPC
grpc_services:
- envoy_grpc: {cluster_name: xds_cluster}
cds_config:
ads: {}
lds_config:
ads: {}
admin:
access_log_path: /dev/null
address:
socket_address:
address: 0.0.0.0
port_value: {{.AdminPort}}
`
var parsedTemplate = template.Must(template.New("bootstrap").Parse(envoyConfigTemplate))
type EnvoyFactory struct {
envoypath string
tmpdir string
useDocker bool
instances []*EnvoyInstance
}
func getEnvoyImageTag() string {
eit := os.Getenv("ENVOY_IMAGE_TAG")
if eit != "" {
return eit
}
// get project base
gomod, err := exec.Command("go", "env", "GOMOD").CombinedOutput()
Expect(err).NotTo(HaveOccurred())
gomodfile := strings.TrimSpace(string(gomod))
projectbase, _ := filepath.Split(gomodfile)
makefile := filepath.Join(projectbase, "Makefile")
inFile, err := os.Open(makefile)
Expect(err).NotTo(HaveOccurred())
defer inFile.Close()
const prefix = "ENVOY_GLOO_IMAGE ?= quay.io/solo-io/envoy-gloo:"
scanner := bufio.NewScanner(inFile)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, prefix) {
return strings.TrimSpace(strings.TrimPrefix(line, prefix))
}
}
return ""
}
func NewEnvoyFactory() (*EnvoyFactory, error) {
// if an envoy binary is explicitly specified
// use it
envoyPath := os.Getenv("ENVOY_BINARY")
if envoyPath != "" {
log.Printf("Using envoy from environment variable: %s", envoyPath)
return &EnvoyFactory{
envoypath: envoyPath,
}, nil
}
// maybe it is in the path?!
// only try to use local path if FETCH_ENVOY_BINARY is not set;
// there are two options:
// - you are using local envoy binary you just built and want to test (don't set the variable)
// - you want to use the envoy version gloo is shipped with (set the variable)
if os.Getenv("FETCH_ENVOY_BINARY") != "" {
envoyPath, err := exec.LookPath("envoy")
if err == nil {
log.Printf("Using envoy from PATH: %s", envoyPath)
return &EnvoyFactory{
envoypath: envoyPath,
}, nil
}
}
switch runtime.GOOS {
case "darwin":
log.Printf("Using docker to run envoy")
return &EnvoyFactory{useDocker: true}, nil
case "linux":
// try to grab one form docker...
tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "envoy")
if err != nil {
return nil, err
}
envoyImageTag := getEnvoyImageTag()
if envoyImageTag == "" {
panic("Must set the ENVOY_IMAGE_TAG env var. Find valid tag names here https://quay.io/repository/solo-io/gloo-envoy-wrapper?tab=tags")
}
log.Printf("Using envoy docker image tag: %s", envoyImageTag)
bash := fmt.Sprintf(`
set -ex
CID=$(docker run -d quay.io/solo-io/envoy-gloo:%s /bin/bash -c exit)
# just print the image sha for repoducibility
echo "Using Envoy Image:"
docker inspect quay.io/solo-io/envoy-gloo:%s -f "{{.RepoDigests}}"
docker cp $CID:/usr/local/bin/envoy .
docker rm $CID
`, envoyImageTag, envoyImageTag)
scriptfile := filepath.Join(tmpdir, "getenvoy.sh")
ioutil.WriteFile(scriptfile, []byte(bash), 0755)
cmd := exec.Command("bash", scriptfile)
cmd.Dir = tmpdir
cmd.Stdout = ginkgo.GinkgoWriter
cmd.Stderr = ginkgo.GinkgoWriter
if err := cmd.Run(); err != nil {
return nil, err
}
return &EnvoyFactory{
envoypath: filepath.Join(tmpdir, "envoy"),
tmpdir: tmpdir,
}, nil
default:
return nil, errors.New("Unsupported OS: " + runtime.GOOS)
}
}
func (ef *EnvoyFactory) EnvoyPath() string {
return ef.envoypath
}
func (ef *EnvoyFactory) Clean() error {
if ef == nil {
return nil
}
if ef.tmpdir != "" {
os.RemoveAll(ef.tmpdir)
}
instances := ef.instances
ef.instances = nil
for _, ei := range instances {
ei.Clean()
}
return nil
}
type EnvoyInstance struct {
MetricsAddr string
MetricsPort uint32
AccessLogAddr string
AccessLogPort uint32
RatelimitAddr string
RatelimitPort uint32
ID string
Role string
envoypath string
envoycfg string
logs *bytes.Buffer
cmd *exec.Cmd
UseDocker bool
GlooAddr string // address for gloo and services
Port uint32
AdminPort uint32
// Path to access logs for binary run
AccessLogs string
DockerOptions
}
// Extra options for running in docker
type DockerOptions struct {
// Extra volume arguments
Volumes []string
// Extra env arguments.
// see https://docs.docker.com/engine/reference/run/#env-environment-variables for more info
Env []string
}
func (ef *EnvoyFactory) MustEnvoyInstance() *EnvoyInstance {
envoyInstance, err := ef.NewEnvoyInstance()
Expect(err).NotTo(HaveOccurred())
return envoyInstance
}
func (ef *EnvoyFactory) NewEnvoyInstance() (*EnvoyInstance, error) {
gloo := "127.0.0.1"
if ef.useDocker {
var err error
gloo, err = localAddr()
if err != nil {
return nil, err
}
}
ei := &EnvoyInstance{
envoypath: ef.envoypath,
UseDocker: ef.useDocker,
GlooAddr: gloo,
AccessLogAddr: gloo,
MetricsAddr: gloo,
AdminPort: atomic.AddUint32(&adminPort, 1) + uint32(config.GinkgoConfig.ParallelNode*1000),
}
ef.instances = append(ef.instances, ei)
return ei, nil
}
func (ei *EnvoyInstance) RunWithId(id string) error {
ei.ID = id
ei.Role = "default~proxy"
// TODO: refactor this function to include a context.
return ei.runWithPort(context.TODO(), 8081, "")
}
func (ei *EnvoyInstance) Run(port int) error {
ei.Role = "default~proxy"
// TODO: refactor this function to include a context.
return ei.runWithPort(context.TODO(), uint32(port), "")
}
func (ei *EnvoyInstance) RunWithConfig(port int, configFile string) error {
ei.Role = "default~proxy"
// TODO: refactor this function to include a context.
return ei.runWithPort(context.TODO(), uint32(port), configFile)
}
func (ei *EnvoyInstance) RunWithRole(role string, port int) error {
ei.Role = role
// TODO: refactor this function to include a context.
return ei.runWithPort(context.TODO(), uint32(port), "")
}
type EnvoyInstanceConfig interface {
Role() string
Port() uint32
Context() context.Context
}
func (ei *EnvoyInstance) RunWith(eic EnvoyInstanceConfig) error {
ei.Role = eic.Role()
return ei.runWithPort(eic.Context(), eic.Port(), "")
}
func (ei *EnvoyInstance) runWithPort(ctx context.Context, port uint32, configFile string) error {
go func() {
<-ctx.Done()
ei.Clean()
}()
if ei.ID == "" {
ei.ID = "ingress~for-testing"
}
ei.Port = port
if configFile == "" {
ei.envoycfg = ei.buildBootstrap()
} else {
var err error
ei.envoycfg, err = ei.buildBootstrapFromConfig(configFile)
if err != nil {
return err
}
}
if ei.UseDocker {
err := ei.runContainer(ctx)
if err != nil {
return err
}
return nil
}
args := []string{"--config-yaml", ei.envoycfg, "--disable-hot-restart", "--log-level", "debug"}
// run directly
cmd := exec.CommandContext(ctx, ei.envoypath, args...)
buf := &bytes.Buffer{}
ei.logs = buf
w := io.MultiWriter(ginkgo.GinkgoWriter, buf)
cmd.Stdout = w
cmd.Stderr = w
runner := Runner{Sourcepath: ei.envoypath, ComponentName: "ENVOY"}
cmd, err := runner.run(cmd)
if err != nil {
return err
}
ei.cmd = cmd
return nil
}
func (ei *EnvoyInstance) Binary() string {
return ei.envoypath
}
func (ei *EnvoyInstance) LocalAddr() string {
return ei.GlooAddr
}
func (ei *EnvoyInstance) SetPanicThreshold() error {
_, err := http.Post(fmt.Sprintf("http://localhost:%d/runtime_modify?upstream.healthy_panic_threshold=%d", ei.AdminPort, 0), "", nil)
return err
}
func (ei *EnvoyInstance) Clean() error {
if ei == nil {
return nil
}
http.Post(fmt.Sprintf("http://localhost:%d/quitquitquit", ei.AdminPort), "", nil)
if ei.cmd != nil {
ei.cmd.Process.Kill()
ei.cmd.Wait()
}
if ei.UseDocker {
if err := stopContainer(); err != nil {
return err
}
}
return nil
}
func (ei *EnvoyInstance) runContainer(ctx context.Context) error {
envoyImageTag := getEnvoyImageTag()
if envoyImageTag == "" {
return errors.New("Must set the ENVOY_IMAGE_TAG env var. Find valid tag names here https://quay.io/repository/solo-io/gloo-envoy-wrapper?tab=tags")
}
image := "quay.io/solo-io/gloo-envoy-wrapper:" + envoyImageTag
args := []string{"run", "--rm", "--name", containerName,
"-p", fmt.Sprintf("%d:%d", defaults.HttpPort, defaults.HttpPort),
"-p", fmt.Sprintf("%d:%d", defaults.HttpsPort, defaults.HttpsPort),
"-p", fmt.Sprintf("%d:%d", ei.AdminPort, ei.AdminPort),
}
for _, volume := range ei.DockerOptions.Volumes {
args = append(args, "-v", volume)
}
for _, env := range ei.DockerOptions.Env {
args = append(args, "-e", env)
}
args = append(args,
"--entrypoint=envoy",
image,
"--disable-hot-restart", "--log-level", "debug",
"--config-yaml", ei.envoycfg,
)
fmt.Fprintln(ginkgo.GinkgoWriter, args)
cmd := exec.CommandContext(ctx, "docker", args...)
cmd.Stdout = ginkgo.GinkgoWriter
cmd.Stderr = ginkgo.GinkgoWriter
if err := cmd.Start(); err != nil {
return errors.Wrap(err, "Unable to start envoy container")
}
return nil
}
func stopContainer() error {
cmd := exec.Command("docker", "stop", containerName)
cmd.Stdout = ginkgo.GinkgoWriter
cmd.Stderr = ginkgo.GinkgoWriter
err := cmd.Run()
if err != nil {
return errors.Wrap(err, "Error stopping container "+containerName)
}
return nil
}
func localAddr() (string, error) {
ip := os.Getenv("GLOO_IP")
if ip != "" {
return ip, nil
}
// go over network interfaces
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, i := range ifaces {
if (i.Flags&net.FlagUp == 0) ||
(i.Flags&net.FlagLoopback != 0) ||
(i.Flags&net.FlagPointToPoint != 0) {
continue
}
addrs, err := i.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
if v.IP.To4() != nil {
return v.IP.String(), nil
}
case *net.IPAddr:
if v.IP.To4() != nil {
return v.IP.String(), nil
}
}
}
}
return "", errors.New("unable to find Gloo IP")
}
func (ei *EnvoyInstance) Logs() (string, error) {
if ei.UseDocker {
logsArgs := []string{"logs", containerName}
cmd := exec.Command("docker", logsArgs...)
byt, err := cmd.CombinedOutput()
if err != nil {
return "", errors.Wrap(err, "Unable to fetch logs from envoy container")
}
return string(byt), nil
}
return ei.logs.String(), nil
}