-
Notifications
You must be signed in to change notification settings - Fork 53
/
main.go
113 lines (91 loc) · 2.26 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"github.com/cenk/backoff"
"github.com/monzo/typhon"
)
type ServerInfo struct {
State string `json:"state"`
}
func main() {
// Should be in format `http://127.0.0.1:9010`
host, ok := os.LookupEnv("ENVOY_ADMIN_API")
if ok && os.Getenv("START_WITHOUT_ENVOY") != "true" {
block(host)
}
killAPI, killOk := os.LookupEnv("ENVOY_KILL_API")
if !killOk {
killAPI = fmt.Sprintf("%s/quitquitquit", host)
}
if len(os.Args) < 2 {
return
}
binary, err := exec.LookPath(os.Args[1])
if err != nil {
panic(err)
}
var proc *os.Process
// Pass signals to the child process
go func() {
stop := make(chan os.Signal, 2)
signal.Notify(stop)
for sig := range stop {
if proc != nil {
proc.Signal(sig)
} else {
// Signal received before the process even started. Let's just exit.
os.Exit(1)
}
}
}()
proc, err = os.StartProcess(binary, os.Args[1:], &os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
})
if err != nil {
panic(err)
}
state, err := proc.Wait()
if err != nil {
panic(err)
}
exitCode := state.ExitCode()
switch {
case !ok:
// We don't have an ENVOY_ADMIN_API env var, do nothing
case !strings.Contains(host, "127.0.0.1") && !strings.Contains(host, "localhost"):
// Envoy is not local; do nothing
case os.Getenv("NEVER_KILL_ENVOY") == "true":
// We're configured never to kill envoy, do nothing
case os.Getenv("ALWAYS_KILL_ENVOY") == "true", exitCode == 0:
// Either we had a clean exit, or we are configured to kill envoy anyway
_ = typhon.NewRequest(context.Background(), "POST", killAPI, nil).Send().Response()
}
os.Exit(exitCode)
}
func block(host string) {
if os.Getenv("START_WITHOUT_ENVOY") == "true" {
return
}
url := fmt.Sprintf("%s/server_info", host)
b := backoff.NewExponentialBackOff()
// We wait forever for envoy to start. In practice k8s will kill the pod if we take too long.
b.MaxElapsedTime = 0
_ = backoff.Retry(func() error {
rsp := typhon.NewRequest(context.Background(), "GET", url, nil).Send().Response()
info := &ServerInfo{}
err := rsp.Decode(info)
if err != nil {
return err
}
if info.State != "LIVE" {
return errors.New("not live yet")
}
return nil
}, b)
}