-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
186 lines (152 loc) · 4.83 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
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
package main
import (
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"regexp"
"strconv"
"strings"
"github.com/cenk/backoff"
"github.com/monzo/typhon"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type ServerInfo struct {
State string `json:"state"`
}
type Stats struct {
CDSUpdateSuccess int `json:"cluster_manager.cds.update_success"`
LDSUpdateSuccess int `json:"listener_manager.lds.update_success"`
}
var cdsRegex = regexp.MustCompile(`cluster_manager\.cds\.update_success: (\d)`)
var ldsRegex = regexp.MustCompile(`listener_manager\.lds\.update_success: (\d)`)
func ParseStats(bs []byte) (*Stats, error) {
cdsMatch := cdsRegex.FindSubmatch(bs)
if len(cdsMatch) != 2 {
return nil, errors.New("could not match cds update success")
}
cds, err := strconv.Atoi(string(cdsMatch[1]))
if err != nil {
return nil, errors.New("could not parse cds update success as int")
}
ldsMatch := ldsRegex.FindSubmatch(bs)
if len(ldsMatch) != 2 {
return nil, errors.New("could not match lds update success")
}
lds, err := strconv.Atoi(string(ldsMatch[1]))
if err != nil {
return nil, errors.New("could not parse lds update success as int")
}
return &Stats{
CDSUpdateSuccess: cds,
LDSUpdateSuccess: lds,
}, nil
}
func main() {
logger := logrus.New()
logger.Formatter = &logrus.TextFormatter{
TimestampFormat: "2006-01-02T15:04:05.000Z07:00",
}
logger.Level = logrus.InfoLevel
// 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" {
logger.Infof("Blocking on an Envoy located at: %v being LIVE and receiving a LDS and CDS update", host)
block(logger, host)
}
if len(os.Args) < 2 {
return
}
binary, err := exec.LookPath(os.Args[1])
if err != nil {
logger.WithError(err).Fatal("entrypoint must be provided as an argument")
}
logger.Infof("Handing over to %v", binary)
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
url := fmt.Sprintf("%s/quitquitquit", host)
_ = typhon.NewRequest(context.Background(), "POST", url, nil).Send().Response()
}
os.Exit(exitCode)
}
func block(logger *logrus.Logger, envoyHost string) {
if os.Getenv("START_WITHOUT_ENVOY") == "true" {
return
}
infoUrl := fmt.Sprintf("%s/server_info", envoyHost)
b := backoff.NewExponentialBackOff()
// We wait forever for envoy to start and then for a LDS and CDS push. In practice k8s will kill the pod if we take too long.
b.MaxElapsedTime = 0
_ = backoff.Retry(func() error {
infoRsp := typhon.NewRequest(context.Background(), "GET", infoUrl, nil).Send().Response()
info := &ServerInfo{}
err := infoRsp.Decode(info)
if err != nil {
logger.WithError(err).Infof("Envoy at %v not reachable yet", envoyHost)
return err
}
if info.State != "LIVE" {
logger.WithError(err).Infof("Envoy at %v not LIVE", envoyHost)
return errors.New("not live yet")
}
logger.Infof("Envoy at %v is LIVE", envoyHost)
return nil
}, b)
statsURL := fmt.Sprintf("%s/stats", envoyHost)
b = backoff.NewExponentialBackOff()
// We wait forever for a LDS and CDS push. In practice k8s will kill the pod if we take too long.
b.MaxElapsedTime = 0
_ = backoff.Retry(func() error {
statsRsp := typhon.NewRequest(context.Background(), "GET", statsURL, nil).Send().Response()
statsRspBytes, _ := statsRsp.BodyBytes(false)
stats, err := ParseStats(statsRspBytes)
if err != nil {
logger.WithError(err).Infof("Could not parse stats response from Envoy at %v: response: %v", envoyHost, statsRspBytes)
return err
}
if stats.LDSUpdateSuccess == 0 {
logger.Infof("Envoy at %v has not received a LDS update successfully yet", envoyHost)
return errors.New("no LDS push yet")
}
if stats.CDSUpdateSuccess == 0 {
logger.Infof("Envoy at %v has not received a CDS update successfully yet", envoyHost)
return errors.New("no CDS push yet")
}
logger.Infof("Envoy at %v has received a LDS and CDS push", envoyHost)
return nil
}, b)
}