-
Notifications
You must be signed in to change notification settings - Fork 685
/
envoy.go
118 lines (107 loc) · 3.08 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
package entrypoint
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/datawire/dlib/dcontext"
"github.com/datawire/dlib/dexec"
"github.com/datawire/dlib/dgroup"
"github.com/datawire/dlib/dlog"
"github.com/emissary-ingress/emissary/v3/pkg/envoytest"
)
func runEnvoy(ctx context.Context, envoyHUP chan os.Signal) error {
// Wait until we get a SIGHUP to start envoy.
//var bootstrap string
select {
case <-ctx.Done():
return nil
case <-envoyHUP:
}
// Try to run envoy directly, but fallback to running it inside docker if there is
// no envoy executable available.
if IsEnvoyAvailable() {
cmd := subcommand(ctx, "envoy", GetEnvoyFlags()...)
if envbool("DEV_SHUTUP_ENVOY") {
cmd.Stdout = nil
cmd.Stderr = nil
}
return cmd.Run()
} else {
// For some reason docker only sometimes passes the signal onto the process inside
// the container, so we setup this cleanup function so that in the docker case we
// can do a docker kill, just to be sure it is really dead and we don't leave an
// envoy lying around.
// Create a label unique to this invocation so we can use it to do a docker
// kill for cleanup.
label := fmt.Sprintf("amb-envoy-label-%d", os.Getpid())
snapdir := GetSnapshotDir()
group := dgroup.NewGroup(ctx, dgroup.GroupConfig{
ShutdownOnNonError: true,
})
group.Go("envoy", func(ctx context.Context) error {
dockerFlags := []string{
"--label=" + label,
// FIXME(lukeshu): --network=host doesn't work on macOS, so that
// will inconvenience our macOS-using contributors.
"--network=host",
"--volume=" + snapdir + ":" + snapdir,
"--volume=" + GetEnvoyBootstrapFile() + ":" + GetEnvoyBootstrapFile(),
}
if envbool("DEV_ENVOY_DOCKER_PRIVILEGED") {
dockerFlags = append(dockerFlags,
"--privileged")
}
cmd, err := envoytest.LocalEnvoyCmd(ctx, dockerFlags, GetEnvoyFlags())
if err != nil {
return err
}
return cmd.Run()
})
group.Go("cleanup", func(ctx context.Context) error {
<-ctx.Done()
ctx = dcontext.HardContext(ctx)
for {
cids, err := cidsForLabel(ctx, label)
if err != nil {
return err
}
if len(cids) == 0 {
return nil
}
// Give the container one second to exit
tctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
wait := subcommand(tctx, "docker", append([]string{"wait"}, cids...)...)
wait.Stdout = nil
if err := wait.Run(); err != nil {
var exitErr *dexec.ExitError
if errors.As(err, &exitErr) {
dlog.Errorf(ctx, "docker wait: %v\n%s", err, string(exitErr.Stderr))
} else {
return err
}
}
cids, err = cidsForLabel(ctx, label)
if err != nil {
return err
}
if len(cids) == 0 {
return nil
}
kill := subcommand(ctx, "docker", append([]string{"kill"}, cids...)...)
kill.Stdout = nil
if err := wait.Run(); err != nil {
var exitErr *dexec.ExitError
if errors.As(err, &exitErr) {
dlog.Errorf(ctx, "docker kill: %v\n%s", err, string(exitErr.Stderr))
} else {
return err
}
}
}
})
return group.Wait()
}
}